xpcom/tests/unit/test_streams.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial