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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/modules/libjar/zipwriter/test/unit/test_storedata.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/.
     1.7 + */
     1.8 +
     1.9 +const DATA = "ZIP WRITER TEST DATA";
    1.10 +const FILENAME = "test.txt";
    1.11 +const FILENAME2 = "test2.txt";
    1.12 +const CRC = 0xe6164331;
    1.13 +// XXX Must use a constant time here away from DST changes. See bug 402434.
    1.14 +const time = 1199145600000; // Jan 1st 2008
    1.15 +
    1.16 +function testpass(source)
    1.17 +{
    1.18 +  // Should exist.
    1.19 +  do_check_true(source.hasEntry(FILENAME));
    1.20 +
    1.21 +  var entry = source.getEntry(FILENAME);
    1.22 +  do_check_neq(entry, null);
    1.23 +
    1.24 +  do_check_false(entry.isDirectory);
    1.25 +
    1.26 +  // Should be stored
    1.27 +  do_check_eq(entry.compression, ZIP_METHOD_STORE);
    1.28 +
    1.29 +  do_check_eq(entry.lastModifiedTime / PR_USEC_PER_MSEC, time);
    1.30 +
    1.31 +  // File size should match our data size.
    1.32 +  do_check_eq(entry.realSize, DATA.length);
    1.33 +  // When stored sizes should match.
    1.34 +  do_check_eq(entry.size, entry.realSize);
    1.35 +
    1.36 +  // Check that the CRC is accurate
    1.37 +  do_check_eq(entry.CRC32, CRC);
    1.38 +}
    1.39 +
    1.40 +function run_test()
    1.41 +{
    1.42 +  zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE);
    1.43 +
    1.44 +  // Shouldn't be there to start with.
    1.45 +  do_check_false(zipW.hasEntry(FILENAME));
    1.46 +
    1.47 +  do_check_false(zipW.inQueue);
    1.48 +
    1.49 +  var stream = Cc["@mozilla.org/io/string-input-stream;1"]
    1.50 +                .createInstance(Ci.nsIStringInputStream);
    1.51 +  stream.setData(DATA, DATA.length);
    1.52 +  zipW.addEntryStream(FILENAME, time * PR_USEC_PER_MSEC,
    1.53 +                      Ci.nsIZipWriter.COMPRESSION_NONE, stream, false);
    1.54 +
    1.55 +  // Check that zip state is right at this stage.
    1.56 +  testpass(zipW);
    1.57 +  zipW.close();
    1.58 +
    1.59 +  do_check_eq(tmpFile.fileSize,
    1.60 +              DATA.length + ZIP_FILE_HEADER_SIZE + ZIP_CDS_HEADER_SIZE +
    1.61 +              (ZIP_EXTENDED_TIMESTAMP_SIZE * 2) +
    1.62 +              (FILENAME.length * 2) + ZIP_EOCDR_HEADER_SIZE);
    1.63 +
    1.64 +  // Check to see if we get the same results loading afresh.
    1.65 +  zipW.open(tmpFile, PR_RDWR);
    1.66 +  testpass(zipW);
    1.67 +  zipW.close();
    1.68 +
    1.69 +  // Test the stored data with the zipreader
    1.70 +  var zipR = new ZipReader(tmpFile);
    1.71 +  testpass(zipR);
    1.72 +  zipR.test(FILENAME);
    1.73 +  var stream = Cc["@mozilla.org/scriptableinputstream;1"]
    1.74 +                .createInstance(Ci.nsIScriptableInputStream);
    1.75 +  stream.init(zipR.getInputStream(FILENAME));
    1.76 +  var result = stream.read(DATA.length);
    1.77 +  stream.close();
    1.78 +  zipR.close();
    1.79 +
    1.80 +  do_check_eq(result, DATA);
    1.81 +}

mercurial