Bug 1559088 - Fix test_redirect-caching_failure.js to again do what it has to, r=kershaw, a=test-only
authorHonza Bambas <honzab.moz@firemni.cz>
Tue, 03 Sep 2019 12:24:11 +0000
changeset 554909 9399706f30aac2ac36c8a32311a1f30e418b88b0
parent 554908 3735e8cb4cad14ea19e9f005205bf93236e9bba5
child 554910 98ff4e66fdd3d6471df4626efdc56df135428af6
push id2165
push userffxbld-merge
push dateMon, 14 Oct 2019 16:30:58 +0000
treeherdermozilla-release@0eae18af659f [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskershaw, test-only
bugs1559088
milestone70.0
Bug 1559088 - Fix test_redirect-caching_failure.js to again do what it has to, r=kershaw, a=test-only Differential Revision: https://phabricator.services.mozilla.com/D43796
netwerk/test/unit/test_redirect-caching_failure.js
netwerk/test/unit/test_redirect_failure.js
netwerk/test/unit/xpcshell.ini
netwerk/test/unit_ipc/test_redirect-caching_failure_wrap.js
netwerk/test/unit_ipc/test_redirect_failure_wrap.js
--- a/netwerk/test/unit/test_redirect-caching_failure.js
+++ b/netwerk/test/unit/test_redirect-caching_failure.js
@@ -1,9 +1,24 @@
 const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
+/*
+ * The test is checking async redirect code path that is loading a cached
+ * redirect.  But creation of the target channel fails before we even try
+ * to do async open on it. We force the creation error by forbidding
+ * the port number the URI contains. It must be done only after we have
+ * attempted to do the redirect (open the target URL) otherwise it's not
+ * cached.
+ */
+
+function inChildProcess() {
+  return (
+    Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
+      .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT
+  );
+}
 
 XPCOMUtils.defineLazyGetter(this, "URL", function() {
   return "http://localhost:" + httpServer.identity.primaryPort;
 });
 
 var httpServer = null;
 // Need to randomize, because apparently no one clears our cache
 var randomPath = "/redirect/" + Math.random();
@@ -11,45 +26,55 @@ var randomPath = "/redirect/" + Math.ran
 XPCOMUtils.defineLazyGetter(this, "randomURI", function() {
   return URL + randomPath;
 });
 
 function make_channel(url, callback, ctx) {
   return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
 }
 
+var serverRequestCount = 0;
+
 function redirectHandler(metadata, response) {
+  ++serverRequestCount;
   response.setStatusLine(metadata.httpVersion, 301, "Moved");
-  response.setHeader(
-    "Location",
-    "httpx://localhost:" + httpServer.identity.primaryPort + "/content",
-    false
-  );
+  response.setHeader("Location", "http://non-existent.tld:65400", false);
   response.setHeader("Cache-control", "max-age=1000", false);
 }
 
-function makeSureNotInCache(request, buffer) {
-  Assert.equal(request.status, Cr.NS_ERROR_UNKNOWN_PROTOCOL);
+function firstTimeThrough(request) {
+  Assert.equal(request.status, Cr.NS_ERROR_UNKNOWN_HOST);
+  Assert.equal(serverRequestCount, 1);
 
-  // It's very unlikely that we'd somehow succeed when we try again from cache.
-  // Can't hurt to test though.
-  var chan = make_channel(randomURI);
-  chan.loadFlags |= Ci.nsIRequest.LOAD_ONLY_FROM_CACHE;
-  chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE));
+  const nextHop = () => {
+    var chan = make_channel(randomURI);
+    chan.loadFlags |= Ci.nsIRequest.LOAD_FROM_CACHE;
+    chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE));
+  };
+
+  if (inChildProcess()) {
+    do_send_remote_message("disable-ports");
+    do_await_remote_message("disable-ports-done").then(nextHop);
+  } else {
+    Services.prefs.setCharPref("network.security.ports.banned", "65400");
+    nextHop();
+  }
 }
 
 function finish_test(request, buffer) {
-  Assert.equal(request.status, Cr.NS_ERROR_UNKNOWN_PROTOCOL);
+  Assert.equal(request.status, Cr.NS_ERROR_PORT_ACCESS_NOT_ALLOWED);
+  Assert.equal(serverRequestCount, 1);
   Assert.equal(buffer, "");
+
   httpServer.stop(do_test_finished);
 }
 
 function run_test() {
   httpServer = new HttpServer();
   httpServer.registerPathHandler(randomPath, redirectHandler);
   httpServer.start(-1);
 
   var chan = make_channel(randomURI);
   chan.asyncOpen(
-    new ChannelListener(makeSureNotInCache, null, CL_EXPECT_FAILURE)
+    new ChannelListener(firstTimeThrough, null, CL_EXPECT_FAILURE)
   );
   do_test_pending();
 }
--- a/netwerk/test/unit/test_redirect_failure.js
+++ b/netwerk/test/unit/test_redirect_failure.js
@@ -1,10 +1,24 @@
 const { HttpServer } = ChromeUtils.import("resource://testing-common/httpd.js");
 
+/*
+ * The test is checking async redirect code path that is loading a
+ * redirect.  But creation of the target channel fails before we even try
+ * to do async open on it. We force the creation error by forbidding
+ * the port number the URI contains.
+ */
+
+function inChildProcess() {
+  return (
+    Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime)
+      .processType != Ci.nsIXULRuntime.PROCESS_TYPE_DEFAULT
+  );
+}
+
 XPCOMUtils.defineLazyGetter(this, "URL", function() {
   return "http://localhost:" + httpServer.identity.primaryPort;
 });
 
 var httpServer = null;
 // Need to randomize, because apparently no one clears our cache
 var randomPath = "/redirect/" + Math.random();
 
@@ -13,32 +27,32 @@ XPCOMUtils.defineLazyGetter(this, "rando
 });
 
 function make_channel(url, callback, ctx) {
   return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
 }
 
 function redirectHandler(metadata, response) {
   response.setStatusLine(metadata.httpVersion, 301, "Moved");
-  response.setHeader(
-    "Location",
-    "httpx://localhost:" + httpServer.identity.primaryPort + "/content",
-    false
-  );
+  response.setHeader("Location", "http://non-existent.tld:65400", false);
   response.setHeader("Cache-Control", "no-cache", false);
 }
 
 function finish_test(request, buffer) {
-  Assert.equal(request.status, Cr.NS_ERROR_UNKNOWN_PROTOCOL);
+  Assert.equal(request.status, Cr.NS_ERROR_PORT_ACCESS_NOT_ALLOWED);
 
   Assert.equal(buffer, "");
   httpServer.stop(do_test_finished);
 }
 
 function run_test() {
   httpServer = new HttpServer();
   httpServer.registerPathHandler(randomPath, redirectHandler);
   httpServer.start(-1);
 
+  if (!inChildProcess()) {
+    Services.prefs.setCharPref("network.security.ports.banned", "65400");
+  }
+
   var chan = make_channel(randomURI);
   chan.asyncOpen(new ChannelListener(finish_test, null, CL_EXPECT_FAILURE));
   do_test_pending();
 }
--- a/netwerk/test/unit/xpcshell.ini
+++ b/netwerk/test/unit/xpcshell.ini
@@ -287,23 +287,19 @@ skip-if = os == "android"
 [test_proxy-failover_passing.js]
 [test_proxy-replace_canceled.js]
 [test_proxy-replace_passing.js]
 [test_psl.js]
 [test_range_requests.js]
 [test_readline.js]
 [test_redirect-caching_canceled.js]
 [test_redirect-caching_failure.js]
-# Bug 675039: test fails consistently on Android
-fail-if = os == "android"
 [test_redirect-caching_passing.js]
 [test_redirect_canceled.js]
 [test_redirect_failure.js]
-# Bug 675039: test fails consistently on Android
-fail-if = os == "android"
 [test_redirect_from_script.js]
 [test_redirect_from_script_after-open_passing.js]
 [test_redirect_passing.js]
 [test_redirect_loop.js]
 [test_redirect_baduri.js]
 [test_redirect_different-protocol.js]
 [test_reentrancy.js]
 [test_reopen.js]
--- a/netwerk/test/unit_ipc/test_redirect-caching_failure_wrap.js
+++ b/netwerk/test/unit_ipc/test_redirect-caching_failure_wrap.js
@@ -1,7 +1,13 @@
+const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+
 //
 // Run test script in content process instead of chrome (xpcshell's default)
 //
 //
 function run_test() {
+  do_await_remote_message("disable-ports").then(_ => {
+    Services.prefs.setCharPref("network.security.ports.banned", "65400");
+    do_send_remote_message("disable-ports-done");
+  });
   run_test_in_child("../unit/test_redirect-caching_failure.js");
 }
--- a/netwerk/test/unit_ipc/test_redirect_failure_wrap.js
+++ b/netwerk/test/unit_ipc/test_redirect_failure_wrap.js
@@ -1,7 +1,10 @@
+const { Services } = ChromeUtils.import("resource://gre/modules/Services.jsm");
+
 //
 // Run test script in content process instead of chrome (xpcshell's default)
 //
 
 function run_test() {
+  Services.prefs.setCharPref("network.security.ports.banned", "65400");
   run_test_in_child("../unit/test_redirect_failure.js");
 }