Bug 1444680: Part 3: Convert test_ext_webRequest_permission to xpcshell. r?mixedpuppy draft
authorKris Maglione <maglione.k@gmail.com>
Sat, 10 Mar 2018 18:25:52 -0800
changeset 765937 27188a28141f967aa14e32acfe7b7f6a6261a624
parent 765936 2ccde4e7978cfdc26c68a738a16258e5a45433f9
child 765938 f19da1c1bffea4c5f894b6bc04c9636c96e830a2
push id102183
push usermaglione.k@gmail.com
push dateSun, 11 Mar 2018 06:57:39 +0000
reviewersmixedpuppy
bugs1444680
milestone60.0a1
Bug 1444680: Part 3: Convert test_ext_webRequest_permission to xpcshell. r?mixedpuppy MozReview-Commit-ID: 34BKVmoQACF
toolkit/components/extensions/test/mochitest/file_WebRequest_permission_original.html
toolkit/components/extensions/test/mochitest/file_WebRequest_permission_original.js
toolkit/components/extensions/test/mochitest/file_WebRequest_permission_redirected.html
toolkit/components/extensions/test/mochitest/file_WebRequest_permission_redirected.js
toolkit/components/extensions/test/mochitest/mochitest-common.ini
toolkit/components/extensions/test/mochitest/test_ext_webrequest_permission.html
toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_original.html
toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_original.js
toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_redirected.html
toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_redirected.js
toolkit/components/extensions/test/xpcshell/test_ext_webRequest_permission.js
toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
toolkit/mozapps/extensions/AddonManagerWebAPI.cpp
--- a/toolkit/components/extensions/test/mochitest/mochitest-common.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest-common.ini
@@ -1,19 +1,15 @@
 [DEFAULT]
 support-files =
   chrome_cleanup_script.js
   file_WebNavigation_page1.html
   file_WebNavigation_page2.html
   file_WebNavigation_page3.html
   file_WebRequest_page3.html
-  file_WebRequest_permission_original.html
-  file_WebRequest_permission_original.js
-  file_WebRequest_permission_redirected.html
-  file_WebRequest_permission_redirected.js
   file_csp.html
   file_csp.html^headers^
   file_ext_test_api_injection.js
   file_image_bad.png
   file_image_good.png
   file_image_great.png
   file_image_redirect.png
   file_mixed.html
@@ -159,17 +155,16 @@ skip-if = os == 'android'
 [test_ext_webrequest_background_events.html]
 [test_ext_webrequestblocking_set_cookie.html]
 [test_ext_webrequest_basic.html]
 skip-if = os == 'android' && debug # bug 1397615
 [test_ext_webrequest_errors.html]
 [test_ext_webrequest_filter.html]
 [test_ext_webrequest_frameId.html]
 [test_ext_webrequest_hsts.html]
-[test_ext_webrequest_permission.html]
 [test_ext_webrequest_responseBody.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_websocket.html]
 [test_ext_webrequest_redirect_data_uri.html]
 [test_ext_window_postMessage.html]
rename from toolkit/components/extensions/test/mochitest/file_WebRequest_permission_original.html
rename to toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_original.html
--- a/toolkit/components/extensions/test/mochitest/file_WebRequest_permission_original.html
+++ b/toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_original.html
@@ -1,17 +1,17 @@
 <!DOCTYPE HTML>
 
 <html>
 <head>
 <meta charset="utf-8">
 </head>
 <body>
 
-<script src="https://example.org/tests/toolkit/components/extensions/test/mochitest/file_WebRequest_permission_original.js"></script>
+<script src="http://example.org/data/file_WebRequest_permission_original.js"></script>
 <script>
 "use strict";
 
 window.parent.postMessage({
   page: "original",
   script: window.testScript,
 }, "*");
 </script>
rename from toolkit/components/extensions/test/mochitest/file_WebRequest_permission_original.js
rename to toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_original.js
rename from toolkit/components/extensions/test/mochitest/file_WebRequest_permission_redirected.html
rename to toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_redirected.html
--- a/toolkit/components/extensions/test/mochitest/file_WebRequest_permission_redirected.html
+++ b/toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_redirected.html
@@ -1,17 +1,17 @@
 <!DOCTYPE HTML>
 
 <html>
 <head>
 <meta charset="utf-8">
 </head>
 <body>
 
-<script src="https://example.org/tests/toolkit/components/extensions/test/mochitest/file_WebRequest_permission_original.js"></script>
+<script src="http://example.org/data/file_WebRequest_permission_original.js"></script>
 <script>
 "use strict";
 
 window.parent.postMessage({
   page: "redirected",
   script: window.testScript,
 }, "*");
 </script>
rename from toolkit/components/extensions/test/mochitest/file_WebRequest_permission_redirected.js
rename to toolkit/components/extensions/test/xpcshell/data/file_WebRequest_permission_redirected.js
rename from toolkit/components/extensions/test/mochitest/test_ext_webrequest_permission.html
rename to toolkit/components/extensions/test/xpcshell/test_ext_webRequest_permission.js
--- a/toolkit/components/extensions/test/mochitest/test_ext_webrequest_permission.html
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_webRequest_permission.js
@@ -1,22 +1,30 @@
-<!DOCTYPE HTML>
-<html>
-<head>
-  <title>Test for content script</title>
-  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
-  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.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>
-<script type="text/javascript">
 "use strict";
 
+const PREF_DISABLE_SECURITY = ("security.turn_off_all_security_so_that_" +
+                               "viruses_can_take_over_this_computer");
+
+const HOSTS = new Set([
+  "example.com",
+  "example.org",
+]);
+
+const server = createHttpServer({hosts: HOSTS});
+
+const BASE_URL = "http://example.com";
+
+server.registerDirectory("/data/", do_get_file("data"));
+
+server.registerPathHandler("/dummy", (request, response) => {
+  response.setStatusLine(request.httpVersion, 200, "OK");
+  response.setHeader("Content-Type", "text/html", false);
+  response.write("<!DOCTYPE html><html></html>");
+});
+
 add_task(async function test_permissions() {
   function background() {
     browser.webRequest.onBeforeRequest.addListener(details => {
       if (details.url.includes("_original")) {
         let redirectUrl = details.url
                                  .replace("example.org", "example.com")
                                  .replace("_original", "_redirected");
         return {redirectUrl};
@@ -30,44 +38,52 @@ add_task(async function test_permissions
       permissions: ["webRequest", "webRequestBlocking", "<all_urls>"],
     },
     background,
   };
 
   let extension = ExtensionTestUtils.loadExtension(extensionData);
   await extension.startup();
 
-  async function check() {
-    let iframe = document.createElement("iframe");
-    document.body.appendChild(iframe);
+  const frameScript = () => {
+    const messageListener = {
+      async receiveMessage({target, messageName, recipient, data, name}) {
+        /* globals content */
+        let doc = content.document;
+        let iframe = doc.createElement("iframe");
+        doc.body.appendChild(iframe);
 
-    let promise = new Promise(resolve => {
-      let listener = event => {
-        window.removeEventListener("message", listener);
-        resolve(event.data);
-      };
-      window.addEventListener("message", listener);
-    });
+        let promise = new Promise(resolve => {
+          let listener = event => {
+            content.removeEventListener("message", listener);
+            resolve(event.data);
+          };
+          content.addEventListener("message", listener);
+        });
 
-    iframe.setAttribute("src", "https://example.com/tests/toolkit/components/extensions/test/mochitest/file_WebRequest_permission_original.html");
-    let result = await promise;
-    document.body.removeChild(iframe);
-    return result;
-  }
+        iframe.setAttribute("src", "http://example.com/data/file_WebRequest_permission_original.html");
+        let result = await promise;
+        doc.body.removeChild(iframe);
+        return result;
+      },
+    };
+
+    ChromeUtils.import("resource://gre/modules/MessageChannel.jsm");
+    MessageChannel.addListener(this, "Test:Check", messageListener);
+  };
 
-  let results = await check();
-  is(results.page, "redirected", "Regular webRequest redirect works on an unprivileged page");
-  is(results.script, "redirected", "Regular webRequest redirect works from an unprivileged page");
+  let contentPage = await ExtensionTestUtils.loadContentPage(`${BASE_URL}/dummy`, {remote: true});
+  await contentPage.loadFrameScript(frameScript);
+
+  let results = await contentPage.sendMessage("Test:Check", {});
+  equal(results.page, "redirected", "Regular webRequest redirect works on an unprivileged page");
+  equal(results.script, "redirected", "Regular webRequest redirect works from an unprivileged page");
 
-  await SpecialPowers.pushPrefEnv({
-    set: [["extensions.webapi.testing", true]],
-  });
+  Services.prefs.setBoolPref(PREF_DISABLE_SECURITY, true);
+  Services.prefs.setBoolPref("extensions.webapi.testing", true);
 
-  results = await check();
-  is(results.page, "original", "webRequest redirect fails on a privileged page");
-  is(results.script, "original", "webRequest redirect fails from a privileged page");
+  results = await contentPage.sendMessage("Test:Check", {});
+  equal(results.page, "original", "webRequest redirect fails on a privileged page");
+  equal(results.script, "original", "webRequest redirect fails from a privileged page");
 
   await extension.unload();
+  await contentPage.close();
 });
-</script>
-
-</body>
-</html>
--- a/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
@@ -77,16 +77,17 @@ skip-if = os == "android"
 [test_ext_storage_sync_crypto.js]
 skip-if = os == "android"
 [test_ext_storage_telemetry.js]
 skip-if = os == "android" # checking for telemetry needs to be updated: 1384923
 [test_ext_trustworthy_origin.js]
 [test_ext_topSites.js]
 skip-if = os == "android"
 [test_ext_webRequest_filterResponseData.js]
+[test_ext_webRequest_permission.js]
 [test_ext_webRequest_suspend.js]
 [test_native_manifests.js]
 subprocess = true
 skip-if = os == "android"
 [test_ext_permissions.js]
 skip-if = os == "android" # Bug 1350559
 [test_proxy_listener.js]
 [test_proxy_scripts.js]
--- a/toolkit/mozapps/extensions/AddonManagerWebAPI.cpp
+++ b/toolkit/mozapps/extensions/AddonManagerWebAPI.cpp
@@ -6,16 +6,17 @@
 
 #include "AddonManagerWebAPI.h"
 
 #include "mozilla/dom/Navigator.h"
 #include "mozilla/dom/NavigatorBinding.h"
 
 #include "mozilla/Preferences.h"
 #include "nsGlobalWindow.h"
+#include "xpcpublic.h"
 
 #include "nsIDocShell.h"
 #include "nsIScriptObjectPrincipal.h"
 
 namespace mozilla {
 using namespace mozilla::dom;
 
 static bool
@@ -71,17 +72,19 @@ AddonManagerWebAPI::IsValidSite(nsIURI* 
 {
   if (!uri) {
     return false;
   }
 
   bool isSecure;
   nsresult rv = uri->SchemeIs("https", &isSecure);
   if (NS_FAILED(rv) || !isSecure) {
-    return false;
+    if (!(xpc::IsInAutomation() && Preferences::GetBool("extensions.webapi.testing", false))) {
+      return false;
+    }
   }
 
   nsAutoCString host;
   rv = uri->GetHost(host);
   if (NS_FAILED(rv)) {
     return false;
   }