js/xpconnect/tests/unit/test_params.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 /* 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 Cc = Components.classes;
michael@0 6 const Ci = Components.interfaces;
michael@0 7
michael@0 8 function run_test() {
michael@0 9
michael@0 10 // Load the component manifests.
michael@0 11 Components.manager.autoRegister(do_get_file('../components/native/xpctest.manifest'));
michael@0 12 Components.manager.autoRegister(do_get_file('../components/js/xpctest.manifest'));
michael@0 13
michael@0 14 // Test for each component.
michael@0 15 test_component("@mozilla.org/js/xpc/test/native/Params;1");
michael@0 16 test_component("@mozilla.org/js/xpc/test/js/Params;1");
michael@0 17 }
michael@0 18
michael@0 19 function test_component(contractid) {
michael@0 20
michael@0 21 // Instantiate the object.
michael@0 22 var o = Cc[contractid].createInstance(Ci["nsIXPCTestParams"]);
michael@0 23
michael@0 24 // Possible comparator functions.
michael@0 25 var standardComparator = function(a,b) {return a == b;};
michael@0 26 var dotEqualsComparator = function(a,b) {return a.equals(b); }
michael@0 27 var fuzzComparator = function(a,b) {return Math.abs(a - b) < 0.1;};
michael@0 28 var interfaceComparator = function(a,b) {return a.name == b.name; }
michael@0 29 var arrayComparator = function(innerComparator) {
michael@0 30 return function(a,b) {
michael@0 31 if (a.length != b.length)
michael@0 32 return false;
michael@0 33 for (var i = 0; i < a.length; ++i)
michael@0 34 if (!innerComparator(a[i], b[i]))
michael@0 35 return false;
michael@0 36 return true;
michael@0 37 };
michael@0 38 };
michael@0 39
michael@0 40 // Helper test function - takes the name of test method and two values of
michael@0 41 // the given type.
michael@0 42 //
michael@0 43 // The optional comparator argument can be used for alternative notions of
michael@0 44 // equality. The comparator should return true on equality.
michael@0 45 function doTest(name, val1, val2, comparator) {
michael@0 46 if (!comparator)
michael@0 47 comparator = standardComparator;
michael@0 48 var a = val1;
michael@0 49 var b = {value: val2};
michael@0 50 var rv = o[name].call(o, a, b);
michael@0 51 do_check_true(comparator(rv, val2));
michael@0 52 do_check_true(comparator(val1, b.value));
michael@0 53 };
michael@0 54
michael@0 55 function doIsTest(name, val1, val1Is, val2, val2Is, valComparator, isComparator) {
michael@0 56 if (!isComparator)
michael@0 57 isComparator = standardComparator;
michael@0 58 var a = val1;
michael@0 59 var aIs = val1Is;
michael@0 60 var b = {value: val2};
michael@0 61 var bIs = {value: val2Is};
michael@0 62 var rvIs = {};
michael@0 63 var rv = o[name].call(o, aIs, a, bIs, b, rvIs);
michael@0 64 do_check_true(valComparator(rv, val2));
michael@0 65 do_check_true(isComparator(rvIs.value, val2Is));
michael@0 66 do_check_true(valComparator(val1, b.value));
michael@0 67 do_check_true(isComparator(val1Is, bIs.value));
michael@0 68 }
michael@0 69
michael@0 70 // Special-purpose function for testing arrays of iid_is interfaces, where we
michael@0 71 // have 2 distinct sets of dependent parameters.
michael@0 72 function doIs2Test(name, val1, val1Size, val1IID, val2, val2Size, val2IID) {
michael@0 73 var a = val1;
michael@0 74 var aSize = val1Size;
michael@0 75 var aIID = val1IID;
michael@0 76 var b = {value: val2};
michael@0 77 var bSize = {value: val2Size};
michael@0 78 var bIID = {value: val2IID};
michael@0 79 var rvSize = {};
michael@0 80 var rvIID = {};
michael@0 81 var rv = o[name].call(o, aSize, aIID, a, bSize, bIID, b, rvSize, rvIID);
michael@0 82 do_check_true(arrayComparator(interfaceComparator)(rv, val2));
michael@0 83 do_check_true(standardComparator(rvSize.value, val2Size));
michael@0 84 do_check_true(dotEqualsComparator(rvIID.value, val2IID));
michael@0 85 do_check_true(arrayComparator(interfaceComparator)(val1, b.value));
michael@0 86 do_check_true(standardComparator(val1Size, bSize.value));
michael@0 87 do_check_true(dotEqualsComparator(val1IID, bIID.value));
michael@0 88 }
michael@0 89
michael@0 90 // Check that the given call (type mismatch) results in an exception being thrown.
michael@0 91 function doTypedArrayMismatchTest(name, val1, val1Size, val2, val2Size) {
michael@0 92 var comparator = arrayComparator(standardComparator);
michael@0 93 var error = false;
michael@0 94 try {
michael@0 95 doIsTest(name, val1, val1Size, val2, val2Size, comparator);
michael@0 96
michael@0 97 // An exception was not thrown as would have been expected.
michael@0 98 do_check_true(false);
michael@0 99 }
michael@0 100 catch (e) {
michael@0 101 // An exception was thrown as expected.
michael@0 102 do_check_true(true);
michael@0 103 }
michael@0 104 }
michael@0 105
michael@0 106 // Workaround for bug 687612 (inout parameters broken for dipper types).
michael@0 107 // We do a simple test of copying a into b, and ignore the rv.
michael@0 108 function doTestWorkaround(name, val1) {
michael@0 109 var a = val1;
michael@0 110 var b = {value: ""};
michael@0 111 o[name].call(o, a, b);
michael@0 112 do_check_eq(val1, b.value);
michael@0 113 }
michael@0 114
michael@0 115 // Test all the different types
michael@0 116 doTest("testBoolean", true, false);
michael@0 117 doTest("testOctet", 4, 156);
michael@0 118 doTest("testShort", -456, 1299);
michael@0 119 doTest("testLong", 50060, -12121212);
michael@0 120 doTest("testLongLong", 12345, -10000000000);
michael@0 121 doTest("testUnsignedShort", 1532, 65000);
michael@0 122 doTest("testUnsignedLong", 0, 4000000000);
michael@0 123 doTest("testUnsignedLongLong", 215435, 3453492580348535809);
michael@0 124 doTest("testFloat", 4.9, -11.2, fuzzComparator);
michael@0 125 doTest("testDouble", -80.5, 15000.2, fuzzComparator);
michael@0 126 doTest("testChar", "a", "2");
michael@0 127 doTest("testString", "someString", "another string");
michael@0 128 doTest("testWstring", "Why wasnt this", "turned on before? ಠ_ಠ");
michael@0 129 doTest("testWchar", "z", "ア");
michael@0 130 doTestWorkaround("testDOMString", "Beware: ☠ s");
michael@0 131 doTestWorkaround("testAString", "Frosty the ☃ ;-)");
michael@0 132 doTestWorkaround("testAUTF8String", "We deliver 〠!");
michael@0 133 doTestWorkaround("testACString", "Just a regular C string.");
michael@0 134 doTest("testJsval", {aprop: 12, bprop: "str"}, 4.22);
michael@0 135
michael@0 136 // Helpers to instantiate various test XPCOM objects.
michael@0 137 var numAsMade = 0;
michael@0 138 function makeA() {
michael@0 139 var a = Cc["@mozilla.org/js/xpc/test/js/InterfaceA;1"].createInstance(Ci['nsIXPCTestInterfaceA']);
michael@0 140 a.name = 'testA' + numAsMade++;
michael@0 141 return a;
michael@0 142 };
michael@0 143 var numBsMade = 0;
michael@0 144 function makeB() {
michael@0 145 var b = Cc["@mozilla.org/js/xpc/test/js/InterfaceB;1"].createInstance(Ci['nsIXPCTestInterfaceB']);
michael@0 146 b.name = 'testB' + numBsMade++;
michael@0 147 return b;
michael@0 148 };
michael@0 149
michael@0 150 // Test arrays.
michael@0 151 doIsTest("testShortArray", [2, 4, 6], 3, [1, 3, 5, 7], 4, arrayComparator(standardComparator));
michael@0 152 doIsTest("testDoubleArray", [-10, -0.5], 2, [1, 3, 1e11, -8e-5 ], 4, arrayComparator(fuzzComparator));
michael@0 153
michael@0 154 doIsTest("testStringArray", ["mary", "hat", "hey", "lid", "tell", "lam"], 6,
michael@0 155 ["ids", "fleas", "woes", "wide", "has", "know", "!"], 7, arrayComparator(standardComparator));
michael@0 156 doIsTest("testWstringArray", ["沒有語言", "的偉大嗎?]"], 2,
michael@0 157 ["we", "are", "being", "sooo", "international", "right", "now"], 7, arrayComparator(standardComparator));
michael@0 158 doIsTest("testInterfaceArray", [makeA(), makeA()], 2,
michael@0 159 [makeA(), makeA(), makeA(), makeA(), makeA(), makeA()], 6, arrayComparator(interfaceComparator));
michael@0 160
michael@0 161 // Test typed arrays and ArrayBuffer aliasing.
michael@0 162 var arrayBuffer = new ArrayBuffer(16);
michael@0 163 var int16Array = new Int16Array(arrayBuffer, 2, 3);
michael@0 164 int16Array.set([-32768, 0, 32767]);
michael@0 165 doIsTest("testShortArray", int16Array, 3, new Int16Array([1773, -32768, 32767, 7]), 4, arrayComparator(standardComparator));
michael@0 166 doIsTest("testDoubleArray", new Float64Array([-10, -0.5]), 2, new Float64Array([0, 3.2, 1.0e10, -8.33 ]), 4, arrayComparator(fuzzComparator));
michael@0 167
michael@0 168 // Test sized strings.
michael@0 169 var ssTests = ["Tis not possible, I muttered", "give me back my free hardcore!", "quoth the server:", "4〠4"];
michael@0 170 doIsTest("testSizedString", ssTests[0], ssTests[0].length, ssTests[1], ssTests[1].length, standardComparator);
michael@0 171 doIsTest("testSizedWstring", ssTests[2], ssTests[2].length, ssTests[3], ssTests[3].length, standardComparator);
michael@0 172
michael@0 173 // Test iid_is.
michael@0 174 doIsTest("testInterfaceIs", makeA(), Ci['nsIXPCTestInterfaceA'],
michael@0 175 makeB(), Ci['nsIXPCTestInterfaceB'],
michael@0 176 interfaceComparator, dotEqualsComparator);
michael@0 177
michael@0 178 // Test arrays of iids.
michael@0 179 doIs2Test("testInterfaceIsArray", [makeA(), makeA(), makeA(), makeA(), makeA()], 5, Ci['nsIXPCTestInterfaceA'],
michael@0 180 [makeB(), makeB(), makeB()], 3, Ci['nsIXPCTestInterfaceB']);
michael@0 181
michael@0 182 // Test incorrect (too big) array size parameter; this should throw NOT_ENOUGH_ELEMENTS.
michael@0 183 doTypedArrayMismatchTest("testShortArray", new Int16Array([-3, 7, 4]), 4,
michael@0 184 new Int16Array([1, -32, 6]), 3);
michael@0 185
michael@0 186 // Test type mismatch (int16 <-> uint16); this should throw BAD_CONVERT_JS.
michael@0 187 doTypedArrayMismatchTest("testShortArray", new Uint16Array([0, 7, 4, 3]), 4,
michael@0 188 new Uint16Array([1, 5, 6]), 3);
michael@0 189 }

mercurial