test-sandbox.js 4.7 KB

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