Bug 785860 - fix sts preload list tests to skip private mode tests if private browsing service is missing. r=bsmith
authorSid Stamm <sstamm@mozilla.com>
Mon, 27 Aug 2012 12:01:00 -0700
changeset 105628 807627473028c796f805d27c9601647ef99a48bb
parent 105627 e1b06ed3fc1e1ba124604ce407722c0b64ea2fb0
child 105629 ceb5b26840e0855b4cd4e6596243741f7cbb26b8
push id55
push usershu@rfrn.org
push dateThu, 30 Aug 2012 01:33:09 +0000
reviewersbsmith
bugs785860
milestone17.0a1
Bug 785860 - fix sts preload list tests to skip private mode tests if private browsing service is missing. r=bsmith
security/manager/ssl/tests/unit/test_sts_preloadlist.js
--- a/security/manager/ssl/tests/unit/test_sts_preloadlist.js
+++ b/security/manager/ssl/tests/unit/test_sts_preloadlist.js
@@ -1,15 +1,30 @@
 var Cc = Components.classes;
 var Ci = Components.interfaces;
 
 Components.utils.import("resource://gre/modules/Services.jsm");
 
-var gPBService = Cc["@mozilla.org/privatebrowsing;1"]
-                 .getService(Ci.nsIPrivateBrowsingService);
+var _pbsvc;
+
+// lazy getter, added for bug 785860 to avoid testing private browsing when the
+// private browsing service doesn't exist
+function getPBSvc() {
+  if (_pbsvc)
+    return _pbsvc;
+
+  try {
+    _pbsvc = Cc["@mozilla.org/privatebrowsing;1"]
+               .getService(Ci.nsIPrivateBrowsingService);
+    return _pbsvc;
+  } catch (e) {}
+
+  return null;
+}
+
 var gSTSService = Cc["@mozilla.org/stsservice;1"]
                   .getService(Ci.nsIStrictTransportSecurityService);
 
 function Observer() {}
 Observer.prototype = {
   observe: function(subject, topic, data) {
     run_next_test();
   }
@@ -21,31 +36,35 @@ var gObserver = new Observer();
 // (we have to remove any state added to the sts service so as to not muck
 // with other tests).
 var hosts = ["http://keyerror.com", "http://subdomain.kyps.net",
              "http://subdomain.cert.se", "http://crypto.cat",
              "http://www.logentries.com"];
 
 function cleanup() {
   Services.obs.removeObserver(gObserver, "private-browsing-transition-complete");
-  gPBService.privateBrowsingEnabled = false;
+  if (getPBSvc())
+    getPBSvc().privateBrowsingEnabled = false;
+
   for (var host of hosts) {
     var uri = Services.io.newURI(host, null, null);
     gSTSService.removeStsState(uri);
   }
 }
 
 function run_test() {
   do_register_cleanup(cleanup);
   Services.obs.addObserver(gObserver, "private-browsing-transition-complete", false);
   Services.prefs.setBoolPref("browser.privatebrowsing.keep_current_session", true);
 
   add_test(test_part1);
-  add_test(test_private_browsing1);
-  add_test(test_private_browsing2);
+  if (getPBSvc()) {
+    add_test(test_private_browsing1);
+    add_test(test_private_browsing2);
+  }
 
   run_next_test();
 }
 
 function test_part1() {
   // check that a host not in the list is not identified as an sts host
   do_check_false(gSTSService.isStsHost("nonexistent.mozilla.com"));
 
@@ -119,17 +138,18 @@ function test_part1() {
   //     |-- subdomain.cert.se (include subdomains is false) IS sts host
   //     |   `-- another.subdomain.cert.se                   IS NOT sts host
   //     `-- sibling.cert.se                                 IS sts host
   do_check_true(gSTSService.isStsHost("subdomain.cert.se"));
   do_check_true(gSTSService.isStsHost("sibling.cert.se"));
   do_check_false(gSTSService.isStsHost("another.subdomain.cert.se"));
 
   // test private browsing correctly interacts with removing preloaded sites
-  gPBService.privateBrowsingEnabled = true;
+  if (getPBSvc())
+    getPBSvc().privateBrowsingEnabled = true;
 }
 
 function test_private_browsing1() {
   // sanity - crypto.cat is preloaded, includeSubdomains set
   do_check_true(gSTSService.isStsHost("crypto.cat"));
   do_check_true(gSTSService.isStsHost("a.b.c.subdomain.crypto.cat"));
 
   var uri = Services.io.newURI("http://crypto.cat", null, null);
@@ -159,20 +179,22 @@ function test_private_browsing1() {
   // (sanity check first - this should be in the preload list)
   do_check_true(gSTSService.isStsHost("www.logentries.com"));
   var uri = Services.io.newURI("http://www.logentries.com", null, null);
   // according to the rfc, max-age can't be negative, but this is a great
   // way to test an expired entry
   gSTSService.processStsHeader(uri, "max-age=-1000");
   do_check_false(gSTSService.isStsHost("www.logentries.com"));
 
-  gPBService.privateBrowsingEnabled = false;
+  // if this test gets this far, it means there's a private browsing service
+  getPBSvc().privateBrowsingEnabled = false;
 }
 
 function test_private_browsing2() {
+  // if this test gets this far, it means there's a private browsing service
   do_check_true(gSTSService.isStsHost("crypto.cat"));
   // the crypto.cat entry has includeSubdomains set
   do_check_true(gSTSService.isStsHost("subdomain.crypto.cat"));
 
   // Now that we're out of private browsing mode, we need to make sure
   // we've "forgotten" that we "forgot" this site's sts status.
   do_check_true(gSTSService.isStsHost("www.logentries.com"));