Bug 1255569 - Add coverage for cookie eviction events for expired cookies. r=kmag
authorbsilverberg <bsilverberg@mozilla.com>
Fri, 01 Apr 2016 10:21:47 -0400
changeset 291278 5e0b6fd97fd6e467899d2073667719c693c818fb
parent 291277 75d0e51772db9f842eb5e1dee98230e774c69b33
child 291279 d9c26f4f5a3be0f1023238ab1d7e3932ba165b0b
push id19656
push usergwagner@mozilla.com
push dateMon, 04 Apr 2016 13:43:23 +0000
treeherderb2g-inbound@e99061fde28a [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskmag
bugs1255569
milestone48.0a1
Bug 1255569 - Add coverage for cookie eviction events for expired cookies. r=kmag Fix bug in ext-cookies.js that was reporting expired cookies as evicted Only run test in non-e10s MozReview-Commit-ID: H2o5GnVtTzi
toolkit/components/extensions/ext-cookies.js
toolkit/components/extensions/test/mochitest/mochitest.ini
toolkit/components/extensions/test/mochitest/test_ext_cookies_expiry.html
--- a/toolkit/components/extensions/ext-cookies.js
+++ b/toolkit/components/extensions/ext-cookies.js
@@ -333,17 +333,17 @@ extensions.registerSchemaAPI("cookies", 
             case "changed":
               notify(true, subject, "overwrite");
               notify(false, subject, "explicit");
               break;
             case "batch-deleted":
               subject.QueryInterface(Ci.nsIArray);
               for (let i = 0; i < subject.length; i++) {
                 let cookie = subject.queryElementAt(i, Ci.nsICookie2);
-                if (!cookie.isSession && (cookie.expiry + 1) * 1000 <= Date.now()) {
+                if (!cookie.isSession && cookie.expiry * 1000 <= Date.now()) {
                   notify(true, cookie, "expired");
                 } else {
                   notify(true, cookie, "evicted");
                 }
               }
               break;
           }
         };
--- a/toolkit/components/extensions/test/mochitest/mochitest.ini
+++ b/toolkit/components/extensions/test/mochitest/mochitest.ini
@@ -57,16 +57,18 @@ skip-if = (os == 'android' || buildapp =
 skip-if = (os == 'android' || buildapp == 'b2g') # sender.tab is undefined on b2g. Bug 1258975 on android.
 [test_ext_sendmessage_doublereply.html]
 skip-if = (os == 'android' || buildapp == 'b2g') # sender.tab is undefined on b2g. Bug 1258975 on android.
 [test_ext_storage.html]
 [test_ext_storage_tab.html]
 skip-if = os == 'android' # Android does not currently support tabs.
 [test_ext_background_runtime_connect_params.html]
 [test_ext_cookies.html]
+[test_ext_cookies_expiry.html]
+skip-if = e10s || buildapp == 'b2g' # Uses cookie service via SpecialPowers.Services, which does not support e10s.
 [test_ext_cookies_permissions.html]
 skip-if = e10s || buildapp == 'b2g' # Uses cookie service via SpecialPowers.Services, which does not support e10s.
 [test_ext_bookmarks.html]
 skip-if = (os == 'android' || buildapp == 'b2g') # unimplemented api. Bug 1258975 on android.
 [test_ext_alarms.html]
 [test_ext_background_window_properties.html]
 [test_ext_background_sub_windows.html]
 [test_ext_background_api_injection.html]
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/mochitest/test_ext_cookies_expiry.html
@@ -0,0 +1,71 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+  <title>WebExtension test</title>
+  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
+  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
+  <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";
+
+add_task(function* test_cookies_expiry() {
+  function background() {
+    let expectedEvents = [];
+
+    browser.cookies.onChanged.addListener(event => {
+      expectedEvents.push(`${event.removed}:${event.cause}`);
+      if (expectedEvents.length === 1) {
+        browser.test.assertEq("true:expired", expectedEvents[0], "expired cookie removed");
+        browser.test.assertEq("first", event.cookie.name, "expired cookie has the expected name");
+        browser.test.assertEq("one", event.cookie.value, "expired cookie has the expected value");
+      } else {
+        browser.test.assertEq("false:explicit", expectedEvents[1], "new cookie added");
+        browser.test.assertEq("first", event.cookie.name, "new cookie has the expected name");
+        browser.test.assertEq("one-again", event.cookie.value, "new cookie has the expected value");
+        browser.test.notifyPass("cookie-expiry");
+      }
+    });
+
+    setTimeout(() => {
+      browser.test.sendMessage("change-cookies");
+    }, 1000);
+  }
+
+  let domain = ".example.com";
+  let extension = ExtensionTestUtils.loadExtension({
+    manifest: {
+      "permissions": ["http://example.com/", "cookies"],
+    },
+    background: `(${background})()`,
+  });
+
+  let cookieSvc = SpecialPowers.Services.cookies;
+
+  let cookie = {
+    host: domain,
+    name: "first",
+    path: "/",
+  };
+
+  do {
+    cookieSvc.add(cookie.host, cookie.path, cookie.name, "one", false, false, false, Date.now() / 1000 + 1);
+  } while (!cookieSvc.cookieExists(cookie));
+
+  yield extension.startup();
+  yield extension.awaitMessage("change-cookies");
+
+  cookieSvc.add(cookie.host, cookie.path, cookie.name, "one-again", false, false, false, Date.now() / 1000 + 10);
+
+  yield extension.awaitFinish("cookie-expiry");
+  yield extension.unload();
+});
+
+</script>
+
+</body>
+</html>