Bug 1236118 - Complete test coverage for the WebExtension cookies API, r?kmag draft
authorbsilverberg <bsilverberg@mozilla.com>
Wed, 09 Mar 2016 15:41:17 -0500
changeset 339227 daea8b51f3aa364d2edd29b11ae4f64d54cfa205
parent 337431 5a8c334ac296c94faf804e12a05c3e5c6f555155
child 515939 9226a3c98486b4fad1f50c07eaf8728ee3835e26
push id12672
push userbmo:bob.silverberg@gmail.com
push dateThu, 10 Mar 2016 20:43:46 +0000
reviewerskmag
bugs1236118
milestone47.0a1
Bug 1236118 - Complete test coverage for the WebExtension cookies API, r?kmag Add coverage for: * checkSetCookiePermissions for invalid scheme and failing checks for cross-domain cookies. * |query| - Selecting an enumerator for queries with no |url| or |domain| filter. * |query| - Matching and non-matching cookies for the |domain|, |path|, |secure|, |session|, and |storeId| filters. * |query| - Non-matching cookies for the domain and path portions of the |url| filter. * |cookies.set| calls with an explicit |path| or |httpOnly| value. * |cookies.set| calls without an explicit |name|, |value|, or |expirationDate|. MozReview-Commit-ID: Kq4nGlLFLfw
toolkit/components/extensions/test/mochitest/test_ext_cookies.html
toolkit/components/extensions/test/mochitest/test_ext_cookies_permissions.html
--- a/toolkit/components/extensions/test/mochitest/test_ext_cookies.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_cookies.html
@@ -18,71 +18,156 @@ add_task(function* test_cookies() {
     function assertExpected(expected, cookie) {
       for (let key of Object.keys(cookie)) {
         browser.test.assertTrue(key in expected, `found property ${key}`);
         browser.test.assertEq(expected[key], cookie[key], `property value for ${key} is correct`);
       }
       browser.test.assertEq(Object.keys(expected).length, Object.keys(cookie).length, "all expected properties found");
     }
 
-    let TEST_URL = "http://example.org/";
-    let THE_FUTURE = Date.now() + 5 * 60;
+    const TEST_URL = "http://example.org/";
+    const TEST_SECURE_URL = "https://example.org/";
+    const THE_FUTURE = Date.now() + 5 * 60;
+    const TEST_PATH = "set_path";
+    const TEST_URL_WITH_PATH = TEST_URL + TEST_PATH;
+    const TEST_COOKIE_PATH = `/${TEST_PATH}`;
+    const STORE_ID = "firefox-default";
 
     let expected = {
       name: "name1",
       value: "value1",
       domain: "example.org",
       hostOnly: true,
       path: "/",
       secure: false,
       httpOnly: false,
       session: false,
       expirationDate: THE_FUTURE,
-      storeId: "firefox-default",
+      storeId: STORE_ID,
     };
 
     browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", expirationDate: THE_FUTURE}).then(cookie => {
       assertExpected(expected, cookie);
       return browser.cookies.get({url: TEST_URL, name: "name1"});
     }).then(cookie => {
       assertExpected(expected, cookie);
+      return browser.cookies.getAll({name: "name1"});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 1, "one cookie found for matching name");
+      assertExpected(expected, cookies[0]);
       return browser.cookies.getAll({domain: "example.org"});
     }).then(cookies => {
-      browser.test.assertEq(cookies.length, 1, "only found one cookie for example.org");
+      browser.test.assertEq(cookies.length, 1, "one cookie found for matching domain");
+      assertExpected(expected, cookies[0]);
+      return browser.cookies.getAll({domain: "example.net"});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 0, "no cookies found for non-matching domain");
+      return browser.cookies.getAll({secure: false});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 1, "one non-secure cookie found");
       assertExpected(expected, cookies[0]);
+      return browser.cookies.getAll({secure: true});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 0, "no secure cookies found");
+      return browser.cookies.getAll({storeId: STORE_ID});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 1, "one cookie found for valid storeId");
+      assertExpected(expected, cookies[0]);
+      return browser.cookies.getAll({storeId: "invalid_id"});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 0, "no cookies found for invalid storeId");
       return browser.cookies.remove({url: TEST_URL, name: "name1"});
     }).then(details => {
-      assertExpected({url: TEST_URL, name: "name1", storeId: "firefox-default"}, details);
+      assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
       return browser.cookies.get({url: TEST_URL, name: "name1"});
     }).then(cookie => {
       browser.test.assertEq(null, cookie, "removed cookie not found");
       return browser.cookies.getAllCookieStores();
     }).then(stores => {
       browser.test.assertEq(1, stores.length, "expected number of stores returned");
-      browser.test.assertEq("firefox-default", stores[0].id, "expected store id returned");
+      browser.test.assertEq(STORE_ID, stores[0].id, "expected store id returned");
       browser.test.assertEq(0, stores[0].tabIds.length, "no tabs returned for store"); // Todo: Implement this.
       return browser.cookies.set({url: TEST_URL, name: "name2", domain: ".example.org", expirationDate: THE_FUTURE});
     }).then(cookie => {
       browser.test.assertEq(false, cookie.hostOnly, "cookie is not a hostOnly cookie");
       return browser.cookies.remove({url: TEST_URL, name: "name2"});
     }).then(details => {
-      assertExpected({url: TEST_URL, name: "name2", storeId: "firefox-default"}, details);
+      assertExpected({url: TEST_URL, name: "name2", storeId: STORE_ID}, details);
       // Create a session cookie.
       return browser.cookies.set({url: TEST_URL, name: "name1", value: "value1"});
     }).then(cookie => {
       browser.test.assertEq(true, cookie.session, "session cookie set");
       return browser.cookies.get({url: TEST_URL, name: "name1"});
     }).then(cookie => {
       browser.test.assertEq(true, cookie.session, "got session cookie");
+      return browser.cookies.getAll({session: true});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 1, "one session cookie found");
+      browser.test.assertEq(true, cookies[0].session, "found session cookie");
+      return browser.cookies.getAll({session: false});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 0, "no non-session cookies found");
       return browser.cookies.remove({url: TEST_URL, name: "name1"});
     }).then(details => {
-      assertExpected({url: TEST_URL, name: "name1", storeId: "firefox-default"}, details);
+      assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
       return browser.cookies.get({url: TEST_URL, name: "name1"});
     }).then(cookie => {
       browser.test.assertEq(null, cookie, "removed cookie not found");
+      return browser.cookies.set({url: TEST_SECURE_URL, name: "name1", value: "value1", secure: true});
+    }).then(cookie => {
+      browser.test.assertEq(true, cookie.secure, "secure cookie set");
+      return browser.cookies.get({url: TEST_SECURE_URL, name: "name1"});
+    }).then(cookie => {
+      browser.test.assertEq(true, cookie.session, "got secure cookie");
+      return browser.cookies.getAll({secure: true});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 1, "one secure cookie found");
+      browser.test.assertEq(true, cookies[0].secure, "found secure cookie");
+      return browser.cookies.getAll({secure: false});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 0, "no non-secure cookies found");
+      return browser.cookies.remove({url: TEST_SECURE_URL, name: "name1"});
+    }).then(details => {
+      assertExpected({url: TEST_SECURE_URL, name: "name1", storeId: STORE_ID}, details);
+      return browser.cookies.get({url: TEST_SECURE_URL, name: "name1"});
+    }).then(cookie => {
+      browser.test.assertEq(null, cookie, "removed cookie not found");
+      return browser.cookies.set({url: TEST_URL_WITH_PATH, path: TEST_COOKIE_PATH, name: "name1", value: "value1", expirationDate: THE_FUTURE});
+    }).then(cookie => {
+      browser.test.assertEq(TEST_COOKIE_PATH, cookie.path, "created cookie with path");
+      return browser.cookies.get({url: TEST_URL_WITH_PATH, name: "name1"});
+    }).then(cookie => {
+      browser.test.assertEq(TEST_COOKIE_PATH, cookie.path, "got cookie with path");
+      return browser.cookies.getAll({path: TEST_COOKIE_PATH});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 1, "one cookie with path found");
+      browser.test.assertEq(TEST_COOKIE_PATH, cookies[0].path, "found cookie with path");
+      return browser.cookies.get({url: TEST_URL + "invalid_path", name: "name1"});
+    }).then(cookie => {
+      browser.test.assertEq(null, cookie, "get with invalid path returns null");
+      return browser.cookies.getAll({path: "/invalid_path"});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 0, "getAll with invalid path returns 0 cookies");
+      return browser.cookies.remove({url: TEST_URL_WITH_PATH, name: "name1"});
+    }).then(details => {
+      assertExpected({url: TEST_URL_WITH_PATH, name: "name1", storeId: STORE_ID}, details);
+      return browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", httpOnly: true});
+    }).then(cookie => {
+      browser.test.assertEq(true, cookie.httpOnly, "httpOnly cookie set");
+      return browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", httpOnly: false});
+    }).then(cookie => {
+      browser.test.assertEq(false, cookie.httpOnly, "non-httpOnly cookie set");
+      return browser.cookies.remove({url: TEST_URL, name: "name1"});
+    }).then(details => {
+      assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
+      return browser.cookies.set({url: TEST_URL});
+    }).then(cookie => {
+      browser.test.assertEq("", cookie.name, "default name set");
+      browser.test.assertEq("", cookie.value, "default value set");
+      browser.test.assertEq(true, cookie.session, "no expiry date created session cookie");
       browser.test.notifyPass("cookies");
     });
   }
 
   let extension = ExtensionTestUtils.loadExtension({
     background: `(${backgroundScript})()`,
     manifest: {
       permissions: ["cookies", "*://example.org/"],
--- a/toolkit/components/extensions/test/mochitest/test_ext_cookies_permissions.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_cookies_permissions.html
@@ -295,14 +295,34 @@ add_task(function* test_bad_cookie_permi
   yield testCookies({
     permissions: ["http://example.com/", "cookies"],
     url: "https://example.com/",
     domain: "example.com",
     secure: true,
     shouldPass: false,
     shouldWrite: false,
   });
+
+  info("Test non-matching domain");
+  yield testCookies({
+    permissions: ["http://example.com/", "cookies"],
+    url: "http://example.com/",
+    domain: "example.net",
+    secure: false,
+    shouldPass: false,
+    shouldWrite: false,
+  });
+
+  info("Test invalid scheme");
+  yield testCookies({
+    permissions: ["ftp://example.com/", "cookies"],
+    url: "ftp://example.com/",
+    domain: "example.com",
+    secure: false,
+    shouldPass: false,
+    shouldWrite: false,
+  });
 });
 
 </script>
 
 </body>
 </html>