Bug 1236118 - Complete test coverage for the WebExtension cookies API. r=kmag
authorbsilverberg <bsilverberg@mozilla.com>
Wed, 09 Mar 2016 15:41:17 -0500
changeset 288346 93f0785edcc78554577ed7e5417ecb38bc100bd0
parent 288345 35d1f60e32f3111ef1b4fe4324e8439cd40b581c
child 288347 282a763e8fe7d6a3def65e07bd483da9d9c0ddba
push id73397
push userryanvm@gmail.com
push dateFri, 11 Mar 2016 23:35:33 +0000
treeherdermozilla-inbound@282a763e8fe7 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1236118
milestone48.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 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>