tor-browser

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

test_scopes.html (4716B)


      1 <!--
      2  Any copyright is dedicated to the Public Domain.
      3  http://creativecommons.org/publicdomain/zero/1.0/
      4 -->
      5 <!DOCTYPE HTML>
      6 <html>
      7 <head>
      8  <title>Bug 984048 - Test scope glob matching.</title>
      9  <script src="/tests/SimpleTest/SimpleTest.js"></script>
     10  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
     11 </head>
     12 <body>
     13 <p id="display"></p>
     14 <div id="content" style="display: none"></div>
     15 <pre id="test"></pre>
     16 <script class="testbody" type="text/javascript">
     17 
     18  var scriptsAndScopes = [
     19    [ "worker.js", "./sub/dir/"],
     20    [ "worker.js", "./sub/dir" ],
     21    [ "worker.js", "./sub/dir.html" ],
     22    [ "worker.js", "./sub/dir/a" ],
     23    [ "worker.js", "./sub" ],
     24    [ "worker.js", "./star*" ], // '*' has no special meaning
     25  ];
     26 
     27  function registerWorkers() {
     28    var registerArray = [];
     29    scriptsAndScopes.forEach(function(item) {
     30      registerArray.push(navigator.serviceWorker.register(item[0], { scope: item[1] }));
     31    });
     32 
     33    // Check register()'s step 4 which uses script's url with "./" as the scope if no scope is passed.
     34    // The other tests already check step 5.
     35    registerArray.push(navigator.serviceWorker.register("scope/scope_worker.js"));
     36 
     37    // Check that SW cannot be registered for a scope "above" the script's location.
     38    registerArray.push(new Promise(function(resolve, reject) {
     39      navigator.serviceWorker.register("scope/scope_worker.js", { scope: "./" })
     40        .then(function() {
     41          ok(false, "registration scope has to be inside service worker script scope.");
     42          reject();
     43        }, function() {
     44          ok(true, "registration scope has to be inside service worker script scope.");
     45          resolve();
     46        });
     47    }));
     48    return Promise.all(registerArray);
     49  }
     50 
     51  function unregisterWorkers() {
     52    var unregisterArray = [];
     53    scriptsAndScopes.forEach(function(item) {
     54      var p = navigator.serviceWorker.getRegistration(item[1]);
     55      unregisterArray.push(p.then(function(reg) {
     56        return reg.unregister();
     57      }));
     58    });
     59 
     60    unregisterArray.push(navigator.serviceWorker.getRegistration("scope/").then(function (reg) {
     61      return reg.unregister();
     62    }));
     63 
     64    return Promise.all(unregisterArray);
     65  }
     66 
     67  async function testScopes() {
     68    function chromeScriptSource() {
     69      /* eslint-env mozilla/chrome-script */
     70 
     71      let swm = Cc["@mozilla.org/serviceworkers/manager;1"]
     72                  .getService(Ci.nsIServiceWorkerManager);
     73      let secMan = Cc["@mozilla.org/scriptsecuritymanager;1"]
     74                     .getService(Ci.nsIScriptSecurityManager);
     75      addMessageListener("getScope", (msg) => {
     76        let principal = secMan.createContentPrincipalFromOrigin(msg.principal);
     77        try {
     78          return { scope: swm.getScopeForUrl(principal, msg.path) };
     79        } catch (e) {
     80          return { exception: e.message };
     81        }
     82      });
     83    }
     84 
     85    let chromeScript = SpecialPowers.loadChromeScript(chromeScriptSource);
     86    let docOrigin = SpecialPowers.wrap(document).nodePrincipal.origin;
     87 
     88    getScope = async (path) => {
     89      let rv = await chromeScript.sendQuery("getScope", { principal: docOrigin, path });
     90      if (rv.exception)
     91        throw rv.exception;
     92      return rv.scope;
     93    };
     94 
     95    var base = new URL(".", document.baseURI);
     96 
     97    function p(s) {
     98      return base + s;
     99    }
    100 
    101    async function fail(fn) {
    102      try {
    103        await getScope(p("index.html"));
    104        ok(false, "No registration");
    105      } catch(e) {
    106        ok(true, "No registration");
    107      }
    108    }
    109 
    110    is(await getScope(p("sub.html")), p("sub"), "Scope should match");
    111    is(await getScope(p("sub/dir.html")), p("sub/dir.html"), "Scope should match");
    112    is(await getScope(p("sub/dir")), p("sub/dir"), "Scope should match");
    113    is(await getScope(p("sub/dir/foo")), p("sub/dir/"), "Scope should match");
    114    is(await getScope(p("sub/dir/afoo")), p("sub/dir/a"), "Scope should match");
    115    is(await getScope(p("star*wars")), p("star*"), "Scope should match");
    116    is(await getScope(p("scope/some_file.html")), p("scope/"), "Scope should match");
    117    await fail("index.html");
    118    await fail("sua.html");
    119    await fail("star/a.html");
    120  }
    121 
    122  function runTest() {
    123    registerWorkers()
    124      .then(testScopes)
    125      .then(unregisterWorkers)
    126      .then(function() {
    127        SimpleTest.finish();
    128      }).catch(function(e) {
    129        ok(false, "Some test failed with error " + e);
    130        SimpleTest.finish();
    131      });
    132  }
    133 
    134  SimpleTest.waitForExplicitFinish();
    135  SpecialPowers.pushPrefEnv({"set": [
    136    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
    137    ["dom.serviceWorkers.enabled", true],
    138    ["dom.serviceWorkers.testing.enabled", true]
    139  ]}, runTest);
    140 </script>
    141 </pre>
    142 </body>
    143 </html>