addon-sdk/source/test/test-sandbox.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.

     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 { sandbox, load, evaluate, nuke } = require('sdk/loader/sandbox');
     6 const xulApp = require("sdk/system/xul-app");
     7 const fixturesURI = module.uri.split('test-sandbox.js')[0] + 'fixtures/';
     9 // The following adds Debugger constructor to the global namespace.
    10 const { Cu } = require('chrome');
    11 const { addDebuggerToGlobal } =
    12   Cu.import('resource://gre/modules/jsdebugger.jsm', {});
    13 addDebuggerToGlobal(this);
    15 exports['test basics'] = function(assert) {
    16   let fixture = sandbox('http://example.com');
    17   assert.equal(evaluate(fixture, 'var a = 1;'), undefined,
    18                'returns expression value');
    19   assert.equal(evaluate(fixture, 'b = 2;'), 2,
    20                'returns expression value');
    21   assert.equal(fixture.b, 2, 'global is defined as property');
    22   assert.equal(fixture.a, 1, 'global is defined as property');
    23   assert.equal(evaluate(fixture, 'a + b;'), 3, 'returns correct sum');
    24 };
    26 exports['test non-privileged'] = function(assert) {
    27   let fixture = sandbox('http://example.com');
    28   if (xulApp.versionInRange(xulApp.platformVersion, "15.0a1", "18.*")) {
    29     let rv = evaluate(fixture, 'Compo' + 'nents.utils');
    30     assert.equal(rv, undefined,
    31                  "Components's attributes are undefined in content sandboxes");
    32   }
    33   else {
    34     assert.throws(function() {
    35       evaluate(fixture, 'Compo' + 'nents.utils');
    36     }, 'Access to components is restricted');
    37   }
    38   fixture.sandbox = sandbox;
    39   assert.throws(function() {
    40     evaluate(fixture, sandbox('http://foo.com'));
    41   }, 'Can not call privileged code');
    42 };
    44 exports['test injection'] = function(assert) {
    45   let fixture = sandbox();
    46   fixture.hi = function(name) 'Hi ' + name
    47   assert.equal(evaluate(fixture, 'hi("sandbox");'), 'Hi sandbox',
    48                 'injected functions are callable');
    49 };
    51 exports['test exceptions'] = function(assert) {
    52   let fixture = sandbox();
    53   try {
    54     evaluate(fixture, '!' + function() {
    55       var message = 'boom';
    56       throw Error(message);
    57     } + '();');
    58   }
    59   catch (error) {
    60     assert.equal(error.fileName, '', 'no fileName reported');
    61     assert.equal(error.lineNumber, 3, 'reports correct line number');
    62   }
    64   try {
    65     evaluate(fixture, '!' + function() {
    66       var message = 'boom';
    67       throw Error(message);
    68     } + '();', 'foo.js');
    69   }
    70   catch (error) {
    71     assert.equal(error.fileName, 'foo.js', 'correct fileName reported');
    72     assert.equal(error.lineNumber, 3, 'reports correct line number');
    73   }
    75   try {
    76     evaluate(fixture, '!' + function() {
    77       var message = 'boom';
    78       throw Error(message);
    79     } + '();', 'foo.js', 2);
    80   }
    81   catch (error) {
    82     assert.equal(error.fileName, 'foo.js', 'correct fileName reported');
    83     assert.equal(error.lineNumber, 4, 'line number was opted');
    84   }
    85 };
    87 exports['test opt version'] = function(assert) {
    88   let fixture = sandbox();
    89   assert.throws(function() {
    90     evaluate(fixture, 'let a = 2;', 'test.js', 1, '1.5');
    91   }, 'No let in js 1.5');
    92 };
    94 exports['test load'] = function(assert) {
    95   let fixture = sandbox();
    96   load(fixture, fixturesURI + 'sandbox-normal.js');
    97   assert.equal(fixture.a, 1, 'global variable defined');
    98   assert.equal(fixture.b, 2, 'global via `this` property was set');
    99   assert.equal(fixture.f(), 4, 'function was defined');
   100 };
   102 exports['test load with data: URL'] = function(assert) {
   103   let code = "var a = 1; this.b = 2; function f() 4";
   104   let fixture = sandbox();
   105   load(fixture, "data:," + encodeURIComponent(code));
   107   assert.equal(fixture.a, 1, 'global variable defined');
   108   assert.equal(fixture.b, 2, 'global via `this` property was set');
   109   assert.equal(fixture.f(), 4, 'function was defined');
   110 };
   112 exports['test load script with complex char'] = function(assert) {
   113   let fixture = sandbox();
   114   load(fixture, fixturesURI + 'sandbox-complex-character.js');
   115   assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly');
   116 };
   118 exports['test load script with data: URL and complex char'] = function(assert) {
   119   let code = "var chars = 'გამარჯობა';";
   120   let fixture = sandbox();
   121   load(fixture, "data:," + encodeURIComponent(code));
   123   assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly');
   124 };
   126 exports['test metadata']  = function(assert) {
   127   let dbg = new Debugger();
   128   dbg.onNewGlobalObject = function(global) {
   129     let metadata = Cu.getSandboxMetadata(global.unsafeDereference());
   130     assert.ok(metadata, 'this global has attached metadata');
   131     assert.equal(metadata.addonID, self.id, 'addon ID is set');
   133     dbg.onNewGlobalObject = undefined;
   134   }
   136   let fixture = sandbox();
   137   let self = require('sdk/self');
   138 }
   140 exports['test nuke sandbox'] = function(assert) {
   142   let fixture = sandbox('http://example.com');
   143   fixture.foo = 'foo';
   145   let ref = evaluate(fixture, 'let a = {bar: "bar"}; a');
   147   nuke(fixture);
   149   assert.ok(Cu.isDeadWrapper(fixture), 'sandbox should be dead');
   151   assert.throws(
   152     () => fixture.foo,
   153     /can't access dead object/,
   154     'property of nuked sandbox should not be accessible'
   155   );
   157   assert.ok(Cu.isDeadWrapper(ref), 'ref to object from sandbox should be dead');
   159   assert.throws(
   160     () => ref.bar,
   161     /can't access dead object/,
   162     'object from nuked sandbox should not be alive'
   163   );
   164 }
   166 require('test').run(exports);

mercurial