From b0105760cce90a42879781f0284357f8db0b2971 Mon Sep 17 00:00:00 2001 From: Serge Gautherie Date: Mon, 26 Mar 2012 22:59:41 +0200 Subject: [PATCH] Bug 739008. (Av1) Fix and improve test_sts_privatebrowsing.html. r=sstamm. --- .../test_sts_privatebrowsing.html | 102 ++++++++---------- 1 file changed, 45 insertions(+), 57 deletions(-) diff --git a/security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing.html b/security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing.html index c6457a20fd7..fd1a96a1bc9 100644 --- a/security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing.html +++ b/security/manager/ssl/tests/mochitest/stricttransportsecurity/test_sts_privatebrowsing.html @@ -76,41 +76,35 @@ var testsleftinround = 0; var currentround = ""; - var _PBSvc = null; - var _PrefSvc = null; + var gPBSvc = null; + var gPrefSvc = null; - function _getPBService() { - if (_PBSvc) - return _PBSvc; + function _getServices() { + netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect"); // not all apps will have the private browsing service. try { - netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); - _PBSvc = Components.classes["@mozilla.org/privatebrowsing;1"] - .getService(Components.interfaces.nsIPrivateBrowsingService); - return _PBSvc; - } catch (e) {} - return null; - } - function _getPrefService() { - if (_PrefSvc) - return _PrefSvc; + 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 private browsing service. + // Not all apps will have the preference service. try { - netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); - _PrefSvc = Components.classes["@mozilla.org/preferences-service;1"] - .getService(Components.interfaces.nsIPrefService) - .QueryInterface(Components.interfaces.nsIPrefBranch); - return _PrefSvc; - } catch (e) {} - return null; + 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) { currentround = round; testsleftinround = NUM_TEST_FRAMES; - dump("TESTS LEFT IN ROUND: " + testsleftinround + "\n"); + SimpleTest.info("TESTS LEFT IN ROUND " + currentround + ": " + testsleftinround); var frame = document.createElement("iframe"); frame.setAttribute('id', 'ifr_bootstrap'); frame.setAttribute('src', "https://example.com" + STSPATH + @@ -149,9 +143,6 @@ // check if the result (SECURE/INSECURE) is expected for this round/test // combo dump_STSState(); - dump( "*** in ROUND " + currentround + - ", test " + result[1] + - " is " + result[0] + "\n"); SimpleTest.is(result[0], testframes[result[1]].expected[currentround], "in ROUND " + currentround + ", test " + result[1]); @@ -159,7 +150,7 @@ // if this round is complete... if (testsleftinround < 1) { - dump("DONE WITH ROUND " + currentround + "\n"); + SimpleTest.info("DONE WITH ROUND " + currentround); // remove all the iframes in the document document.body.removeChild(document.getElementById('ifr_bootstrap')); for (var test in testframes) @@ -174,36 +165,37 @@ function test_sts_before_private_mode() { dump_STSState(); - dump("*** not in private browsing mode\n"); + SimpleTest.info("Not in private browsing mode"); startRound('plain'); } function test_sts_in_private_mode() { dump_STSState(); - dump("*** Entering private browsing mode\n"); + SimpleTest.info("Entering private browsing mode ..."); netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); - _getPrefService().setBoolPref("browser.privatebrowsing.keep_current_session", - true); - _getPBService().privateBrowsingEnabled = true; - dump("*** ... done\n"); + if (gPrefSvc) + gPrefSvc.setBoolPref("browser.privatebrowsing.keep_current_session", true); + gPBSvc.privateBrowsingEnabled = true; + SimpleTest.info("... done"); dump_STSState(); startRound('subdom'); } function test_sts_after_exiting_private_mode() { dump_STSState(); - dump("*** Exiting private browsing mode\n"); + SimpleTest.info("Exiting private browsing mode ..."); netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); - _getPBService().privateBrowsingEnabled = false; - _getPrefService().clearUserPref("browser.privatebrowsing.keep_current_session"); - dump("*** ... done\n"); + gPBSvc.privateBrowsingEnabled = false; + if (gPrefSvc) + gPrefSvc.clearUserPref("browser.privatebrowsing.keep_current_session"); + SimpleTest.info("... done"); dump_STSState(); startRound('nosts'); } function clean_up_sts_state() { // erase all signs that this test ran. - dump("*** Cleaning up STS data.\n"); + SimpleTest.info("Cleaning up STS data"); netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); const Cc = Components.classes; const Ci = Components.interfaces; @@ -221,43 +213,39 @@ function dump_STSState() { netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); var stss = Components.classes["@mozilla.org/stsservice;1"] .getService(Components.interfaces.nsIStrictTransportSecurityService); - dump("*** State of example.com: " + stss.isStsHost("example.com") + "\n"); + SimpleTest.info("State of example.com: " + stss.isStsHost("example.com")); } // 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) - var tests = []; - { // skip these tests if there's no private mode support - netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect'); - if ("@mozilla.org/privatebrowsing;1" in Components.classes) { - tests = [ - test_sts_before_private_mode, - clean_up_sts_state, - test_sts_in_private_mode, - test_sts_after_exiting_private_mode, - clean_up_sts_state, - ]; - } + // ... clear any STS data ... + // Skip these tests if there is no private mode support. + if (gPBSvc) { + tests += [ + test_sts_in_private_mode, + test_sts_after_exiting_private_mode, + clean_up_sts_state, + ]; } function nextTest() { - if (tests.length) - SimpleTest.executeSoon(tests.shift()); - else - SimpleTest.executeSoon(SimpleTest.finish); + 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); -