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.txt";
8 const FILENAME2 = "test2.txt";
9 const CRC = 0xe6164331;
10 // XXX Must use a constant time here away from DST changes. See bug 402434.
11 const time = 1199145600000; // Jan 1st 2008
13 function testpass(source)
14 {
15 // Should exist.
16 do_check_true(source.hasEntry(FILENAME));
18 var entry = source.getEntry(FILENAME);
19 do_check_neq(entry, null);
21 do_check_false(entry.isDirectory);
23 // Should be stored
24 do_check_eq(entry.compression, ZIP_METHOD_STORE);
26 do_check_eq(entry.lastModifiedTime / PR_USEC_PER_MSEC, time);
28 // File size should match our data size.
29 do_check_eq(entry.realSize, DATA.length);
30 // When stored sizes should match.
31 do_check_eq(entry.size, entry.realSize);
33 // Check that the CRC is accurate
34 do_check_eq(entry.CRC32, CRC);
35 }
37 function run_test()
38 {
39 zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
41 // Shouldn't be there to start with.
42 do_check_false(zipW.hasEntry(FILENAME));
44 do_check_false(zipW.inQueue);
46 var stream = Cc["@mozilla.org/io/string-input-stream;1"]
47 .createInstance(Ci.nsIStringInputStream);
48 stream.setData(DATA, DATA.length);
49 zipW.addEntryStream(FILENAME, time * PR_USEC_PER_MSEC,
50 Ci.nsIZipWriter.COMPRESSION_NONE, stream, false);
52 // Check that zip state is right at this stage.
53 testpass(zipW);
54 zipW.close();
56 do_check_eq(tmpFile.fileSize,
57 DATA.length + ZIP_FILE_HEADER_SIZE + ZIP_CDS_HEADER_SIZE +
58 (ZIP_EXTENDED_TIMESTAMP_SIZE * 2) +
59 (FILENAME.length * 2) + ZIP_EOCDR_HEADER_SIZE);
61 // Check to see if we get the same results loading afresh.
62 zipW.open(tmpFile, PR_RDWR);
63 testpass(zipW);
64 zipW.close();
66 // Test the stored data with the zipreader
67 var zipR = new ZipReader(tmpFile);
68 testpass(zipR);
69 zipR.test(FILENAME);
70 var stream = Cc["@mozilla.org/scriptableinputstream;1"]
71 .createInstance(Ci.nsIScriptableInputStream);
72 stream.init(zipR.getInputStream(FILENAME));
73 var result = stream.read(DATA.length);
74 stream.close();
75 zipR.close();
77 do_check_eq(result, DATA);
78 }