Bug 1082837 - test cases for image redirects loaded from the imagelib cache. r=smaug, ckerschb
authorTanvi Vyas <tanvi@mozilla.com>
Tue, 24 Mar 2015 09:18:51 -0700
changeset 264230 40a24381b8fb0b858d7525d8eb8123a107b273e7
parent 264229 8da6a781c01a08232db9f2206397da99f866c03c
child 264231 cb2fce9d19c752b96d55d1c7c28d71748c2d50ca
push id4718
push userraliiev@mozilla.com
push dateMon, 11 May 2015 18:39:53 +0000
treeherdermozilla-beta@c20c4ef55f08 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssmaug, ckerschb
bugs1082837
milestone39.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 1082837 - test cases for image redirects loaded from the imagelib cache. r=smaug, ckerschb
browser/base/content/test/general/browser.ini
browser/base/content/test/general/browser_mcb_redirect.js
browser/base/content/test/general/test_mcb_double_redirect_image.html
browser/base/content/test/general/test_mcb_redirect.html
browser/base/content/test/general/test_mcb_redirect.sjs
browser/base/content/test/general/test_mcb_redirect_image.html
--- a/browser/base/content/test/general/browser.ini
+++ b/browser/base/content/test/general/browser.ini
@@ -94,16 +94,18 @@ support-files =
   zoom_test.html
   test_no_mcb_on_http_site_img.html
   test_no_mcb_on_http_site_img.css
   test_no_mcb_on_http_site_font.html
   test_no_mcb_on_http_site_font.css
   test_no_mcb_on_http_site_font2.html
   test_no_mcb_on_http_site_font2.css
   test_mcb_redirect.html
+  test_mcb_redirect_image.html
+  test_mcb_double_redirect_image.html
   test_mcb_redirect.js
   test_mcb_redirect.sjs
   xul_tooltiptext.xhtml
   file_bug1045809_1.html
   file_bug1045809_2.html
 
 [browser_URLBarSetURI.js]
 skip-if = (os == "linux" || os == "mac") && debug # bug 970052, bug 970053
--- a/browser/base/content/test/general/browser_mcb_redirect.js
+++ b/browser/base/content/test/general/browser_mcb_redirect.js
@@ -1,33 +1,81 @@
 /*
  * Description of the Tests for
  *  - Bug 418354 - Call Mixed content blocking on redirects
  *
+ * Single redirect script tests
  * 1. Load a script over https inside an https page
  *    - the server responds with a 302 redirect to a >> HTTP << script
  *    - the doorhanger should appear!
  *
  * 2. Load a script over https inside an http page
  *    - the server responds with a 302 redirect to a >> HTTP << script
  *    - the doorhanger should not appear!
+ *
+ * Single redirect image tests
+ * 3. Load an image over https inside an https page
+ *    - the server responds with a 302 redirect to a >> HTTP << image
+ *    - the image should not load
+ *
+ * 4. Load an image over https inside an http page
+ *    - the server responds with a 302 redirect to a >> HTTP << image
+ *    - the image should load and get cached
+ *
+ * Single redirect cached image tests
+ * 5. Using offline mode to ensure we hit the cache, load a cached image over
+ *    https inside an http page
+ *    - the server would have responded with a 302 redirect to a >> HTTP <<
+ *      image, but instead we try to use the cached image.
+ *    - the image should load
+ *
+ * 6. Using offline mode to ensure we hit the cache, load a cached image over
+ *    https inside an https page
+ *    - the server would have responded with a 302 redirect to a >> HTTP <<
+ *      image, but instead we try to use the cached image.
+ *    - the image should not load
+ *
+ * Double redirect image test
+ * 7. Load an image over https inside an http page
+ *    - the server responds with a 302 redirect to a >> HTTP << server
+ *    - the HTTP server responds with a 302 redirect to a >> HTTPS << image
+ *    - the image should load and get cached
+ *
+ * Double redirect cached image tests
+ * 8. Using offline mode to ensure we hit the cache, load a cached image over
+ *    https inside an http page
+ *    - the image would have gone through two redirects: HTTPS->HTTP->HTTPS,
+ *      but instead we try to use the cached image.
+ *    - the image should load
+ *
+ * 9. Using offline mode to ensure we hit the cache, load a cached image over
+ *    https inside an https page
+ *    - the image would have gone through two redirects: HTTPS->HTTP->HTTPS,
+ *      but instead we try to use the cached image.
+ *    - the image should not load
  */
 
 const PREF_ACTIVE = "security.mixed_content.block_active_content";
+const PREF_DISPLAY = "security.mixed_content.block_display_content";
 const gHttpsTestRoot = "https://example.com/browser/browser/base/content/test/general/";
 const gHttpTestRoot = "http://example.com/browser/browser/base/content/test/general/";
 
 let origBlockActive;
+let origBlockDisplay;
 var gTestBrowser = null;
 
 //------------------------ Helper Functions ---------------------
 
 registerCleanupFunction(function() {
   // Set preferences back to their original values
   Services.prefs.setBoolPref(PREF_ACTIVE, origBlockActive);
+  Services.prefs.setBoolPref(PREF_DISPLAY, origBlockDisplay);
+
+  // Make sure we are online again
+  Services.io.offline = false;
 });
 
 function cleanUpAfterTests() {
   gBrowser.removeCurrentTab();
   window.focus();
   finish();
 }
 
@@ -82,29 +130,185 @@ function checkPopUpNotificationsForTest2
   gTestBrowser.removeEventListener("load", checkPopUpNotificationsForTest2, true);
 
   var notification = PopupNotifications.getNotification("bad-content", gTestBrowser.selectedBrowser);
   ok(!notification, "OK: Mixed Content Doorhanger did not appear in 2!");
 
   var expected = "script executed";
   waitForCondition(
     function() content.document.getElementById('mctestdiv').innerHTML == expected,
-    cleanUpAfterTests, "Error: Waited too long for status in Test 2!",
+    test3, "Error: Waited too long for status in Test 2!",
     "OK: Expected result in innerHTML for Test2!");
 }
 
+//------------------------ Test 3 ------------------------------
+// HTTPS page loading insecure image
+function test3() {
+  gTestBrowser.addEventListener("load", checkLoadEventForTest3, true);
+  var url = gHttpsTestRoot + "test_mcb_redirect_image.html"
+  gTestBrowser.contentWindow.location = url;
+}
+
+function checkLoadEventForTest3() {
+  gTestBrowser.removeEventListener("load", checkLoadEventForTest3, true);
+
+  var expected = "image blocked"
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    test4, "Error: Waited too long for status in Test 3!",
+    "OK: Expected result in innerHTML for Test3!");
+}
+
+//------------------------ Test 4 ------------------------------
+// HTTP page loading insecure image
+function test4() {
+  gTestBrowser.addEventListener("load", checkLoadEventForTest4, true);
+  var url = gHttpTestRoot + "test_mcb_redirect_image.html"
+  gTestBrowser.contentWindow.location = url;
+}
+
+function checkLoadEventForTest4() {
+  gTestBrowser.removeEventListener("load", checkLoadEventForTest4, true);
+
+  var expected = "image loaded"
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    test5, "Error: Waited too long for status in Test 4!",
+    "OK: Expected result in innerHTML for Test4!");
+}
+
+//------------------------ Test 5 ------------------------------
+// HTTP page laoding insecure cached image
+// Assuming test 4 succeeded, the image has already been loaded once
+// and hence should be cached per the sjs cache-control header
+// Going into offline mode to ensure we are loading from the cache.
+function test5() {
+  gTestBrowser.addEventListener("load", checkLoadEventForTest5, true);
+  // Go into offline mode
+  Services.io.offline = true;
+  var url = gHttpTestRoot + "test_mcb_redirect_image.html"
+  gTestBrowser.contentWindow.location = url;
+}
+
+function checkLoadEventForTest5() {
+  gTestBrowser.removeEventListener("load", checkLoadEventForTest5, true);
+
+  var expected = "image loaded"
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    test6, "Error: Waited too long for status in Test 5!",
+    "OK: Expected result in innerHTML for Test5!");
+  // Go back online
+  Services.io.offline = false;
+}
+
+//------------------------ Test 6 ------------------------------
+// HTTPS page loading insecure cached image
+// Assuming test 4 succeeded, the image has already been loaded once
+// and hence should be cached per the sjs cache-control header
+// Going into offline mode to ensure we are loading from the cache.
+function test6() {
+  gTestBrowser.addEventListener("load", checkLoadEventForTest6, true);
+  // Go into offline mode
+  Services.io.offline = true;
+  var url = gHttpsTestRoot + "test_mcb_redirect_image.html"
+  gTestBrowser.contentWindow.location = url;
+}
+
+function checkLoadEventForTest6() {
+  gTestBrowser.removeEventListener("load", checkLoadEventForTest6, true);
+
+  var expected = "image blocked"
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    test7, "Error: Waited too long for status in Test 6!",
+    "OK: Expected result in innerHTML for Test6!");
+  // Go back online
+  Services.io.offline = false;
+}
+
+//------------------------ Test 7 ------------------------------
+// HTTP page loading insecure image that went through a double redirect
+function test7() {
+  gTestBrowser.addEventListener("load", checkLoadEventForTest7, true);
+  var url = gHttpTestRoot + "test_mcb_double_redirect_image.html"
+  gTestBrowser.contentWindow.location = url;
+}
+
+function checkLoadEventForTest7() {
+  gTestBrowser.removeEventListener("load", checkLoadEventForTest7, true);
+
+  var expected = "image loaded"
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    test8, "Error: Waited too long for status in Test 7!",
+    "OK: Expected result in innerHTML for Test7!");
+}
+
+//------------------------ Test 8 ------------------------------
+// HTTP page loading insecure cached image that went through a double redirect
+// Assuming test 7 succeeded, the image has already been loaded once
+// and hence should be cached per the sjs cache-control header
+// Going into offline mode to ensure we are loading from the cache.
+function test8() {
+  gTestBrowser.addEventListener("load", checkLoadEventForTest8, true);
+  // Go into offline mode
+  Services.io.offline = true;
+  var url = gHttpTestRoot + "test_mcb_double_redirect_image.html"
+  gTestBrowser.contentWindow.location = url;
+}
+
+function checkLoadEventForTest8() {
+  gTestBrowser.removeEventListener("load", checkLoadEventForTest8, true);
+
+  var expected = "image loaded"
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    test9, "Error: Waited too long for status in Test 8!",
+    "OK: Expected result in innerHTML for Test8!");
+  // Go back online
+  Services.io.offline = false;
+}
+
+//------------------------ Test 9 ------------------------------
+// HTTPS page loading insecure cached image that went through a double redirect
+// Assuming test 7 succeeded, the image has already been loaded once
+// and hence should be cached per the sjs cache-control header
+// Going into offline mode to ensure we are loading from the cache.
+function test9() {
+  gTestBrowser.addEventListener("load", checkLoadEventForTest9, true);
+  // Go into offline mode
+  Services.io.offline = true;
+  var url = gHttpsTestRoot + "test_mcb_double_redirect_image.html"
+  gTestBrowser.contentWindow.location = url;
+}
+
+function checkLoadEventForTest9() {
+  gTestBrowser.removeEventListener("load", checkLoadEventForTest9, true);
+
+  var expected = "image blocked"
+  waitForCondition(
+    function() content.document.getElementById('mctestdiv').innerHTML == expected,
+    cleanUpAfterTests, "Error: Waited too long for status in Test 9!",
+    "OK: Expected result in innerHTML for Test9!");
+  // Go back online
+  Services.io.offline = false;
+}
+
 //------------------------ SETUP ------------------------------
 
 function test() {
   // Performing async calls, e.g. 'onload', we have to wait till all of them finished
   waitForExplicitFinish();
 
   // Store original preferences so we can restore settings after testing
   origBlockActive = Services.prefs.getBoolPref(PREF_ACTIVE);
+  origBlockDisplay = Services.prefs.getBoolPref(PREF_DISPLAY);
   Services.prefs.setBoolPref(PREF_ACTIVE, true);
+  Services.prefs.setBoolPref(PREF_DISPLAY, true);
 
   var newTab = gBrowser.addTab();
   gBrowser.selectedTab = newTab;
   gTestBrowser = gBrowser.selectedBrowser;
   newTab.linkedBrowser.stop();
 
   executeSoon(test1);
 }
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/test_mcb_double_redirect_image.html
@@ -0,0 +1,23 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+  Test 7-9 for Bug 1082837 - See file browser_mcb_redirect.js for description.
+  https://bugzilla.mozilla.org/show_bug.cgi?id=1082837
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Bug 1082837</title>
+  <script>
+    function image_loaded() {
+      document.getElementById("mctestdiv").innerHTML = "image loaded";
+    }
+    function image_blocked() {
+      document.getElementById("mctestdiv").innerHTML = "image blocked";
+    }
+  </script>
+</head>
+<body>
+  <div id="mctestdiv"></div>
+  <img src="https://example.com/browser/browser/base/content/test/general/test_mcb_redirect.sjs?image_redirect_http_sjs" onload="image_loaded()" onerror="image_blocked()" ></image>
+</body>
+</html>
--- a/browser/base/content/test/general/test_mcb_redirect.html
+++ b/browser/base/content/test/general/test_mcb_redirect.html
@@ -5,11 +5,11 @@
   https://bugzilla.mozilla.org/show_bug.cgi?id=418354
 -->
 <head>
   <meta charset="utf-8">
   <title>Bug 418354</title>
 </head>
 <body>
   <div id="mctestdiv">script blocked</div>
-  <script src="https://example.com/browser/browser/base/content/test/general/test_mcb_redirect.sjs" ></script>
+  <script src="https://example.com/browser/browser/base/content/test/general/test_mcb_redirect.sjs?script" ></script>
 </body>
 </html>
--- a/browser/base/content/test/general/test_mcb_redirect.sjs
+++ b/browser/base/content/test/general/test_mcb_redirect.sjs
@@ -1,11 +1,22 @@
 function handleRequest(request, response) {
-  var page = "<!DOCTYPE html><html><body>bug 418354</body></html>";
+  var page = "<!DOCTYPE html><html><body>bug 418354 and bug 1082837</body></html>";
 
-  var redirect = "http://example.com/browser/browser/base/content/test/general/test_mcb_redirect.js";
+  if (request.queryString === "script") { 
+    var redirect = "http://example.com/browser/browser/base/content/test/general/test_mcb_redirect.js";
+    response.setHeader("Cache-Control", "no-cache", false);
+  } else if (request.queryString === "image_http") {
+    var redirect = "http://example.com/tests/image/test/mochitest/blue.png";
+    response.setHeader("Cache-Control", "max-age=3600", false);
+  } else if (request.queryString === "image_redirect_http_sjs") {
+    var redirect = "http://example.com/browser/browser/base/content/test/general/test_mcb_redirect.sjs?image_redirect_https";
+    response.setHeader("Cache-Control", "max-age=3600", false);
+  } else if (request.queryString === "image_redirect_https") {
+    var redirect = "https://example.com/tests/image/test/mochitest/blue.png";
+    response.setHeader("Cache-Control", "max-age=3600", false);
+  }
 
-  response.setHeader("Cache-Control", "no-cache", false);
   response.setHeader("Content-Type", "text/html", false);
   response.setStatusLine(request.httpVersion, "302", "Found");
   response.setHeader("Location", redirect, false);
   response.write(page);
 }
new file mode 100644
--- /dev/null
+++ b/browser/base/content/test/general/test_mcb_redirect_image.html
@@ -0,0 +1,23 @@
+<!DOCTYPE HTML>
+<html>
+<!--
+  Test 3-6 for Bug 1082837 - See file browser_mcb_redirect.js for description.
+  https://bugzilla.mozilla.org/show_bug.cgi?id=1082837
+-->
+<head>
+  <meta charset="utf-8">
+  <title>Bug 1082837</title>
+  <script>
+    function image_loaded() {
+      document.getElementById("mctestdiv").innerHTML = "image loaded";
+    }
+    function image_blocked() {
+      document.getElementById("mctestdiv").innerHTML = "image blocked";
+    }
+  </script>
+</head>
+<body>
+  <div id="mctestdiv"></div>
+  <img src="https://example.com/browser/browser/base/content/test/general/test_mcb_redirect.sjs?image_http" onload="image_loaded()" onerror="image_blocked()" ></image>
+</body>
+</html>