From 36d22d82aa202bb199967e9512281e9a53db42c9 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 7 Apr 2024 21:33:14 +0200 Subject: Adding upstream version 115.7.0esr. Signed-off-by: Daniel Baumann --- .../libjar/zipwriter/test/unit/data/emptyfile.txt | 0 .../libjar/zipwriter/test/unit/data/smallfile.txt | 1 + modules/libjar/zipwriter/test/unit/data/test.png | Bin 0 -> 3402 bytes modules/libjar/zipwriter/test/unit/data/test.txt | 5 + modules/libjar/zipwriter/test/unit/data/test.zip | Bin 0 -> 3824 bytes .../zipwriter/test/unit/data/test_bug399727.html | 160 +++++++++++++++++++++ .../zipwriter/test/unit/data/test_bug399727.zlib | Bin 0 -> 1959 bytes .../unit/data/test_bug446708/thumbs/st14-1.tiff | Bin 0 -> 4940 bytes .../zipwriter/test/unit/data/test_bug717061.gz | Bin 0 -> 1275 bytes .../zipwriter/test/unit/data/test_bug717061.html | 16 +++ .../libjar/zipwriter/test/unit/head_zipwriter.js | 60 ++++++++ .../libjar/zipwriter/test/unit/test_alignment.js | 117 +++++++++++++++ .../libjar/zipwriter/test/unit/test_asyncadd.js | 111 ++++++++++++++ .../libjar/zipwriter/test/unit/test_asyncbadadd.js | 31 ++++ .../zipwriter/test/unit/test_asyncbadremove.js | 27 ++++ .../libjar/zipwriter/test/unit/test_asyncremove.js | 40 ++++++ .../libjar/zipwriter/test/unit/test_bug399727.js | 107 ++++++++++++++ .../libjar/zipwriter/test/unit/test_bug419769_1.js | 75 ++++++++++ .../libjar/zipwriter/test/unit/test_bug419769_2.js | 62 ++++++++ .../libjar/zipwriter/test/unit/test_bug425768.js | 33 +++++ .../libjar/zipwriter/test/unit/test_bug433248.js | 68 +++++++++ .../libjar/zipwriter/test/unit/test_bug446708.js | 39 +++++ .../libjar/zipwriter/test/unit/test_bug467740.js | 35 +++++ .../libjar/zipwriter/test/unit/test_bug717061.js | 106 ++++++++++++++ .../libjar/zipwriter/test/unit/test_createempty.js | 15 ++ .../libjar/zipwriter/test/unit/test_deflatedata.js | 59 ++++++++ .../libjar/zipwriter/test/unit/test_directory.js | 26 ++++ .../zipwriter/test/unit/test_editexisting.js | 60 ++++++++ .../libjar/zipwriter/test/unit/test_storedata.js | 87 +++++++++++ modules/libjar/zipwriter/test/unit/test_sync.js | 65 +++++++++ .../libjar/zipwriter/test/unit/test_undochange.js | 45 ++++++ .../libjar/zipwriter/test/unit/test_zipcomment.js | 33 +++++ .../zipwriter/test/unit/test_zippermissions.js | 102 +++++++++++++ modules/libjar/zipwriter/test/unit/xpcshell.ini | 36 +++++ 34 files changed, 1621 insertions(+) create mode 100644 modules/libjar/zipwriter/test/unit/data/emptyfile.txt create mode 100644 modules/libjar/zipwriter/test/unit/data/smallfile.txt create mode 100644 modules/libjar/zipwriter/test/unit/data/test.png create mode 100644 modules/libjar/zipwriter/test/unit/data/test.txt create mode 100644 modules/libjar/zipwriter/test/unit/data/test.zip create mode 100644 modules/libjar/zipwriter/test/unit/data/test_bug399727.html create mode 100644 modules/libjar/zipwriter/test/unit/data/test_bug399727.zlib create mode 100644 modules/libjar/zipwriter/test/unit/data/test_bug446708/thumbs/st14-1.tiff create mode 100644 modules/libjar/zipwriter/test/unit/data/test_bug717061.gz create mode 100644 modules/libjar/zipwriter/test/unit/data/test_bug717061.html create mode 100644 modules/libjar/zipwriter/test/unit/head_zipwriter.js create mode 100644 modules/libjar/zipwriter/test/unit/test_alignment.js create mode 100644 modules/libjar/zipwriter/test/unit/test_asyncadd.js create mode 100644 modules/libjar/zipwriter/test/unit/test_asyncbadadd.js create mode 100644 modules/libjar/zipwriter/test/unit/test_asyncbadremove.js create mode 100644 modules/libjar/zipwriter/test/unit/test_asyncremove.js create mode 100644 modules/libjar/zipwriter/test/unit/test_bug399727.js create mode 100644 modules/libjar/zipwriter/test/unit/test_bug419769_1.js create mode 100644 modules/libjar/zipwriter/test/unit/test_bug419769_2.js create mode 100644 modules/libjar/zipwriter/test/unit/test_bug425768.js create mode 100644 modules/libjar/zipwriter/test/unit/test_bug433248.js create mode 100644 modules/libjar/zipwriter/test/unit/test_bug446708.js create mode 100644 modules/libjar/zipwriter/test/unit/test_bug467740.js create mode 100644 modules/libjar/zipwriter/test/unit/test_bug717061.js create mode 100644 modules/libjar/zipwriter/test/unit/test_createempty.js create mode 100644 modules/libjar/zipwriter/test/unit/test_deflatedata.js create mode 100644 modules/libjar/zipwriter/test/unit/test_directory.js create mode 100644 modules/libjar/zipwriter/test/unit/test_editexisting.js create mode 100644 modules/libjar/zipwriter/test/unit/test_storedata.js create mode 100644 modules/libjar/zipwriter/test/unit/test_sync.js create mode 100644 modules/libjar/zipwriter/test/unit/test_undochange.js create mode 100644 modules/libjar/zipwriter/test/unit/test_zipcomment.js create mode 100644 modules/libjar/zipwriter/test/unit/test_zippermissions.js create mode 100644 modules/libjar/zipwriter/test/unit/xpcshell.ini (limited to 'modules/libjar/zipwriter/test') diff --git a/modules/libjar/zipwriter/test/unit/data/emptyfile.txt b/modules/libjar/zipwriter/test/unit/data/emptyfile.txt new file mode 100644 index 0000000000..e69de29bb2 diff --git a/modules/libjar/zipwriter/test/unit/data/smallfile.txt b/modules/libjar/zipwriter/test/unit/data/smallfile.txt new file mode 100644 index 0000000000..e068fcf81d --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/data/smallfile.txt @@ -0,0 +1 @@ +Small (16 bytes) \ No newline at end of file diff --git a/modules/libjar/zipwriter/test/unit/data/test.png b/modules/libjar/zipwriter/test/unit/data/test.png new file mode 100644 index 0000000000..c648f7299d Binary files /dev/null and b/modules/libjar/zipwriter/test/unit/data/test.png differ diff --git a/modules/libjar/zipwriter/test/unit/data/test.txt b/modules/libjar/zipwriter/test/unit/data/test.txt new file mode 100644 index 0000000000..1040981a30 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/data/test.txt @@ -0,0 +1,5 @@ +This is a test text file for the zipwriter component. +It will be made available in the unit test directory. +It will also be compressed into a testcase zip file +made by a 3rd party zip tool to test the opening of +existing zip files. diff --git a/modules/libjar/zipwriter/test/unit/data/test.zip b/modules/libjar/zipwriter/test/unit/data/test.zip new file mode 100644 index 0000000000..96581fe8b5 Binary files /dev/null and b/modules/libjar/zipwriter/test/unit/data/test.zip differ diff --git a/modules/libjar/zipwriter/test/unit/data/test_bug399727.html b/modules/libjar/zipwriter/test/unit/data/test_bug399727.html new file mode 100644 index 0000000000..a6556274f1 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/data/test_bug399727.html @@ -0,0 +1,160 @@ + +TK's Sleater-Kinney pages + + + + + + + + + + +
+
+ + + + + + + +

TK's Sleater-Kinney pages

+
+ + + + + + + + + + +
+
+
+ + + + +
+

All Hands on the Bad One +

+

All Hands on the Bad One, "All Hands on the Bad One"

+

This, followed by Youth Decay - wow - most bands would be happy just to have just one of these songs.  S-K has a boatload.

+

Tabbing this was very frustrating. Riff 3 is a +masterpiece of guitar and vocal harmony, rhythm and guitar +interplay.  It has a bass intro and stacks guitars and vocals on +top. This is the definition of Sleater-Kinney magic. You can't get +it all done on two guitars.  You can get close.   I +haven't done it justice.

+

As usual the guitar 2's rhythms are vital to the S-K sound.  It's still good if you don't get it just right.

+

I sure hope I've done this well enough for you to figure out.

+
    +
  • +
    Quarter note = 153 or so.
    +
  • +
    Standard tuning. But tuning down 3 half steps makes Riff 3 easier and more authentic.
    +
  • +
    Tabbed where it felt good on the fretboard but there are alternatives.
    +
  • +
    Tuning down is required for authenticity but it sounds great in standard tuning
+


1:

+

All Hands on the Bad One Riff 1
+

 

+

2:

All Hands on the Bad One Riff 2
+

 

+

Riff 3 (3a and 3b) is a little masterpeice:

All Hands on the Bad One Riff 3a
+

All Hands on the Bad One Riff 3b

+

 

+

The Outro

+

All Hands on the Bad One Riff 4

+
+
+
+
+
+
+ The views expressed within this site pretty much represent those of the author.
+ Copyright (c) 2002 Terry Kearns. All rights reserved. +
+
+
+
+
+
+ \ No newline at end of file diff --git a/modules/libjar/zipwriter/test/unit/data/test_bug399727.zlib b/modules/libjar/zipwriter/test/unit/data/test_bug399727.zlib new file mode 100644 index 0000000000..080b63c520 Binary files /dev/null and b/modules/libjar/zipwriter/test/unit/data/test_bug399727.zlib differ diff --git a/modules/libjar/zipwriter/test/unit/data/test_bug446708/thumbs/st14-1.tiff b/modules/libjar/zipwriter/test/unit/data/test_bug446708/thumbs/st14-1.tiff new file mode 100644 index 0000000000..6b04657580 Binary files /dev/null and b/modules/libjar/zipwriter/test/unit/data/test_bug446708/thumbs/st14-1.tiff differ diff --git a/modules/libjar/zipwriter/test/unit/data/test_bug717061.gz b/modules/libjar/zipwriter/test/unit/data/test_bug717061.gz new file mode 100644 index 0000000000..f990c6e519 Binary files /dev/null and b/modules/libjar/zipwriter/test/unit/data/test_bug717061.gz differ diff --git a/modules/libjar/zipwriter/test/unit/data/test_bug717061.html b/modules/libjar/zipwriter/test/unit/data/test_bug717061.html new file mode 100644 index 0000000000..80ce0319b4 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/data/test_bug717061.html @@ -0,0 +1,16 @@ + +Lorem Ipsum +

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

+ +

Sed ut perspiciatis unde omnis iste natus error sit voluptatem accusantium doloremque laudantium, totam rem aperiam, eaque ipsa quae ab illo inventore veritatis et quasi architecto beatae vitae dicta sunt explicabo. Nemo enim ipsam voluptatem quia voluptas sit aspernatur aut odit aut fugit, sed quia consequuntur magni dolores eos qui ratione voluptatem sequi nesciunt. Neque porro quisquam est, qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit, sed quia non numquam eius modi tempora incidunt ut labore et dolore magnam aliquam quaerat voluptatem. Ut enim ad minima veniam, quis nostrum exercitationem ullam corporis suscipit laboriosam, nisi ut aliquid ex ea commodi consequatur? Quis autem vel eum iure reprehenderit qui in ea voluptate velit esse quam nihil molestiae consequatur, vel illum qui dolorem eum fugiat quo voluptas nulla pariatur?

+ +

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Temporibus autem quibusdam et aut officiis debitis aut rerum necessitatibus saepe eveniet ut et voluptates repudiandae sint et molestiae non recusandae. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

+ + diff --git a/modules/libjar/zipwriter/test/unit/head_zipwriter.js b/modules/libjar/zipwriter/test/unit/head_zipwriter.js new file mode 100644 index 0000000000..43136d6cec --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/head_zipwriter.js @@ -0,0 +1,60 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const NS_ERROR_IN_PROGRESS = 2152398863; + +const PR_RDONLY = 0x01; +const PR_WRONLY = 0x02; +const PR_RDWR = 0x04; +const PR_CREATE_FILE = 0x08; +const PR_APPEND = 0x10; +const PR_TRUNCATE = 0x20; +const PR_SYNC = 0x40; +const PR_EXCL = 0x80; + +const ZIP_EOCDR_HEADER_SIZE = 22; +const ZIP_FILE_HEADER_SIZE = 30; +const ZIP_CDS_HEADER_SIZE = 46; +const ZIP_METHOD_STORE = 0; +const ZIP_METHOD_DEFLATE = 8; +const ZIP_EXTENDED_TIMESTAMP_SIZE = 9; + +const PR_USEC_PER_MSEC = 1000; +const PR_USEC_PER_SEC = 1000000; +const PR_MSEC_PER_SEC = 1000; + +const DATA_DIR = "data/"; + +var ioSvc = Services.io; + +var ZipWriter = Components.Constructor( + "@mozilla.org/zipwriter;1", + "nsIZipWriter" +); +var ZipReader = Components.Constructor( + "@mozilla.org/libjar/zip-reader;1", + "nsIZipReader", + "open" +); + +var tmpDir = do_get_profile(); +var tmpFile = tmpDir.clone(); +tmpFile.append("zipwriter-test.zip"); +if (tmpFile.exists()) { + tmpFile.remove(true); +} + +var zipW = new ZipWriter(); + +registerCleanupFunction(function () { + try { + zipW.close(); + } catch (e) { + // Just ignore a failure here and attempt to delete the file anyway. + } + if (tmpFile.exists()) { + tmpFile.remove(true); + } +}); diff --git a/modules/libjar/zipwriter/test/unit/test_alignment.js b/modules/libjar/zipwriter/test/unit/test_alignment.js new file mode 100644 index 0000000000..afc5cf2b30 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_alignment.js @@ -0,0 +1,117 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const DATA = "ZIP WRITER TEST DATA"; +const FILENAME = "test_data.txt"; +const time = 1199145600000; // Jan 1st 2008 + +var TESTS = [ + { + name: "test.txt", + compression: Ci.nsIZipWriter.COMPRESSION_DEFAULT, + }, + { + name: "test.png", + compression: Ci.nsIZipWriter.COMPRESSION_NONE, + }, +]; + +function swap16(n) { + return (((n >> 8) & 0xff) << 0) | (((n >> 0) & 0xff) << 8); +} + +function swap32(n) { + return ( + (((n >> 24) & 0xff) << 0) | + (((n >> 16) & 0xff) << 8) | + (((n >> 8) & 0xff) << 16) | + (((n >> 0) & 0xff) << 24) + ); +} + +function move_to_data(bis, offset) { + bis.readBytes(18); // Move to compressed size + var size = swap32(bis.read32()); + bis.readBytes(4); + var file_len = swap16(bis.read16()); + var extra_len = swap16(bis.read16()); + bis.readBytes(file_len); + bis.readBytes(extra_len); + offset += ZIP_FILE_HEADER_SIZE + file_len + extra_len; + + return { offset, size }; +} + +function test_alignment(align_size) { + // Create zip for testing. + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + for (var i = 0; i < TESTS.length; i++) { + var source = do_get_file(DATA_DIR + TESTS[i].name); + zipW.addEntryFile(TESTS[i].name, TESTS[i].compression, source, false); + } + var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance( + Ci.nsIStringInputStream + ); + stream.setData(DATA, DATA.length); + zipW.addEntryStream( + FILENAME, + time * PR_USEC_PER_MSEC, + Ci.nsIZipWriter.COMPRESSION_NONE, + stream, + false + ); + zipW.alignStoredFiles(align_size); + zipW.close(); + + // Check data can be decompressed. + var zipR = new ZipReader(tmpFile); + stream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance( + Ci.nsIScriptableInputStream + ); + stream.init(zipR.getInputStream(FILENAME)); + var result = stream.read(DATA.length); + Assert.equal(result, DATA); + stream.close(); + zipR.close(); + + // Check data is correct and aligned. + var fis = Cc["@mozilla.org/network/file-input-stream;1"].createInstance( + Ci.nsIFileInputStream + ); + fis.init(tmpFile, -1, -1, null); + let bis = Cc["@mozilla.org/binaryinputstream;1"].createInstance( + Ci.nsIBinaryInputStream + ); + bis.setInputStream(fis); + var offset = 0; + + var ret = move_to_data(bis, offset); // "test.txt" + offset = ret.offset; + bis.readBytes(ret.size); + offset += ret.size; + + ret = move_to_data(bis, offset); // "test.png" + offset = ret.offset; + Assert.equal(offset % align_size, 0); + bis.readBytes(ret.size); + offset += ret.size; + + ret = move_to_data(bis, offset); // "test_data.txt" + offset = ret.offset; + result = bis.readBytes(DATA.length); + Assert.equal(result, DATA); + Assert.equal(offset % align_size, 0); + + fis.close(); + bis.close(); +} + +function run_test() { + test_alignment(2); + test_alignment(4); + test_alignment(16); + test_alignment(4096); + test_alignment(32768); +} diff --git a/modules/libjar/zipwriter/test/unit/test_asyncadd.js b/modules/libjar/zipwriter/test/unit/test_asyncadd.js new file mode 100644 index 0000000000..ef70d292d7 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_asyncadd.js @@ -0,0 +1,111 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +// Values taken from using zipinfo to list the test.zip contents +var TESTS = [ + { + name: "test.txt", + size: 232, + crc: 0x0373ac26, + }, + { + name: "test.png", + size: 3402, + crc: 0x504a5c30, + }, +]; + +var size = 0; + +var observer = { + onStartRequest(request) {}, + + onStopRequest(request, status) { + Assert.equal(status, Cr.NS_OK); + + zipW.close(); + size += ZIP_EOCDR_HEADER_SIZE; + + Assert.equal(size, tmpFile.fileSize); + + // Test the stored data with the zipreader + var zipR = new ZipReader(tmpFile); + + for (var i = 0; i < TESTS.length; i++) { + var source = do_get_file(DATA_DIR + TESTS[i].name); + for (let method in methods) { + var entryName = method + "/" + TESTS[i].name; + Assert.ok(zipR.hasEntry(entryName)); + + var entry = zipR.getEntry(entryName); + Assert.equal(entry.realSize, TESTS[i].size); + Assert.equal(entry.size, TESTS[i].size); + Assert.equal(entry.CRC32, TESTS[i].crc); + Assert.equal( + Math.floor(entry.lastModifiedTime / PR_USEC_PER_SEC), + Math.floor(source.lastModifiedTime / PR_MSEC_PER_SEC) + ); + + zipR.test(entryName); + } + } + + zipR.close(); + do_test_finished(); + }, +}; + +var methods = { + file: function method_file(entry, source) { + zipW.addEntryFile(entry, Ci.nsIZipWriter.COMPRESSION_NONE, source, true); + }, + channel: function method_channel(entry, source) { + zipW.addEntryChannel( + entry, + source.lastModifiedTime * PR_MSEC_PER_SEC, + Ci.nsIZipWriter.COMPRESSION_NONE, + NetUtil.newChannel({ + uri: ioSvc.newFileURI(source), + loadUsingSystemPrincipal: true, + }), + true + ); + }, + stream: function method_stream(entry, source) { + zipW.addEntryStream( + entry, + source.lastModifiedTime * PR_MSEC_PER_SEC, + Ci.nsIZipWriter.COMPRESSION_NONE, + NetUtil.newChannel({ + uri: ioSvc.newFileURI(source), + loadUsingSystemPrincipal: true, + }).open(), + true + ); + }, +}; + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + for (var i = 0; i < TESTS.length; i++) { + var source = do_get_file(DATA_DIR + TESTS[i].name); + for (let method in methods) { + var entry = method + "/" + TESTS[i].name; + methods[method](entry, source); + size += + ZIP_FILE_HEADER_SIZE + + ZIP_CDS_HEADER_SIZE + + ZIP_EXTENDED_TIMESTAMP_SIZE * 2 + + entry.length * 2 + + TESTS[i].size; + } + } + do_test_pending(); + zipW.processQueue(observer, null); + Assert.ok(zipW.inQueue); +} diff --git a/modules/libjar/zipwriter/test/unit/test_asyncbadadd.js b/modules/libjar/zipwriter/test/unit/test_asyncbadadd.js new file mode 100644 index 0000000000..7d1c4ec610 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_asyncbadadd.js @@ -0,0 +1,31 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const FILENAME = "missing.txt"; + +var observer = { + onStartRequest(request) {}, + + onStopRequest(request, status) { + Assert.equal(status, Cr.NS_ERROR_FILE_NOT_FOUND); + zipW.close(); + Assert.equal(ZIP_EOCDR_HEADER_SIZE, tmpFile.fileSize); + do_test_finished(); + }, +}; + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + var source = tmpDir.clone(); + source.append(FILENAME); + zipW.addEntryFile(FILENAME, Ci.nsIZipWriter.COMPRESSION_NONE, source, true); + + do_test_pending(); + zipW.processQueue(observer, null); + + // With nothing to actually do the queue would have completed immediately + Assert.ok(!zipW.inQueue); +} diff --git a/modules/libjar/zipwriter/test/unit/test_asyncbadremove.js b/modules/libjar/zipwriter/test/unit/test_asyncbadremove.js new file mode 100644 index 0000000000..623bc46c59 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_asyncbadremove.js @@ -0,0 +1,27 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const FILENAME = "missing.txt"; + +var observer = { + onStartRequest(request) {}, + + onStopRequest(request, status) { + Assert.equal(status, Cr.NS_ERROR_FILE_NOT_FOUND); + zipW.close(); + Assert.equal(ZIP_EOCDR_HEADER_SIZE, tmpFile.fileSize); + do_test_finished(); + }, +}; + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + zipW.removeEntry(FILENAME, true); + do_test_pending(); + zipW.processQueue(observer, null); + + // With nothing to actually do the queue would have completed immediately + Assert.ok(!zipW.inQueue); +} diff --git a/modules/libjar/zipwriter/test/unit/test_asyncremove.js b/modules/libjar/zipwriter/test/unit/test_asyncremove.js new file mode 100644 index 0000000000..7342e18e4f --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_asyncremove.js @@ -0,0 +1,40 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +var TESTS = ["test.txt", "test.png"]; + +var observer = { + onStartRequest(request) {}, + + onStopRequest(request, status) { + Assert.equal(status, Cr.NS_OK); + + zipW.close(); + + // Empty zip file should just be the end of central directory marker + var newTmpFile = tmpFile.clone(); + Assert.equal(newTmpFile.fileSize, ZIP_EOCDR_HEADER_SIZE); + do_test_finished(); + }, +}; + +function run_test() { + // Copy our test zip to the tmp dir so we can modify it + var testzip = do_get_file(DATA_DIR + "test.zip"); + testzip.copyTo(tmpDir, tmpFile.leafName); + + Assert.ok(tmpFile.exists()); + + zipW.open(tmpFile, PR_RDWR); + + for (var i = 0; i < TESTS.length; i++) { + Assert.ok(zipW.hasEntry(TESTS[i])); + zipW.removeEntry(TESTS[i], true); + } + + do_test_pending(); + zipW.processQueue(observer, null); + Assert.ok(zipW.inQueue); +} diff --git a/modules/libjar/zipwriter/test/unit/test_bug399727.js b/modules/libjar/zipwriter/test/unit/test_bug399727.js new file mode 100644 index 0000000000..2967f69737 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_bug399727.js @@ -0,0 +1,107 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +function BinaryComparer(file, callback) { + var fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance( + Ci.nsIFileInputStream + ); + fstream.init(file, -1, 0, 0); + this.length = file.fileSize; + this.fileStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance( + Ci.nsIBinaryInputStream + ); + this.fileStream.setInputStream(fstream); + this.offset = 0; + this.callback = callback; +} + +BinaryComparer.prototype = { + fileStream: null, + offset: null, + length: null, + callback: null, + + onStartRequest(aRequest) {}, + + onStopRequest(aRequest, aStatusCode) { + this.fileStream.close(); + Assert.equal(aStatusCode, Cr.NS_OK); + Assert.equal(this.offset, this.length); + this.callback(); + }, + + onDataAvailable(aRequest, aInputStream, aOffset, aCount) { + var stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance( + Ci.nsIBinaryInputStream + ); + stream.setInputStream(aInputStream); + var source, actual; + for (var i = 0; i < aCount; i++) { + try { + source = this.fileStream.read8(); + } catch (e) { + do_throw("Unable to read from file at offset " + this.offset + " " + e); + } + try { + actual = stream.read8(); + } catch (e) { + do_throw( + "Unable to read from converted stream at offset " + + this.offset + + " " + + e + ); + } + if (source != actual) { + do_throw( + "Invalid value " + + actual + + " at offset " + + this.offset + + ", should have been " + + source + ); + } + this.offset++; + } + }, +}; + +function comparer_callback() { + do_test_finished(); +} + +function run_test() { + var source = do_get_file(DATA_DIR + "test_bug399727.html"); + var comparer = new BinaryComparer( + do_get_file(DATA_DIR + "test_bug399727.zlib"), + comparer_callback + ); + + // Prepare the stream converter + var scs = Cc["@mozilla.org/streamConverters;1"].getService( + Ci.nsIStreamConverterService + ); + var converter = scs.asyncConvertData( + "uncompressed", + "deflate", + comparer, + null + ); + + // Open the expected output file + var fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance( + Ci.nsIFileInputStream + ); + fstream.init(source, -1, 0, 0); + + // Set up a pump to push data from the file to the stream converter + var pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance( + Ci.nsIInputStreamPump + ); + pump.init(fstream, 0, 0, true); + pump.asyncRead(converter); + do_test_pending(); +} diff --git a/modules/libjar/zipwriter/test/unit/test_bug419769_1.js b/modules/libjar/zipwriter/test/unit/test_bug419769_1.js new file mode 100644 index 0000000000..0e4e70fe01 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_bug419769_1.js @@ -0,0 +1,75 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const DATA = ""; +const FILENAME = "test.txt"; +const CRC = 0x00000000; +// XXX Must use a constant time here away from DST changes. See bug 402434. +const time = 1199145600000; // Jan 1st 2008 + +function testpass(source) { + // Should exist. + Assert.ok(source.hasEntry(FILENAME)); + + var entry = source.getEntry(FILENAME); + Assert.notEqual(entry, null); + + Assert.ok(!entry.isDirectory); + + // Should be stored + Assert.equal(entry.compression, ZIP_METHOD_DEFLATE); + + Assert.equal(entry.lastModifiedTime / PR_USEC_PER_MSEC, time); + + // File size should match our data size. + Assert.equal(entry.realSize, DATA.length); + + // Check that the CRC is accurate + Assert.equal(entry.CRC32, CRC); +} + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + // Shouldn't be there to start with. + Assert.ok(!zipW.hasEntry(FILENAME)); + + Assert.ok(!zipW.inQueue); + + var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance( + Ci.nsIStringInputStream + ); + stream.setData(DATA, DATA.length); + zipW.addEntryStream( + FILENAME, + time * PR_USEC_PER_MSEC, + Ci.nsIZipWriter.COMPRESSION_BEST, + stream, + false + ); + + // Check that zip state is right at this stage. + testpass(zipW); + zipW.close(); + + // Check to see if we get the same results loading afresh. + zipW.open(tmpFile, PR_RDWR); + testpass(zipW); + zipW.close(); + + // Test the stored data with the zipreader + var zipR = new ZipReader(tmpFile); + testpass(zipR); + zipR.test(FILENAME); + stream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance( + Ci.nsIScriptableInputStream + ); + stream.init(zipR.getInputStream(FILENAME)); + var result = stream.read(DATA.length); + stream.close(); + zipR.close(); + + Assert.equal(result, DATA); +} diff --git a/modules/libjar/zipwriter/test/unit/test_bug419769_2.js b/modules/libjar/zipwriter/test/unit/test_bug419769_2.js new file mode 100644 index 0000000000..9841f52d2e --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_bug419769_2.js @@ -0,0 +1,62 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const DATA = ""; +const FILENAME = "test.txt"; +const CRC = 0x00000000; + +function testpass(source) { + // Should exist. + Assert.ok(source.hasEntry(FILENAME)); + + var entry = source.getEntry(FILENAME); + Assert.notEqual(entry, null); + + Assert.ok(!entry.isDirectory); + + // Should be stored + Assert.equal(entry.compression, ZIP_METHOD_DEFLATE); + + // File size should match our data size. + Assert.equal(entry.realSize, DATA.length); + + // Check that the CRC is accurate + Assert.equal(entry.CRC32, CRC); +} + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + // Shouldn't be there to start with. + Assert.ok(!zipW.hasEntry(FILENAME)); + + Assert.ok(!zipW.inQueue); + + var file = do_get_file(DATA_DIR + "emptyfile.txt"); + zipW.addEntryFile(FILENAME, Ci.nsIZipWriter.COMPRESSION_BEST, file, false); + + // Check that zip state is right at this stage. + testpass(zipW); + zipW.close(); + + // Check to see if we get the same results loading afresh. + zipW.open(tmpFile, PR_RDWR); + testpass(zipW); + zipW.close(); + + // Test the stored data with the zipreader + var zipR = new ZipReader(tmpFile); + testpass(zipR); + zipR.test(FILENAME); + var stream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance( + Ci.nsIScriptableInputStream + ); + stream.init(zipR.getInputStream(FILENAME)); + var result = stream.read(DATA.length); + stream.close(); + zipR.close(); + + Assert.equal(result, DATA); +} diff --git a/modules/libjar/zipwriter/test/unit/test_bug425768.js b/modules/libjar/zipwriter/test/unit/test_bug425768.js new file mode 100644 index 0000000000..d3a9b30ba6 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_bug425768.js @@ -0,0 +1,33 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const DIRNAME = "test/"; +const time = Date.now(); + +function run_test() { + // Copy in the test file. + var source = do_get_file("data/test.zip"); + source.copyTo(tmpFile.parent, tmpFile.leafName); + + // Open it and add something so the CDS is rewritten. + zipW.open(tmpFile, PR_RDWR | PR_APPEND); + zipW.addEntryDirectory(DIRNAME, time * PR_USEC_PER_MSEC, false); + Assert.ok(zipW.hasEntry(DIRNAME)); + zipW.close(); + + var zipR = new ZipReader(tmpFile); + Assert.ok(zipR.hasEntry(DIRNAME)); + zipR.close(); + + // Adding the directory would have added a fixed amount to the file size. + // Any difference suggests the CDS was written out incorrectly. + var extra = + ZIP_FILE_HEADER_SIZE + + ZIP_CDS_HEADER_SIZE + + DIRNAME.length * 2 + + ZIP_EXTENDED_TIMESTAMP_SIZE * 2; + + Assert.equal(source.fileSize + extra, tmpFile.fileSize); +} diff --git a/modules/libjar/zipwriter/test/unit/test_bug433248.js b/modules/libjar/zipwriter/test/unit/test_bug433248.js new file mode 100644 index 0000000000..7b8c0525b8 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_bug433248.js @@ -0,0 +1,68 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +function run_test() { + // zipW is an uninitialised zipwriter at this point. + try { + zipW.file; + do_throw("Should have thrown uninitialized error."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_NOT_INITIALIZED); + } + + try { + zipW.comment; + do_throw("Should have thrown uninitialized error."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_NOT_INITIALIZED); + } + + try { + zipW.comment = "test"; + do_throw("Should have thrown uninitialized error."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_NOT_INITIALIZED); + } + + try { + zipW.addEntryDirectory("test", 0, false); + do_throw("Should have thrown uninitialized error."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_NOT_INITIALIZED); + } + + try { + zipW.addEntryFile( + "test", + Ci.nsIZipWriter.COMPRESSION_DEFAULT, + tmpDir, + false + ); + do_throw("Should have thrown uninitialized error."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_NOT_INITIALIZED); + } + + try { + zipW.removeEntry("test", false); + do_throw("Should have thrown uninitialized error."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_NOT_INITIALIZED); + } + + try { + zipW.processQueue(null, null); + do_throw("Should have thrown uninitialized error."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_NOT_INITIALIZED); + } + + try { + zipW.close(); + do_throw("Should have thrown uninitialized error."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_NOT_INITIALIZED); + } +} diff --git a/modules/libjar/zipwriter/test/unit/test_bug446708.js b/modules/libjar/zipwriter/test/unit/test_bug446708.js new file mode 100644 index 0000000000..cff67d848a --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_bug446708.js @@ -0,0 +1,39 @@ +function run_test() { + var testBundle = do_get_file("data/test_bug446708"); + + RecursivelyZipDirectory(testBundle); +} + +// Add |file| to the zip. |path| is the current path for the file. +function AddToZip(zipWriter, path, file) { + var currentPath = path + file.leafName; + + if (file.isDirectory()) { + currentPath += "/"; + } + + // THIS IS WHERE THE ERROR OCCURS, FOR THE FILE "st14-1.tiff" IN "test_bug446708" + zipWriter.addEntryFile( + currentPath, + Ci.nsIZipWriter.COMPRESSION_DEFAULT, + file, + false + ); + + // if it's a dir, continue adding its contents recursively... + if (file.isDirectory()) { + var entries = file.QueryInterface(Ci.nsIFile).directoryEntries; + while (entries.hasMoreElements()) { + var entry = entries.nextFile; + AddToZip(zipWriter, currentPath, entry); + } + } + + // ...otherwise, we're done +} + +function RecursivelyZipDirectory(bundle) { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + AddToZip(zipW, "", bundle); + zipW.close(); +} diff --git a/modules/libjar/zipwriter/test/unit/test_bug467740.js b/modules/libjar/zipwriter/test/unit/test_bug467740.js new file mode 100644 index 0000000000..4108003bd3 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_bug467740.js @@ -0,0 +1,35 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +function run_test() { + // In this test we try to open some files that aren't archives: + // - An empty file, that is certainly not an archive. + // - A file that couldn't be mistaken for archive, since it is too small. + // - A file that could be mistaken for archive, if we checked only the file + // size, but is invalid since it contains no ZIP signature. + var invalidArchives = ["emptyfile.txt", "smallfile.txt", "test.png"]; + + invalidArchives.forEach(function (invalidArchive) { + // Get a reference to the invalid file + var invalidFile = do_get_file(DATA_DIR + invalidArchive); + + // Opening the invalid file should fail (but not crash) + try { + zipW.open(invalidFile, PR_RDWR); + do_throw( + "Should have thrown NS_ERROR_FILE_CORRUPTED on " + invalidArchive + " !" + ); + } catch (e) { + if ( + !( + e instanceof Ci.nsIException && e.result == Cr.NS_ERROR_FILE_CORRUPTED + ) + ) { + throw e; + } + // do nothing + } + }); +} diff --git a/modules/libjar/zipwriter/test/unit/test_bug717061.js b/modules/libjar/zipwriter/test/unit/test_bug717061.js new file mode 100644 index 0000000000..cb3faa3aa7 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_bug717061.js @@ -0,0 +1,106 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0 + */ + +function BinaryComparer(file, callback) { + var fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance( + Ci.nsIFileInputStream + ); + fstream.init(file, -1, 0, 0); + this.length = file.fileSize; + this.fileStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance( + Ci.nsIBinaryInputStream + ); + this.fileStream.setInputStream(fstream); + this.offset = 0; + this.callback = callback; +} + +BinaryComparer.prototype = { + fileStream: null, + offset: null, + length: null, + callback: null, + + onStartRequest(aRequest) {}, + + onStopRequest(aRequest, aStatusCode) { + this.fileStream.close(); + Assert.equal(aStatusCode, Cr.NS_OK); + Assert.equal(this.offset, this.length); + this.callback(); + }, + + onDataAvailable(aRequest, aInputStream, aOffset, aCount) { + var stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance( + Ci.nsIBinaryInputStream + ); + stream.setInputStream(aInputStream); + var source, actual; + for (var i = 0; i < aCount; i++) { + try { + source = this.fileStream.read8(); + } catch (e) { + do_throw("Unable to read from file at offset " + this.offset + " " + e); + } + try { + actual = stream.read8(); + } catch (e) { + do_throw( + "Unable to read from converted stream at offset " + + this.offset + + " " + + e + ); + } + // The byte at offset 9 is the OS byte (see RFC 1952, section 2.3), which + // can legitimately differ when the source is compressed on different + // operating systems. The actual .gz for this test was created on a Unix + // system, but we want the test to work correctly everywhere. So ignore + // the byte at offset 9. + if (this.offset != 9 && source != actual) { + do_throw( + "Invalid value " + + actual + + " at offset " + + this.offset + + ", should have been " + + source + ); + } + this.offset++; + } + }, +}; + +function comparer_callback() { + do_test_finished(); +} + +function run_test() { + var source = do_get_file(DATA_DIR + "test_bug717061.html"); + var comparer = new BinaryComparer( + do_get_file(DATA_DIR + "test_bug717061.gz"), + comparer_callback + ); + + // Prepare the stream converter + var scs = Cc["@mozilla.org/streamConverters;1"].getService( + Ci.nsIStreamConverterService + ); + var converter = scs.asyncConvertData("uncompressed", "gzip", comparer, null); + + // Open the expected output file + var fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance( + Ci.nsIFileInputStream + ); + fstream.init(source, -1, 0, 0); + + // Set up a pump to push data from the file to the stream converter + var pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance( + Ci.nsIInputStreamPump + ); + pump.init(fstream, 0, 0, true); + pump.asyncRead(converter); + do_test_pending(); +} diff --git a/modules/libjar/zipwriter/test/unit/test_createempty.js b/modules/libjar/zipwriter/test/unit/test_createempty.js new file mode 100644 index 0000000000..464c3b243f --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_createempty.js @@ -0,0 +1,15 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + zipW.close(); + + // Should have created a zip file + Assert.ok(tmpFile.exists()); + + // Empty zip file should just be the end of central directory marker + Assert.equal(tmpFile.fileSize, ZIP_EOCDR_HEADER_SIZE); +} diff --git a/modules/libjar/zipwriter/test/unit/test_deflatedata.js b/modules/libjar/zipwriter/test/unit/test_deflatedata.js new file mode 100644 index 0000000000..a8a5a0c536 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_deflatedata.js @@ -0,0 +1,59 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const DATA = "ZIP WRITER TEST DATA"; +const FILENAME = "test.txt"; +const CRC = 0xe6164331; +// XXX Must use a constant time here away from DST changes. See bug 402434. +const time = 1199145600000; // Jan 1st 2008 + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + // Shouldn't be there to start with. + Assert.ok(!zipW.hasEntry(FILENAME)); + + Assert.ok(!zipW.inQueue); + + var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance( + Ci.nsIStringInputStream + ); + stream.setData(DATA, DATA.length); + zipW.addEntryStream( + FILENAME, + time * PR_USEC_PER_MSEC, + Ci.nsIZipWriter.COMPRESSION_BEST, + stream, + false + ); + + var entry = zipW.getEntry(FILENAME); + + Assert.ok(entry != null); + + // Check entry seems right. + Assert.equal(entry.compression, ZIP_METHOD_DEFLATE); + Assert.equal(entry.CRC32, CRC); + Assert.equal(entry.realSize, DATA.length); + Assert.equal(entry.lastModifiedTime / PR_USEC_PER_MSEC, time); + + zipW.close(); + + // Test the stored data with the zipreader + var zipR = new ZipReader(tmpFile); + Assert.ok(zipR.hasEntry(FILENAME)); + + zipR.test(FILENAME); + + stream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance( + Ci.nsIScriptableInputStream + ); + stream.init(zipR.getInputStream(FILENAME)); + var result = stream.read(DATA.length); + stream.close(); + zipR.close(); + + Assert.equal(result, DATA); +} diff --git a/modules/libjar/zipwriter/test/unit/test_directory.js b/modules/libjar/zipwriter/test/unit/test_directory.js new file mode 100644 index 0000000000..70e93bbf08 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_directory.js @@ -0,0 +1,26 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const DIRNAME1 = "test"; +const DIRNAME1_CORRECT = "test/"; +const DIRNAME2 = "test2/"; +const time = Date.now(); + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + zipW.addEntryDirectory(DIRNAME1, time * PR_USEC_PER_MSEC, false); + Assert.ok(!zipW.hasEntry(DIRNAME1)); + Assert.ok(zipW.hasEntry(DIRNAME1_CORRECT)); + var entry = zipW.getEntry(DIRNAME1_CORRECT); + Assert.ok(entry.isDirectory); + + zipW.addEntryDirectory(DIRNAME2, time * PR_USEC_PER_MSEC, false); + Assert.ok(zipW.hasEntry(DIRNAME2)); + entry = zipW.getEntry(DIRNAME2); + Assert.ok(entry.isDirectory); + + zipW.close(); +} diff --git a/modules/libjar/zipwriter/test/unit/test_editexisting.js b/modules/libjar/zipwriter/test/unit/test_editexisting.js new file mode 100644 index 0000000000..c58447260e --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_editexisting.js @@ -0,0 +1,60 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +// Values taken from using zipinfo to list the test.zip contents +var TESTS = [ + { + name: "test.txt", + size: 232, + crc: 0x0373ac26, + time: Date.UTC(2007, 4, 1, 20, 44, 55), + }, + { + name: "test.png", + size: 3402, + crc: 0x504a5c30, + time: Date.UTC(2007, 4, 1, 20, 49, 39), + }, +]; +var BADENTRY = "unknown.txt"; + +function run_test() { + // Copy our test zip to the tmp dir so we can modify it + var testzip = do_get_file(DATA_DIR + "test.zip"); + testzip.copyTo(tmpDir, tmpFile.leafName); + + Assert.ok(tmpFile.exists()); + + zipW.open(tmpFile, PR_RDWR); + + for (let i = 0; i < TESTS.length; i++) { + Assert.ok(zipW.hasEntry(TESTS[i].name)); + var entry = zipW.getEntry(TESTS[i].name); + Assert.ok(entry != null); + + Assert.equal(entry.realSize, TESTS[i].size); + Assert.equal(entry.CRC32, TESTS[i].crc); + Assert.equal(entry.lastModifiedTime / PR_USEC_PER_MSEC, TESTS[i].time); + } + + try { + zipW.removeEntry(BADENTRY, false); + do_throw("shouldn't be able to remove an entry that doesn't exist"); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_FILE_NOT_FOUND); + } + + for (let i = 0; i < TESTS.length; i++) { + zipW.removeEntry(TESTS[i].name, false); + } + + zipW.close(); + + // Certain platforms cache the file size so get a fresh file to check. + tmpFile = tmpFile.clone(); + + // Empty zip file should just be the end of central directory marker + Assert.equal(tmpFile.fileSize, ZIP_EOCDR_HEADER_SIZE); +} diff --git a/modules/libjar/zipwriter/test/unit/test_storedata.js b/modules/libjar/zipwriter/test/unit/test_storedata.js new file mode 100644 index 0000000000..8983f7be42 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_storedata.js @@ -0,0 +1,87 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const DATA = "ZIP WRITER TEST DATA"; +const FILENAME = "test.txt"; +const CRC = 0xe6164331; +// XXX Must use a constant time here away from DST changes. See bug 402434. +const time = 1199145600000; // Jan 1st 2008 + +function testpass(source) { + // Should exist. + Assert.ok(source.hasEntry(FILENAME)); + + var entry = source.getEntry(FILENAME); + Assert.notEqual(entry, null); + + Assert.ok(!entry.isDirectory); + + // Should be stored + Assert.equal(entry.compression, ZIP_METHOD_STORE); + + Assert.equal(entry.lastModifiedTime / PR_USEC_PER_MSEC, time); + + // File size should match our data size. + Assert.equal(entry.realSize, DATA.length); + // When stored sizes should match. + Assert.equal(entry.size, entry.realSize); + + // Check that the CRC is accurate + Assert.equal(entry.CRC32, CRC); +} + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + // Shouldn't be there to start with. + Assert.ok(!zipW.hasEntry(FILENAME)); + + Assert.ok(!zipW.inQueue); + + var stream = Cc["@mozilla.org/io/string-input-stream;1"].createInstance( + Ci.nsIStringInputStream + ); + stream.setData(DATA, DATA.length); + zipW.addEntryStream( + FILENAME, + time * PR_USEC_PER_MSEC, + Ci.nsIZipWriter.COMPRESSION_NONE, + stream, + false + ); + + // Check that zip state is right at this stage. + testpass(zipW); + zipW.close(); + + Assert.equal( + tmpFile.fileSize, + DATA.length + + ZIP_FILE_HEADER_SIZE + + ZIP_CDS_HEADER_SIZE + + ZIP_EXTENDED_TIMESTAMP_SIZE * 2 + + FILENAME.length * 2 + + ZIP_EOCDR_HEADER_SIZE + ); + + // Check to see if we get the same results loading afresh. + zipW.open(tmpFile, PR_RDWR); + testpass(zipW); + zipW.close(); + + // Test the stored data with the zipreader + var zipR = new ZipReader(tmpFile); + testpass(zipR); + zipR.test(FILENAME); + stream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance( + Ci.nsIScriptableInputStream + ); + stream.init(zipR.getInputStream(FILENAME)); + var result = stream.read(DATA.length); + stream.close(); + zipR.close(); + + Assert.equal(result, DATA); +} diff --git a/modules/libjar/zipwriter/test/unit/test_sync.js b/modules/libjar/zipwriter/test/unit/test_sync.js new file mode 100644 index 0000000000..39a27db548 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_sync.js @@ -0,0 +1,65 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +// Values taken from using zipinfo to list the test.zip contents +var TESTS = [ + { + name: "test.txt", + size: 232, + crc: 0x0373ac26, + }, + { + name: "test.png", + size: 3402, + crc: 0x504a5c30, + }, +]; + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + var size = 0; + for (let i = 0; i < TESTS.length; i++) { + let source = do_get_file(DATA_DIR + TESTS[i].name); + zipW.addEntryFile( + TESTS[i].name, + Ci.nsIZipWriter.COMPRESSION_NONE, + source, + false + ); + size += + ZIP_FILE_HEADER_SIZE + + ZIP_CDS_HEADER_SIZE + + ZIP_EXTENDED_TIMESTAMP_SIZE * 2 + + TESTS[i].name.length * 2 + + TESTS[i].size; + } + + zipW.close(); + size += ZIP_EOCDR_HEADER_SIZE; + + Assert.equal(size, tmpFile.fileSize); + + // Test the stored data with the zipreader + var zipR = new ZipReader(tmpFile); + + for (let i = 0; i < TESTS.length; i++) { + let source = do_get_file(DATA_DIR + TESTS[i].name); + Assert.ok(zipR.hasEntry(TESTS[i].name)); + + var entry = zipR.getEntry(TESTS[i].name); + Assert.equal(entry.realSize, TESTS[i].size); + Assert.equal(entry.size, TESTS[i].size); + Assert.equal(entry.CRC32, TESTS[i].crc); + Assert.equal( + Math.floor(entry.lastModifiedTime / PR_USEC_PER_SEC), + Math.floor(source.lastModifiedTime / PR_MSEC_PER_SEC) + ); + + zipR.test(TESTS[i].name); + } + + zipR.close(); +} diff --git a/modules/libjar/zipwriter/test/unit/test_undochange.js b/modules/libjar/zipwriter/test/unit/test_undochange.js new file mode 100644 index 0000000000..bb1de36b1d --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_undochange.js @@ -0,0 +1,45 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +// Values taken from using zipinfo to list the test.zip contents +var TESTS = ["test.txt", "test.png"]; + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + + for (let i = 0; i < TESTS.length; i++) { + let source = do_get_file(DATA_DIR + TESTS[i]); + zipW.addEntryFile( + TESTS[i], + Ci.nsIZipWriter.COMPRESSION_NONE, + source, + false + ); + } + + try { + let source = do_get_file(DATA_DIR + TESTS[0]); + zipW.addEntryFile( + TESTS[0], + Ci.nsIZipWriter.COMPRESSION_NONE, + source, + false + ); + do_throw("Should not be able to add the same file twice"); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_FILE_ALREADY_EXISTS); + } + + // Remove all the tests and see if we are left with an empty zip + for (let i = 0; i < TESTS.length; i++) { + zipW.removeEntry(TESTS[i], false); + } + + zipW.close(); + + // Empty zip file should just be the end of central directory marker + var newTmpFile = tmpFile.clone(); + Assert.equal(newTmpFile.fileSize, ZIP_EOCDR_HEADER_SIZE); +} diff --git a/modules/libjar/zipwriter/test/unit/test_zipcomment.js b/modules/libjar/zipwriter/test/unit/test_zipcomment.js new file mode 100644 index 0000000000..afe0fd7886 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_zipcomment.js @@ -0,0 +1,33 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +const DATA = "ZIP WRITER TEST COMMENT"; +const DATA2 = "ANOTHER ONE"; + +function run_test() { + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + zipW.comment = DATA; + zipW.close(); + + // Should have created a zip file + Assert.ok(tmpFile.exists()); + + // Empty zip file should just be the end of central directory marker + // and comment + Assert.equal(tmpFile.fileSize, ZIP_EOCDR_HEADER_SIZE + DATA.length); + + zipW.open(tmpFile, PR_RDWR); + // Should have the set comment + Assert.equal(zipW.comment, DATA); + zipW.comment = DATA2; + zipW.close(); + + // Certain platforms cache the file size so get a fresh file to check. + tmpFile = tmpFile.clone(); + + // Empty zip file should just be the end of central directory marker + // and comment. This should now be shorter + Assert.equal(tmpFile.fileSize, ZIP_EOCDR_HEADER_SIZE + DATA2.length); +} diff --git a/modules/libjar/zipwriter/test/unit/test_zippermissions.js b/modules/libjar/zipwriter/test/unit/test_zippermissions.js new file mode 100644 index 0000000000..2b094c0570 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/test_zippermissions.js @@ -0,0 +1,102 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +var TESTS = []; + +function build_tests() { + var id = 0; + + // Minimum mode is 0o400 + for (let u = 4; u <= 7; u++) { + for (let g = 0; g <= 7; g++) { + for (let o = 0; o <= 7; o++) { + TESTS[id] = { + name: "test" + u + g + o, + permission: (u << 6) + (g << 3) + o, + }; + id++; + } + } + } +} + +function run_test() { + build_tests(); + + var foStream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance( + Ci.nsIFileOutputStream + ); + + var tmp = tmpDir.clone(); + tmp.append("temp-permissions"); + tmp.createUnique(Ci.nsIFile.DIRECTORY_TYPE, 0o755); + + var file = tmp.clone(); + file.append("tempfile"); + + zipW.open(tmpFile, PR_RDWR | PR_CREATE_FILE | PR_TRUNCATE); + for (let i = 0; i < TESTS.length; i++) { + // Open the file with the permissions to match how the zipreader extracts + // This obeys the umask + foStream.init(file, 0x02 | 0x08 | 0x20, TESTS[i].permission, 0); + foStream.close(); + + // umask may have altered the permissions so test against what they really were. + // This reduces the coverage of the test but there isn't much we can do + var perm = file.permissions & 0xfff; + if (TESTS[i].permission != perm) { + dump( + "File permissions for " + + TESTS[i].name + + " were " + + perm.toString(8) + + "\n" + ); + TESTS[i].permission = perm; + } + + zipW.addEntryFile( + TESTS[i].name, + Ci.nsIZipWriter.COMPRESSION_NONE, + file, + false + ); + Assert.equal( + zipW.getEntry(TESTS[i].name).permissions, + TESTS[i].permission | 0o400 + ); + file.permissions = 0o600; + file.remove(true); + } + zipW.close(); + + zipW.open(tmpFile, PR_RDWR); + for (let i = 0; i < TESTS.length; i++) { + dump("Testing zipwriter file permissions for " + TESTS[i].name + "\n"); + Assert.equal( + zipW.getEntry(TESTS[i].name).permissions, + TESTS[i].permission | 0o400 + ); + } + zipW.close(); + + var zipR = new ZipReader(tmpFile); + for (let i = 0; i < TESTS.length; i++) { + dump("Testing zipreader file permissions for " + TESTS[i].name + "\n"); + Assert.equal( + zipR.getEntry(TESTS[i].name).permissions, + TESTS[i].permission | 0o400 + ); + dump("Testing extracted file permissions for " + TESTS[i].name + "\n"); + zipR.extract(TESTS[i].name, file); + Assert.equal(file.permissions & 0xfff, TESTS[i].permission); + Assert.ok(!file.isDirectory()); + file.permissions = 0o600; + file.remove(true); + } + zipR.close(); + + tmp.remove(true); +} diff --git a/modules/libjar/zipwriter/test/unit/xpcshell.ini b/modules/libjar/zipwriter/test/unit/xpcshell.ini new file mode 100644 index 0000000000..4873a92c17 --- /dev/null +++ b/modules/libjar/zipwriter/test/unit/xpcshell.ini @@ -0,0 +1,36 @@ +[DEFAULT] +head = head_zipwriter.js +support-files = + data/test_bug446708/thumbs/st14-1.tiff + data/emptyfile.txt + data/smallfile.txt + data/test.png + data/test.txt + data/test.zip + data/test_bug399727.html + data/test_bug399727.zlib + data/test_bug717061.gz + data/test_bug717061.html + +[test_asyncadd.js] +[test_asyncbadadd.js] +[test_asyncbadremove.js] +[test_asyncremove.js] +[test_bug399727.js] +[test_bug419769_1.js] +[test_bug419769_2.js] +[test_bug425768.js] +[test_bug433248.js] +[test_bug446708.js] +[test_bug467740.js] +[test_createempty.js] +[test_deflatedata.js] +[test_directory.js] +[test_editexisting.js] +[test_storedata.js] +[test_sync.js] +[test_undochange.js] +[test_zipcomment.js] +[test_zippermissions.js] +[test_bug717061.js] +[test_alignment.js] -- cgit v1.2.3