1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/addon-sdk/source/test/test-sandbox.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,166 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +const { sandbox, load, evaluate, nuke } = require('sdk/loader/sandbox'); 1.9 +const xulApp = require("sdk/system/xul-app"); 1.10 +const fixturesURI = module.uri.split('test-sandbox.js')[0] + 'fixtures/'; 1.11 + 1.12 +// The following adds Debugger constructor to the global namespace. 1.13 +const { Cu } = require('chrome'); 1.14 +const { addDebuggerToGlobal } = 1.15 + Cu.import('resource://gre/modules/jsdebugger.jsm', {}); 1.16 +addDebuggerToGlobal(this); 1.17 + 1.18 +exports['test basics'] = function(assert) { 1.19 + let fixture = sandbox('http://example.com'); 1.20 + assert.equal(evaluate(fixture, 'var a = 1;'), undefined, 1.21 + 'returns expression value'); 1.22 + assert.equal(evaluate(fixture, 'b = 2;'), 2, 1.23 + 'returns expression value'); 1.24 + assert.equal(fixture.b, 2, 'global is defined as property'); 1.25 + assert.equal(fixture.a, 1, 'global is defined as property'); 1.26 + assert.equal(evaluate(fixture, 'a + b;'), 3, 'returns correct sum'); 1.27 +}; 1.28 + 1.29 +exports['test non-privileged'] = function(assert) { 1.30 + let fixture = sandbox('http://example.com'); 1.31 + if (xulApp.versionInRange(xulApp.platformVersion, "15.0a1", "18.*")) { 1.32 + let rv = evaluate(fixture, 'Compo' + 'nents.utils'); 1.33 + assert.equal(rv, undefined, 1.34 + "Components's attributes are undefined in content sandboxes"); 1.35 + } 1.36 + else { 1.37 + assert.throws(function() { 1.38 + evaluate(fixture, 'Compo' + 'nents.utils'); 1.39 + }, 'Access to components is restricted'); 1.40 + } 1.41 + fixture.sandbox = sandbox; 1.42 + assert.throws(function() { 1.43 + evaluate(fixture, sandbox('http://foo.com')); 1.44 + }, 'Can not call privileged code'); 1.45 +}; 1.46 + 1.47 +exports['test injection'] = function(assert) { 1.48 + let fixture = sandbox(); 1.49 + fixture.hi = function(name) 'Hi ' + name 1.50 + assert.equal(evaluate(fixture, 'hi("sandbox");'), 'Hi sandbox', 1.51 + 'injected functions are callable'); 1.52 +}; 1.53 + 1.54 +exports['test exceptions'] = function(assert) { 1.55 + let fixture = sandbox(); 1.56 + try { 1.57 + evaluate(fixture, '!' + function() { 1.58 + var message = 'boom'; 1.59 + throw Error(message); 1.60 + } + '();'); 1.61 + } 1.62 + catch (error) { 1.63 + assert.equal(error.fileName, '', 'no fileName reported'); 1.64 + assert.equal(error.lineNumber, 3, 'reports correct line number'); 1.65 + } 1.66 + 1.67 + try { 1.68 + evaluate(fixture, '!' + function() { 1.69 + var message = 'boom'; 1.70 + throw Error(message); 1.71 + } + '();', 'foo.js'); 1.72 + } 1.73 + catch (error) { 1.74 + assert.equal(error.fileName, 'foo.js', 'correct fileName reported'); 1.75 + assert.equal(error.lineNumber, 3, 'reports correct line number'); 1.76 + } 1.77 + 1.78 + try { 1.79 + evaluate(fixture, '!' + function() { 1.80 + var message = 'boom'; 1.81 + throw Error(message); 1.82 + } + '();', 'foo.js', 2); 1.83 + } 1.84 + catch (error) { 1.85 + assert.equal(error.fileName, 'foo.js', 'correct fileName reported'); 1.86 + assert.equal(error.lineNumber, 4, 'line number was opted'); 1.87 + } 1.88 +}; 1.89 + 1.90 +exports['test opt version'] = function(assert) { 1.91 + let fixture = sandbox(); 1.92 + assert.throws(function() { 1.93 + evaluate(fixture, 'let a = 2;', 'test.js', 1, '1.5'); 1.94 + }, 'No let in js 1.5'); 1.95 +}; 1.96 + 1.97 +exports['test load'] = function(assert) { 1.98 + let fixture = sandbox(); 1.99 + load(fixture, fixturesURI + 'sandbox-normal.js'); 1.100 + assert.equal(fixture.a, 1, 'global variable defined'); 1.101 + assert.equal(fixture.b, 2, 'global via `this` property was set'); 1.102 + assert.equal(fixture.f(), 4, 'function was defined'); 1.103 +}; 1.104 + 1.105 +exports['test load with data: URL'] = function(assert) { 1.106 + let code = "var a = 1; this.b = 2; function f() 4"; 1.107 + let fixture = sandbox(); 1.108 + load(fixture, "data:," + encodeURIComponent(code)); 1.109 + 1.110 + assert.equal(fixture.a, 1, 'global variable defined'); 1.111 + assert.equal(fixture.b, 2, 'global via `this` property was set'); 1.112 + assert.equal(fixture.f(), 4, 'function was defined'); 1.113 +}; 1.114 + 1.115 +exports['test load script with complex char'] = function(assert) { 1.116 + let fixture = sandbox(); 1.117 + load(fixture, fixturesURI + 'sandbox-complex-character.js'); 1.118 + assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly'); 1.119 +}; 1.120 + 1.121 +exports['test load script with data: URL and complex char'] = function(assert) { 1.122 + let code = "var chars = 'გამარჯობა';"; 1.123 + let fixture = sandbox(); 1.124 + load(fixture, "data:," + encodeURIComponent(code)); 1.125 + 1.126 + assert.equal(fixture.chars, 'გამარჯობა', 'complex chars were loaded correctly'); 1.127 +}; 1.128 + 1.129 +exports['test metadata'] = function(assert) { 1.130 + let dbg = new Debugger(); 1.131 + dbg.onNewGlobalObject = function(global) { 1.132 + let metadata = Cu.getSandboxMetadata(global.unsafeDereference()); 1.133 + assert.ok(metadata, 'this global has attached metadata'); 1.134 + assert.equal(metadata.addonID, self.id, 'addon ID is set'); 1.135 + 1.136 + dbg.onNewGlobalObject = undefined; 1.137 + } 1.138 + 1.139 + let fixture = sandbox(); 1.140 + let self = require('sdk/self'); 1.141 +} 1.142 + 1.143 +exports['test nuke sandbox'] = function(assert) { 1.144 + 1.145 + let fixture = sandbox('http://example.com'); 1.146 + fixture.foo = 'foo'; 1.147 + 1.148 + let ref = evaluate(fixture, 'let a = {bar: "bar"}; a'); 1.149 + 1.150 + nuke(fixture); 1.151 + 1.152 + assert.ok(Cu.isDeadWrapper(fixture), 'sandbox should be dead'); 1.153 + 1.154 + assert.throws( 1.155 + () => fixture.foo, 1.156 + /can't access dead object/, 1.157 + 'property of nuked sandbox should not be accessible' 1.158 + ); 1.159 + 1.160 + assert.ok(Cu.isDeadWrapper(ref), 'ref to object from sandbox should be dead'); 1.161 + 1.162 + assert.throws( 1.163 + () => ref.bar, 1.164 + /can't access dead object/, 1.165 + 'object from nuked sandbox should not be alive' 1.166 + ); 1.167 +} 1.168 + 1.169 +require('test').run(exports);