modules/libjar/zipwriter/test/unit/test_storedata.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 2 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
michael@0 4 */
michael@0 5
michael@0 6 const DATA = "ZIP WRITER TEST DATA";
michael@0 7 const FILENAME = "test.txt";
michael@0 8 const FILENAME2 = "test2.txt";
michael@0 9 const CRC = 0xe6164331;
michael@0 10 // XXX Must use a constant time here away from DST changes. See bug 402434.
michael@0 11 const time = 1199145600000; // Jan 1st 2008
michael@0 12
michael@0 13 function testpass(source)
michael@0 14 {
michael@0 15 // Should exist.
michael@0 16 do_check_true(source.hasEntry(FILENAME));
michael@0 17
michael@0 18 var entry = source.getEntry(FILENAME);
michael@0 19 do_check_neq(entry, null);
michael@0 20
michael@0 21 do_check_false(entry.isDirectory);
michael@0 22
michael@0 23 // Should be stored
michael@0 24 do_check_eq(entry.compression, ZIP_METHOD_STORE);
michael@0 25
michael@0 26 do_check_eq(entry.lastModifiedTime / PR_USEC_PER_MSEC, time);
michael@0 27
michael@0 28 // File size should match our data size.
michael@0 29 do_check_eq(entry.realSize, DATA.length);
michael@0 30 // When stored sizes should match.
michael@0 31 do_check_eq(entry.size, entry.realSize);
michael@0 32
michael@0 33 // Check that the CRC is accurate
michael@0 34 do_check_eq(entry.CRC32, CRC);
michael@0 35 }
michael@0 36
michael@0 37 function run_test()
michael@0 38 {
michael@0 39 zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
michael@0 40
michael@0 41 // Shouldn't be there to start with.
michael@0 42 do_check_false(zipW.hasEntry(FILENAME));
michael@0 43
michael@0 44 do_check_false(zipW.inQueue);
michael@0 45
michael@0 46 var stream = Cc["@mozilla.org/io/string-input-stream;1"]
michael@0 47 .createInstance(Ci.nsIStringInputStream);
michael@0 48 stream.setData(DATA, DATA.length);
michael@0 49 zipW.addEntryStream(FILENAME, time * PR_USEC_PER_MSEC,
michael@0 50 Ci.nsIZipWriter.COMPRESSION_NONE, stream, false);
michael@0 51
michael@0 52 // Check that zip state is right at this stage.
michael@0 53 testpass(zipW);
michael@0 54 zipW.close();
michael@0 55
michael@0 56 do_check_eq(tmpFile.fileSize,
michael@0 57 DATA.length + ZIP_FILE_HEADER_SIZE + ZIP_CDS_HEADER_SIZE +
michael@0 58 (ZIP_EXTENDED_TIMESTAMP_SIZE * 2) +
michael@0 59 (FILENAME.length * 2) + ZIP_EOCDR_HEADER_SIZE);
michael@0 60
michael@0 61 // Check to see if we get the same results loading afresh.
michael@0 62 zipW.open(tmpFile, PR_RDWR);
michael@0 63 testpass(zipW);
michael@0 64 zipW.close();
michael@0 65
michael@0 66 // Test the stored data with the zipreader
michael@0 67 var zipR = new ZipReader(tmpFile);
michael@0 68 testpass(zipR);
michael@0 69 zipR.test(FILENAME);
michael@0 70 var stream = Cc["@mozilla.org/scriptableinputstream;1"]
michael@0 71 .createInstance(Ci.nsIScriptableInputStream);
michael@0 72 stream.init(zipR.getInputStream(FILENAME));
michael@0 73 var result = stream.read(DATA.length);
michael@0 74 stream.close();
michael@0 75 zipR.close();
michael@0 76
michael@0 77 do_check_eq(result, DATA);
michael@0 78 }

mercurial