Bug 1455406: Convert test_same_site_cookies_webextension to an xpcshell test. r=mixedpuppy
authorKris Maglione <maglione.k@gmail.com>
Thu, 19 Apr 2018 12:49:19 -0700
changeset 468938 f21d1cfe5d07734767e217eeda91bab9d83f12d1
parent 468937 2087cea3bdf81170856fc773c2d9de684510e001
child 468939 5336e6fd895be772cf603dbd8a6580be723829aa
push id9165
push userasasaki@mozilla.com
push dateThu, 26 Apr 2018 21:04:54 +0000
treeherdermozilla-beta@064c3804de2e [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmixedpuppy
bugs1455406
milestone61.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 1455406: Convert test_same_site_cookies_webextension to an xpcshell test. r=mixedpuppy MozReview-Commit-ID: BFAmZUn349n
toolkit/components/extensions/test/mochitest/file_same_site_cookies_webextension.sjs
toolkit/components/extensions/test/mochitest/mochitest-common.ini
toolkit/components/extensions/test/mochitest/test_same_site_cookies_webextension.html
toolkit/components/extensions/test/xpcshell/test_ext_same_site_cookies.js
toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
deleted file mode 100644
--- a/toolkit/components/extensions/test/mochitest/file_same_site_cookies_webextension.sjs
+++ /dev/null
@@ -1,40 +0,0 @@
-// Custom *.sjs file specifically for the needs of Bug 1454914
-
-const WIN = `<html><body>dummy page setting a same-site cookie</body></html>`;
-const FRAME = `<html><body>dummy frame getting a same-site cookie</body></html>`;
-
-// small red image
-const IMG_BYTES = atob(
-  "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12" +
-  "P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==");
-
-function handleRequest(request, response)
-{
-  // avoid confusing cache behaviors
-  response.setHeader("Cache-Control", "no-cache", false);
-
-  if (request.queryString === "loadWin") {
-    response.write(WIN);
-    return;
-  }
-
-  // using startsWith and discard the math random
-  if (request.queryString.startsWith("loadImage")) {
-    response.setHeader("Set-Cookie", "myKey=mySameSiteExtensionCookie; samesite=strict", true);
-    response.setHeader("Content-Type", "image/png");
-    response.write(IMG_BYTES);
-    return;
-  }
-
-  if (request.queryString === "loadXHR") {
-    let cookie = "noCookie";
-    if (request.hasHeader("Cookie")) {
-      cookie = request.getHeader("Cookie");
-    }
-    response.write(cookie);
-    return;
-  }
-
-  // we should never get here, but just in case return something unexpected
-  response.write("D'oh");
-}
--- a/toolkit/components/extensions/test/mochitest/mochitest-common.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest-common.ini
@@ -46,17 +46,16 @@ support-files =
   redirect_auto.sjs
   redirection.sjs
   return_headers.sjs
   slow_response.sjs
   webrequest_worker.js
   !/dom/tests/mochitest/geolocation/network_geolocation.sjs
   !/toolkit/components/passwordmgr/test/authenticate.sjs
   file_redirect_data_uri.html
-  file_same_site_cookies_webextension.sjs
 prefs =
   security.mixed_content.upgrade_display_content=false
 
 [test_ext_background_canvas.html]
 [test_ext_background_page.html]
 skip-if = (toolkit == 'android') # android doesn't have devtools
 [test_ext_clipboard.html]
 [test_ext_clipboard_image.html]
@@ -128,9 +127,8 @@ skip-if = os == 'android' && debug # bug
 [test_ext_webrequest_frameId.html]
 [test_ext_webrequest_hsts.html]
 skip-if = os == 'android' || os == 'linux' # linux, bug 1398120
 [test_ext_webrequest_upgrade.html]
 [test_ext_webrequest_upload.html]
 skip-if = os == 'android' # Currently fails in emulator tests
 [test_ext_webrequest_redirect_data_uri.html]
 [test_ext_window_postMessage.html]
-[test_same_site_cookies_webextension.html]
rename from toolkit/components/extensions/test/mochitest/test_same_site_cookies_webextension.html
rename to toolkit/components/extensions/test/xpcshell/test_ext_same_site_cookies.js
--- a/toolkit/components/extensions/test/mochitest/test_same_site_cookies_webextension.html
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_same_site_cookies.js
@@ -1,48 +1,75 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Bug 1454914: Exempt web-extensions from same-site cookie policy</title>
-  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
-  <script type="text/javascript" src="/tests/SimpleTest/AddTask.js"></script>
-  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
-  <script type="text/javascript" src="head.js"></script>
-  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
-</head>
-<body>
+"use strict";
+
+const server = createHttpServer({hosts: ["example.com"]});
+
+const WIN = `<html><body>dummy page setting a same-site cookie</body></html>`;
+
+// Small red image.
+const IMG_BYTES = atob(
+  "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12" +
+  "P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==");
+
+server.registerPathHandler("/same_site_cookies", (request, response) => {
+  // avoid confusing cache behaviors
+  response.setHeader("Cache-Control", "no-cache", false);
+
+  if (request.queryString === "loadWin") {
+    response.write(WIN);
+    return;
+  }
 
-<script type="text/javascript">
-"use strict";
+  // using startsWith and discard the math random
+  if (request.queryString.startsWith("loadImage")) {
+    response.setHeader("Set-Cookie", "myKey=mySameSiteExtensionCookie; samesite=strict", true);
+    response.setHeader("Content-Type", "image/png");
+    response.write(IMG_BYTES);
+    return;
+  }
+
+  if (request.queryString === "loadXHR") {
+    let cookie = "noCookie";
+    if (request.hasHeader("Cookie")) {
+      cookie = request.getHeader("Cookie");
+    }
+    response.setHeader("Content-Type", "text/plain");
+    response.write(cookie);
+    return;
+  }
+
+  // We should never get here, but just in case return something unexpected.
+  response.write("D'oh");
+});
 
 /* Description of the test:
  * (1) We load an image from mochi.test which sets a same site cookie
  * (2) We have the web extension perform an XHR request to mochi.test
  * (3) We verify the web-extension can access the same-site cookie
  */
 
 add_task(async function test_webRequest_same_site_cookie_access() {
   let extension = ExtensionTestUtils.loadExtension({
     manifest: {
       permissions: [
         "http://example.com/*",
       ],
       content_scripts: [{
         matches: ["http://example.com/*"],
-        run_at: "document_start",
+        run_at: "document_end",
         js: ["content_script.js"],
       }],
     },
 
     background() {
       browser.test.onMessage.addListener(msg => {
         if (msg === "verify-same-site-cookie-moz-extension") {
           let xhr = new XMLHttpRequest();
           try {
-            xhr.open("GET", "http://example.com/tests/toolkit/components/extensions/test/mochitest/file_same_site_cookies_webextension.sjs?loadXHR", true);
+            xhr.open("GET", "http://example.com/same_site_cookies?loadXHR", true);
             xhr.onload = function() {
               browser.test.assertEq("myKey=mySameSiteExtensionCookie", xhr.responseText,
                                     "cookie should be accessible from moz-extension context");
               browser.test.sendMessage("same-site-cookie-test-done");
             };
             xhr.onerror = function() {
               browser.test.fail("xhr onerror");
               browser.test.sendMessage("same-site-cookie-test-done");
@@ -55,34 +82,33 @@ add_task(async function test_webRequest_
       });
     },
 
     files: {
       "content_script.js": function() {
         let myImage = document.createElement("img");
         // Set the src via wrappedJSObject so the load is triggered with the
         // content page's principal rather than ours.
-        myImage.wrappedJSObject.setAttribute("src", "http://example.com/tests/toolkit/components/extensions/test/mochitest/file_same_site_cookies_webextension.sjs?loadImage" + Math.random());
+        myImage.wrappedJSObject.setAttribute("src", "http://example.com/same_site_cookies?loadImage" + Math.random());
         myImage.onload = function() {
           browser.test.log("image onload");
           browser.test.sendMessage("image-loaded-and-same-site-cookie-set");
         };
         myImage.onerror = function() {
           browser.test.log("image onerror");
         };
         document.body.appendChild(myImage);
       },
     },
   });
 
   await extension.startup();
-  let win = window.open("http://example.com/tests/toolkit/components/extensions/test/mochitest/file_same_site_cookies_webextension.sjs?loadWin");
+
+  let contentPage = await ExtensionTestUtils.loadContentPage("http://example.com/same_site_cookies?loadWin");
+
   await extension.awaitMessage("image-loaded-and-same-site-cookie-set");
+
   extension.sendMessage("verify-same-site-cookie-moz-extension");
   await extension.awaitMessage("same-site-cookie-test-done");
-  win.close();
+
+  await contentPage.close();
   await extension.unload();
 });
-
-</script>
-
-</body>
-</html>
--- a/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
@@ -77,19 +77,20 @@ skip-if = os == "android" # proxy settin
 [test_ext_runtime_getPlatformInfo.js]
 [test_ext_runtime_id.js]
 [test_ext_runtime_onInstalled_and_onStartup.js]
 skip-if = true # bug 1315829
 [test_ext_runtime_sendMessage.js]
 [test_ext_runtime_sendMessage_errors.js]
 [test_ext_runtime_sendMessage_no_receiver.js]
 [test_ext_runtime_sendMessage_self.js]
-[test_ext_shutdown_cleanup.js]
+[test_ext_same_site_cookies.js]
 [test_ext_sandbox_var.js]
 [test_ext_schema.js]
+[test_ext_shutdown_cleanup.js]
 [test_ext_simple.js]
 [test_ext_startupData.js]
 [test_ext_startup_cache.js]
 skip-if = os == "android"
 [test_ext_startup_perf.js]
 [test_ext_storage.js]
 [test_ext_storage_content.js]
 [test_ext_storage_managed.js]