Bug 1130101 - Part 3: Add unit tests for the handling of the Service-Worker-Allowed header; r=nsm
authorEhsan Akhgari <ehsan@mozilla.com>
Wed, 06 May 2015 01:12:23 -0400
changeset 242903 db23832bc2816c961c100a234fcc1d829630e764
parent 242902 78224d2bdde4cbadc3607d9b7ed50c0ca43db873
child 242904 98f15494f0d47b1f1d9db26cfa34d55d415fb7cc
push id59541
push usereakhgari@mozilla.com
push dateFri, 08 May 2015 01:07:30 +0000
treeherdermozilla-inbound@db23832bc281 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersnsm
bugs1130101
milestone40.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1130101 - Part 3: Add unit tests for the handling of the Service-Worker-Allowed header; r=nsm
dom/workers/test/serviceworkers/mochitest.ini
dom/workers/test/serviceworkers/swa/worker_scope_different.js
dom/workers/test/serviceworkers/swa/worker_scope_different.js^headers^
dom/workers/test/serviceworkers/swa/worker_scope_different2.js
dom/workers/test/serviceworkers/swa/worker_scope_different2.js^headers^
dom/workers/test/serviceworkers/swa/worker_scope_precise.js
dom/workers/test/serviceworkers/swa/worker_scope_precise.js^headers^
dom/workers/test/serviceworkers/swa/worker_scope_too_deep.js
dom/workers/test/serviceworkers/swa/worker_scope_too_deep.js^headers^
dom/workers/test/serviceworkers/swa/worker_scope_too_narrow.js
dom/workers/test/serviceworkers/swa/worker_scope_too_narrow.js^headers^
dom/workers/test/serviceworkers/test_service_worker_allowed.html
--- a/dom/workers/test/serviceworkers/mochitest.ini
+++ b/dom/workers/test/serviceworkers/mochitest.ini
@@ -77,16 +77,26 @@ support-files =
   periodic/frame.html
   periodic/register.html
   periodic/wait_for_update.html
   periodic/unregister.html
   sanitize/frame.html
   sanitize/register.html
   sanitize/example_check_and_unregister.html
   sanitize_worker.js
+  swa/worker_scope_different.js
+  swa/worker_scope_different.js^headers^
+  swa/worker_scope_different2.js
+  swa/worker_scope_different2.js^headers^
+  swa/worker_scope_precise.js
+  swa/worker_scope_precise.js^headers^
+  swa/worker_scope_too_deep.js
+  swa/worker_scope_too_deep.js^headers^
+  swa/worker_scope_too_narrow.js
+  swa/worker_scope_too_narrow.js^headers^
 
 [test_unregister.html]
 [test_installation_simple.html]
 [test_fetch_event.html]
 [test_https_fetch.html]
 [test_https_fetch_cloned_response.html]
 [test_https_synth_fetch_from_cached_sw.html]
 [test_match_all.html]
@@ -111,8 +121,9 @@ support-files =
 [test_client_focus.html]
 [test_bug1151916.html]
 [test_workerupdatefoundevent.html]
 [test_empty_serviceworker.html]
 [test_periodic_update.html]
 [test_periodic_https_update.html]
 [test_sanitize.html]
 [test_sanitize_domain.html]
+[test_service_worker_allowed.html]
new file mode 100644
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/swa/worker_scope_different.js^headers^
@@ -0,0 +1,1 @@
+Service-Worker-Allowed: different/path
new file mode 100644
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/swa/worker_scope_different2.js^headers^
@@ -0,0 +1,1 @@
+Service-Worker-Allowed: /different/path
new file mode 100644
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/swa/worker_scope_precise.js^headers^
@@ -0,0 +1,1 @@
+Service-Worker-Allowed: /tests/dom/workers/test/serviceworkers/swa
new file mode 100644
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/swa/worker_scope_too_deep.js^headers^
@@ -0,0 +1,1 @@
+Service-Worker-Allowed: /tests/dom/workers/test/serviceworkers/swa/deep/way/too/specific
new file mode 100644
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/swa/worker_scope_too_narrow.js^headers^
@@ -0,0 +1,1 @@
+Service-Worker-Allowed: /tests/dom/workers
new file mode 100644
--- /dev/null
+++ b/dom/workers/test/serviceworkers/test_service_worker_allowed.html
@@ -0,0 +1,74 @@
+<!--
+  Any copyright is dedicated to the Public Domain.
+  http://creativecommons.org/publicdomain/zero/1.0/
+-->
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>Test the Service-Worker-Allowed header</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+</head>
+<body>
+<div id="content"></div>
+<script class="testbody" type="text/javascript">
+  var gTests = [
+    "worker_scope_different.js",
+    "worker_scope_different2.js",
+    "worker_scope_too_deep.js",
+  ];
+
+  function testPermissiveHeader() {
+    // Make sure that this registration succeeds, as the prefix check should pass.
+    return navigator.serviceWorker.register("swa/worker_scope_too_narrow.js", {scope: "swa/"})
+      .then(swr => {
+        ok(true, "Registration should finish successfully");
+        return swr.unregister();
+      }, err => {
+        ok(false, "Unexpected error when registering the service worker: " + err);
+      });
+  }
+
+  function testPreciseHeader() {
+    // Make sure that this registration succeeds, as the prefix check should pass
+    // given that we parse the use the full pathname from this URL..
+    return navigator.serviceWorker.register("swa/worker_scope_precise.js", {scope: "swa/"})
+      .then(swr => {
+        ok(true, "Registration should finish successfully");
+        return swr.unregister();
+      }, err => {
+        ok(false, "Unexpected error when registering the service worker: " + err);
+      });
+  }
+
+  function runTest() {
+    Promise.all(gTests.map(testName => {
+      return new Promise((resolve, reject) => {
+        // Make sure that registration fails.
+        navigator.serviceWorker.register("swa/" + testName, {scope: "swa/"})
+          .then(reject, resolve);
+      });
+    })).then(values => {
+      values.forEach(error => {
+        is(error.name, "SecurityError", "Registration should fail");
+      });
+      Promise.all([
+          testPermissiveHeader(),
+          testPreciseHeader(),
+      ]).then(SimpleTest.finish, SimpleTest.finish);
+    }, (x) => {
+      ok(false, "Registration should not succeed, but it did");
+      SimpleTest.finish();
+    });
+  }
+
+  SimpleTest.waitForExplicitFinish();
+  SpecialPowers.pushPrefEnv({"set": [
+    ["dom.serviceWorkers.enabled", true],
+    ["dom.serviceWorkers.testing.enabled", true],
+    ["dom.serviceWorkers.exemptFromPerDomainMax", true],
+  ]}, runTest);
+</script>
+</pre>
+</body>
+</html>