Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
4 */
6 const DATA = "ZIP WRITER TEST DATA";
7 const FILENAME = "test_data.txt";
8 const CRC = 0xe6164331;
9 const time = 1199145600000; // Jan 1st 2008
11 var TESTS = [
12 {
13 name: "test.txt",
14 compression: Ci.nsIZipWriter.COMPRESSION_DEFAULT
15 },
16 {
17 name: "test.png",
18 compression: Ci.nsIZipWriter.COMPRESSION_NONE
19 }
20 ];
22 function swap16(n)
23 {
24 return (((n >> 8) & 0xFF) << 0) |
25 (((n >> 0) & 0xFF) << 8);
26 }
28 function swap32(n)
29 {
30 return (((n >> 24) & 0xFF) << 0) |
31 (((n >> 16) & 0xFF) << 8) |
32 (((n >> 8) & 0xFF) << 16) |
33 (((n >> 0) & 0xFF) << 24);
34 }
36 function move_to_data(bis, offset)
37 {
38 bis.readBytes(18); // Move to compressed size
39 var size = swap32(bis.read32());
40 bis.readBytes(4);
41 var file_len = swap16(bis.read16());
42 var extra_len = swap16(bis.read16());
43 var file = bis.readBytes(file_len);
44 bis.readBytes(extra_len);
45 offset += ZIP_FILE_HEADER_SIZE + file_len + extra_len;
47 return {offset: offset, size: size};
48 }
50 function test_alignment(align_size)
51 {
52 // Create zip for testing.
53 zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
54 for (var i = 0; i < TESTS.length; i++) {
55 var source = do_get_file(DATA_DIR + TESTS[i].name);
56 zipW.addEntryFile(TESTS[i].name, TESTS[i].compression, source, false);
57 }
58 var stream = Cc["@mozilla.org/io/string-input-stream;1"]
59 .createInstance(Ci.nsIStringInputStream);
60 stream.setData(DATA, DATA.length);
61 zipW.addEntryStream(FILENAME, time * PR_USEC_PER_MSEC,
62 Ci.nsIZipWriter.COMPRESSION_NONE, stream, false);
63 zipW.alignStoredFiles(align_size);
64 zipW.close();
66 // Check data can be decompressed.
67 var zipR = new ZipReader(tmpFile);
68 var stream = Cc["@mozilla.org/scriptableinputstream;1"]
69 .createInstance(Ci.nsIScriptableInputStream);
70 stream.init(zipR.getInputStream(FILENAME));
71 var result = stream.read(DATA.length);
72 do_check_eq(result, DATA);
73 stream.close();
74 zipR.close();
76 // Check data is correct and aligned.
77 var fis = Cc["@mozilla.org/network/file-input-stream;1"]
78 .createInstance(Ci.nsIFileInputStream);
79 fis.init(tmpFile, -1, -1, null);
80 let bis = Cc["@mozilla.org/binaryinputstream;1"]
81 .createInstance(Ci.nsIBinaryInputStream);
82 bis.setInputStream(fis);
83 var offset = 0;
85 var ret = move_to_data(bis, offset); // "test.txt"
86 offset = ret.offset;
87 bis.readBytes(ret.size);
88 offset += ret.size;
90 ret = move_to_data(bis, offset); // "test.png"
91 offset = ret.offset;
92 do_check_eq(offset % align_size, 0);
93 bis.readBytes(ret.size);
94 offset += ret.size;
96 ret = move_to_data(bis, offset); // "test_data.txt"
97 offset = ret.offset;
98 var result = bis.readBytes(DATA.length);
99 do_check_eq(result, DATA);
100 do_check_eq(offset % align_size, 0);
102 fis.close();
103 bis.close();
104 }
106 function run_test()
107 {
108 test_alignment(2);
109 test_alignment(4);
110 test_alignment(16);
111 test_alignment(4096);
112 test_alignment(32768);
113 }