tor-browser

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

test_beaconContentPolicy.html (3540B)


      1 <!DOCTYPE HTML>
      2 <html>
      3 <!--
      4 https://bugzilla.mozilla.org/show_bug.cgi?id=936340
      5 -->
      6 <head>
      7  <title>Test that sendBeacon obeys content policy directives</title>
      8  <script src="/tests/SimpleTest/SimpleTest.js"></script>
      9  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     10 </head>
     11 <body>
     12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=936340">Mozilla Bug 936340</a>
     13 <p id="display"></p>
     14 <div id="content" style="display: none">
     15  
     16 </div>
     17 <pre id="test">
     18 <script class="testbody" type="text/javascript">
     19 
     20 var beaconUrl = "http://mochi.test:8888/tests/dom/tests/mochitest/beacon/beacon-handler.sjs";
     21 
     22 const Cc = SpecialPowers.Cc;
     23 const Ci = SpecialPowers.Ci;
     24 
     25 // not enabled by default yet.
     26 SimpleTest.waitForExplicitFinish();
     27 
     28 var policy;
     29 
     30 SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, beginTest);
     31 
     32 function setupPolicy() {
     33  var policyID = SpecialPowers.wrap(SpecialPowers.Components).ID("{b80e19d0-878f-d41b-2654-194714a4115c}");
     34  var policyName = "@mozilla.org/testpolicy;1";
     35  var policy = {
     36    // nsISupports implementation
     37    QueryInterface(iid) {
     38      iid = SpecialPowers.wrap(iid);
     39      if (iid.equals(Ci.nsISupports) ||
     40        iid.equals(Ci.nsIFactory) ||
     41        iid.equals(Ci.nsIContentPolicy))
     42        return this;
     43      throw SpecialPowers.Cr.NS_ERROR_NO_INTERFACE;
     44    },
     45 
     46    // nsIFactory implementation
     47    createInstance(iid) {
     48      return this.QueryInterface(iid);
     49    },
     50 
     51    // nsIContentPolicy implementation
     52    shouldLoad(contentLocation, loadInfo) {
     53      // Remember last content type seen for the test url
     54      let contentType = loadInfo.externalContentPolicyType;
     55 
     56      if (SpecialPowers.wrap(contentLocation).spec == beaconUrl) {
     57        is(contentType,  Ci.nsIContentPolicy.TYPE_BEACON, "Beacon content type should match expected.  is: " + contentType + " should be: " + Ci.nsIContentPolicy.TYPE_BEACON);
     58        teardownPolicy();
     59        SimpleTest.finish();
     60      }
     61 
     62      return Ci.nsIContentPolicy.ACCEPT;
     63    },
     64 
     65    shouldProcess(contentLocation, loadInfo) {
     66      return Ci.nsIContentPolicy.ACCEPT;
     67    }
     68  }
     69  policy = SpecialPowers.wrapCallbackObject(policy);
     70 
     71  // Register content policy
     72  var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar);
     73  componentManager.registerFactory(policyID, "Test content policy", policyName, policy);
     74 
     75  var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
     76  categoryManager.addCategoryEntry("content-policy", policyName, policyName, false, true);
     77 
     78  return { 'policy': policy, 'policyID': policyID, 'policyName': policyName };
     79 }
     80 
     81 function teardownPolicy() {
     82  setTimeout(function() {
     83    // policy will not be removed from the category correctly
     84    var componentManager = SpecialPowers.wrap(SpecialPowers.Components).manager.QueryInterface(Ci.nsIComponentRegistrar);
     85    componentManager.unregisterFactory(policy.policyID, policy.policy);
     86    var categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
     87    categoryManager.deleteCategoryEntry("content-policy", policy.policyName, false);
     88  }, 0);
     89 }
     90 
     91 function beginTest() {
     92  policy = setupPolicy();
     93  // Make sure to hit the event loop here in order to ensure that nsContentPolicy
     94  // has been notified of the newly registered policy.
     95  SimpleTest.executeSoon(function() {
     96    navigator.sendBeacon(beaconUrl, "bacon would have been a better name than beacon");
     97  });
     98 }
     99 
    100 </script>
    101 </pre>
    102 </body>
    103 </html>