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