xpcom/tests/unit/test_storagestream.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 /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* vim:set ts=2 sw=2 sts=2 et: */
michael@0 3 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 const Cc = Components.classes;
michael@0 8 const Ci = Components.interfaces;
michael@0 9 const Cr = Components.results;
michael@0 10
michael@0 11 function run_test()
michael@0 12 {
michael@0 13 test1();
michael@0 14 test2();
michael@0 15 test3();
michael@0 16 test4();
michael@0 17 }
michael@0 18
michael@0 19 /**
michael@0 20 * Checks that getting an input stream from a storage stream which has never had
michael@0 21 * anything written to it throws a not-initialized exception.
michael@0 22 */
michael@0 23 function test1()
michael@0 24 {
michael@0 25 var ss = Cc["@mozilla.org/storagestream;1"]
michael@0 26 .createInstance(Ci.nsIStorageStream);
michael@0 27 ss.init(1024, 1024, null);
michael@0 28
michael@0 29 var out = ss.getOutputStream(0);
michael@0 30 var inp2 = ss.newInputStream(0);
michael@0 31 do_check_eq(inp2.available(), 0);
michael@0 32 do_check_true(inp2.isNonBlocking());
michael@0 33
michael@0 34 var sis =
michael@0 35 Cc["@mozilla.org/scriptableinputstream;1"]
michael@0 36 .createInstance(Ci.nsIScriptableInputStream);
michael@0 37 sis.init(inp2);
michael@0 38
michael@0 39 var threw = false;
michael@0 40 try {
michael@0 41 sis.read(1);
michael@0 42 } catch (ex if ex.result == Cr.NS_BASE_STREAM_WOULD_BLOCK) {
michael@0 43 threw = true;
michael@0 44 }
michael@0 45 do_check_true(threw);
michael@0 46 }
michael@0 47
michael@0 48 /**
michael@0 49 * Checks that getting an input stream from a storage stream to which 0 bytes of
michael@0 50 * data have been explicitly written doesn't throw an exception.
michael@0 51 */
michael@0 52 function test2()
michael@0 53 {
michael@0 54 var ss = Cc["@mozilla.org/storagestream;1"]
michael@0 55 .createInstance(Ci.nsIStorageStream);
michael@0 56 ss.init(1024, 1024, null);
michael@0 57
michael@0 58 var out = ss.getOutputStream(0);
michael@0 59 out.write("", 0);
michael@0 60 try
michael@0 61 {
michael@0 62 var inp2 = ss.newInputStream(0);
michael@0 63 }
michael@0 64 catch (e)
michael@0 65 {
michael@0 66 do_throw("shouldn't throw exception when new input stream created");
michael@0 67 }
michael@0 68 }
michael@0 69
michael@0 70 /**
michael@0 71 * Checks that reading any non-zero amount of data from a storage stream
michael@0 72 * which has had 0 bytes written to it explicitly works correctly.
michael@0 73 */
michael@0 74 function test3()
michael@0 75 {
michael@0 76 var ss = Cc["@mozilla.org/storagestream;1"]
michael@0 77 .createInstance(Ci.nsIStorageStream);
michael@0 78 ss.init(1024, 1024, null);
michael@0 79
michael@0 80 var out = ss.getOutputStream(0);
michael@0 81 out.write("", 0);
michael@0 82 try
michael@0 83 {
michael@0 84 var inp = ss.newInputStream(0);
michael@0 85 }
michael@0 86 catch (e)
michael@0 87 {
michael@0 88 do_throw("newInputStream(0) shouldn't throw if write() is called: " + e);
michael@0 89 }
michael@0 90
michael@0 91 do_check_true(inp.isNonBlocking(), "next test expects a non-blocking stream");
michael@0 92
michael@0 93 try
michael@0 94 {
michael@0 95 var threw = false;
michael@0 96 var bis = BIS(inp);
michael@0 97 var dummy = bis.readByteArray(5);
michael@0 98 }
michael@0 99 catch (e)
michael@0 100 {
michael@0 101 if (e.result != Cr.NS_BASE_STREAM_WOULD_BLOCK)
michael@0 102 do_throw("wrong error thrown: " + e);
michael@0 103 threw = true;
michael@0 104 }
michael@0 105 do_check_true(threw,
michael@0 106 "should have thrown (nsStorageInputStream is nonblocking)");
michael@0 107 }
michael@0 108
michael@0 109 /**
michael@0 110 * Basic functionality test for storagestream: write data to it, get an input
michael@0 111 * stream, and read the data back to see that it matches.
michael@0 112 */
michael@0 113 function test4()
michael@0 114 {
michael@0 115 var bytes = [65, 66, 67, 68, 69, 70, 71, 72, 73, 74];
michael@0 116
michael@0 117 var ss = Cc["@mozilla.org/storagestream;1"]
michael@0 118 .createInstance(Ci.nsIStorageStream);
michael@0 119 ss.init(1024, 1024, null);
michael@0 120
michael@0 121 var outStream = ss.getOutputStream(0);
michael@0 122
michael@0 123 var bos = Cc["@mozilla.org/binaryoutputstream;1"]
michael@0 124 .createInstance(Ci.nsIBinaryOutputStream);
michael@0 125 bos.setOutputStream(outStream);
michael@0 126
michael@0 127 bos.writeByteArray(bytes, bytes.length);
michael@0 128 bos.close();
michael@0 129
michael@0 130 var inp = ss.newInputStream(0);
michael@0 131 var bis = BIS(inp);
michael@0 132
michael@0 133 var count = 0;
michael@0 134 while (count < bytes.length)
michael@0 135 {
michael@0 136 var data = bis.read8(1);
michael@0 137 do_check_eq(data, bytes[count++]);
michael@0 138 }
michael@0 139
michael@0 140 var threw = false;
michael@0 141 try
michael@0 142 {
michael@0 143 data = bis.read8(1);
michael@0 144 }
michael@0 145 catch (e)
michael@0 146 {
michael@0 147 if (e.result != Cr.NS_ERROR_FAILURE)
michael@0 148 do_throw("wrong error thrown: " + e);
michael@0 149 threw = true;
michael@0 150 }
michael@0 151 if (!threw)
michael@0 152 do_throw("should have thrown but instead returned: '" + data + "'");
michael@0 153 }
michael@0 154
michael@0 155
michael@0 156 function BIS(input)
michael@0 157 {
michael@0 158 var bis = Cc["@mozilla.org/binaryinputstream;1"]
michael@0 159 .createInstance(Ci.nsIBinaryInputStream);
michael@0 160 bis.setInputStream(input);
michael@0 161 return bis;
michael@0 162 }

mercurial