Bug 1238269 - Setting session cookie using chrome.cookies.set fails, r=kmag
authorbsilverberg <bsilverberg@mozilla.com>
Mon, 22 Feb 2016 17:37:48 -0500
changeset 285278 623ba7f8c57acbe8f776a97a0e7c485a49fe93c2
parent 285277 38e5c1ca1eda9d14f49333fd5d262e1559071c56
child 285279 800b8a648489394189bb15777dafb26e7d240ae9
push id30027
push usercbook@mozilla.com
push dateWed, 24 Feb 2016 11:04:26 +0000
treeherderautoland@d848a5628d80 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1238269
milestone47.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 1238269 - Setting session cookie using chrome.cookies.set fails, r=kmag Fix bug to allow session cookies to be set Add tests for session cookies Clean up existing tests MozReview-Commit-ID: 3teHqBd1tw5
toolkit/components/extensions/ext-cookies.js
toolkit/components/extensions/test/mochitest/test_ext_cookies.html
--- a/toolkit/components/extensions/ext-cookies.js
+++ b/toolkit/components/extensions/ext-cookies.js
@@ -272,17 +272,17 @@ extensions.registerSchemaAPI("cookies", 
           path = uri.directory;
         }
 
         let name = details.name !== null ? details.name : "";
         let value = details.value !== null ? details.value : "";
         let secure = details.secure !== null ? details.secure : false;
         let httpOnly = details.httpOnly !== null ? details.httpOnly : false;
         let isSession = details.expirationDate === null;
-        let expiry = isSession ? 0 : details.expirationDate;
+        let expiry = isSession ? Number.MAX_SAFE_INTEGER : details.expirationDate;
         // Ignore storeID.
 
         let cookieAttrs = {host: details.domain, path: path, isSecure: secure};
         if (!checkSetCookiePermissions(extension, uri, cookieAttrs)) {
           return Promise.reject({message: `Permission denied to set cookie ${JSON.stringify(details)}`});
         }
 
         // The permission check may have modified the domain, so use
--- a/toolkit/components/extensions/test/mochitest/test_ext_cookies.html
+++ b/toolkit/components/extensions/test/mochitest/test_ext_cookies.html
@@ -8,81 +8,92 @@
   <script type="text/javascript" src="head.js"></script>
   <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
 </head>
 <body>
 
 <script type="text/javascript">
 "use strict";
 
-function backgroundScript() {
-  function assertExpected(cookie, expected) {
-    for (let key of Object.keys(cookie)) {
-      browser.test.assertTrue(key in expected, "found property " + key);
-      browser.test.assertEq(cookie[key], expected[key], "property value for " + key + " is wrong");
+add_task(function* test_cookies() {
+  function backgroundScript() {
+    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");
     }
-    browser.test.assertEq(Object.keys(cookie).length, Object.keys(expected).length, "all expected properties found");
+
+    let TEST_URL = "http://example.org/";
+    let THE_FUTURE = Date.now() + 5 * 60;
+
+    let expected = {
+      name: "name1",
+      value: "value1",
+      domain: "example.org",
+      hostOnly: true,
+      path: "/",
+      secure: false,
+      httpOnly: false,
+      session: false,
+      expirationDate: THE_FUTURE,
+      storeId: "firefox-default",
+    };
+
+    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({domain: "example.org"});
+    }).then(cookies => {
+      browser.test.assertEq(cookies.length, 1, "only found one cookie for example.org");
+      assertExpected(expected, cookies[0]);
+      return browser.cookies.remove({url: TEST_URL, name: "name1"});
+    }).then(details => {
+      assertExpected({url: TEST_URL, name: "name1", storeId: "firefox-default"}, 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(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);
+      // 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.remove({url: TEST_URL, name: "name1"});
+    }).then(details => {
+      assertExpected({url: TEST_URL, name: "name1", storeId: "firefox-default"}, details);
+      return browser.cookies.get({url: TEST_URL, name: "name1"});
+    }).then(cookie => {
+      browser.test.assertEq(null, cookie, "removed cookie not found");
+      browser.test.notifyPass("cookies");
+    });
   }
 
-  let TEST_URL = "http://example.org/";
-  let THE_FUTURE = Date.now() + 5 * 60;
-
-  let expected = {
-    name: "name1",
-    value: "value1",
-    domain: "example.org",
-    hostOnly: true,
-    path: "/",
-    secure: false,
-    httpOnly: false,
-    session: false,
-    expirationDate: THE_FUTURE,
-    storeId: "firefox-default",
-  };
+  let extension = ExtensionTestUtils.loadExtension({
+    background: `(${backgroundScript})()`,
+    manifest: {
+      permissions: ["cookies", "*://example.org/"],
+    },
+  });
 
-  browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", expirationDate: THE_FUTURE}).then(cookie => {
-    assertExpected(cookie, expected);
-    return browser.cookies.get({url: TEST_URL, name: "name1"});
-  }).then(cookie => {
-    assertExpected(cookie, expected);
-    return browser.cookies.getAll({domain: "example.org"});
-  }).then(cookies => {
-    browser.test.assertEq(cookies.length, 1, "only found one cookie for example.org");
-    assertExpected(cookies[0], expected);
-    return browser.cookies.remove({url: TEST_URL, name: "name1"});
-  }).then(details => {
-    assertExpected(details, {url: TEST_URL, name: "name1", storeId: "firefox-default"});
-    return browser.cookies.get({url: TEST_URL, name: "name1"});
-  }).then(cookie => {
-    browser.test.assertEq(cookie, null);
-    return browser.cookies.getAllCookieStores();
-  }).then(stores => {
-    browser.test.assertEq(stores.length, 1);
-    browser.test.assertEq(stores[0].id, "firefox-default");
-    browser.test.assertEq(stores[0].tabIds.length, 0); // Todo: Implement this.
-    return browser.cookies.set({url: TEST_URL, name: "name2", domain: ".example.org", expirationDate: THE_FUTURE});
-  }).then(cookie => {
-    browser.test.assertEq(cookie.hostOnly, false, "not a hostOnly cookie");
-    return browser.cookies.remove({url: TEST_URL, name: "name2"});
-  }).then(details => {
-    assertExpected(details, {url: TEST_URL, name: "name2", storeId: "firefox-default"});
-  }).then(() => {
-    browser.test.notifyPass("cookies");
-  });
-}
-
-let extensionData = {
-  background: "(" + backgroundScript.toString() + ")()",
-  manifest: {
-    permissions: ["cookies", "*://example.org/"],
-  },
-};
-
-add_task(function* test_cookies() {
-  let extension = ExtensionTestUtils.loadExtension(extensionData);
   yield extension.startup();
   info("extension loaded");
   yield extension.awaitFinish("cookies");
   yield extension.unload();
   info("extension unloaded");
 });
 
 </script>