Bug 1267910 - Part 3: Add test cases for add() and getCookiesFromHost() of the nsICookieManager2. r=jdm
☠☠ backed out by f1f2644d3444 ☠ ☠
authorTim Huang <tihuang@mozilla.com>
Wed, 18 May 2016 20:44:00 -0400
changeset 298162 5962f8e6d030e37a5f3c883180a2f35efe010b11
parent 298161 f8afc5cf9e0e90b6320e7436cbf6647b05cf1f1d
child 298163 e5e3a65e822049305fcf019b69bc1370bf8f091c
push id30273
push userkwierso@gmail.com
push dateFri, 20 May 2016 21:08:12 +0000
treeherdermozilla-central@c403ac05b8f4 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjdm
bugs1267910
milestone49.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 1267910 - Part 3: Add test cases for add() and getCookiesFromHost() of the nsICookieManager2. r=jdm
netwerk/cookie/test/unit/test_bug1267910.js
netwerk/cookie/test/unit/xpcshell.ini
new file mode 100644
--- /dev/null
+++ b/netwerk/cookie/test/unit/test_bug1267910.js
@@ -0,0 +1,196 @@
+/*
+ * Bug 1267910 - Add test cases for the backward compatiability and originAttributes
+ *               of nsICookieManager2.
+ */
+
+var {utils: Cu, interfaces: Ci, classes: Cc} = Components;
+
+Cu.import("resource://gre/modules/Services.jsm");
+
+const BASE_URL = "http://example.org/";
+
+const COOKIE = {
+  host: BASE_URL,
+  path: "/",
+  name: "test1",
+  value: "yes",
+  isSecure: false,
+  isHttpOnly: false,
+  isSession: true,
+  expiry: 2145934800,
+};
+
+const COOKIE_OA_DEFAULT = {
+  host: BASE_URL,
+  path: "/",
+  name: "test0",
+  value: "yes0",
+  isSecure: false,
+  isHttpOnly: false,
+  isSession: true,
+  expiry: 2145934800,
+  originAttributes: {},
+};
+
+const COOKIE_OA_1 = {
+  host: BASE_URL,
+  path: "/",
+  name: "test1",
+  value: "yes1",
+  isSecure: false,
+  isHttpOnly: false,
+  isSession: true,
+  expiry: 2145934800,
+  originAttributes: {userContextId: 1},
+};
+
+function checkCookie(cookie, cookieObj) {
+  for (let prop of Object.keys(cookieObj)) {
+    if (prop === "originAttributes") {
+      ok(ChromeUtils.isOriginAttributesEqual(cookie[prop], cookieObj[prop]),
+        "Check cookie: " + prop);
+    } else {
+      equal(cookie[prop], cookieObj[prop], "Check cookie: " + prop);
+    }
+  }
+}
+
+function countCookies(enumerator) {
+  let cnt = 0;
+
+  while (enumerator.hasMoreElements()) {
+    cnt++;
+    enumerator.getNext();
+  }
+
+  return cnt;
+}
+
+function run_test() {
+  // Allow all cookies.
+  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);
+
+  // Enable user context id
+  Services.prefs.setBoolPref("privacy.userContext.enabled", true);
+
+  add_test(test_backward_compatiability);
+  add_test(test_originAttributes);
+
+
+  run_next_test();
+}
+
+/*
+ * Test for backward compatiablility that APIs works correctly without
+ * originAttributes.
+ */
+function test_backward_compatiability() {
+  // Clear cookies.
+  Services.cookies.removeAll();
+
+  // Call Add() to add a cookie without originAttributes
+  Services.cookies.add(COOKIE.host,
+                       COOKIE.path,
+                       COOKIE.name,
+                       COOKIE.value,
+                       COOKIE.isSecure,
+                       COOKIE.isHttpOnly,
+                       COOKIE.isSession,
+                       COOKIE.expiry);
+
+  // Call getCookiesFromHost() to get cookies without originAttributes
+  let enumerator = Services.cookies.getCookiesFromHost(BASE_URL);
+
+  ok(enumerator.hasMoreElements(), "Cookies available");
+  let foundCookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
+
+  checkCookie(foundCookie, COOKIE);
+
+  ok(!enumerator.hasMoreElements(), "We should get only one cookie");
+
+  run_next_test();
+}
+
+/*
+ * Test for originAttributes.
+ */
+function test_originAttributes() {
+  // Clear cookies.
+  Services.cookies.removeAll();
+
+  // Add a cookie for default originAttributes.
+  Services.cookies.add(COOKIE_OA_DEFAULT.host,
+                       COOKIE_OA_DEFAULT.path,
+                       COOKIE_OA_DEFAULT.name,
+                       COOKIE_OA_DEFAULT.value,
+                       COOKIE_OA_DEFAULT.isSecure,
+                       COOKIE_OA_DEFAULT.isHttpOnly,
+                       COOKIE_OA_DEFAULT.isSession,
+                       COOKIE_OA_DEFAULT.expiry,
+                       COOKIE_OA_DEFAULT.originAttributes);
+
+  // Get cookies for default originAttributes.
+  let enumerator = Services.cookies.getCookiesFromHost(BASE_URL, COOKIE_OA_DEFAULT.originAttributes);
+
+  // Check that do we get cookie correctly.
+  ok(enumerator.hasMoreElements(), "Cookies available");
+  let foundCookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
+  checkCookie(foundCookie, COOKIE_OA_DEFAULT);
+
+  // We should only get one cookie.
+  ok(!enumerator.hasMoreElements(), "We should get only one cookie");
+
+  // Get cookies for originAttributes with user context id 1.
+  enumerator = Services.cookies.getCookiesFromHost(BASE_URL, COOKIE_OA_1.originAttributes);
+
+  // Check that we will not get cookies if the originAttributes is different.
+  ok(!enumerator.hasMoreElements(), "No cookie should be here");
+
+  // Add a cookie for originAttributes with user context id 1.
+  Services.cookies.add(COOKIE_OA_1.host,
+                       COOKIE_OA_1.path,
+                       COOKIE_OA_1.name,
+                       COOKIE_OA_1.value,
+                       COOKIE_OA_1.isSecure,
+                       COOKIE_OA_1.isHttpOnly,
+                       COOKIE_OA_1.isSession,
+                       COOKIE_OA_1.expiry,
+                       COOKIE_OA_1.originAttributes);
+
+  // Get cookies for originAttributes with user context id 1.
+  enumerator = Services.cookies.getCookiesFromHost(BASE_URL, COOKIE_OA_1.originAttributes);
+
+  // Check that do we get cookie correctly.
+  ok(enumerator.hasMoreElements(), "Cookies available");
+  foundCookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
+  checkCookie(foundCookie, COOKIE_OA_1);
+
+  // We should only get one cookie.
+  ok(!enumerator.hasMoreElements(), "We should get only one cookie");
+
+  // Check that add a cookie will not affect cookies in different originAttributes.
+  enumerator = Services.cookies.getCookiesFromHost(BASE_URL, COOKIE_OA_DEFAULT.originAttributes);
+  equal(countCookies(enumerator), 1, "We should get only one cookie for default originAttributes");
+
+  // Remove a cookie for originAttributes with user context id 1.
+  Services.cookies.remove(COOKIE_OA_1.host, COOKIE_OA_1.name, COOKIE_OA_1.path,
+                          false, COOKIE_OA_1.originAttributes);
+
+  // Check that remove will not affect cookies in default originAttributes.
+  enumerator = Services.cookies.getCookiesFromHost(BASE_URL, COOKIE_OA_DEFAULT.originAttributes);
+  equal(countCookies(enumerator), 1, "Get one cookie for default originAttributes.");
+
+  // Check that should be no cookie for originAttributes with user context id 1.
+  enumerator = Services.cookies.getCookiesFromHost(BASE_URL, COOKIE_OA_1.originAttributes);
+  equal(countCookies(enumerator), 0, "No cookie shold be here");
+
+  // Remove a cookie for default originAttributes.
+  Services.cookies.remove(COOKIE_OA_DEFAULT.host, COOKIE_OA_DEFAULT.name, COOKIE_OA_DEFAULT.path,
+                          false, COOKIE_OA_DEFAULT.originAttributes);
+
+  // Check remove() works correctly for default originAttributes.
+  enumerator = Services.cookies.getCookiesFromHost(BASE_URL, COOKIE_OA_DEFAULT.originAttributes);
+  equal(countCookies(enumerator), 0, "No cookie shold be here");
+
+  run_next_test();
+}
--- a/netwerk/cookie/test/unit/xpcshell.ini
+++ b/netwerk/cookie/test/unit/xpcshell.ini
@@ -1,9 +1,10 @@
 [DEFAULT]
 head = 
 tail = 
 skip-if = toolkit == 'gonk'
 
 [test_bug643051.js]
 [test_bug1155169.js]
+[test_bug1267910.js]
 [test_parser_0001.js]
 [test_parser_0019.js]