tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_instanceof_error_message.html (4433B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=1530413
      5 -->
      6 <head>
      7  <meta charset="utf-8">
      8  <title>Test for Bug 1530413</title>
      9  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
     11  <script type="application/javascript">
     12  add_task(() => {
     13    var Cu = SpecialPowers.Cu;
     14    try {
     15      // Test case 1
     16      ({} instanceof {});
     17      ok(false, "Should throw a type error");
     18    } catch (e) {
     19      is(e.message, "({}) is not a function", "We should get a correct error message when calling instanceof");
     20    }
     21    // Test case 2
     22    try {
     23      ({} instanceof document);
     24      ok(false, "Should throw a type error");
     25    } catch (e) {
     26      is(e.message, "document is not a function", "We should get a correct error message when calling instanceof");
     27    }
     28    // Test case 3
     29    try {
     30      ({} instanceof new Proxy(document, {}));
     31      ok(false, "Should throw a type error");
     32    } catch (e) {
     33      is(e.message, "(new Proxy(...)) is not a function", "We should get a correct error message when calling instanceof");
     34    }
     35    // Test case 4 - Verify invoking instanceof on an object from a different compartment yields the same error
     36    var sandbox = SpecialPowers.unwrap(Cu.Sandbox(this, { sameZoneAs: this, freshCompartment: true, wantXrays: false }));
     37    sandbox.window = window;
     38    sandbox.crossCompartmentObject = {}; // object created in the test compartment
     39    try {
     40      Cu.evalInSandbox("({} instanceof window);", sandbox);
     41      ok(false, "Should throw a type error");
     42    } catch (e) {
     43      is(e.message, "window is not a function", "We should get a correct error message when calling instanceof");
     44    }
     45 
     46    // Test case 5 - Verify we get the same error when the LHS is an object created in a different compartment
     47    try {
     48      Cu.evalInSandbox("(crossCompartmentObject instanceof window);", sandbox);
     49      ok(false, "Should throw a type error");
     50    } catch (e) {
     51      is(e.message, "window is not a function", "We should get a correct error message when calling instanceof");
     52    }
     53 
     54    // Test case 6 - Test that we are correctly wrapping the window into sandbox's compartment
     55    window[Symbol.hasInstance] = function(instance) {
     56      instance.window = this;
     57      return true;
     58    }
     59    var x = Cu.evalInSandbox("(crossCompartmentObject instanceof window);", sandbox);
     60    ok(x, "Symbol.hasInstance for window should return true");
     61    is(sandbox.crossCompartmentObject.window, window, "We shouldn't leak the window");
     62    delete window[Symbol.hasInstance];
     63    Cu.nukeSandbox(sandbox);
     64 
     65    // Test case 7 - Test instanceof with RHS being a same-origin Xray to a Window
     66    sandbox = SpecialPowers.unwrap(Cu.Sandbox(this, { sameZoneAs: this, freshCompartment: true}));
     67    sandbox.window = window;
     68    sandbox.crossCompartmentObject = {};
     69 
     70    window[Symbol.hasInstance] = function(instance) {
     71      instance.window = this;
     72      return true;
     73    }
     74    try {
     75      Cu.evalInSandbox("(crossCompartmentObject instanceof window);", sandbox);
     76      ok(false, "Should throw a type error");
     77    } catch (e) {
     78      is(e.message, "window is not a function",
     79        "We should get a correct error thrown when the RHS of instanceof is an Xray to a Window.");
     80    }
     81    delete window[Symbol.hasInstance];
     82    Cu.nukeSandbox(sandbox);
     83 
     84    // Test case 8 - Test instanceof with RHS being a same-origin Xray waiver
     85    sandbox = SpecialPowers.unwrap(Cu.Sandbox(this, { sameZoneAs: this, freshCompartment: true}));
     86    sandbox.window = window;
     87    sandbox.crossCompartmentObject = {};
     88    sandbox.waiveXrays = SpecialPowers.wrapFor(Cu.waiveXrays, sandbox);
     89 
     90    window[Symbol.hasInstance] = function(instance) {
     91      instance.window = this;
     92      return true;
     93    }
     94    Cu.evalInSandbox("(crossCompartmentObject instanceof waiveXrays(window));", sandbox);
     95    ok(x, "Symbol.hasInstance for window should return true");
     96    is(sandbox.crossCompartmentObject.window, window,
     97      "The window pointed to by the crossCompartmentObject should be the same as the window in our compartment");
     98    delete window[Symbol.hasInstance];
     99    Cu.nukeSandbox(sandbox);
    100  });
    101  </script>
    102 </head>
    103 <body>
    104 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1530413">Mozilla Bug 1530413</a>
    105 <p id="display"></p>
    106 <div id="content" style="display: none">
    107 </div>
    108 <pre id="test">
    109 </pre>
    110 </body>
    111 </html>