diff options
Diffstat (limited to 'modules/libjar/test')
45 files changed, 1016 insertions, 0 deletions
diff --git a/modules/libjar/test/mochitest/bug1173171.zip b/modules/libjar/test/mochitest/bug1173171.zip Binary files differnew file mode 100644 index 0000000000..48ba268ddf --- /dev/null +++ b/modules/libjar/test/mochitest/bug1173171.zip diff --git a/modules/libjar/test/mochitest/bug1173171.zip^headers^ b/modules/libjar/test/mochitest/bug1173171.zip^headers^ new file mode 100644 index 0000000000..28b8aa0a57 --- /dev/null +++ b/modules/libjar/test/mochitest/bug1173171.zip^headers^ @@ -0,0 +1 @@ +Content-Type: application/java-archive diff --git a/modules/libjar/test/mochitest/mochitest.ini b/modules/libjar/test/mochitest/mochitest.ini new file mode 100644 index 0000000000..71c315b91b --- /dev/null +++ b/modules/libjar/test/mochitest/mochitest.ini @@ -0,0 +1,5 @@ + +[test_bug1173171.html] +support-files = + bug1173171.zip + bug1173171.zip^headers^
\ No newline at end of file diff --git a/modules/libjar/test/mochitest/test_bug1173171.html b/modules/libjar/test/mochitest/test_bug1173171.html new file mode 100644 index 0000000000..6f18513f86 --- /dev/null +++ b/modules/libjar/test/mochitest/test_bug1173171.html @@ -0,0 +1,50 @@ +<!DOCTYPE HTML> +<html> +<!-- +https://bugzilla.mozilla.org/show_bug.cgi?id=1173171 +--> +<head> + <title>Test for Bug 1173171</title> + <script src="/tests/SimpleTest/SimpleTest.js"></script> + <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> +</head> +<body> + +<iframe id="testFrame"></iframe> + +<pre id="test"> +<script class="testbody" type="application/javascript"> + +/** Test for Bug 1173171 **/ + +// __xhr(method, url, responseType__. +// A simple async XMLHttpRequest call. +// Returns a promise with the response. +let xhr = function(method, url, responseType) { + return new Promise(function(resolve, reject) { + let xhrInstance = new XMLHttpRequest(); + xhrInstance.open(method, url, true); + xhrInstance.onload = function() { + resolve(xhrInstance.response); + }; + xhrInstance.onerror = function() { + resolve(null); + }; + xhrInstance.responseType = responseType; + xhrInstance.send(); + }); +}; + +let jarURL = "jar:http://mochi.test:8888/tests/modules/libjar/test/mochitest/bug403331.zip!/test.html"; + +// Test behavior when blocking is deactivated and activated. +add_task(async function() { + let response = await xhr("GET", jarURL, "document"); + is(response, null, "Remote jars should be blocked."); +}); + +</script> +</pre> + +</body> +</html> diff --git a/modules/libjar/test/unit/data/empty b/modules/libjar/test/unit/data/empty new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/modules/libjar/test/unit/data/empty diff --git a/modules/libjar/test/unit/data/test_bug333423.zip b/modules/libjar/test/unit/data/test_bug333423.zip Binary files differnew file mode 100644 index 0000000000..42662a4085 --- /dev/null +++ b/modules/libjar/test/unit/data/test_bug333423.zip diff --git a/modules/libjar/test/unit/data/test_bug336691.zip b/modules/libjar/test/unit/data/test_bug336691.zip Binary files differnew file mode 100644 index 0000000000..16bd2d6f2c --- /dev/null +++ b/modules/libjar/test/unit/data/test_bug336691.zip diff --git a/modules/libjar/test/unit/data/test_bug370103.jar b/modules/libjar/test/unit/data/test_bug370103.jar Binary files differnew file mode 100644 index 0000000000..7723568bad --- /dev/null +++ b/modules/libjar/test/unit/data/test_bug370103.jar diff --git a/modules/libjar/test/unit/data/test_bug379841.zip b/modules/libjar/test/unit/data/test_bug379841.zip Binary files differnew file mode 100644 index 0000000000..f3c5205197 --- /dev/null +++ b/modules/libjar/test/unit/data/test_bug379841.zip diff --git a/modules/libjar/test/unit/data/test_bug589292.zip b/modules/libjar/test/unit/data/test_bug589292.zip Binary files differnew file mode 100644 index 0000000000..b7b9e65b47 --- /dev/null +++ b/modules/libjar/test/unit/data/test_bug589292.zip diff --git a/modules/libjar/test/unit/data/test_bug597702.zip b/modules/libjar/test/unit/data/test_bug597702.zip Binary files differnew file mode 100644 index 0000000000..55ce8fff42 --- /dev/null +++ b/modules/libjar/test/unit/data/test_bug597702.zip diff --git a/modules/libjar/test/unit/data/test_bug637286.zip b/modules/libjar/test/unit/data/test_bug637286.zip Binary files differnew file mode 100644 index 0000000000..5dc8e747ea --- /dev/null +++ b/modules/libjar/test/unit/data/test_bug637286.zip diff --git a/modules/libjar/test/unit/data/test_bug658093.zip b/modules/libjar/test/unit/data/test_bug658093.zip Binary files differnew file mode 100644 index 0000000000..d32180101a --- /dev/null +++ b/modules/libjar/test/unit/data/test_bug658093.zip diff --git a/modules/libjar/test/unit/data/test_corrupt.zip b/modules/libjar/test/unit/data/test_corrupt.zip Binary files differnew file mode 100644 index 0000000000..d7f5f42f93 --- /dev/null +++ b/modules/libjar/test/unit/data/test_corrupt.zip diff --git a/modules/libjar/test/unit/data/test_corrupt2.zip b/modules/libjar/test/unit/data/test_corrupt2.zip new file mode 100644 index 0000000000..0cfbf08886 --- /dev/null +++ b/modules/libjar/test/unit/data/test_corrupt2.zip @@ -0,0 +1 @@ +2 diff --git a/modules/libjar/test/unit/data/test_corrupt3.zip b/modules/libjar/test/unit/data/test_corrupt3.zip Binary files differnew file mode 100644 index 0000000000..86f8d76c9e --- /dev/null +++ b/modules/libjar/test/unit/data/test_corrupt3.zip diff --git a/modules/libjar/test/unit/data/test_crx_dummy.crx b/modules/libjar/test/unit/data/test_crx_dummy.crx Binary files differnew file mode 100644 index 0000000000..516e4ff807 --- /dev/null +++ b/modules/libjar/test/unit/data/test_crx_dummy.crx diff --git a/modules/libjar/test/unit/data/test_empty_file.zip b/modules/libjar/test/unit/data/test_empty_file.zip Binary files differnew file mode 100644 index 0000000000..b613b60c02 --- /dev/null +++ b/modules/libjar/test/unit/data/test_empty_file.zip diff --git a/modules/libjar/test/unit/data/test_umlaute.zip b/modules/libjar/test/unit/data/test_umlaute.zip Binary files differnew file mode 100644 index 0000000000..d147138e18 --- /dev/null +++ b/modules/libjar/test/unit/data/test_umlaute.zip diff --git a/modules/libjar/test/unit/data/uncompressed.zip b/modules/libjar/test/unit/data/uncompressed.zip Binary files differnew file mode 100644 index 0000000000..192bf15616 --- /dev/null +++ b/modules/libjar/test/unit/data/uncompressed.zip diff --git a/modules/libjar/test/unit/test_bug1328865.js b/modules/libjar/test/unit/test_bug1328865.js new file mode 100644 index 0000000000..ec6e98e98b --- /dev/null +++ b/modules/libjar/test/unit/test_bug1328865.js @@ -0,0 +1,51 @@ +/* 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/. */ + +"use strict"; + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +// Check that reading non existant inner jars results in the right error + +add_task(async function() { + var file = do_get_file("data/test_bug597702.zip"); + var outerJarBase = "jar:" + Services.io.newFileURI(file).spec + "!/"; + var goodSpec = + "jar:" + outerJarBase + "inner.jar!/hello#!/ignore%20this%20part"; + var goodChannel = NetUtil.newChannel({ + uri: goodSpec, + loadUsingSystemPrincipal: true, + }); + var instr = goodChannel.open(); + + ok(!!instr, "Should be able to open channel"); +}); + +add_task(async function() { + var file = do_get_file("data/test_bug597702.zip"); + var outerJarBase = "jar:" + Services.io.newFileURI(file).spec + "!/"; + var goodSpec = + "jar:" + outerJarBase + "inner.jar!/hello?ignore%20this%20part!/"; + var goodChannel = NetUtil.newChannel({ + uri: goodSpec, + loadUsingSystemPrincipal: true, + }); + var instr = goodChannel.open(); + + ok(!!instr, "Should be able to open channel"); +}); + +add_task(async function() { + var file = do_get_file("data/test_bug597702.zip"); + var outerJarBase = "jar:" + Services.io.newFileURI(file).spec + "!/"; + var goodSpec = "jar:" + outerJarBase + "inner.jar!/hello?ignore#this!/part"; + var goodChannel = NetUtil.newChannel({ + uri: goodSpec, + loadUsingSystemPrincipal: true, + }); + var instr = goodChannel.open(); + + ok(!!instr, "Should be able to open channel"); +}); diff --git a/modules/libjar/test/unit/test_bug1550815.js b/modules/libjar/test/unit/test_bug1550815.js new file mode 100644 index 0000000000..01e332de4f --- /dev/null +++ b/modules/libjar/test/unit/test_bug1550815.js @@ -0,0 +1,32 @@ +// Test checks SIGBUS handling on Linux. The test cannot be used to check page +// error exception on Windows because the file cannot be truncated while it's +// being used by zipreader. +function run_test() { + var file = do_get_file("data/test_bug333423.zip"); + var tmpFile = do_get_tempdir(); + + file.copyTo(tmpFile, "bug1550815.zip"); + tmpFile.append("bug1550815.zip"); + + var zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipReader.open(tmpFile); + + // Truncate the file + var ostream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance( + Ci.nsIFileOutputStream + ); + ostream.init(tmpFile, -1, -1, 0); + ostream.close(); + + try { + zipReader.test("modules/libjar/test/Makefile.in"); + Assert.ok(false, "Should not reach here."); + } catch (e) { + Assert.equal(e.result, Cr.NS_ERROR_FILE_TARGET_DOES_NOT_EXIST); + } + + zipReader.close(); + tmpFile.remove(false); +} diff --git a/modules/libjar/test/unit/test_bug278262.js b/modules/libjar/test/unit/test_bug278262.js new file mode 100644 index 0000000000..21d62495de --- /dev/null +++ b/modules/libjar/test/unit/test_bug278262.js @@ -0,0 +1,34 @@ +// Regression test for bug 278262 - JAR URIs should resolve relative URIs in the base section. + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); + +const path = "data/test_bug333423.zip"; + +function test_relative_sub() { + var ios = Services.io; + var spec = "jar:" + ios.newFileURI(do_get_file(path)).spec + "!/"; + var base = ios.newURI(spec); + var uri = ios.newURI("../modules/libjar", null, base); + + // This is the URI we expect to see. + var expected = + "jar:" + ios.newFileURI(do_get_file(path)).spec + "!/modules/libjar"; + + Assert.equal(uri.spec, expected); +} + +function test_relative_base() { + var ios = Services.io; + var base = ios.newFileURI(do_get_file("data/empty")); + var uri = ios.newURI("jar:../" + path + "!/", null, base); + + // This is the URI we expect to see. + var expected = "jar:" + ios.newFileURI(do_get_file(path)).spec + "!/"; + + Assert.equal(uri.spec, expected); +} + +function run_test() { + test_relative_sub(); + test_relative_base(); +} diff --git a/modules/libjar/test/unit/test_bug333423.js b/modules/libjar/test/unit/test_bug333423.js new file mode 100644 index 0000000000..0f60b285a5 --- /dev/null +++ b/modules/libjar/test/unit/test_bug333423.js @@ -0,0 +1,20 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* 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/. */ + +// Regression test for bug 333423 - crash enumerating entries of a +// closed nsIZipReader +function run_test() { + // the build script have created the zip we can test on in the current dir. + var file = do_get_file("data/test_bug333423.zip"); + + var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipreader.open(file); + zipreader.close(); + var entries = zipreader.findEntries("*.*"); + Assert.ok(!entries.hasMore()); // this shouldn't crash +} diff --git a/modules/libjar/test/unit/test_bug336691.js b/modules/libjar/test/unit/test_bug336691.js new file mode 100644 index 0000000000..3740335acc --- /dev/null +++ b/modules/libjar/test/unit/test_bug336691.js @@ -0,0 +1,10 @@ +// Regression test for bug 336691 - nsZipArchive::Test shouldn't try to ExtractFile on directories. +function run_test() { + var file = do_get_file("data/test_bug336691.zip"); + var zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipReader.open(file); + zipReader.test(null); // We shouldn't crash here. + zipReader.close(); +} diff --git a/modules/libjar/test/unit/test_bug370103.js b/modules/libjar/test/unit/test_bug370103.js new file mode 100644 index 0000000000..059cadee03 --- /dev/null +++ b/modules/libjar/test/unit/test_bug370103.js @@ -0,0 +1,26 @@ +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +// Regression test for bug 370103 - crash when passing a null listener to +// nsIChannel.asyncOpen +function run_test() { + // Compose the jar: url + var file = do_get_file("data/test_bug370103.jar"); + var url = Services.io.newFileURI(file).spec; + url = "jar:" + url + "!/test_bug370103"; + + // Try opening channel with null listener + var channel = NetUtil.newChannel({ + uri: url, + loadUsingSystemPrincipal: true, + }); + + var exception = false; + try { + channel.asyncOpen(null); + } catch (e) { + exception = true; + } + + Assert.ok(exception); // should throw exception instead of crashing +} diff --git a/modules/libjar/test/unit/test_bug379841.js b/modules/libjar/test/unit/test_bug379841.js new file mode 100644 index 0000000000..c8bd026bb3 --- /dev/null +++ b/modules/libjar/test/unit/test_bug379841.js @@ -0,0 +1,23 @@ +// Regression test for bug 379841 - nsIZipReader's last modified time ignores seconds + +const path = "data/test_bug379841.zip"; +// Retrieved time should be within 2 seconds of original file's time. +const MAX_TIME_DIFF = 2000000; + +var ENTRY_NAME = "test"; +// Actual time of file was 07 May 2007 13:35:49 UTC +var ENTRY_TIME = new Date(Date.UTC(2007, 4, 7, 13, 35, 49, 0)); + +function run_test() { + var file = do_get_file(path); + var zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipReader.open(file); + var entry = zipReader.getEntry(ENTRY_NAME); + var diff = Math.abs(entry.lastModifiedTime - ENTRY_TIME.getTime() * 1000); + zipReader.close(); + if (diff >= MAX_TIME_DIFF) { + do_throw(diff); + } +} diff --git a/modules/libjar/test/unit/test_bug453254.js b/modules/libjar/test/unit/test_bug453254.js new file mode 100644 index 0000000000..150a15daa3 --- /dev/null +++ b/modules/libjar/test/unit/test_bug453254.js @@ -0,0 +1,17 @@ +function run_test() { + const zipCache = Cc["@mozilla.org/libjar/zip-reader-cache;1"].createInstance( + Ci.nsIZipReaderCache + ); + zipCache.init(1024); + try { + zipCache.getZip(null); + do_throw("Shouldn't get here!"); + } catch (e) { + if ( + !(e instanceof Ci.nsIException && e.result == Cr.NS_ERROR_INVALID_POINTER) + ) { + throw e; + } + // do nothing, this test passes + } +} diff --git a/modules/libjar/test/unit/test_bug458158.js b/modules/libjar/test/unit/test_bug458158.js new file mode 100644 index 0000000000..15e327fba5 --- /dev/null +++ b/modules/libjar/test/unit/test_bug458158.js @@ -0,0 +1,16 @@ +function run_test() { + var zReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + try { + zReader.open(null); + do_throw("Shouldn't get here!"); + } catch (e) { + if ( + !(e instanceof Ci.nsIException && e.result == Cr.NS_ERROR_NULL_POINTER) + ) { + throw e; + } + // do nothing, this test passes + } +} diff --git a/modules/libjar/test/unit/test_bug589292.js b/modules/libjar/test/unit/test_bug589292.js new file mode 100644 index 0000000000..313cc3cf6b --- /dev/null +++ b/modules/libjar/test/unit/test_bug589292.js @@ -0,0 +1,24 @@ +// Make sure we behave appropriately when asking for content-disposition + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +const path = "data/test_bug589292.zip"; + +function run_test() { + var spec = + "jar:" + Services.io.newFileURI(do_get_file(path)).spec + "!/foo.txt"; + var channel = NetUtil.newChannel({ + uri: spec, + loadUsingSystemPrincipal: true, + }); + channel.open(); + try { + channel.contentDisposition; + Assert.ok(false, "The channel has content disposition?!"); + } catch (e) { + // This is what we want to happen - there's no underlying channel, so no + // content-disposition header is available + Assert.ok(true, "How are you reading this?!"); + } +} diff --git a/modules/libjar/test/unit/test_bug597702.js b/modules/libjar/test/unit/test_bug597702.js new file mode 100644 index 0000000000..b7d08a0999 --- /dev/null +++ b/modules/libjar/test/unit/test_bug597702.js @@ -0,0 +1,36 @@ +/* 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 { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +// Check that reading non existant inner jars results in the right error + +function run_test() { + var file = do_get_file("data/test_bug597702.zip"); + var outerJarBase = "jar:" + Services.io.newFileURI(file).spec + "!/"; + var goodSpec = "jar:" + outerJarBase + "inner.jar!/hello"; + var badSpec = "jar:" + outerJarBase + "jar_that_isnt_in_the.jar!/hello"; + var goodChannel = NetUtil.newChannel({ + uri: goodSpec, + loadUsingSystemPrincipal: true, + }); + var badChannel = NetUtil.newChannel({ + uri: badSpec, + loadUsingSystemPrincipal: true, + }); + + try { + goodChannel.open(); + } catch (e) { + do_throw("Failed to open file in inner jar"); + } + + try { + badChannel.open(); + do_throw("Failed to report that file doesn't exist"); + } catch (e) { + Assert.ok(e.name == "NS_ERROR_FILE_NOT_FOUND"); + } +} diff --git a/modules/libjar/test/unit/test_bug637286.js b/modules/libjar/test/unit/test_bug637286.js new file mode 100644 index 0000000000..34df98ecd3 --- /dev/null +++ b/modules/libjar/test/unit/test_bug637286.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 { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +// Check that the zip cache can expire entries from nested jars + +function open_inner_zip(base, idx) { + var spec = "jar:" + base + "inner" + idx + ".zip!/foo"; + var channel = NetUtil.newChannel({ + uri: spec, + loadUsingSystemPrincipal: true, + }); + channel.open(); +} + +function run_test() { + var file = do_get_file("data/test_bug637286.zip"); + var outerJarBase = "jar:" + Services.io.newFileURI(file).spec + "!/"; + + for (var i = 0; i < 40; i++) { + open_inner_zip(outerJarBase, i); + gc(); + } +} diff --git a/modules/libjar/test/unit/test_bug658093.js b/modules/libjar/test/unit/test_bug658093.js new file mode 100644 index 0000000000..909afbd335 --- /dev/null +++ b/modules/libjar/test/unit/test_bug658093.js @@ -0,0 +1,24 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +// Check that we don't crash on reading a directory entry signature + +function run_test() { + var file = do_get_file("data/test_bug658093.zip"); + var spec = "jar:" + Services.io.newFileURI(file).spec + "!/0000"; + var channel = NetUtil.newChannel({ + uri: spec, + loadUsingSystemPrincipal: true, + }); + var failed = false; + try { + channel.open(); + } catch (e) { + failed = true; + } + Assert.ok(failed); +} diff --git a/modules/libjar/test/unit/test_corrupt_1211262.js b/modules/libjar/test/unit/test_corrupt_1211262.js new file mode 100644 index 0000000000..c6fc6b2761 --- /dev/null +++ b/modules/libjar/test/unit/test_corrupt_1211262.js @@ -0,0 +1,28 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +// Regression test ensuring that that a STORED entry with differing compressed +// and uncompressed sizes is considered to be corrupt. +function run_test() { + var file = do_get_file("data/test_corrupt3.zip"); + + var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipreader.open(file); + + var failed = false; + for (let entryPath of zipreader.findEntries("*")) { + let entry = zipreader.getEntry(entryPath); + if (!entry.isDirectory) { + try { + zipreader.getInputStream(entryPath); + } catch (e) { + failed = true; + } + } + } + + Assert.ok(failed); +} diff --git a/modules/libjar/test/unit/test_corrupt_536911.js b/modules/libjar/test/unit/test_corrupt_536911.js new file mode 100644 index 0000000000..1b8bb9d7e4 --- /dev/null +++ b/modules/libjar/test/unit/test_corrupt_536911.js @@ -0,0 +1,32 @@ +/* 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 wrapInputStream(input) { + var nsIScriptableInputStream = Ci.nsIScriptableInputStream; + var factory = Cc["@mozilla.org/scriptableinputstream;1"]; + var wrapper = factory.createInstance(nsIScriptableInputStream); + wrapper.init(input); + return wrapper; +} + +// Check that files can be read from after closing zipreader +function run_test() { + // the build script have created the zip we can test on in the current dir. + var file = do_get_file("data/test_corrupt.zip"); + + var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipreader.open(file); + // var entries = zipreader.findEntries(null); + // the signature for file is corrupt, should not segfault + var failed = false; + try { + var stream = wrapInputStream(zipreader.getInputStream("file")); + stream.read(1024); + } catch (ex) { + failed = true; + } + Assert.ok(failed); +} diff --git a/modules/libjar/test/unit/test_corrupt_541828.js b/modules/libjar/test/unit/test_corrupt_541828.js new file mode 100644 index 0000000000..dfd0ace015 --- /dev/null +++ b/modules/libjar/test/unit/test_corrupt_541828.js @@ -0,0 +1,21 @@ +/* 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/. */ + +// Check that files can be read from after closing zipreader +function run_test() { + // the build script have created the zip we can test on in the current dir. + var file = do_get_file("data/test_corrupt2.zip"); + + var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + var failed = false; + try { + zipreader.open(file); + // corrupt files should trigger an exception + } catch (ex) { + failed = true; + } + Assert.ok(failed); +} diff --git a/modules/libjar/test/unit/test_crx.js b/modules/libjar/test/unit/test_crx.js new file mode 100644 index 0000000000..1524afa0a9 --- /dev/null +++ b/modules/libjar/test/unit/test_crx.js @@ -0,0 +1,43 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* 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 wrapInputStream(input) { + let nsIScriptableInputStream = Ci.nsIScriptableInputStream; + let factory = Cc["@mozilla.org/scriptableinputstream;1"]; + let wrapper = factory.createInstance(nsIScriptableInputStream); + wrapper.init(input); + return wrapper; +} + +// Make sure that we can read from CRX files as if they were ZIP files. +function run_test() { + // Note: test_crx_dummy.crx is a dummy crx file created for this test. The + // public key and signature fields in the header are both empty. + let file = do_get_file("data/test_crx_dummy.crx"); + + let zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipreader.open(file); + // do crc stuff + function check_archive_crc() { + zipreader.test(null); + return true; + } + Assert.ok(check_archive_crc()); + zipreader.findEntries(null); + let stream = wrapInputStream( + zipreader.getInputStream("modules/libjar/test/Makefile.in") + ); + let dirstream = wrapInputStream( + zipreader.getInputStream("modules/libjar/test/") + ); + zipreader.close(); + zipreader = null; + Cu.forceGC(); + Assert.ok(stream.read(1024).length > 0); + Assert.ok(dirstream.read(100).length > 0); +} diff --git a/modules/libjar/test/unit/test_dirjar_bug525755.js b/modules/libjar/test/unit/test_dirjar_bug525755.js new file mode 100644 index 0000000000..4e718e927d --- /dev/null +++ b/modules/libjar/test/unit/test_dirjar_bug525755.js @@ -0,0 +1,23 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* 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/. */ + +// Check that we refuse to open weird files +function run_test() { + // open a bogus file + var file = do_get_file("/"); + + var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + var failed = false; + try { + zipreader.open(file); + } catch (e) { + failed = true; + } + Assert.ok(failed); + zipreader = null; +} diff --git a/modules/libjar/test/unit/test_empty_jar_telemetry.js b/modules/libjar/test/unit/test_empty_jar_telemetry.js new file mode 100644 index 0000000000..4645edf60c --- /dev/null +++ b/modules/libjar/test/unit/test_empty_jar_telemetry.js @@ -0,0 +1,109 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +"use strict"; + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +const { TelemetryTestUtils } = ChromeUtils.import( + "resource://testing-common/TelemetryTestUtils.jsm" +); + +const { TelemetryController } = ChromeUtils.import( + "resource://gre/modules/TelemetryController.jsm" +); + +const nsIBinaryInputStream = Components.Constructor( + "@mozilla.org/binaryinputstream;1", + "nsIBinaryInputStream", + "setInputStream" +); + +// Enable the collection (during test) for all products so even products +// that don't collect the data will be able to run the test without failure. +Services.prefs.setBoolPref( + "toolkit.telemetry.testing.overrideProductsCheck", + true +); + +const fileBase = "test_empty_file.zip"; +const file = do_get_file("data/" + fileBase); +const jarBase = "jar:" + Services.io.newFileURI(file).spec + "!"; +const tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + +function Listener(callback) { + this._callback = callback; +} +Listener.prototype = { + gotStartRequest: false, + available: -1, + gotStopRequest: false, + QueryInterface: ChromeUtils.generateQI(["nsIRequestObserver"]), + onDataAvailable(request, stream, offset, count) { + try { + this.available = stream.available(); + Assert.equal(this.available, count); + // Need to consume stream to avoid assertion + new nsIBinaryInputStream(stream).readBytes(count); + } catch (ex) { + do_throw(ex); + } + }, + onStartRequest(request) { + this.gotStartRequest = true; + }, + onStopRequest(request, status) { + this.gotStopRequest = true; + Assert.equal(status, 0); + if (this._callback) { + this._callback.call(null, this); + } + }, +}; + +const TELEMETRY_EVENTS_FILTERS = { + category: "network.jar.channel", + method: "noData", +}; + +add_task(async function test_empty_jar_file() { + var copy = tmpDir.clone(); + copy.append("zzdxphd909dbc6r2bxtqss2m000017"); + copy.append("zzdxphd909dbc6r2bxtqss2m000017"); + copy.append(fileBase); + file.copyTo(copy.parent, copy.leafName); + + var uri = "jar:" + Services.io.newFileURI(copy).spec + "!/test.txt"; + var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true }); + + Services.telemetry.setEventRecordingEnabled("network.jar.channel", true); + Services.telemetry.clearEvents(); + + await new Promise(resolve => { + chan.asyncOpen( + new Listener(function(l) { + Assert.ok(chan.contentLength == 0); + + resolve(); + }) + ); + }); + + try { + copy.remove(false); + } catch (e) {} + + TelemetryTestUtils.assertEvents( + [ + { + category: "network.jar.channel", + method: "noData", + object: "onStop", + value: `${fileBase}!/test.txt`, + }, + ], + TELEMETRY_EVENTS_FILTERS + ); +}); diff --git a/modules/libjar/test/unit/test_jarchannel.js b/modules/libjar/test/unit/test_jarchannel.js new file mode 100644 index 0000000000..af1afc5f3c --- /dev/null +++ b/modules/libjar/test/unit/test_jarchannel.js @@ -0,0 +1,205 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +/** + * Tests some basic jar channel functionality + */ + +const { Constructor: ctor } = Components; + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); + +const ios = Services.io; +const dirSvc = Services.dirsvc; +const obs = Services.obs; + +const nsIBinaryInputStream = ctor( + "@mozilla.org/binaryinputstream;1", + "nsIBinaryInputStream", + "setInputStream" +); + +const fileBase = "test_bug637286.zip"; +const file = do_get_file("data/" + fileBase); +const jarBase = "jar:" + ios.newFileURI(file).spec + "!"; +const tmpDir = dirSvc.get("TmpD", Ci.nsIFile); + +function Listener(callback) { + this._callback = callback; +} +Listener.prototype = { + gotStartRequest: false, + available: -1, + gotStopRequest: false, + QueryInterface: ChromeUtils.generateQI(["nsIRequestObserver"]), + onDataAvailable(request, stream, offset, count) { + try { + this.available = stream.available(); + Assert.equal(this.available, count); + // Need to consume stream to avoid assertion + new nsIBinaryInputStream(stream).readBytes(count); + } catch (ex) { + do_throw(ex); + } + }, + onStartRequest(request) { + this.gotStartRequest = true; + }, + onStopRequest(request, status) { + this.gotStopRequest = true; + Assert.equal(status, 0); + if (this._callback) { + this._callback.call(null, this); + } + }, +}; + +/** + * Basic reading test for asynchronously opened jar channel + */ +function testAsync() { + var uri = jarBase + "/inner40.zip"; + var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true }); + Assert.ok(chan.contentLength < 0); + chan.asyncOpen( + new Listener(function(l) { + Assert.ok(chan.contentLength > 0); + Assert.ok(l.gotStartRequest); + Assert.ok(l.gotStopRequest); + Assert.equal(l.available, chan.contentLength); + + run_next_test(); + }) + ); +} + +add_test(testAsync); +// Run same test again so we test the codepath for a zipcache hit +add_test(testAsync); + +/** + * Basic test for nsIZipReader. + */ +function testZipEntry() { + var uri = jarBase + "/inner40.zip"; + var chan = NetUtil.newChannel({ + uri, + loadUsingSystemPrincipal: true, + }).QueryInterface(Ci.nsIJARChannel); + var entry = chan.zipEntry; + Assert.ok(entry.CRC32 == 0x8b635486); + Assert.ok(entry.realSize == 184); + run_next_test(); +} + +add_test(testZipEntry); + +/** + * Basic reading test for synchronously opened jar channels + */ +add_test(function testSync() { + var uri = jarBase + "/inner40.zip"; + var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true }); + var stream = chan.open(); + Assert.ok(chan.contentLength > 0); + Assert.equal(stream.available(), chan.contentLength); + stream.close(); + stream.close(); // should still not throw + + run_next_test(); +}); + +/** + * Basic reading test for synchronously opened, nested jar channels + */ +add_test(function testSyncNested() { + var uri = "jar:" + jarBase + "/inner40.zip!/foo"; + var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true }); + var stream = chan.open(); + Assert.ok(chan.contentLength > 0); + Assert.equal(stream.available(), chan.contentLength); + stream.close(); + stream.close(); // should still not throw + + run_next_test(); +}); + +/** + * Basic reading test for asynchronously opened, nested jar channels + */ +add_test(function testAsyncNested(next) { + var uri = "jar:" + jarBase + "/inner40.zip!/foo"; + var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true }); + chan.asyncOpen( + new Listener(function(l) { + Assert.ok(chan.contentLength > 0); + Assert.ok(l.gotStartRequest); + Assert.ok(l.gotStopRequest); + Assert.equal(l.available, chan.contentLength); + + run_next_test(); + }) + ); +}); + +/** + * Verify that file locks are released when closing a synchronously + * opened jar channel stream + */ +add_test(function testSyncCloseUnlocks() { + var copy = tmpDir.clone(); + copy.append(fileBase); + file.copyTo(copy.parent, copy.leafName); + var uri = "jar:" + ios.newFileURI(copy).spec + "!/inner40.zip"; + var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true }); + var stream = chan.open(); + Assert.ok(chan.contentLength > 0); + stream.close(); + + // Drop any jar caches + obs.notifyObservers(null, "chrome-flush-caches"); + + try { + copy.remove(false); + } catch (ex) { + do_throw(ex); + } + + run_next_test(); +}); + +/** + * Verify that file locks are released when closing an asynchronously + * opened jar channel stream + */ +add_test(function testAsyncCloseUnlocks() { + var copy = tmpDir.clone(); + copy.append(fileBase); + file.copyTo(copy.parent, copy.leafName); + + var uri = "jar:" + ios.newFileURI(copy).spec + "!/inner40.zip"; + var chan = NetUtil.newChannel({ uri, loadUsingSystemPrincipal: true }); + + chan.asyncOpen( + new Listener(function(l) { + Assert.ok(chan.contentLength > 0); + + // Drop any jar caches + obs.notifyObservers(null, "chrome-flush-caches"); + + try { + copy.remove(false); + } catch (ex) { + do_throw(ex); + } + + run_next_test(); + }) + ); +}); + +function run_test() { + return run_next_test(); +} diff --git a/modules/libjar/test/unit/test_jarinput_stream_zipreader_reference.js b/modules/libjar/test/unit/test_jarinput_stream_zipreader_reference.js new file mode 100644 index 0000000000..f24c786bab --- /dev/null +++ b/modules/libjar/test/unit/test_jarinput_stream_zipreader_reference.js @@ -0,0 +1,42 @@ +/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et: */ +/* 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 wrapInputStream(input) { + var nsIScriptableInputStream = Ci.nsIScriptableInputStream; + var factory = Cc["@mozilla.org/scriptableinputstream;1"]; + var wrapper = factory.createInstance(nsIScriptableInputStream); + wrapper.init(input); + return wrapper; +} + +// Check that files can be read from after closing zipreader +function run_test() { + // the build script have created the zip we can test on in the current dir. + var file = do_get_file("data/test_bug333423.zip"); + + var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipreader.open(file); + // do crc stuff + function check_archive_crc() { + zipreader.test(null); + return true; + } + Assert.ok(check_archive_crc()); + zipreader.findEntries(null); + var stream = wrapInputStream( + zipreader.getInputStream("modules/libjar/test/Makefile.in") + ); + var dirstream = wrapInputStream( + zipreader.getInputStream("modules/libjar/test/") + ); + zipreader.close(); + zipreader = null; + Cu.forceGC(); + Assert.ok(stream.read(1024).length > 0); + Assert.ok(dirstream.read(100).length > 0); +} diff --git a/modules/libjar/test/unit/test_not_found.js b/modules/libjar/test/unit/test_not_found.js new file mode 100644 index 0000000000..7e96e05fd8 --- /dev/null +++ b/modules/libjar/test/unit/test_not_found.js @@ -0,0 +1,19 @@ +// Should report file not found on non-existent files + +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); +const { NetUtil } = ChromeUtils.import("resource://gre/modules/NetUtil.jsm"); +const path = "data/test_bug333423.zip"; + +function run_test() { + var spec = "jar:" + Services.io.newFileURI(do_get_file(path)).spec + "!/"; + var channel = NetUtil.newChannel({ + uri: spec + "file_that_isnt_in.archive", + loadUsingSystemPrincipal: true, + }); + try { + channel.open(); + do_throw("Failed to report that file doesn't exist"); + } catch (e) { + Assert.ok(e.name == "NS_ERROR_FILE_NOT_FOUND"); + } +} diff --git a/modules/libjar/test/unit/test_umlaute.js b/modules/libjar/test/unit/test_umlaute.js new file mode 100644 index 0000000000..7ef43c0d11 --- /dev/null +++ b/modules/libjar/test/unit/test_umlaute.js @@ -0,0 +1,39 @@ +const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm"); + +function run_test() { + var tmpDir = Services.dirsvc.get("TmpD", Ci.nsIFile); + + var zipfile = do_get_file("data/test_umlaute.zip"); + + var testFile = tmpDir.clone(); + testFile.append("test_\u00FC.txt"); + if (testFile.exists()) { + testFile.remove(false); + } + + var zipreader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipreader.open(zipfile); + + var entries = zipreader.findEntries(null); + Assert.ok(entries.hasMore()); + + var entryName = entries.getNext(); + Assert.equal(entryName, "test_\u00FC.txt"); + + Assert.ok(zipreader.hasEntry(entryName)); + + var target = tmpDir.clone(); + target.append(entryName); + target.create(Ci.nsIFile.NORMAL_FILE_TYPE, 0o640); // 0640 + + zipreader.extract(entryName, target); + + var entry = zipreader.getEntry(entryName); + Assert.ok(entry != null); + + zipreader.test(entryName); + + zipreader.close(); +} diff --git a/modules/libjar/test/unit/test_uncompressed.js b/modules/libjar/test/unit/test_uncompressed.js new file mode 100644 index 0000000000..2b0e4820e9 --- /dev/null +++ b/modules/libjar/test/unit/test_uncompressed.js @@ -0,0 +1,10 @@ +// Make sure uncompressed files pass crc +function run_test() { + var file = do_get_file("data/uncompressed.zip"); + var zipReader = Cc["@mozilla.org/libjar/zip-reader;1"].createInstance( + Ci.nsIZipReader + ); + zipReader.open(file); + zipReader.test("hello"); + zipReader.close(); +} diff --git a/modules/libjar/test/unit/xpcshell.ini b/modules/libjar/test/unit/xpcshell.ini new file mode 100644 index 0000000000..8643ac4a36 --- /dev/null +++ b/modules/libjar/test/unit/xpcshell.ini @@ -0,0 +1,48 @@ +[DEFAULT] +head = +skip-if = toolkit == 'android' +support-files = + data/empty + data/test_bug333423.zip + data/test_bug336691.zip + data/test_bug370103.jar + data/test_bug379841.zip + data/test_bug589292.zip + data/test_bug597702.zip + data/test_bug637286.zip + data/test_bug658093.zip + data/test_corrupt.zip + data/test_corrupt2.zip + data/test_corrupt3.zip + data/test_crx_dummy.crx + data/test_umlaute.zip + data/uncompressed.zip + data/test_empty_file.zip + +[test_jarchannel.js] +skip-if = os == "mac" +[test_bug278262.js] +[test_bug333423.js] +[test_bug336691.js] +[test_bug370103.js] +[test_bug379841.js] +[test_bug453254.js] +[test_bug458158.js] +[test_bug589292.js] +[test_bug597702.js] +[test_bug637286.js] +[test_bug658093.js] +[test_corrupt_536911.js] +[test_corrupt_541828.js] +[test_corrupt_1211262.js] +[test_crx.js] +[test_dirjar_bug525755.js] +[test_jarinput_stream_zipreader_reference.js] +[test_not_found.js] +[test_uncompressed.js] +[test_umlaute.js] +[test_bug1328865.js] +[test_bug1550815.js] +# recovering from SIGBUS is temporarily disabled by bug 1583735 +skip-if = true +[test_empty_jar_telemetry.js] |