Bug 806731 - Port test_sts_privatebrowsing.html to the new per-window PB APIs; r=ehsan
authorAndres Hernandez <andres@appcoast.com>
Fri, 14 Dec 2012 15:35:33 -0600
changeset 125248 e570d3a26106cc0050daf1781ed0455798facf72
parent 125247 e28f0f4b25a4e76ab368a88b12fd67a29c9faefd
child 125249 35a2b968dc98511f21a4c402ef00272db225d636
push id2151
push userlsblakk@mozilla.com
push dateTue, 19 Feb 2013 18:06:57 +0000
treeherdermozilla-beta@4952e88741ec [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs806731
milestone20.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 806731 - Port test_sts_privatebrowsing.html to the new per-window PB APIs; r=ehsan
security/manager/ssl/tests/mochitest/stricttransportsecurity/Makefile.in
security/manager/ssl/tests/mochitest/stricttransportsecurity/page_blank.html
security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing_perwindowpb.html
--- a/security/manager/ssl/tests/mochitest/stricttransportsecurity/Makefile.in
+++ b/security/manager/ssl/tests/mochitest/stricttransportsecurity/Makefile.in
@@ -6,25 +6,30 @@ DEPTH		= @DEPTH@
 topsrcdir	= @top_srcdir@
 srcdir		= @srcdir@
 VPATH		= @srcdir@
 relativesrcdir	= security/ssl/stricttransportsecurity
 
 include $(DEPTH)/config/autoconf.mk
 
 MOCHITEST_FILES = \
-	plain_bootstrap.html \
-	plain_bootstrap.html^headers^ \
-	subdom_bootstrap.html \
-	subdom_bootstrap.html^headers^ \
-	nosts_bootstrap.html \
-	nosts_bootstrap.html^headers^ \
-	verify.sjs \
-	test_stricttransportsecurity.html \
-	$(NULL)
+  plain_bootstrap.html \
+  plain_bootstrap.html^headers^ \
+  subdom_bootstrap.html \
+  subdom_bootstrap.html^headers^ \
+  nosts_bootstrap.html \
+  nosts_bootstrap.html^headers^ \
+  verify.sjs \
+  test_stricttransportsecurity.html \
+  $(NULL)
 
-ifndef MOZ_PER_WINDOW_PRIVATE_BROWSING
+ifdef MOZ_PER_WINDOW_PRIVATE_BROWSING
+MOCHITEST_CHROME_FILES += \
+  test_sts_privatebrowsing_perwindowpb.html \
+  page_blank.html \
+  $(NULL)
+else
 MOCHITEST_FILES += \
-        test_sts_privatebrowsing.html \
-        $(NULL)
+  test_sts_privatebrowsing.html \
+  $(NULL)
 endif
 
 include $(topsrcdir)/config/rules.mk
new file mode 100644
--- /dev/null
+++ b/security/manager/ssl/tests/mochitest/stricttransportsecurity/page_blank.html
@@ -0,0 +1,5 @@
+<html>
+<body>
+  PAGE BLANK
+</body>
+</html>
copy from security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing.html
copy to security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing_perwindowpb.html
--- a/security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing.html
+++ b/security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing_perwindowpb.html
@@ -1,25 +1,39 @@
 <!-- This Source Code Form is subject to the terms of the Mozilla Public
    - License, v. 2.0. If a copy of the MPL was not distributed with this
    - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
 
 <!DOCTYPE HTML>
 <html>
 <head>
   <title>opens additional content that should be converted to https</title>
-  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
-  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" />
+  <script type="application/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"?>
 
   <script class="testbody" type="text/javascript">
   SimpleTest.waitForExplicitFinish();
 
+  const Cc = Components.classes;
+  const Ci = Components.interfaces;
   const STSPATH = "/tests/security/ssl/stricttransportsecurity";
+  const NUM_TEST_FRAMES = 4;
+  const CONTENT_PAGE =
+    "http://mochi.test:8888/chrome/security/ssl/stricttransportsecurity/page_blank.html";
 
-  const NUM_TEST_FRAMES = 4;
+  // This is how many sub-tests (testframes) in each round.
+  // When the round begins, this will be initialized.
+  var testsleftinround = 0;
+  var currentround = "";
+  var mainWindow =
+    window.QueryInterface(Ci.nsIInterfaceRequestor).
+    getInterface(Ci.nsIWebNavigation).QueryInterface(Ci.nsIDocShellTreeItem).
+    rootTreeItem.QueryInterface(Ci.nsIInterfaceRequestor).
+    getInterface(Ci.nsIDOMWindow);
+
   var testframes = {
     'samedom':
       {'url':     "http://example.com" + STSPATH + "/verify.sjs",
         'expected': {'plain': 'SECURE',
                      'subdom': 'SECURE',
                      'nosts': 'INSECURE'}},
     'subdom':
       {'url':     "http://test1.example.com" + STSPATH + "/verify.sjs",
@@ -33,201 +47,183 @@
                      'nosts': 'INSECURE'}},
     'alreadysecure':
       {'url':     "https://test2.example.com" + STSPATH + "/verify.sjs",
         'expected': {'plain': 'SECURE',
                      'subdom': 'SECURE',
                      'nosts': 'SECURE'}},
   };
 
-  // This is how many sub-tests (testframes) in each round.
-  // When the round begins, this will be initialized.
-  var testsleftinround = 0;
-  var currentround = "";
-
-  var gPBSvc = null;
-  var gPrefSvc = null;
-
-  function _getServices() {
-    netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
+  function testOnWindow(aIsPrivate, aCallback) {
+    var win = mainWindow.OpenBrowserWindow({private: aIsPrivate});
+    win.addEventListener("load", function onLoad() {
+      win.removeEventListener("load", onLoad, false);
+      win.addEventListener("DOMContentLoaded", function onInnerLoad() {
+        if (win.content.location.href == "about:privatebrowsing") {
+          win.gBrowser.loadURI(CONTENT_PAGE);
+          return;
+        }
+        win.removeEventListener("DOMContentLoaded", onInnerLoad, true);
+        SimpleTest.executeSoon(function() { aCallback(win); });
+      }, true);
+      win.gBrowser.loadURI(CONTENT_PAGE);
+    }, true);
+  }
 
-    // not all apps will have the private browsing service.
-    try {
-      gPBSvc = Components.classes["@mozilla.org/privatebrowsing;1"]
-                         .getService(Components.interfaces.nsIPrivateBrowsingService);
-    } catch (ex) {
-      SimpleTest.todo(false, "PB service is not available, will skip dependent tests");
-    }
-
-    // Not all apps will have the preference service.
-    try {
-      gPrefSvc = Components.classes["@mozilla.org/preferences-service;1"]
-                           .getService(Components.interfaces.nsIPrefService)
-                           .QueryInterface(Components.interfaces.nsIPrefBranch);
-    } catch (ex) {
-      SimpleTest.todo(false, "Pref service is not available, won't be able to manage preferences");
-    }
-  }
-  _getServices();
-
-  function startRound(round) {
+  function startRound(win, isPrivate, round) {
     currentround = round;
     testsleftinround = NUM_TEST_FRAMES;
     SimpleTest.info("TESTS LEFT IN ROUND " + currentround + ": " + testsleftinround);
-    var frame = document.createElement("iframe");
+    var frame = win.content.document.createElement("iframe");
     frame.setAttribute('id', 'ifr_bootstrap');
-    frame.setAttribute('src', "https://example.com" + STSPATH +
-                              "/" + round + "_bootstrap.html");
-    document.body.appendChild(frame);
+    frame.setAttribute('src', "https://example.com" + STSPATH + "/" + round + "_bootstrap.html");
+    frame.addEventListener("load", function() {
+      onMessageReceived(win, isPrivate, "BOOTSTRAP " + round);
+    });
+    win.content.document.body.appendChild(frame);
+  }
+
+  function loadVerifyFrames(win, isPrivate, round) {
+    loadVerifyFrame(win, isPrivate, testframes.samedom, 'samedom', function() {
+      loadVerifyFrame(win, isPrivate, testframes.subdom, 'subdom', function() {
+        loadVerifyFrame(win, isPrivate, testframes.otherdom, 'otherdom', function() {
+          loadVerifyFrame(win, isPrivate, testframes.alreadysecure, 'alreadysecure');
+        });
+      });
+    });
   }
 
-  function loadVerifyFrames(round) {
-    for (var test in testframes) {
-      var frame = document.createElement("iframe");
-      frame.setAttribute('id', 'ifr_' + test);
-      frame.setAttribute('src', testframes[test].url + '?id=' + test);
-      document.body.appendChild(frame);
-    }
+  function loadVerifyFrame(win, isPrivate, test, testName, aCallback) {
+    var frame = win.content.document.createElement("iframe");
+    frame.setAttribute('id', 'ifr_' + testName);
+    frame.setAttribute('src', test.url + '?id=' + testName);
+    frame.addEventListener("load", function() {
+      if (frame.contentDocument.location.protocol === 'https:') {
+        onMessageReceived(win, isPrivate, "SECURE " + testName);
+      } else {
+        onMessageReceived(win, isPrivate, "INSECURE " + testName);
+      }
+      if (aCallback)
+        aCallback();
+    });
+    win.content.document.body.appendChild(frame);
   }
 
-  /* Messages received are in this format:
+  /**
+   * Messages received are in this format:
    *  (BOOTSTRAP|SECURE|INSECURE) testid
    * For example: "BOOTSTRAP subdom"
    *          or: "INSECURE otherdom"
    */
-  function onMessageReceived(event) {
-
+  function onMessageReceived(win, isPrivate, data) {
     // otherwise, it's a test result
-    var result = event.data.split(/\s+/);
+    var result = data.split(/\s+/);
     if (result.length != 2) {
-      SimpleTest.ok(false, event.data);
+      SimpleTest.ok(false, data);
       return;
     }
 
     if (result[0] === "BOOTSTRAP") {
-      loadVerifyFrames(currentround);
+      loadVerifyFrames(win, isPrivate, currentround);
       return;
     }
 
     // check if the result (SECURE/INSECURE) is expected for this round/test
     // combo
-    dump_STSState();
+    dump_STSState(isPrivate);
     SimpleTest.is(result[0], testframes[result[1]].expected[currentround],
                              "in ROUND " + currentround +
                              ", test " + result[1]);
     testsleftinround--;
 
     // if this round is complete...
     if (testsleftinround < 1) {
       SimpleTest.info("DONE WITH ROUND " + currentround);
       // remove all the iframes in the document
-      document.body.removeChild(document.getElementById('ifr_bootstrap'));
+      win.content.document.body.removeChild(
+        win.content.document.getElementById('ifr_bootstrap'));
       for (var test in testframes)
-        document.body.removeChild(document.getElementById('ifr_' + test));
+        win.content.document.body.removeChild(
+          win.content.document.getElementById('ifr_' + test));
       currentround = "";
 
+      if (!isPrivate)
+        clean_up_sts_state(isPrivate);
+      // Close test window.
+      win.close();
       // And advance to the next test.
       // Defer this so it doesn't muck with the stack too much.
       SimpleTest.executeSoon(nextTest);
     }
   }
 
   function test_sts_before_private_mode() {
-    dump_STSState();
-    SimpleTest.info("Not in private browsing mode");
-    startRound('plain');
+    testOnWindow(false, function(win) {
+      SimpleTest.info("In public window");
+      dump_STSState(false);
+      startRound(win, false, 'plain');
+    });
   }
 
   function test_sts_in_private_mode() {
-    dump_STSState();
-    SimpleTest.info("Entering private browsing mode ...");
-    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
-    if (gPrefSvc)
-      gPrefSvc.setBoolPref("browser.privatebrowsing.keep_current_session", true);
-    gPBSvc.privateBrowsingEnabled = true;
-    SimpleTest.info("... done");
-    dump_STSState();
-    startRound('subdom');
+    testOnWindow(true, function(win) {
+      SimpleTest.info("In private window");
+      dump_STSState(true);
+      startRound(win, true, 'subdom');
+    });
   }
 
   function test_sts_after_exiting_private_mode() {
-    dump_STSState();
-    SimpleTest.info("Exiting private browsing mode ...");
-    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
-    gPBSvc.privateBrowsingEnabled = false;
-    if (gPrefSvc)
-      gPrefSvc.clearUserPref("browser.privatebrowsing.keep_current_session");
-    SimpleTest.info("... done");
-    dump_STSState();
-    startRound('nosts');
+    testOnWindow(false, function(win) {
+      SimpleTest.info("In a new public window");
+      dump_STSState(false);
+      startRound(win, false, 'nosts');
+    });
   }
 
-  function clean_up_sts_state() {
+  function clean_up_sts_state(isPrivate) {
     // erase all signs that this test ran.
     SimpleTest.info("Cleaning up STS data");
-    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
-    const Cc = Components.classes;
-    const Ci = Components.interfaces;
-    var ios = Cc["@mozilla.org/network/io-service;1"]
-                .getService(Ci.nsIIOService);
+    var ios =
+      Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
     var thehost = ios.newURI("http://example.com", null, null);
-    var stss = Cc["@mozilla.org/stsservice;1"]
-                 .getService(Ci.nsIStrictTransportSecurityService);
-    var loadContext = window.QueryInterface(Ci.nsIInterfaceRequestor)
-                            .getInterface(Ci.nsIWebNavigation)
-                            .QueryInterface(Ci.nsILoadContext);
-    var flags = loadContext.usePrivateBrowsing ?
-      Ci.nsISocketProvider.NO_PERMANENT_STORAGE : 0
+    var stss =
+      Cc["@mozilla.org/stsservice;1"].
+        getService(Ci.nsIStrictTransportSecurityService);
+    var flags = isPrivate ? Ci.nsISocketProvider.NO_PERMANENT_STORAGE : 0
     stss.removeStsState(thehost, flags);
-    dump_STSState();
-    SimpleTest.executeSoon(nextTest);
+    dump_STSState(isPrivate);
   }
 
-function dump_STSState() {
-    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
-    var stss = Components.classes["@mozilla.org/stsservice;1"]
-          .getService(Components.interfaces.nsIStrictTransportSecurityService);
-    var loadContext = window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
-                            .getInterface(Components.interfaces.nsIWebNavigation)
-                            .QueryInterface(Components.interfaces.nsILoadContext);
-    var flags = loadContext.usePrivateBrowsing ?
-      Components.interfaces.nsISocketProvider.NO_PERMANENT_STORAGE : 0
+  function dump_STSState(isPrivate) {
+    var stss =
+      Cc["@mozilla.org/stsservice;1"].
+        getService(Ci.nsIStrictTransportSecurityService);
+    var flags = isPrivate ? Ci.nsISocketProvider.NO_PERMANENT_STORAGE : 0
     SimpleTest.info("State of example.com: " + stss.isStsHost("example.com", flags));
-}
+  }
 
-  // these are executed in the order presented.
+  // These are executed in the order presented.
   // 0.  test that STS works before entering private browsing mode.
   //     (load sts-bootstrapped "plain" tests)
   //  ... clear any STS data ...
-  var tests = [
-    test_sts_before_private_mode,
-    clean_up_sts_state
-  ];
   // 1.  test that STS works in private browsing mode
   //     (load sts-bootstrapped "subdomain" tests)
   // 2.  test that after exiting private browsing, STS data is forgotten
   //     (verified with non-sts-bootstrapped pages)
   //  ... clear any STS data ...
-  // Skip these tests if there is no private mode support.
-  if (gPBSvc) {
-    tests = tests.concat(
-      test_sts_in_private_mode,
-      test_sts_after_exiting_private_mode,
-      clean_up_sts_state
-    );
-  }
+  var tests = [
+    test_sts_before_private_mode,
+    test_sts_in_private_mode,
+    test_sts_after_exiting_private_mode
+  ];
 
   function nextTest() {
     SimpleTest.executeSoon(tests.length ? tests.shift() : SimpleTest.finish);
   }
-
-  // listen for calls back from the sts-setting iframe and then
-  // the verification frames.
-  window.addEventListener("message", onMessageReceived, false);
   window.addEventListener('load', nextTest, false);
   </script>
 </head>
 
 <body>
   This test will load some iframes and do some tests.
-
 </body>
 </html>