Bug 1465063 - Add tests for get/getAll w/o firstPartyDomain draft
authorRob Wu <rob@robwu.nl>
Fri, 08 Jun 2018 13:36:36 +0200
changeset 805884 d5ccbff0e13d5d6c6789b7f5fe70c50026cd51e2
parent 805804 d47bb3dbf7d477409905ebd7d6d703cc08caa1b3
child 805885 544c45fb747495d10558b087f006bfe463d0f210
push id112798
push userbmo:rob@robwu.nl
push dateFri, 08 Jun 2018 17:17:58 +0000
bugs1465063
milestone62.0a1
Bug 1465063 - Add tests for get/getAll w/o firstPartyDomain The test coverage for cookies.get was incomplete, as scenarios with empty, null and undefined firstPartyDomain were not checked. There is a distinction between "empty" and "w/o firstPartyDomain". MozReview-Commit-ID: 1l54nUOCneK
toolkit/components/extensions/test/mochitest/test_ext_cookies_first_party.html
--- a/toolkit/components/extensions/test/mochitest/test_ext_cookies_first_party.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_cookies_first_party.html
@@ -9,16 +9,19 @@
 
 async function background() {
   const url = "http://ext-cookie-first-party.mochi.test/";
   const firstPartyDomain = "ext-cookie-first-party.mochi.test";
   const expectedError = "First-Party Isolation is enabled, but the required 'firstPartyDomain' attribute was not set.";
 
   const assertExpectedCookies = (expected, cookies, message) => {
     let matches = (cookie, expected) => {
+      if (!cookie || !expected) {
+        return cookie === expected; // true if both are null.
+      }
       for (let key of Object.keys(expected)) {
         if (cookie[key] !== expected[key]) {
           return false;
         }
       }
       return true;
     };
     browser.test.assertEq(expected.length, cookies.length, `Got expected number of cookies - ${message}`);
@@ -35,37 +38,64 @@ async function background() {
   // Test when FPI is disabled.
   const test_fpi_disabled = async () => {
     let cookie, cookies;
 
     // set
     cookie = await browser.cookies.set({url, name: "foo1", value: "bar1"});
     assertExpectedCookies([
       {name: "foo1", value: "bar1", firstPartyDomain: ""},
-    ], [cookie], "set: FPI off, w/o firstPartyDomain, non-FP cookie");
+    ], [cookie], "set: FPI off, w/ empty firstPartyDomain, non-FP cookie");
     cookie = await browser.cookies.set({url, name: "foo2", value: "bar2", firstPartyDomain});
     assertExpectedCookies([
       {name: "foo2", value: "bar2", firstPartyDomain},
     ], [cookie], "set: FPI off, w/ firstPartyDomain, FP cookie");
 
     // get
+    // When FPI is disabled, missing key/null/undefined is equivalent to "".
     cookie = await browser.cookies.get({url, name: "foo1"});
     assertExpectedCookies([
       {name: "foo1", value: "bar1", firstPartyDomain: ""},
     ], [cookie], "get: FPI off, w/o firstPartyDomain, non-FP cookie");
+    cookie = await browser.cookies.get({url, name: "foo1", firstPartyDomain: ""});
+    assertExpectedCookies([
+      {name: "foo1", value: "bar1", firstPartyDomain: ""},
+    ], [cookie], "get: FPI off, w/ empty firstPartyDomain, non-FP cookie");
+    cookie = await browser.cookies.get({url, name: "foo1", firstPartyDomain: null});
+    assertExpectedCookies([
+      {name: "foo1", value: "bar1", firstPartyDomain: ""},
+    ], [cookie], "get: FPI off, w/ null firstPartyDomain, non-FP cookie");
+    cookie = await browser.cookies.get({url, name: "foo1", firstPartyDomain: undefined});
+    assertExpectedCookies([
+      {name: "foo1", value: "bar1", firstPartyDomain: ""},
+    ], [cookie], "get: FPI off, w/ undefined firstPartyDomain, non-FP cookie");
+
     cookie = await browser.cookies.get({url, name: "foo2", firstPartyDomain});
     assertExpectedCookies([
       {name: "foo2", value: "bar2", firstPartyDomain},
     ], [cookie], "get: FPI off, w/ firstPartyDomain, FP cookie");
+    // There is no match for non-FP cookies with name "foo2".
+    cookie = await browser.cookies.get({url, name: "foo2"});
+    assertExpectedCookies([null], [cookie], "get: FPI off, w/o firstPartyDomain, no cookie");
+    cookie = await browser.cookies.get({url, name: "foo2", firstPartyDomain: ""});
+    assertExpectedCookies([null], [cookie], "get: FPI off, w/ empty firstPartyDomain, no cookie");
+    cookie = await browser.cookies.get({url, name: "foo2", firstPartyDomain: null});
+    assertExpectedCookies([null], [cookie], "get: FPI off, w/ null firstPartyDomain, no cookie");
+    cookie = await browser.cookies.get({url, name: "foo2", firstPartyDomain: undefined});
+    assertExpectedCookies([null], [cookie], "get: FPI off, w/ undefined firstPartyDomain, no cookie");
 
     // getAll
     cookies = await browser.cookies.getAll({});
     assertExpectedCookies([
       {name: "foo1", value: "bar1", firstPartyDomain: ""},
     ], cookies, "getAll: FPI off, w/o firstPartyDomain, non-FP cookies");
+    cookies = await browser.cookies.getAll({firstPartyDomain: ""});
+    assertExpectedCookies([
+      {name: "foo1", value: "bar1", firstPartyDomain: ""},
+    ], cookies, "getAll: FPI off, w/ empty firstPartyDomain, non-FP cookies");
     cookies = await browser.cookies.getAll({firstPartyDomain: null});
     assertExpectedCookies([
       {name: "foo1", value: "bar1", firstPartyDomain: ""},
       {name: "foo2", value: "bar2", firstPartyDomain},
     ], cookies, "getAll: FPI off, w/ null firstPartyDomain, all cookies");
     cookies = await browser.cookies.getAll({firstPartyDomain: undefined});
     assertExpectedCookies([
       {name: "foo1", value: "bar1", firstPartyDomain: ""},
@@ -75,17 +105,17 @@ async function background() {
     assertExpectedCookies([
       {name: "foo2", value: "bar2", firstPartyDomain},
     ], cookies, "getAll: FPI off, w/ firstPartyDomain, FP cookies");
 
     // remove
     cookie = await browser.cookies.remove({url, name: "foo1"});
     assertExpectedCookies([
       {url, name: "foo1", firstPartyDomain: ""},
-    ], [cookie], "remove: FPI off, w/o firstPartyDomain, non-FP cookie");
+    ], [cookie], "remove: FPI off, w/ empty firstPartyDomain, non-FP cookie");
     cookie = await browser.cookies.remove({url, name: "foo2", firstPartyDomain});
     assertExpectedCookies([
       {url, name: "foo2", firstPartyDomain},
     ], [cookie], "remove: FPI off, w/ firstPartyDomain, FP cookie");
 
     // Test if FP cookies set when FPI off can be accessed when FPI on.
     await browser.cookies.set({url, name: "foo1", value: "bar1"});
     await browser.cookies.set({url, name: "foo2", value: "bar2", firstPartyDomain});
@@ -107,36 +137,58 @@ async function background() {
       {name: "foo4", value: "bar4", firstPartyDomain},
     ], [cookie], "set: FPI on, w/ firstPartyDomain, FP cookie");
 
     // get
     await browser.test.assertRejects(
       browser.cookies.get({url, name: "foo3"}),
       expectedError,
       "get: FPI on, w/o firstPartyDomain, rejection");
+    await browser.test.assertRejects(
+      browser.cookies.get({url, name: "foo3", firstPartyDomain: null}),
+      expectedError,
+      "get: FPI on, w/ null firstPartyDomain, rejection");
+    await browser.test.assertRejects(
+      browser.cookies.get({url, name: "foo3", firstPartyDomain: undefined}),
+      expectedError,
+      "get: FPI on, w/ undefined firstPartyDomain, rejection");
+    cookie = await browser.cookies.get({url, name: "foo1", firstPartyDomain: ""});
+    assertExpectedCookies([
+      {name: "foo1", value: "bar1", firstPartyDomain: ""},
+    ], [cookie], "get: FPI on, w/ empty firstPartyDomain, non-FP cookie");
     cookie = await browser.cookies.get({url, name: "foo4", firstPartyDomain});
     assertExpectedCookies([
       {name: "foo4", value: "bar4", firstPartyDomain},
     ], [cookie], "get: FPI on, w/ firstPartyDomain, FP cookie");
     cookie = await browser.cookies.get({url, name: "foo2", firstPartyDomain});
     assertExpectedCookies([
       {name: "foo2", value: "bar2", firstPartyDomain},
     ], [cookie], "get: FPI on, w/ firstPartyDomain, FP cookie (set when FPI off)");
 
     // getAll
     await browser.test.assertRejects(
       browser.cookies.getAll({}),
       expectedError,
       "getAll: FPI on, w/o firstPartyDomain, rejection");
+    cookies = await browser.cookies.getAll({firstPartyDomain: ""});
+    assertExpectedCookies([
+      {name: "foo1", value: "bar1", firstPartyDomain: ""},
+    ], cookies, "getAll: FPI on, w/ empty firstPartyDomain, non-FP cookies");
     cookies = await browser.cookies.getAll({firstPartyDomain: null});
     assertExpectedCookies([
       {name: "foo1", value: "bar1", firstPartyDomain: ""},
       {name: "foo2", value: "bar2", firstPartyDomain},
       {name: "foo4", value: "bar4", firstPartyDomain},
     ], cookies, "getAll: FPI on, w/ null firstPartyDomain, all cookies");
+    cookies = await browser.cookies.getAll({firstPartyDomain: undefined});
+    assertExpectedCookies([
+      {name: "foo1", value: "bar1", firstPartyDomain: ""},
+      {name: "foo2", value: "bar2", firstPartyDomain},
+      {name: "foo4", value: "bar4", firstPartyDomain},
+    ], cookies, "getAll: FPI on, w/ undefined firstPartyDomain, all cookies");
     cookies = await browser.cookies.getAll({firstPartyDomain});
     assertExpectedCookies([
       {name: "foo2", value: "bar2", firstPartyDomain},
       {name: "foo4", value: "bar4", firstPartyDomain},
     ], cookies, "getAll: FPI on, w/ firstPartyDomain, FP cookies");
 
     // remove
     await browser.test.assertRejects(