Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 const Ci = Components.interfaces;
6 const Cr = Components.results;
7 const CC = Components.Constructor;
9 var Pipe = CC("@mozilla.org/pipe;1",
10 "nsIPipe",
11 "init");
12 var BinaryOutput = CC("@mozilla.org/binaryoutputstream;1",
13 "nsIBinaryOutputStream",
14 "setOutputStream");
15 var BinaryInput = CC("@mozilla.org/binaryinputstream;1",
16 "nsIBinaryInputStream",
17 "setInputStream");
19 /**
20 * Binary stream tests.
21 */
22 function test_binary_streams() {
23 var p, is, os;
25 p = new Pipe(false, false, 1024, 1, null);
26 is = new BinaryInput(p.inputStream);
27 os = new BinaryOutput(p.outputStream);
29 const LargeNum = Math.pow(2, 18) + Math.pow(2, 12) + 1;
30 const HugeNum = Math.pow(2, 62);
31 const HelloStr = "Hello World";
32 const HelloArray = Array.map(HelloStr, function(c) {return c.charCodeAt(0)});
33 var countObj = {};
34 var msg = {};
36 // Test reading immediately after writing.
37 os.writeBoolean(true);
38 do_check_eq(is.readBoolean(), true);
39 os.write8(4);
40 do_check_eq(is.read8(), 4);
41 os.write16(4);
42 do_check_eq(is.read16(), 4);
43 os.write16(1024);
44 do_check_eq(is.read16(), 1024);
45 os.write32(7);
46 do_check_eq(is.read32(), 7);
47 os.write32(LargeNum);
48 do_check_eq(is.read32(), LargeNum);
49 os.write64(LargeNum);
50 do_check_eq(is.read64(), LargeNum);
51 os.write64(1024);
52 do_check_eq(is.read64(), 1024);
53 os.write64(HugeNum);
54 do_check_eq(is.read64(), HugeNum);
55 os.writeFloat(2.5);
56 do_check_eq(is.readFloat(), 2.5);
57 // os.writeDouble(Math.SQRT2);
58 // do_check_eq(is.readDouble(), Math.SQRT2);
59 os.writeStringZ("Mozilla");
60 do_check_eq(is.readCString(), "Mozilla");
61 os.writeWStringZ("Gecko");
62 do_check_eq(is.readString(), "Gecko");
63 os.writeBytes(HelloStr, HelloStr.length);
64 do_check_eq(is.available(), HelloStr.length);
65 msg = is.readBytes(HelloStr.length);
66 do_check_eq(msg, HelloStr);
67 msg = null;
68 countObj.value = -1;
69 os.writeByteArray(HelloArray, HelloArray.length);
70 do_check_eq(is.available(), HelloStr.length);
71 msg = is.readByteArray(HelloStr.length)
72 do_check_eq(typeof msg, typeof HelloArray);
73 do_check_eq(msg.toSource(), HelloArray.toSource());
74 do_check_eq(is.available(), 0);
76 // Test writing in one big chunk.
77 os.writeBoolean(true);
78 os.write8(4);
79 os.write16(4);
80 os.write16(1024);
81 os.write32(7);
82 os.write32(LargeNum);
83 os.write64(LargeNum);
84 os.write64(1024);
85 os.write64(HugeNum);
86 os.writeFloat(2.5);
87 // os.writeDouble(Math.SQRT2);
88 os.writeStringZ("Mozilla");
89 os.writeWStringZ("Gecko");
90 os.writeBytes(HelloStr, HelloStr.length);
91 os.writeByteArray(HelloArray, HelloArray.length);
92 // Available should not be zero after a long write like this.
93 do_check_neq(is.available(), 0);
95 // Test reading in one big chunk.
96 do_check_eq(is.readBoolean(), true);
97 do_check_eq(is.read8(), 4);
98 do_check_eq(is.read16(), 4);
99 do_check_eq(is.read16(), 1024);
100 do_check_eq(is.read32(), 7);
101 do_check_eq(is.read32(), LargeNum);
102 do_check_eq(is.read64(), LargeNum);
103 do_check_eq(is.read64(), 1024);
104 do_check_eq(is.read64(), HugeNum);
105 do_check_eq(is.readFloat(), 2.5);
106 // do_check_eq(is.readDouble(), Math.SQRT2);
107 do_check_eq(is.readCString(), "Mozilla");
108 do_check_eq(is.readString(), "Gecko");
109 // Remember, we wrote HelloStr twice - once as a string, and then as an array.
110 do_check_eq(is.available(), HelloStr.length * 2);
111 msg = is.readBytes(HelloStr.length);
112 do_check_eq(msg, HelloStr);
113 msg = null;
114 countObj.value = -1;
115 do_check_eq(is.available(), HelloStr.length);
116 msg = is.readByteArray(HelloStr.length)
117 do_check_eq(typeof msg, typeof HelloArray);
118 do_check_eq(msg.toSource(), HelloArray.toSource());
119 do_check_eq(is.available(), 0);
121 // Test for invalid actions.
122 os.close();
123 is.close();
125 try {
126 os.writeBoolean(false);
127 do_throw("Not reached!");
128 } catch (e if (e instanceof Ci.nsIException &&
129 e.result == Cr.NS_BASE_STREAM_CLOSED)) {
130 // do nothing
131 }
133 try {
134 is.available();
135 do_throw("Not reached!");
136 } catch (e if (e instanceof Ci.nsIException &&
137 e.result == Cr.NS_BASE_STREAM_CLOSED)) {
138 // do nothing
139 }
141 try {
142 is.readBoolean();
143 do_throw("Not reached!");
144 } catch (e if (e instanceof Ci.nsIException &&
145 e.result == Cr.NS_ERROR_FAILURE)) {
146 // do nothing
147 }
148 }
150 function run_test() {
151 test_binary_streams();
152 }