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 | /* 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 | 'use strict'; |
michael@0 | 6 | |
michael@0 | 7 | let { |
michael@0 | 8 | Loader, main, unload, parseStack, generateMap, resolve, join |
michael@0 | 9 | } = require('toolkit/loader'); |
michael@0 | 10 | let { readURI } = require('sdk/net/url'); |
michael@0 | 11 | |
michael@0 | 12 | let root = module.uri.substr(0, module.uri.lastIndexOf('/')) |
michael@0 | 13 | |
michael@0 | 14 | |
michael@0 | 15 | // The following adds Debugger constructor to the global namespace. |
michael@0 | 16 | const { Cu } = require('chrome'); |
michael@0 | 17 | const { addDebuggerToGlobal } = Cu.import('resource://gre/modules/jsdebugger.jsm', {}); |
michael@0 | 18 | addDebuggerToGlobal(this); |
michael@0 | 19 | |
michael@0 | 20 | exports['test resolve'] = function (assert) { |
michael@0 | 21 | let cuddlefish_id = 'sdk/loader/cuddlefish'; |
michael@0 | 22 | assert.equal(resolve('../index.js', './dir/c.js'), './index.js'); |
michael@0 | 23 | assert.equal(resolve('./index.js', './dir/c.js'), './dir/index.js'); |
michael@0 | 24 | assert.equal(resolve('./dir/c.js', './index.js'), './dir/c.js'); |
michael@0 | 25 | assert.equal(resolve('../utils/file.js', './dir/b.js'), './utils/file.js'); |
michael@0 | 26 | |
michael@0 | 27 | assert.equal(resolve('../utils/./file.js', './dir/b.js'), './utils/file.js'); |
michael@0 | 28 | assert.equal(resolve('../utils/file.js', './'), './../utils/file.js'); |
michael@0 | 29 | assert.equal(resolve('./utils/file.js', './'), './utils/file.js'); |
michael@0 | 30 | assert.equal(resolve('./utils/file.js', './index.js'), './utils/file.js'); |
michael@0 | 31 | |
michael@0 | 32 | assert.equal(resolve('../utils/./file.js', cuddlefish_id), 'sdk/utils/file.js'); |
michael@0 | 33 | assert.equal(resolve('../utils/file.js', cuddlefish_id), 'sdk/utils/file.js'); |
michael@0 | 34 | assert.equal(resolve('./utils/file.js', cuddlefish_id), 'sdk/loader/utils/file.js'); |
michael@0 | 35 | |
michael@0 | 36 | assert.equal(resolve('..//index.js', './dir/c.js'), './index.js'); |
michael@0 | 37 | assert.equal(resolve('../../gre/modules/XPCOMUtils.jsm', 'resource://thing/utils/file.js'), 'resource://gre/modules/XPCOMUtils.jsm'); |
michael@0 | 38 | assert.equal(resolve('../../gre/modules/XPCOMUtils.jsm', 'chrome://thing/utils/file.js'), 'chrome://gre/modules/XPCOMUtils.jsm'); |
michael@0 | 39 | assert.equal(resolve('../../a/b/c.json', 'file:///thing/utils/file.js'), 'file:///a/b/c.json'); |
michael@0 | 40 | |
michael@0 | 41 | // Does not change absolute paths |
michael@0 | 42 | assert.equal(resolve('resource://gre/modules/file.js', './dir/b.js'), |
michael@0 | 43 | 'resource://gre/modules/file.js'); |
michael@0 | 44 | assert.equal(resolve('file:///gre/modules/file.js', './dir/b.js'), |
michael@0 | 45 | 'file:///gre/modules/file.js'); |
michael@0 | 46 | assert.equal(resolve('/root.js', './dir/b.js'), |
michael@0 | 47 | '/root.js'); |
michael@0 | 48 | }; |
michael@0 | 49 | |
michael@0 | 50 | exports['test join'] = function (assert) { |
michael@0 | 51 | assert.equal(join('a/path', '../../../module'), '../module'); |
michael@0 | 52 | assert.equal(join('a/path/to', '../module'), 'a/path/module'); |
michael@0 | 53 | assert.equal(join('a/path/to', './module'), 'a/path/to/module'); |
michael@0 | 54 | assert.equal(join('a/path/to', '././../module'), 'a/path/module'); |
michael@0 | 55 | assert.equal(join('resource://my/path/yeah/yuh', '../whoa'), |
michael@0 | 56 | 'resource://my/path/yeah/whoa'); |
michael@0 | 57 | assert.equal(join('resource://my/path/yeah/yuh', './whoa'), |
michael@0 | 58 | 'resource://my/path/yeah/yuh/whoa'); |
michael@0 | 59 | assert.equal(join('file:///my/path/yeah/yuh', '../whoa'), |
michael@0 | 60 | 'file:///my/path/yeah/whoa'); |
michael@0 | 61 | assert.equal(join('file:///my/path/yeah/yuh', './whoa'), |
michael@0 | 62 | 'file:///my/path/yeah/yuh/whoa'); |
michael@0 | 63 | assert.equal(join('a/path/to', '..//module'), 'a/path/module'); |
michael@0 | 64 | }; |
michael@0 | 65 | |
michael@0 | 66 | exports['test dependency cycles'] = function(assert) { |
michael@0 | 67 | let uri = root + '/fixtures/loader/cycles/'; |
michael@0 | 68 | let loader = Loader({ paths: { '': uri } }); |
michael@0 | 69 | |
michael@0 | 70 | let program = main(loader, 'main'); |
michael@0 | 71 | |
michael@0 | 72 | assert.equal(program.a.b, program.b, 'module `a` gets correct `b`'); |
michael@0 | 73 | assert.equal(program.b.a, program.a, 'module `b` gets correct `a`'); |
michael@0 | 74 | assert.equal(program.c.main, program, 'module `c` gets correct `main`'); |
michael@0 | 75 | |
michael@0 | 76 | unload(loader); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | exports['test syntax errors'] = function(assert) { |
michael@0 | 80 | let uri = root + '/fixtures/loader/syntax-error/'; |
michael@0 | 81 | let loader = Loader({ paths: { '': uri } }); |
michael@0 | 82 | |
michael@0 | 83 | try { |
michael@0 | 84 | let program = main(loader, 'main'); |
michael@0 | 85 | } catch (error) { |
michael@0 | 86 | assert.equal(error.name, "SyntaxError", "throws syntax error"); |
michael@0 | 87 | assert.equal(error.fileName.split("/").pop(), "error.js", |
michael@0 | 88 | "Error contains filename"); |
michael@0 | 89 | assert.equal(error.lineNumber, 11, "error is on line 11"); |
michael@0 | 90 | let stack = parseStack(error.stack); |
michael@0 | 91 | |
michael@0 | 92 | assert.equal(stack.pop().fileName, uri + "error.js", |
michael@0 | 93 | "last frame file containing syntax error"); |
michael@0 | 94 | assert.equal(stack.pop().fileName, uri + "main.js", |
michael@0 | 95 | "previous frame is a requirer module"); |
michael@0 | 96 | assert.equal(stack.pop().fileName, module.uri, |
michael@0 | 97 | "previous to it is a test module"); |
michael@0 | 98 | |
michael@0 | 99 | } finally { |
michael@0 | 100 | unload(loader); |
michael@0 | 101 | } |
michael@0 | 102 | } |
michael@0 | 103 | |
michael@0 | 104 | exports['test sandboxes are not added if error'] = function (assert) { |
michael@0 | 105 | let uri = root + '/fixtures/loader/missing-twice/'; |
michael@0 | 106 | let loader = Loader({ paths: { '': uri } }); |
michael@0 | 107 | let program = main(loader, 'main'); |
michael@0 | 108 | assert.ok(!(uri + 'not-found.js' in loader.sandboxes), 'not-found.js not in loader.sandboxes'); |
michael@0 | 109 | } |
michael@0 | 110 | |
michael@0 | 111 | exports['test missing module'] = function(assert) { |
michael@0 | 112 | let uri = root + '/fixtures/loader/missing/' |
michael@0 | 113 | let loader = Loader({ paths: { '': uri } }); |
michael@0 | 114 | |
michael@0 | 115 | try { |
michael@0 | 116 | let program = main(loader, 'main') |
michael@0 | 117 | } catch (error) { |
michael@0 | 118 | assert.equal(error.message, "Module `not-found` is not found at " + |
michael@0 | 119 | uri + "not-found.js", "throws if error not found"); |
michael@0 | 120 | |
michael@0 | 121 | assert.equal(error.fileName.split("/").pop(), "main.js", |
michael@0 | 122 | "Error fileName is requirer module"); |
michael@0 | 123 | |
michael@0 | 124 | assert.equal(error.lineNumber, 7, "error is on line 7"); |
michael@0 | 125 | |
michael@0 | 126 | let stack = parseStack(error.stack); |
michael@0 | 127 | |
michael@0 | 128 | assert.equal(stack.pop().fileName, uri + "main.js", |
michael@0 | 129 | "loader stack is omitted"); |
michael@0 | 130 | |
michael@0 | 131 | assert.equal(stack.pop().fileName, module.uri, |
michael@0 | 132 | "previous in the stack is test module"); |
michael@0 | 133 | } finally { |
michael@0 | 134 | unload(loader); |
michael@0 | 135 | } |
michael@0 | 136 | } |
michael@0 | 137 | |
michael@0 | 138 | exports["test invalid module not cached and throws everytime"] = function(assert) { |
michael@0 | 139 | let uri = root + "/fixtures/loader/missing-twice/"; |
michael@0 | 140 | let loader = Loader({ paths: { "": uri } }); |
michael@0 | 141 | |
michael@0 | 142 | let { firstError, secondError, invalidJSON1, invalidJSON2 } = main(loader, "main"); |
michael@0 | 143 | assert.equal(firstError.message, "Module `not-found` is not found at " + |
michael@0 | 144 | uri + "not-found.js", "throws on first invalid require"); |
michael@0 | 145 | assert.equal(firstError.lineNumber, 8, "first error is on line 7"); |
michael@0 | 146 | assert.equal(secondError.message, "Module `not-found` is not found at " + |
michael@0 | 147 | uri + "not-found.js", "throws on second invalid require"); |
michael@0 | 148 | assert.equal(secondError.lineNumber, 14, "second error is on line 14"); |
michael@0 | 149 | |
michael@0 | 150 | assert.equal(invalidJSON1.message, |
michael@0 | 151 | "JSON.parse: unexpected character at line 1 column 1 of the JSON data", |
michael@0 | 152 | "throws on invalid JSON"); |
michael@0 | 153 | assert.equal(invalidJSON2.message, |
michael@0 | 154 | "JSON.parse: unexpected character at line 1 column 1 of the JSON data", |
michael@0 | 155 | "throws on invalid JSON second time"); |
michael@0 | 156 | }; |
michael@0 | 157 | |
michael@0 | 158 | exports['test exceptions in modules'] = function(assert) { |
michael@0 | 159 | let uri = root + '/fixtures/loader/exceptions/' |
michael@0 | 160 | |
michael@0 | 161 | let loader = Loader({ paths: { '': uri } }); |
michael@0 | 162 | |
michael@0 | 163 | try { |
michael@0 | 164 | let program = main(loader, 'main') |
michael@0 | 165 | } catch (error) { |
michael@0 | 166 | assert.equal(error.message, "Boom!", "thrown errors propagate"); |
michael@0 | 167 | |
michael@0 | 168 | assert.equal(error.fileName.split("/").pop(), "boomer.js", |
michael@0 | 169 | "Error comes from the module that threw it"); |
michael@0 | 170 | |
michael@0 | 171 | assert.equal(error.lineNumber, 8, "error is on line 8"); |
michael@0 | 172 | |
michael@0 | 173 | let stack = parseStack(error.stack); |
michael@0 | 174 | |
michael@0 | 175 | let frame = stack.pop() |
michael@0 | 176 | assert.equal(frame.fileName, uri + "boomer.js", |
michael@0 | 177 | "module that threw is first in the stack"); |
michael@0 | 178 | assert.equal(frame.name, "exports.boom", |
michael@0 | 179 | "name is in the stack"); |
michael@0 | 180 | |
michael@0 | 181 | frame = stack.pop() |
michael@0 | 182 | assert.equal(frame.fileName, uri + "main.js", |
michael@0 | 183 | "module that called it is next in the stack"); |
michael@0 | 184 | assert.equal(frame.lineNumber, 9, "caller line is in the stack"); |
michael@0 | 185 | |
michael@0 | 186 | |
michael@0 | 187 | assert.equal(stack.pop().fileName, module.uri, |
michael@0 | 188 | "this test module is next in the stack"); |
michael@0 | 189 | } finally { |
michael@0 | 190 | unload(loader); |
michael@0 | 191 | } |
michael@0 | 192 | } |
michael@0 | 193 | |
michael@0 | 194 | exports['test early errors in module'] = function(assert) { |
michael@0 | 195 | let uri = root + '/fixtures/loader/errors/'; |
michael@0 | 196 | let loader = Loader({ paths: { '': uri } }); |
michael@0 | 197 | |
michael@0 | 198 | try { |
michael@0 | 199 | let program = main(loader, 'main') |
michael@0 | 200 | } catch (error) { |
michael@0 | 201 | assert.equal(String(error), |
michael@0 | 202 | "Error: opening input stream (invalid filename?)", |
michael@0 | 203 | "thrown errors propagate"); |
michael@0 | 204 | |
michael@0 | 205 | assert.equal(error.fileName.split("/").pop(), "boomer.js", |
michael@0 | 206 | "Error comes from the module that threw it"); |
michael@0 | 207 | |
michael@0 | 208 | assert.equal(error.lineNumber, 7, "error is on line 7"); |
michael@0 | 209 | |
michael@0 | 210 | let stack = parseStack(error.stack); |
michael@0 | 211 | |
michael@0 | 212 | let frame = stack.pop() |
michael@0 | 213 | assert.equal(frame.fileName, uri + "boomer.js", |
michael@0 | 214 | "module that threw is first in the stack"); |
michael@0 | 215 | |
michael@0 | 216 | frame = stack.pop() |
michael@0 | 217 | assert.equal(frame.fileName, uri + "main.js", |
michael@0 | 218 | "module that called it is next in the stack"); |
michael@0 | 219 | assert.equal(frame.lineNumber, 7, "caller line is in the stack"); |
michael@0 | 220 | |
michael@0 | 221 | |
michael@0 | 222 | assert.equal(stack.pop().fileName, module.uri, |
michael@0 | 223 | "this test module is next in the stack"); |
michael@0 | 224 | } finally { |
michael@0 | 225 | unload(loader); |
michael@0 | 226 | } |
michael@0 | 227 | }; |
michael@0 | 228 | |
michael@0 | 229 | exports['test require json'] = function (assert) { |
michael@0 | 230 | let data = require('./fixtures/loader/json/manifest.json'); |
michael@0 | 231 | assert.equal(data.name, 'Jetpack Loader Test', 'loads json with strings'); |
michael@0 | 232 | assert.equal(data.version, '1.0.1', 'loads json with strings'); |
michael@0 | 233 | assert.equal(data.dependencies.async, '*', 'loads json with objects'); |
michael@0 | 234 | assert.equal(data.dependencies.underscore, '*', 'loads json with objects'); |
michael@0 | 235 | assert.equal(data.contributors.length, 4, 'loads json with arrays'); |
michael@0 | 236 | assert.ok(Array.isArray(data.contributors), 'loads json with arrays'); |
michael@0 | 237 | data.version = '2.0.0'; |
michael@0 | 238 | let newdata = require('./fixtures/loader/json/manifest.json'); |
michael@0 | 239 | assert.equal(newdata.version, '2.0.0', |
michael@0 | 240 | 'JSON objects returned should be cached and the same instance'); |
michael@0 | 241 | |
michael@0 | 242 | try { |
michael@0 | 243 | require('./fixtures/loader/json/invalid.json'); |
michael@0 | 244 | assert.fail('Error not thrown when loading invalid json'); |
michael@0 | 245 | } catch (err) { |
michael@0 | 246 | assert.ok(err, 'error thrown when loading invalid json'); |
michael@0 | 247 | assert.ok(/JSON\.parse/.test(err.message), |
michael@0 | 248 | 'should thrown an error from JSON.parse, not attempt to load .json.js'); |
michael@0 | 249 | } |
michael@0 | 250 | |
michael@0 | 251 | // Try again to ensure an empty module isn't loaded from cache |
michael@0 | 252 | try { |
michael@0 | 253 | require('./fixtures/loader/json/invalid.json'); |
michael@0 | 254 | assert.fail('Error not thrown when loading invalid json a second time'); |
michael@0 | 255 | } catch (err) { |
michael@0 | 256 | assert.ok(err, |
michael@0 | 257 | 'error thrown when loading invalid json a second time'); |
michael@0 | 258 | assert.ok(/JSON\.parse/.test(err.message), |
michael@0 | 259 | 'should thrown an error from JSON.parse a second time, not attempt to load .json.js'); |
michael@0 | 260 | } |
michael@0 | 261 | }; |
michael@0 | 262 | |
michael@0 | 263 | exports['test setting metadata for newly created sandboxes'] = function(assert) { |
michael@0 | 264 | let addonID = 'random-addon-id'; |
michael@0 | 265 | let uri = root + '/fixtures/loader/cycles/'; |
michael@0 | 266 | let loader = Loader({ paths: { '': uri }, id: addonID }); |
michael@0 | 267 | |
michael@0 | 268 | let dbg = new Debugger(); |
michael@0 | 269 | dbg.onNewGlobalObject = function(global) { |
michael@0 | 270 | dbg.onNewGlobalObject = undefined; |
michael@0 | 271 | |
michael@0 | 272 | let metadata = Cu.getSandboxMetadata(global.unsafeDereference()); |
michael@0 | 273 | assert.ok(metadata, 'this global has attached metadata'); |
michael@0 | 274 | assert.equal(metadata.URI, uri + 'main.js', 'URI is set properly'); |
michael@0 | 275 | assert.equal(metadata.addonID, addonID, 'addon ID is set'); |
michael@0 | 276 | } |
michael@0 | 277 | |
michael@0 | 278 | let program = main(loader, 'main'); |
michael@0 | 279 | }; |
michael@0 | 280 | |
michael@0 | 281 | exports['test require .json, .json.js'] = function (assert) { |
michael@0 | 282 | let testjson = require('./fixtures/loader/json/test.json'); |
michael@0 | 283 | assert.equal(testjson.filename, 'test.json', |
michael@0 | 284 | 'require("./x.json") should load x.json, not x.json.js'); |
michael@0 | 285 | |
michael@0 | 286 | let nodotjson = require('./fixtures/loader/json/nodotjson.json'); |
michael@0 | 287 | assert.equal(nodotjson.filename, 'nodotjson.json.js', |
michael@0 | 288 | 'require("./x.json") should load x.json.js when x.json does not exist'); |
michael@0 | 289 | nodotjson.data.prop = 'hydralisk'; |
michael@0 | 290 | |
michael@0 | 291 | // require('nodotjson.json') and require('nodotjson.json.js') |
michael@0 | 292 | // should resolve to the same file |
michael@0 | 293 | let nodotjsonjs = require('./fixtures/loader/json/nodotjson.json.js'); |
michael@0 | 294 | assert.equal(nodotjsonjs.data.prop, 'hydralisk', |
michael@0 | 295 | 'js modules are cached whether access via .json.js or .json'); |
michael@0 | 296 | }; |
michael@0 | 297 | |
michael@0 | 298 | exports['test invisibleToDebugger: false'] = function (assert) { |
michael@0 | 299 | let uri = root + '/fixtures/loader/cycles/'; |
michael@0 | 300 | let loader = Loader({ paths: { '': uri } }); |
michael@0 | 301 | main(loader, 'main'); |
michael@0 | 302 | |
michael@0 | 303 | let dbg = new Debugger(); |
michael@0 | 304 | let sandbox = loader.sandboxes[uri + 'main.js']; |
michael@0 | 305 | |
michael@0 | 306 | try { |
michael@0 | 307 | dbg.addDebuggee(sandbox); |
michael@0 | 308 | assert.ok(true, 'debugger added visible value'); |
michael@0 | 309 | } catch(e) { |
michael@0 | 310 | assert.fail('debugger could not add visible value'); |
michael@0 | 311 | } |
michael@0 | 312 | }; |
michael@0 | 313 | |
michael@0 | 314 | exports['test invisibleToDebugger: true'] = function (assert) { |
michael@0 | 315 | let uri = root + '/fixtures/loader/cycles/'; |
michael@0 | 316 | let loader = Loader({ paths: { '': uri }, invisibleToDebugger: true }); |
michael@0 | 317 | main(loader, 'main'); |
michael@0 | 318 | |
michael@0 | 319 | let dbg = new Debugger(); |
michael@0 | 320 | let sandbox = loader.sandboxes[uri + 'main.js']; |
michael@0 | 321 | |
michael@0 | 322 | try { |
michael@0 | 323 | dbg.addDebuggee(sandbox); |
michael@0 | 324 | assert.fail('debugger added invisible value'); |
michael@0 | 325 | } catch(e) { |
michael@0 | 326 | assert.ok(true, 'debugger did not add invisible value'); |
michael@0 | 327 | } |
michael@0 | 328 | }; |
michael@0 | 329 | |
michael@0 | 330 | exports['test console global by default'] = function (assert) { |
michael@0 | 331 | let uri = root + '/fixtures/loader/globals/'; |
michael@0 | 332 | let loader = Loader({ paths: { '': uri }}); |
michael@0 | 333 | let program = main(loader, 'main'); |
michael@0 | 334 | |
michael@0 | 335 | assert.ok(typeof program.console === 'object', 'global `console` exists'); |
michael@0 | 336 | assert.ok(typeof program.console.log === 'function', 'global `console.log` exists'); |
michael@0 | 337 | |
michael@0 | 338 | let loader2 = Loader({ paths: { '': uri }, globals: { console: fakeConsole }}); |
michael@0 | 339 | let program2 = main(loader2, 'main'); |
michael@0 | 340 | |
michael@0 | 341 | assert.equal(program2.console, fakeConsole, |
michael@0 | 342 | 'global console can be overridden with Loader options'); |
michael@0 | 343 | function fakeConsole () {}; |
michael@0 | 344 | }; |
michael@0 | 345 | |
michael@0 | 346 | require('test').run(exports); |