Sat, 03 Jan 2015 20:18:00 +0100
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 | // Ported from dom/src/json/test/unit/test_wrappers.js and |
michael@0 | 2 | // dom/src/json/test/unit/test_decode.js |
michael@0 | 3 | |
michael@0 | 4 | function assertIsObject(x) |
michael@0 | 5 | { |
michael@0 | 6 | assertEq(typeof x, "object"); |
michael@0 | 7 | assertEq(x instanceof Object, true); |
michael@0 | 8 | } |
michael@0 | 9 | |
michael@0 | 10 | function assertIsArray(x) |
michael@0 | 11 | { |
michael@0 | 12 | assertIsObject(x); |
michael@0 | 13 | assertEq(Array.isArray(x), true); |
michael@0 | 14 | assertEq(Object.getPrototypeOf(x), Array.prototype); |
michael@0 | 15 | assertEq(x instanceof Array, true); |
michael@0 | 16 | assertEq(x.constructor, Array); |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | var x; |
michael@0 | 20 | var props; |
michael@0 | 21 | |
michael@0 | 22 | // empty object |
michael@0 | 23 | x = JSON.parse("{}"); |
michael@0 | 24 | assertIsObject(x); |
michael@0 | 25 | assertEq(Object.getOwnPropertyNames(x).length, 0); |
michael@0 | 26 | |
michael@0 | 27 | // empty array |
michael@0 | 28 | x = JSON.parse("[]"); |
michael@0 | 29 | assertIsArray(x); |
michael@0 | 30 | assertEq(x.length, 0); |
michael@0 | 31 | |
michael@0 | 32 | // one element array |
michael@0 | 33 | x = JSON.parse("[[]]"); |
michael@0 | 34 | assertIsArray(x); |
michael@0 | 35 | assertEq(x.length, 1); |
michael@0 | 36 | assertIsArray(x[0]); |
michael@0 | 37 | assertEq(x[0].length, 0); |
michael@0 | 38 | |
michael@0 | 39 | // multiple arrays |
michael@0 | 40 | x = JSON.parse("[[],[],[]]"); |
michael@0 | 41 | assertIsArray(x); |
michael@0 | 42 | assertEq(x.length, 3); |
michael@0 | 43 | assertIsArray(x[0]); |
michael@0 | 44 | assertEq(x[0].length, 0); |
michael@0 | 45 | assertIsArray(x[1]); |
michael@0 | 46 | assertEq(x[1].length, 0); |
michael@0 | 47 | assertIsArray(x[2]); |
michael@0 | 48 | assertEq(x[2].length, 0); |
michael@0 | 49 | |
michael@0 | 50 | // array key/value |
michael@0 | 51 | x = JSON.parse('{"foo":[]}'); |
michael@0 | 52 | assertIsObject(x); |
michael@0 | 53 | props = Object.getOwnPropertyNames(x); |
michael@0 | 54 | assertEq(props.length, 1); |
michael@0 | 55 | assertEq(props[0], "foo"); |
michael@0 | 56 | assertIsArray(x.foo); |
michael@0 | 57 | assertEq(x.foo.length, 0); |
michael@0 | 58 | |
michael@0 | 59 | x = JSON.parse('{"foo":[], "bar":[]}'); |
michael@0 | 60 | assertIsObject(x); |
michael@0 | 61 | props = Object.getOwnPropertyNames(x).sort(); |
michael@0 | 62 | assertEq(props.length, 2); |
michael@0 | 63 | assertEq(props[0], "bar"); |
michael@0 | 64 | assertEq(props[1], "foo"); |
michael@0 | 65 | assertIsArray(x.foo); |
michael@0 | 66 | assertEq(x.foo.length, 0); |
michael@0 | 67 | assertIsArray(x.bar); |
michael@0 | 68 | assertEq(x.bar.length, 0); |
michael@0 | 69 | |
michael@0 | 70 | // nesting |
michael@0 | 71 | x = JSON.parse('{"foo":[{}]}'); |
michael@0 | 72 | assertIsObject(x); |
michael@0 | 73 | props = Object.getOwnPropertyNames(x); |
michael@0 | 74 | assertEq(props.length, 1); |
michael@0 | 75 | assertEq(props[0], "foo"); |
michael@0 | 76 | assertIsArray(x.foo); |
michael@0 | 77 | assertEq(x.foo.length, 1); |
michael@0 | 78 | assertIsObject(x.foo[0]); |
michael@0 | 79 | assertEq(Object.getOwnPropertyNames(x.foo[0]).length, 0); |
michael@0 | 80 | |
michael@0 | 81 | x = JSON.parse('{"foo":[{"foo":[{"foo":{}}]}]}'); |
michael@0 | 82 | assertIsObject(x.foo[0].foo[0].foo); |
michael@0 | 83 | |
michael@0 | 84 | x = JSON.parse('{"foo":[{"foo":[{"foo":[]}]}]}'); |
michael@0 | 85 | assertIsArray(x.foo[0].foo[0].foo); |
michael@0 | 86 | |
michael@0 | 87 | // strings |
michael@0 | 88 | x = JSON.parse('{"foo":"bar"}'); |
michael@0 | 89 | assertIsObject(x); |
michael@0 | 90 | props = Object.getOwnPropertyNames(x); |
michael@0 | 91 | assertEq(props.length, 1); |
michael@0 | 92 | assertEq(props[0], "foo"); |
michael@0 | 93 | assertEq(x.foo, "bar"); |
michael@0 | 94 | |
michael@0 | 95 | x = JSON.parse('["foo", "bar", "baz"]'); |
michael@0 | 96 | assertIsArray(x); |
michael@0 | 97 | assertEq(x.length, 3); |
michael@0 | 98 | assertEq(x[0], "foo"); |
michael@0 | 99 | assertEq(x[1], "bar"); |
michael@0 | 100 | assertEq(x[2], "baz"); |
michael@0 | 101 | |
michael@0 | 102 | // numbers |
michael@0 | 103 | x = JSON.parse('{"foo":5.5, "bar":5}'); |
michael@0 | 104 | assertIsObject(x); |
michael@0 | 105 | props = Object.getOwnPropertyNames(x).sort(); |
michael@0 | 106 | assertEq(props.length, 2); |
michael@0 | 107 | assertEq(props[0], "bar"); |
michael@0 | 108 | assertEq(props[1], "foo"); |
michael@0 | 109 | assertEq(x.foo, 5.5); |
michael@0 | 110 | assertEq(x.bar, 5); |
michael@0 | 111 | |
michael@0 | 112 | // keywords |
michael@0 | 113 | x = JSON.parse('{"foo": true, "bar":false, "baz":null}'); |
michael@0 | 114 | assertIsObject(x); |
michael@0 | 115 | props = Object.getOwnPropertyNames(x).sort(); |
michael@0 | 116 | assertEq(props.length, 3); |
michael@0 | 117 | assertEq(props[0], "bar"); |
michael@0 | 118 | assertEq(props[1], "baz"); |
michael@0 | 119 | assertEq(props[2], "foo"); |
michael@0 | 120 | assertEq(x.foo, true); |
michael@0 | 121 | assertEq(x.bar, false); |
michael@0 | 122 | assertEq(x.baz, null); |
michael@0 | 123 | |
michael@0 | 124 | // short escapes |
michael@0 | 125 | x = JSON.parse('{"foo": "\\"", "bar":"\\\\", "baz":"\\b","qux":"\\f", "quux":"\\n", "quuux":"\\r","quuuux":"\\t"}'); |
michael@0 | 126 | props = Object.getOwnPropertyNames(x).sort(); |
michael@0 | 127 | assertEq(props.length, 7); |
michael@0 | 128 | assertEq(props[0], "bar"); |
michael@0 | 129 | assertEq(props[1], "baz"); |
michael@0 | 130 | assertEq(props[2], "foo"); |
michael@0 | 131 | assertEq(props[3], "quuuux"); |
michael@0 | 132 | assertEq(props[4], "quuux"); |
michael@0 | 133 | assertEq(props[5], "quux"); |
michael@0 | 134 | assertEq(props[6], "qux"); |
michael@0 | 135 | assertEq(x.foo, '"'); |
michael@0 | 136 | assertEq(x.bar, '\\'); |
michael@0 | 137 | assertEq(x.baz, '\b'); |
michael@0 | 138 | assertEq(x.qux, '\f'); |
michael@0 | 139 | assertEq(x.quux, "\n"); |
michael@0 | 140 | assertEq(x.quuux, "\r"); |
michael@0 | 141 | assertEq(x.quuuux, "\t"); |
michael@0 | 142 | |
michael@0 | 143 | // unicode escape |
michael@0 | 144 | x = JSON.parse('{"foo":"hmm\\u006dmm"}'); |
michael@0 | 145 | assertIsObject(x); |
michael@0 | 146 | props = Object.getOwnPropertyNames(x); |
michael@0 | 147 | assertEq(props.length, 1); |
michael@0 | 148 | assertEq(props[0], "foo"); |
michael@0 | 149 | assertEq("hmm\u006dmm", x.foo); |
michael@0 | 150 | |
michael@0 | 151 | x = JSON.parse('{"hmm\\u006dmm":"foo"}'); |
michael@0 | 152 | assertIsObject(x); |
michael@0 | 153 | props = Object.getOwnPropertyNames(x); |
michael@0 | 154 | assertEq(props.length, 1); |
michael@0 | 155 | assertEq(props[0], "hmmmmm"); |
michael@0 | 156 | assertEq(x.hmm\u006dmm, "foo"); |
michael@0 | 157 | |
michael@0 | 158 | // miscellaneous |
michael@0 | 159 | x = JSON.parse('{"JSON Test Pattern pass3": {"The outermost value": "must be an object or array.","In this test": "It is an object." }}'); |
michael@0 | 160 | assertIsObject(x); |
michael@0 | 161 | props = Object.getOwnPropertyNames(x); |
michael@0 | 162 | assertEq(props.length, 1); |
michael@0 | 163 | assertEq(props[0], "JSON Test Pattern pass3"); |
michael@0 | 164 | assertIsObject(x["JSON Test Pattern pass3"]); |
michael@0 | 165 | props = Object.getOwnPropertyNames(x["JSON Test Pattern pass3"]).sort(); |
michael@0 | 166 | assertEq(props.length, 2); |
michael@0 | 167 | assertEq(props[0], "In this test"); |
michael@0 | 168 | assertEq(props[1], "The outermost value"); |
michael@0 | 169 | assertEq(x["JSON Test Pattern pass3"]["The outermost value"], |
michael@0 | 170 | "must be an object or array."); |
michael@0 | 171 | assertEq(x["JSON Test Pattern pass3"]["In this test"], "It is an object."); |
michael@0 | 172 | |
michael@0 | 173 | /******************************************************************************/ |
michael@0 | 174 | |
michael@0 | 175 | if (typeof reportCompare === "function") |
michael@0 | 176 | reportCompare(true, true); |
michael@0 | 177 | |
michael@0 | 178 | print("Tests complete"); |