Bug 573061 - Add persistent cookie tests. Part 2: revise and add tests. r=sdwilsh
authorDan Witte <dwitte@mozilla.com>
Mon, 21 Jun 2010 17:50:35 -0700
changeset 43934 4f1f2e61ca9e023556a832dfb9b047c68f77021b
parent 43933 f4bd9605e451e616e95a876f4e637cfd3e7a6b12
child 43935 e64d6eefbce9a3e44282d90ae0dbd68ac64fb7ef
push id1
push userroot
push dateTue, 26 Apr 2011 22:38:44 +0000
treeherdermozilla-beta@bfdb6e623a36 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerssdwilsh
bugs573061
milestone1.9.3a6pre
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 573061 - Add persistent cookie tests. Part 2: revise and add tests. r=sdwilsh
extensions/cookie/test/unit/cookieprompt.js
extensions/cookie/test/unit/head_cookies.js
extensions/cookie/test/unit/test_bug468700.js
extensions/cookie/test/unit/test_bug481775.js
extensions/cookie/test/unit/test_bug526789.js
extensions/cookie/test/unit/test_cookies_persistence.js
extensions/cookie/test/unit/test_cookies_thirdparty.js
extensions/cookie/test/unit/test_cookies_thirdparty_session.js
extensions/cookie/test/unit/test_domain_eviction.js
extensions/cookie/test/unit/test_eviction.js
extensions/cookie/test/unit/test_permmanager_expiration.js
extensions/cookie/test/unit/test_permmanager_notifications.js
extensions/cookie/test/unit/test_permmanager_removeall.js
--- a/extensions/cookie/test/unit/cookieprompt.js
+++ b/extensions/cookie/test/unit/cookieprompt.js
@@ -1,13 +1,15 @@
-const Cc = Components.classes;
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
 const Ci = Components.interfaces;
-const Cu = Components.utils;
-
-Cu.import("resource://gre/modules/XPCOMUtils.jsm");
 
 function CookiePromptService() {
 }
 
 CookiePromptService.prototype = {
   classDescription: "Cookie Prompt Test Service",
   contractID: "@mozilla.org/embedcomp/cookieprompt-service;1",
   classID: Components.ID("{509b5540-c87c-11dd-ad8b-0800200c9a66}"),
new file mode 100644
--- /dev/null
+++ b/extensions/cookie/test/unit/head_cookies.js
@@ -0,0 +1,75 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+Components.utils.import("resource://gre/modules/Services.jsm");
+Components.utils.import("resource://gre/modules/NetUtil.jsm");
+Components.utils.import("resource://gre/modules/XPCOMUtils.jsm");
+
+const Cc = Components.classes;
+const Ci = Components.interfaces;
+const Cr = Components.results;
+
+XPCOMUtils.defineLazyServiceGetter(Services, "cookies",
+                                   "@mozilla.org/cookieService;1",
+                                   "nsICookieService");
+XPCOMUtils.defineLazyServiceGetter(Services, "cookiemgr",
+                                   "@mozilla.org/cookiemanager;1",
+                                   "nsICookieManager2");
+
+// Close and reload the cookie database.
+function do_reload_profile(generator, profile, cleanse) {
+  function _observer(generator, service, topic) {
+    Services.obs.addObserver(this, topic, false);
+
+    this.service = service;
+    this.generator = generator;
+    this.topic = topic;
+  }
+
+  _observer.prototype = {
+    observe: function (subject, topic, data) {
+      do_check_eq(this.topic, topic);
+
+      Services.obs.removeObserver(this, this.topic);
+
+      // Fire the notification to reload the database, and continue executing
+      // the generator function.
+      this.service.observe(null, "profile-do-change", "");
+      this.generator.next();
+
+      this.generator = null;
+      this.service = null;
+      this.topic = null;
+    }
+  }
+
+  let dbfile = profile.QueryInterface(Ci.nsILocalFile).clone();
+  dbfile.append("cookies.sqlite");
+
+  // Register an observer for db close.
+  let service = Services.cookies.QueryInterface(Ci.nsIObserver);
+  let obs = new _observer(generator, service, "cookie-db-closed");
+
+  // Close the db.
+  service.observe(null, "profile-before-change", cleanse ? cleanse : "");
+}
+
+// Set four cookies; with & without channel, http and non-http; and test
+// the cookie count against 'expected' after each set.
+function do_set_cookies(uri, channel, session, expected) {
+  let suffix = session ? "" : "; max-age=1000";
+
+  // without channel
+  Services.cookies.setCookieString(uri, null, "oh=hai" + suffix, null);
+  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri.host), expected[0]);
+  // with channel
+  Services.cookies.setCookieString(uri, null, "can=has" + suffix, channel);
+  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri.host), expected[1]);
+  // without channel, from http
+  Services.cookies.setCookieStringFromHttp(uri, null, null, "cheez=burger" + suffix, null, null);
+  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri.host), expected[2]);
+  // with channel, from http
+  Services.cookies.setCookieStringFromHttp(uri, null, null, "hot=dog" + suffix, null, channel);
+  do_check_eq(Services.cookiemgr.countCookiesFromHost(uri.host), expected[3]);
+}
--- a/extensions/cookie/test/unit/test_bug468700.js
+++ b/extensions/cookie/test/unit/test_bug468700.js
@@ -1,10 +1,11 @@
-const Cc = Components.classes;
-const Ci = Components.interfaces;
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
 
 function run_test() {
   do_load_module("cookieprompt.js");
 
   var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
   var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
   var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
   var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
--- a/extensions/cookie/test/unit/test_bug481775.js
+++ b/extensions/cookie/test/unit/test_bug481775.js
@@ -1,10 +1,11 @@
-const Cc = Components.classes;
-const Ci = Components.interfaces;
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
 
 function run_test() {
   var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
   var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
   var pb = null;
   try {
     pb = Cc["@mozilla.org/privatebrowsing;1"].getService(Ci.nsIPrivateBrowsingService);
   } catch (e) {}
--- a/extensions/cookie/test/unit/test_bug526789.js
+++ b/extensions/cookie/test/unit/test_bug526789.js
@@ -1,13 +1,11 @@
-const Cc = Components.classes;
-const Ci = Components.interfaces;
-const Cr = Components.results;
-
-Components.utils.import("resource://gre/modules/NetUtil.jsm");
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
 
 function do_check_throws(f, result, stack)
 {
   if (!stack)
     stack = Components.stack.caller;
 
   try {
     f();
new file mode 100644
--- /dev/null
+++ b/extensions/cookie/test/unit/test_cookies_persistence.js
@@ -0,0 +1,74 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+// test for cookie persistence across sessions, for the cases:
+// 1) network.cookie.lifetimePolicy = 0 (expire naturally)
+// 2) network.cookie.lifetimePolicy = 2 (expire at end of session)
+
+let test_generator = do_run_test();
+
+function run_test() {
+  do_test_pending();
+  test_generator.next();
+}
+
+function finish_test() {
+  do_execute_soon(function() {
+    test_generator.close();
+    do_test_finished();
+  });
+}
+
+function do_run_test() {
+  // Set up a profile.
+  let profile = do_get_profile();
+
+  // Create URIs and channels pointing to foo.com and bar.com.
+  // We will use these to put foo.com into first and third party contexts.
+  var spec1 = "http://foo.com/foo.html";
+  var spec2 = "http://bar.com/bar.html";
+  var uri1 = NetUtil.newURI(spec1);
+  var uri2 = NetUtil.newURI(spec2);
+  var channel1 = NetUtil.newChannel(uri1);
+  var channel2 = NetUtil.newChannel(uri2);
+
+  // Force the channel URI to be used when determining the originating URI of
+  // the channel.
+  var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
+  var httpchannel2 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
+  httpchannel1.forceAllowThirdPartyCookie = true;
+  httpchannel2.forceAllowThirdPartyCookie = true;
+
+  // test with cookies enabled, and third party cookies persistent.
+  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
+  Services.prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", false);
+  do_set_cookies(uri1, channel1, false, [1, 2, 3, 4]);
+  do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);
+
+  // fake a profile change
+  do_reload_profile(test_generator, profile);
+  yield;
+  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
+  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
+
+  // cleanse them
+  do_reload_profile(test_generator, profile, "shutdown-cleanse");
+  yield;
+  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
+  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
+
+  // test with cookies set to session-only
+  Services.prefs.setIntPref("network.cookie.lifetimePolicy", 2);
+  do_set_cookies(uri1, channel1, false, [1, 2, 3, 4]);
+  do_set_cookies(uri2, channel2, true, [1, 2, 3, 4]);
+
+  // fake a profile change
+  do_reload_profile(test_generator, profile);
+  yield;
+  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
+  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
+
+  finish_test();
+}
+
--- a/extensions/cookie/test/unit/test_cookies_thirdparty.js
+++ b/extensions/cookie/test/unit/test_cookies_thirdparty.js
@@ -1,66 +1,54 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
 // test third party cookie blocking, for the cases:
 // 1) with null channel
 // 2) with channel, but with no docshell parent
 
-const Cc = Components.classes;
-const Ci = Components.interfaces;
-
 function run_test() {
-  var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
-  var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
-  var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
-  var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
-
   // Create URIs and channels pointing to foo.com and bar.com.
   // We will use these to put foo.com into first and third party contexts.
   var spec1 = "http://foo.com/foo.html";
   var spec2 = "http://bar.com/bar.html";
-  var uri1 = ios.newURI(spec1, null, null);
-  var uri2 = ios.newURI(spec2, null, null);
-  var channel1 = ios.newChannelFromURI(uri1);
-  var channel2 = ios.newChannelFromURI(uri2);
+  var uri1 = NetUtil.newURI(spec1);
+  var uri2 = NetUtil.newURI(spec2);
+  var channel1 = NetUtil.newChannel(uri1);
+  var channel2 = NetUtil.newChannel(uri2);
 
   // test with cookies enabled
-  prefs.setIntPref("network.cookie.cookieBehavior", 0);
-  run_cookie_test(cs, uri1, channel1, [1, 2, 3, 4]);
-  run_cookie_test(cs, uri1, channel2, [1, 2, 3, 4]);
+  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
+  do_set_cookies(uri1, channel1, true, [1, 2, 3, 4]);
+  Services.cookies.removeAll();
+  do_set_cookies(uri1, channel2, true, [1, 2, 3, 4]);
+  Services.cookies.removeAll();
 
   // test with third party cookies blocked
-  prefs.setIntPref("network.cookie.cookieBehavior", 1);
-  run_cookie_test(cs, uri1, channel1, [0, 0, 0, 0]);
-  run_cookie_test(cs, uri1, channel2, [0, 0, 0, 0]);
+  Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
+  do_set_cookies(uri1, channel1, true, [0, 0, 0, 0]);
+  Services.cookies.removeAll();
+  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
+  Services.cookies.removeAll();
 
   // Force the channel URI to be used when determining the originating URI of
   // the channel.
   var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
   var httpchannel2 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
   httpchannel1.forceAllowThirdPartyCookie = true;
   httpchannel2.forceAllowThirdPartyCookie = true;
 
   // test with cookies enabled
-  prefs.setIntPref("network.cookie.cookieBehavior", 0);
-  run_cookie_test(cs, uri1, channel1, [1, 2, 3, 4]);
-  run_cookie_test(cs, uri1, channel2, [1, 2, 3, 4]);
+  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
+  do_set_cookies(uri1, channel1, true, [1, 2, 3, 4]);
+  Services.cookies.removeAll();
+  do_set_cookies(uri1, channel2, true, [1, 2, 3, 4]);
+  Services.cookies.removeAll();
 
   // test with third party cookies blocked
-  prefs.setIntPref("network.cookie.cookieBehavior", 1);
-  run_cookie_test(cs, uri1, channel1, [0, 1, 1, 2]);
-  run_cookie_test(cs, uri1, channel2, [0, 0, 0, 0]);
+  Services.prefs.setIntPref("network.cookie.cookieBehavior", 1);
+  do_set_cookies(uri1, channel1, true, [0, 1, 1, 2]);
+  Services.cookies.removeAll();
+  do_set_cookies(uri1, channel2, true, [0, 0, 0, 0]);
+  Services.cookies.removeAll();
 }
 
-function run_cookie_test(cs, uri, channel, expected) {
-  // without channel
-  cs.setCookieString(uri, null, "oh=hai", null);
-  do_check_eq(cs.countCookiesFromHost("foo.com"), expected[0]);
-  // with channel
-  cs.setCookieString(uri, null, "can=has", channel);
-  do_check_eq(cs.countCookiesFromHost("foo.com"), expected[1]);
-  // without channel, from http
-  cs.setCookieStringFromHttp(uri, null, null, "cheez=burger", null, null);
-  do_check_eq(cs.countCookiesFromHost("foo.com"), expected[2]);
-  // with channel, from http
-  cs.setCookieStringFromHttp(uri, null, null, "hot=dog", null, channel);
-  do_check_eq(cs.countCookiesFromHost("foo.com"), expected[3]);
-  cs.removeAll();
-}
-
new file mode 100644
--- /dev/null
+++ b/extensions/cookie/test/unit/test_cookies_thirdparty_session.js
@@ -0,0 +1,74 @@
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
+
+// test third party persistence across sessions, for the cases:
+// 1) network.cookie.thirdparty.sessionOnly = false
+// 2) network.cookie.thirdparty.sessionOnly = true
+
+let test_generator = do_run_test();
+
+function run_test() {
+  do_test_pending();
+  test_generator.next();
+}
+
+function finish_test() {
+  do_execute_soon(function() {
+    test_generator.close();
+    do_test_finished();
+  });
+}
+
+function do_run_test() {
+  // Set up a profile.
+  let profile = do_get_profile();
+
+  // Create URIs and channels pointing to foo.com and bar.com.
+  // We will use these to put foo.com into first and third party contexts.
+  var spec1 = "http://foo.com/foo.html";
+  var spec2 = "http://bar.com/bar.html";
+  var uri1 = NetUtil.newURI(spec1);
+  var uri2 = NetUtil.newURI(spec2);
+  var channel1 = NetUtil.newChannel(uri1);
+  var channel2 = NetUtil.newChannel(uri2);
+
+  // Force the channel URI to be used when determining the originating URI of
+  // the channel.
+  var httpchannel1 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
+  var httpchannel2 = channel1.QueryInterface(Ci.nsIHttpChannelInternal);
+  httpchannel1.forceAllowThirdPartyCookie = true;
+  httpchannel2.forceAllowThirdPartyCookie = true;
+
+  // test with cookies enabled, and third party cookies persistent.
+  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
+  Services.prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", false);
+  do_set_cookies(uri1, channel2, false, [1, 2, 3, 4]);
+  do_set_cookies(uri2, channel1, true, [1, 2, 3, 4]);
+
+  // fake a profile change
+  do_reload_profile(test_generator, profile);
+  yield;
+  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 4);
+  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
+
+  // cleanse them
+  do_reload_profile(test_generator, profile, "shutdown-cleanse");
+  yield;
+  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
+  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
+
+  // test with third party cookies for session only.
+  Services.prefs.setBoolPref("network.cookie.thirdparty.sessionOnly", true);
+  do_set_cookies(uri1, channel2, false, [1, 2, 3, 4]);
+  do_set_cookies(uri2, channel1, true, [1, 2, 3, 4]);
+
+  // fake a profile change
+  do_reload_profile(test_generator, profile);
+  yield;
+  do_check_eq(Services.cookies.countCookiesFromHost(uri1.host), 0);
+  do_check_eq(Services.cookies.countCookiesFromHost(uri2.host), 0);
+
+  finish_test();
+}
+
--- a/extensions/cookie/test/unit/test_domain_eviction.js
+++ b/extensions/cookie/test/unit/test_domain_eviction.js
@@ -1,10 +1,11 @@
-const Cc = Components.classes;
-const Ci = Components.interfaces;
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
 
 function run_test()
 {
   var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
 
   cm.removeAll();
 
   // test eviction under the 50 cookies per base domain limit. this means
--- a/extensions/cookie/test/unit/test_eviction.js
+++ b/extensions/cookie/test/unit/test_eviction.js
@@ -1,10 +1,11 @@
-const Cc = Components.classes;
-const Ci = Components.interfaces;
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
 
 function run_test()
 {
   var cs = Cc["@mozilla.org/cookieService;1"].getService(Ci.nsICookieService);
   var cm = Cc["@mozilla.org/cookiemanager;1"].getService(Ci.nsICookieManager2);
   var ios = Cc["@mozilla.org/network/io-service;1"].getService(Ci.nsIIOService);
   var prefs = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefBranch);
 
--- a/extensions/cookie/test/unit/test_permmanager_expiration.js
+++ b/extensions/cookie/test/unit/test_permmanager_expiration.js
@@ -1,21 +1,22 @@
-const Cc = Components.classes;
-const Ci = Components.interfaces;
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
 
-  // setup a profile directory
-  var dir = do_get_profile();
+// setup a profile directory
+var dir = do_get_profile();
 
-  // initialize the permission manager service
-  var pm = Cc["@mozilla.org/permissionmanager;1"]
-           .getService(Ci.nsIPermissionManager);
+// initialize the permission manager service
+var pm = Cc["@mozilla.org/permissionmanager;1"]
+         .getService(Ci.nsIPermissionManager);
 
-  var ios = Cc["@mozilla.org/network/io-service;1"]
-            .getService(Ci.nsIIOService);
-  var permURI = ios.newURI("http://example.com", null, null);
+var ios = Cc["@mozilla.org/network/io-service;1"]
+          .getService(Ci.nsIIOService);
+var permURI = ios.newURI("http://example.com", null, null);
 
 function run_test() {
 
   // add a permission with *now* expiration
   pm.add(permURI, "test/expiration-perm-exp", 1, pm.EXPIRE_TIME, (new Date()).getTime());
 
   // add a permission with future expiration (100 milliseconds)
   pm.add(permURI, "test/expiration-perm-exp2", 1, pm.EXPIRE_TIME, (new Date()).getTime() + 100);
--- a/extensions/cookie/test/unit/test_permmanager_notifications.js
+++ b/extensions/cookie/test/unit/test_permmanager_notifications.js
@@ -1,10 +1,11 @@
-const Cc = Components.classes;
-const Ci = Components.interfaces;
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
 
 // setup a profile directory
 var dir = do_get_profile();
 
 // initialize the permission manager service
 var pm = Cc["@mozilla.org/permissionmanager;1"]
           .getService(Ci.nsIPermissionManager);
 
--- a/extensions/cookie/test/unit/test_permmanager_removeall.js
+++ b/extensions/cookie/test/unit/test_permmanager_removeall.js
@@ -1,10 +1,11 @@
-const Cc = Components.classes;
-const Ci = Components.interfaces;
+/* Any copyright is dedicated to the Public Domain.
+ * http://creativecommons.org/publicdomain/zero/1.0/
+ */
 
 function run_test() {
   // setup a profile directory
   var dir = do_get_profile();
 
   // initialize the permission manager service
   var pm = Cc["@mozilla.org/permissionmanager;1"].
            getService(Ci.nsIPermissionManager);