summaryrefslogtreecommitdiffstats
path: root/modules/libjar/zipwriter/test/unit/test_alignment.js
blob: afc5cf2b3024d5372d83617d3f68515b99ab6739 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

const DATA = "ZIP WRITER TEST DATA";
const FILENAME = "test_data.txt";
const time = 1199145600000; // Jan 1st 2008

var TESTS = [
  {
    name: "test.txt",
    compression: Ci.nsIZipWriter.COMPRESSION_DEFAULT,
  },
  {
    name: "test.png",
    compression: Ci.nsIZipWriter.COMPRESSION_NONE,
  },
];

function swap16(n) {
  return (((n >> 8) & 0xff) << 0) | (((n >> 0) & 0xff) << 8);
}

function swap32(n) {
  return (
    (((n >> 24) & 0xff) << 0) |
    (((n >> 16) & 0xff) << 8) |
    (((n >> 8) & 0xff) << 16) |
    (((n >> 0) & 0xff) << 24)
  );
}

function move_to_data(bis, offset) {
  bis.readBytes(18); // Move to compressed size
  var size = swap32(bis.read32());
  bis.readBytes(4);
  var file_len = swap16(bis.read16());
  var extra_len = swap16(bis.read16());
  bis.readBytes(file_len);
  bis.readBytes(extra_len);
  offset += ZIP_FILE_HEADER_SIZE + file_len + extra_len;

  return { offset, size };
}

function test_alignment(align_size) {
  // Create zip for testing.
  zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
  for (var i = 0; i < TESTS.length; i++) {
    var source = do_get_file(DATA_DIR + TESTS[i].name);
    zipW.addEntryFile(TESTS[i].name, TESTS[i].compression, source, false);
  }
  var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
    Ci.nsIStringInputStream
  );
  stream.setData(DATA, DATA.length);
  zipW.addEntryStream(
    FILENAME,
    time * PR_USEC_PER_MSEC,
    Ci.nsIZipWriter.COMPRESSION_NONE,
    stream,
    false
  );
  zipW.alignStoredFiles(align_size);
  zipW.close();

  // Check data can be decompressed.
  var zipR = new ZipReader(tmpFile);
  stream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
    Ci.nsIScriptableInputStream
  );
  stream.init(zipR.getInputStream(FILENAME));
  var result = stream.read(DATA.length);
  Assert.equal(result, DATA);
  stream.close();
  zipR.close();

  // Check data is correct and aligned.
  var fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
    Ci.nsIFileInputStream
  );
  fis.init(tmpFile, -1, -1, null);
  let bis = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
    Ci.nsIBinaryInputStream
  );
  bis.setInputStream(fis);
  var offset = 0;

  var ret = move_to_data(bis, offset); // "test.txt"
  offset = ret.offset;
  bis.readBytes(ret.size);
  offset += ret.size;

  ret = move_to_data(bis, offset); // "test.png"
  offset = ret.offset;
  Assert.equal(offset % align_size, 0);
  bis.readBytes(ret.size);
  offset += ret.size;

  ret = move_to_data(bis, offset); // "test_data.txt"
  offset = ret.offset;
  result = bis.readBytes(DATA.length);
  Assert.equal(result, DATA);
  Assert.equal(offset % align_size, 0);

  fis.close();
  bis.close();
}

function run_test() {
  test_alignment(2);
  test_alignment(4);
  test_alignment(16);
  test_alignment(4096);
  test_alignment(32768);
}