Bug 1515665 - Tests for SessionStorage and cookie-behavior REJECTED, r=ehsan
authorAndrea Marchesini <amarchesini@mozilla.com>
Wed, 23 Jan 2019 19:19:21 +0100
changeset 515165 6a1c4140f4796d03368e9ab4b8f547209422fdd5
parent 515164 c6b3ea3602511a0cb54c02cdd912584420f705d4
child 515166 309abca54dae664c346136ec2d0be957b39b676c
push id1953
push userffxbld-merge
push dateMon, 11 Mar 2019 12:10:20 +0000
treeherdermozilla-release@9c35dcbaa899 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersehsan
bugs1515665
milestone66.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 1515665 - Tests for SessionStorage and cookie-behavior REJECTED, r=ehsan
toolkit/components/antitracking/test/browser/browser_blockingCookies.js
toolkit/components/antitracking/test/browser/browser_blockingDOMCache.js
toolkit/components/antitracking/test/browser/browser_blockingIndexedDb.js
toolkit/components/antitracking/test/browser/browser_blockingIndexedDbInWorkers.js
toolkit/components/antitracking/test/browser/browser_blockingLocalStorage.js
toolkit/components/antitracking/test/browser/browser_blockingMessaging.js
toolkit/components/antitracking/test/browser/browser_blockingServiceWorkers.js
toolkit/components/antitracking/test/browser/browser_blockingSessionStorage.js
toolkit/components/antitracking/test/browser/browser_blockingSharedWorkers.js
toolkit/components/antitracking/test/browser/browser_partitionedLocalStorage.js
toolkit/components/antitracking/test/browser/browser_siteSpecificWorkArounds.js
toolkit/components/antitracking/test/browser/head.js
--- a/toolkit/components/antitracking/test/browser/browser_blockingCookies.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingCookies.js
@@ -64,17 +64,22 @@ AntiTracking.runTest("Cookies and Storag
 
     is(document.cookie, "", "Still no cookies for me");
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
     is(document.cookie, "", "No cookies for me");
     document.cookie = "name=value";
-    is(document.cookie, "name=value", "I have the cookies!");
+
+    if (SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT) {
+      is(document.cookie, "", "No cookies for me");
+    } else {
+      is(document.cookie, "name=value", "I have the cookies!");
+    }
   },
 
   // Non blocking callback
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
     is(document.cookie, "", "No cookies for me");
--- a/toolkit/components/antitracking/test/browser/browser_blockingDOMCache.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingDOMCache.js
@@ -24,19 +24,21 @@ AntiTracking.runTest("DOM Cache and Stor
 
     await caches.open("wow").then(
       _ => { ok(false, "DOM Cache cannot be used!"); },
       _ => { ok(true, "DOM Cache cannot be used!"); });
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
+    let shouldThrow = SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
+
     await caches.open("wow").then(
-      _ => { ok(true, "DOM Cache can be used!"); },
-      _ => { ok(false, "DOM Cache can be used!"); });
+      _ => { ok(!shouldThrow, "DOM Cache can be used!"); },
+      _ => { ok(shouldThrow, "DOM Cache can be used!"); });
   },
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
     await caches.open("wow").then(
       _ => { ok(true, "DOM Cache can be used!"); },
       _ => { ok(false, "DOM Cache can be used!"); });
--- a/toolkit/components/antitracking/test/browser/browser_blockingIndexedDb.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingIndexedDb.js
@@ -33,18 +33,28 @@ AntiTracking.runTest("IndexedDB and Stor
     } catch (e) {
       ok(true, "IDB should be blocked");
       is(e.name, "SecurityError", "We want a security error message.");
     }
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    indexedDB.open("test", "1");
-    ok(true, "IDB should be allowed");
+    let shouldThrow = SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
+
+    let hasThrown;
+    try {
+      indexedDB.open("test", "1");
+      hasThrown = false;
+    } catch (e) {
+      hasThrown = true;
+      is(e.name, "SecurityError", "We want a security error message.");
+    }
+
+    is(hasThrown, shouldThrow, "IDB should be allowed if not in BEHAVIOR_REJECT");
   },
   // non-blocking callback
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
     indexedDB.open("test", "1");
     ok(true, "IDB should be allowed");
--- a/toolkit/components/antitracking/test/browser/browser_blockingIndexedDbInWorkers.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingIndexedDbInWorkers.js
@@ -94,17 +94,21 @@ AntiTracking.runTest("IndexedDB in worke
           reject();
         }
       };
     });
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    blob = new Blob([nonBlockCode.toString() + "; nonBlockCode();"]);
+    if (SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT) {
+      blob = new Blob([blockCode.toString() + "; blockCode();"]);
+    } else {
+      blob = new Blob([nonBlockCode.toString() + "; nonBlockCode();"]);
+    }
     ok(blob, "Blob has been created");
 
     blobURL = URL.createObjectURL(blob);
     ok(blobURL, "Blob URL has been created");
 
     worker = new Worker(blobURL);
     ok(worker, "Worker has been created");
 
--- a/toolkit/components/antitracking/test/browser/browser_blockingLocalStorage.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingLocalStorage.js
@@ -29,18 +29,28 @@ AntiTracking.runTest("localStorage and S
     } catch (e) {
       ok(true, "LocalStorage cannot be used!");
       is(e.name, "SecurityError", "We want a security error message.");
     }
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    localStorage.foo = 42;
-    ok(true, "LocalStorage is allowed");
+    if (SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT) {
+      try {
+        localStorage.foo = 42;
+        ok(false, "LocalStorage cannot be used!");
+      } catch (e) {
+        ok(true, "LocalStorage cannot be used!");
+        is(e.name, "SecurityError", "We want a security error message.");
+      }
+    } else {
+      localStorage.foo = 42;
+      ok(true, "LocalStorage is allowed");
+    }
   },
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
     localStorage.foo = 42;
     ok(true, "LocalStorage is allowed");
 
--- a/toolkit/components/antitracking/test/browser/browser_blockingMessaging.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingMessaging.js
@@ -90,18 +90,28 @@ AntiTracking.runTest("BroadcastChannel a
     } catch (e) {
       ok(true, "BroadcastChannel cannot be used!");
       is(e.name, "SecurityError", "We want a security error message.");
     }
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    new BroadcastChannel("hello");
-    ok(true, "BroadcastChannel can be used");
+    if (SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT) {
+      try {
+        new BroadcastChannel("hello");
+        ok(false, "BroadcastChannel cannot be used!");
+      } catch (e) {
+        ok(true, "BroadcastChannel cannot be used!");
+        is(e.name, "SecurityError", "We want a security error message.");
+      }
+    } else {
+      new BroadcastChannel("hello");
+      ok(true, "BroadcastChannel can be used");
+    }
   },
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
     new BroadcastChannel("hello");
     ok(true, "BroadcastChanneli can be used");
 
@@ -153,17 +163,22 @@ AntiTracking.runTest("BroadcastChannel i
           reject();
         }
       };
     });
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    blob = new Blob([nonBlockingCode.toString() + "; nonBlockingCode();"]);
+    if (SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT) {
+      blob = new Blob([blockingCode.toString() + "; blockingCode();"]);
+    } else {
+      blob = new Blob([nonBlockingCode.toString() + "; nonBlockingCode();"]);
+    }
+
     ok(blob, "Blob has been created");
 
     blobURL = URL.createObjectURL(blob);
     ok(blobURL, "Blob URL has been created");
 
     worker = new Worker(blobURL);
     ok(worker, "Worker has been created");
 
--- a/toolkit/components/antitracking/test/browser/browser_blockingServiceWorkers.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingServiceWorkers.js
@@ -33,22 +33,29 @@ AntiTracking.runTest("ServiceWorkers and
     await navigator.serviceWorker.register("empty.js").then(
       _ => { ok(false, "ServiceWorker cannot be used!"); },
       _ => { ok(true, "ServiceWorker cannot be used!"); }).
       catch(e => ok(false, "Promise rejected: " + e));
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    await navigator.serviceWorker.register("empty.js").then(
-      reg => { ok(true, "ServiceWorker can be used!"); return reg; },
-      _ => { ok(false, "ServiceWorker cannot be used! " + _); }).then(
-      reg => reg.unregister(),
-      _ => { ok(false, "unregister failed"); }).
-      catch(e => ok(false, "Promise rejected: " + e));
+    if (SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT) {
+      await navigator.serviceWorker.register("empty.js").then(
+        _ => { ok(false, "ServiceWorker cannot be used!"); },
+        _ => { ok(true, "ServiceWorker cannot be used!"); }).
+        catch(e => ok(false, "Promise rejected: " + e));
+    } else {
+      await navigator.serviceWorker.register("empty.js").then(
+        reg => { ok(true, "ServiceWorker can be used!"); return reg; },
+        _ => { ok(false, "ServiceWorker cannot be used! " + _); }).then(
+        reg => reg.unregister(),
+        _ => { ok(false, "unregister failed"); }).
+        catch(e => ok(false, "Promise rejected: " + e));
+    }
   },
   async _ => {
     await SpecialPowers.pushPrefEnv({"set": [
        ["dom.serviceWorkers.exemptFromPerDomainMax", true],
        ["dom.ipc.processCount", 1],
        ["dom.serviceWorkers.enabled", true],
        ["dom.serviceWorkers.testing.enabled", true],
     ]});
--- a/toolkit/components/antitracking/test/browser/browser_blockingSessionStorage.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingSessionStorage.js
@@ -1,12 +1,22 @@
 AntiTracking.runTest("sessionStorage",
   async _ => {
-    sessionStorage.foo = 42;
-    ok(true, "SessionStorage is always allowed");
+    let shouldThrow = SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
+
+    let hasThrown;
+    try {
+      sessionStorage.foo = 42;
+      hasThrown = false;
+    } catch (e) {
+      hasThrown = true;
+      is(e.name, "SecurityError", "We want a security error message.");
+    }
+
+    is(hasThrown, shouldThrow, "SessionStorage show thrown only if cookieBehavior is REJECT");
   },
   async _ => {
     sessionStorage.foo = 42;
     ok(true, "SessionStorage is always allowed");
   },
   async _ => {
     await new Promise(resolve => {
       Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
@@ -17,24 +27,41 @@ AntiTracking.runTest("sessionStorage",
   true,
   0);
 
 AntiTracking.runTest("sessionStorage and Storage Access API",
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
-    sessionStorage.foo = 42;
-    ok(true, "SessionStorage is always allowed");
+    let shouldThrow = SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
+
+    let hasThrown;
+    try {
+      sessionStorage.foo = 42;
+      hasThrown = false;
+    } catch (e) {
+      hasThrown = true;
+      is(e.name, "SecurityError", "We want a security error message.");
+    }
+
+    is(hasThrown, shouldThrow, "SessionStorage show thrown only if cookieBehavior is REJECT");
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    sessionStorage.foo = 42;
-    ok(true, "SessionStorage is allowed after calling the storage access API too");
+    try {
+      sessionStorage.foo = 42;
+      hasThrown = false;
+    } catch (e) {
+      hasThrown = true;
+      is(e.name, "SecurityError", "We want a security error message.");
+    }
+
+    is(hasThrown, shouldThrow, "SessionStorage show thrown only if cookieBehavior is REJECT");
   },
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
     sessionStorage.foo = 42;
     ok(true, "SessionStorage is always allowed");
 
--- a/toolkit/components/antitracking/test/browser/browser_blockingSharedWorkers.js
+++ b/toolkit/components/antitracking/test/browser/browser_blockingSharedWorkers.js
@@ -31,18 +31,28 @@ AntiTracking.runTest("SharedWorkers and 
     } catch (e) {
       ok(true, "SharedWorker cannot be used!");
       is(e.name, "SecurityError", "We want a security error message.");
     }
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    new SharedWorker("a.js", "foo");
-    ok(true, "SharedWorker is allowed");
+    if (SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT) {
+      try {
+        new SharedWorker("a.js", "foo");
+        ok(false, "SharedWorker cannot be used!");
+      } catch (e) {
+        ok(true, "SharedWorker cannot be used!");
+        is(e.name, "SecurityError", "We want a security error message.");
+      }
+    } else {
+      new SharedWorker("a.js", "foo");
+      ok(true, "SharedWorker is allowed");
+    }
   },
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
     new SharedWorker("a.js", "foo");
     ok(true, "SharedWorker is allowed");
 
--- a/toolkit/components/antitracking/test/browser/browser_partitionedLocalStorage.js
+++ b/toolkit/components/antitracking/test/browser/browser_partitionedLocalStorage.js
@@ -1,28 +1,51 @@
 AntiTracking.runTest("localStorage and Storage Access API",
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
-    localStorage.foo = 42;
-    ok(true, "LocalStorage is allowed");
-    is(localStorage.foo, "42", "The value matches");
+    let shouldThrow = SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
 
-    var prevLocalStorage = localStorage;
+    let hasThrown;
+    try {
+      localStorage.foo = 42;
+      ok(true, "LocalStorage is allowed");
+      is(localStorage.foo, "42", "The value matches");
+      hasThrown = false;
+    } catch (e) {
+      is(e.name, "SecurityError", "We want a security error message.");
+      hasThrown = true;
+    }
+
+    is(hasThrown, shouldThrow, "LocalStorage has been exposed correctly");
+
+    let prevLocalStorage;
+    if (!shouldThrow) {
+      prevLocalStorage = localStorage;
+    }
 
     /* import-globals-from storageAccessAPIHelpers.js */
     await callRequestStorageAccess();
 
-    ok(localStorage != prevLocalStorage, "We have a new localStorage");
-    is(localStorage.foo, undefined, "Undefined value after.");
+    if (shouldThrow) {
+      try {
+        is(localStorage.foo, undefined, "Undefined value after.");
+        ok(false, "localStorage should not be available");
+      } catch (e) {
+        ok(true, "localStorage should not be available");
+      }
+    } else {
+      ok(localStorage != prevLocalStorage, "We have a new localStorage");
+      is(localStorage.foo, undefined, "Undefined value after.");
 
-    localStorage.foo = 42;
-    ok(true, "LocalStorage is still allowed");
-    is(localStorage.foo, "42", "The value matches");
+      localStorage.foo = 42;
+      ok(true, "LocalStorage is still allowed");
+      is(localStorage.foo, "42", "The value matches");
+    }
   },
   async _ => {
     /* import-globals-from storageAccessAPIHelpers.js */
     await noStorageAccessInitially();
 
     localStorage.foo = 42;
     ok(true, "LocalStorage is allowed");
     is(localStorage.foo, "42", "The value matches");
--- a/toolkit/components/antitracking/test/browser/browser_siteSpecificWorkArounds.js
+++ b/toolkit/components/antitracking/test/browser/browser_siteSpecificWorkArounds.js
@@ -1,12 +1,21 @@
 AntiTracking.runTest("localStorage with a tracker that is whitelisted via a pref",
   async _ => {
-    localStorage.foo = 42;
-    ok(true, "LocalStorage is allowed");
+    let shouldThrow = SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
+
+    let hasThrown;
+    try {
+      localStorage.foo = 42;
+      hasThrown = false;
+    } catch (e) {
+      hasThrown = true;
+    }
+
+    is(hasThrown, shouldThrow, "LocalStorage is allowed");
   },
   async _ => {
     localStorage.foo = 42;
     ok(true, "LocalStorage is allowed");
   },
   async _ => {
     await new Promise(resolve => {
       Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
@@ -15,18 +24,27 @@ AntiTracking.runTest("localStorage with 
   [["urlclassifier.trackingAnnotationSkipURLs", "tracking.example.org"]],
   false, // run the window.open() test
   false, // run the user interaction test
   0, // don't expect blocking notifications
   false); // run in a normal window
 
 AntiTracking.runTest("localStorage with a tracker that is whitelisted via a fancy pref",
   async _ => {
-    localStorage.foo = 42;
-    ok(true, "LocalStorage is allowed");
+    let shouldThrow = SpecialPowers.Services.prefs.getIntPref("network.cookie.cookieBehavior") == SpecialPowers.Ci.nsICookieService.BEHAVIOR_REJECT;
+
+    let hasThrown;
+    try {
+      localStorage.foo = 42;
+      hasThrown = false;
+    } catch (e) {
+      hasThrown = true;
+    }
+
+    is(hasThrown, shouldThrow, "LocalStorage is allowed");
   },
   async _ => {
     localStorage.foo = 42;
     ok(true, "LocalStorage is allowed");
   },
   async _ => {
     await new Promise(resolve => {
       Services.clearData.deleteData(Ci.nsIClearDataService.CLEAR_ALL, value => resolve());
--- a/toolkit/components/antitracking/test/browser/head.js
+++ b/toolkit/components/antitracking/test/browser/head.js
@@ -22,16 +22,17 @@ const TEST_POPUP_PAGE = TEST_DOMAIN + TE
 const TEST_3RD_PARTY_PAGE = TEST_3RD_PARTY_DOMAIN + TEST_PATH + "3rdParty.html";
 const TEST_3RD_PARTY_PAGE_WO = TEST_3RD_PARTY_DOMAIN + TEST_PATH + "3rdPartyWO.html";
 const TEST_3RD_PARTY_PAGE_UI = TEST_3RD_PARTY_DOMAIN + TEST_PATH + "3rdPartyUI.html";
 const TEST_3RD_PARTY_PAGE_WITH_SVG = TEST_3RD_PARTY_DOMAIN + TEST_PATH + "3rdPartySVG.html";
 const TEST_4TH_PARTY_PAGE = TEST_4TH_PARTY_DOMAIN + TEST_PATH + "3rdParty.html";
 const TEST_ANOTHER_3RD_PARTY_PAGE = TEST_ANOTHER_3RD_PARTY_DOMAIN + TEST_PATH + "3rdParty.html";
 
 const BEHAVIOR_ACCEPT         = Ci.nsICookieService.BEHAVIOR_ACCEPT;
+const BEHAVIOR_REJECT         = Ci.nsICookieService.BEHAVIOR_REJECT;
 const BEHAVIOR_LIMIT_FOREIGN  = Ci.nsICookieService.BEHAVIOR_LIMIT_FOREIGN;
 const BEHAVIOR_REJECT_FOREIGN = Ci.nsICookieService.BEHAVIOR_REJECT_FOREIGN;
 const BEHAVIOR_REJECT_TRACKER = Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER;
 
 var gFeatures = undefined;
 
 let {UrlClassifierTestUtils} = ChromeUtils.import("resource://testing-common/UrlClassifierTestUtils.jsm", {});
 
@@ -153,16 +154,31 @@ this.AntiTracking = {
           iframeSandbox,
           accessRemoval: null, // only passed with non-blocking callback
           callbackAfterRemoval: null,
         });
         this._createCleanupTask(cleanupFunction);
 
         this._createTask({
           name,
+          cookieBehavior: BEHAVIOR_REJECT,
+          blockingByContentBlockingRTUI: true,
+          allowList: false,
+          callback: callbackTracking,
+          extraPrefs: [],
+          expectedBlockingNotifications: 0,
+          runInPrivateWindow,
+          iframeSandbox,
+          accessRemoval: null, // only passed with non-blocking callback
+          callbackAfterRemoval: null,
+        });
+        this._createCleanupTask(cleanupFunction);
+
+        this._createTask({
+          name,
           cookieBehavior: BEHAVIOR_LIMIT_FOREIGN,
           blockingByContentBlockingRTUI: true,
           allowList: true,
           callback: callbackNonTracking,
           extraPrefs: [],
           expectedBlockingNotifications: 0,
           runInPrivateWindow,
           iframeSandbox,