toolkit/components/osfile/tests/xpcshell/test_exception.js

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

michael@0 1 /* Any copyright is dedicated to the Public Domain.
michael@0 2 * http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 /**
michael@0 5 * Test that functions throw the appropriate exceptions.
michael@0 6 */
michael@0 7
michael@0 8 "use strict";
michael@0 9
michael@0 10 let EXISTING_FILE = do_get_file("xpcshell.ini").path;
michael@0 11
michael@0 12
michael@0 13 // Tests on |open|
michael@0 14
michael@0 15 add_test_pair(function test_typeerror() {
michael@0 16 let exn;
michael@0 17 try {
michael@0 18 let fd = yield OS.File.open("/tmp", {no_such_key: 1});
michael@0 19 do_print("Fd: " + fd);
michael@0 20 } catch (ex) {
michael@0 21 exn = ex;
michael@0 22 }
michael@0 23 do_print("Exception: " + exn);
michael@0 24 do_check_true(exn.constructor.name == "TypeError");
michael@0 25 });
michael@0 26
michael@0 27 // Tests on |read|
michael@0 28
michael@0 29 add_test_pair(function* test_bad_encoding() {
michael@0 30 do_print("Testing with a wrong encoding");
michael@0 31 try {
michael@0 32 yield OS.File.read(EXISTING_FILE, { encoding: "baby-speak-encoded" });
michael@0 33 do_throw("Should have thrown with an ex.becauseInvalidArgument");
michael@0 34 } catch (ex if ex.becauseInvalidArgument) {
michael@0 35 do_print("Wrong encoding caused the correct exception");
michael@0 36 }
michael@0 37
michael@0 38 try {
michael@0 39 yield OS.File.read(EXISTING_FILE, { encoding: 4 });
michael@0 40 do_throw("Should have thrown a TypeError");
michael@0 41 } catch (ex if ex.constructor.name == "TypeError") {
michael@0 42 // Note that TypeError doesn't carry across compartments
michael@0 43 do_print("Non-string encoding caused the correct exception");
michael@0 44 }
michael@0 45 });
michael@0 46
michael@0 47 add_test_pair(function* test_bad_compression() {
michael@0 48 do_print("Testing with a non-existing compression");
michael@0 49 try {
michael@0 50 yield OS.File.read(EXISTING_FILE, { compression: "mmmh-crunchy" });
michael@0 51 do_throw("Should have thrown with an ex.becauseInvalidArgument");
michael@0 52 } catch (ex if ex.becauseInvalidArgument) {
michael@0 53 do_print("Wrong encoding caused the correct exception");
michael@0 54 }
michael@0 55
michael@0 56 do_print("Testing with a bad type for option compression");
michael@0 57 try {
michael@0 58 yield OS.File.read(EXISTING_FILE, { compression: 5 });
michael@0 59 do_throw("Should have thrown a TypeError");
michael@0 60 } catch (ex if ex.constructor.name == "TypeError") {
michael@0 61 // Note that TypeError doesn't carry across compartments
michael@0 62 do_print("Non-string encoding caused the correct exception");
michael@0 63 }
michael@0 64 });
michael@0 65
michael@0 66 add_test_pair(function* test_bad_bytes() {
michael@0 67 do_print("Testing with a bad type for option bytes");
michael@0 68 try {
michael@0 69 yield OS.File.read(EXISTING_FILE, { bytes: "five" });
michael@0 70 do_throw("Should have thrown a TypeError");
michael@0 71 } catch (ex if ex.constructor.name == "TypeError") {
michael@0 72 // Note that TypeError doesn't carry across compartments
michael@0 73 do_print("Non-number bytes caused the correct exception");
michael@0 74 }
michael@0 75 });
michael@0 76
michael@0 77 add_test_pair(function* read_non_existent() {
michael@0 78 do_print("Testing with a non-existent file");
michael@0 79 try {
michael@0 80 yield OS.File.read("I/do/not/exist");
michael@0 81 do_throw("Should have thrown with an ex.becauseNoSuchFile");
michael@0 82 } catch (ex if ex.becauseNoSuchFile) {
michael@0 83 do_print("Correct exceptions");
michael@0 84 }
michael@0 85 });
michael@0 86
michael@0 87 function run_test() {
michael@0 88 run_next_test();
michael@0 89 }

mercurial