Bug 1493563 - Part 11: Add tests for the new onSecurityChange progresslistener arguments; r=baku
authorEhsan Akhgari <ehsan@mozilla.com>
Sun, 23 Sep 2018 21:31:59 -0400
changeset 494541 cdb8932d1d8daf03d782e837456f768f9ea70fa3
parent 494540 dc92e014bec48af8dd18164b0cb4f8dbdf0e52a1
child 494542 8db78a0b0d4f7b2921f4779306d933168effef1a
push id9984
push userffxbld-merge
push dateMon, 15 Oct 2018 21:07:35 +0000
treeherdermozilla-beta@183d27ea8570 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersbaku
bugs1493563
milestone64.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 1493563 - Part 11: Add tests for the new onSecurityChange progresslistener arguments; r=baku Differential Revision: https://phabricator.services.mozilla.com/D6601
toolkit/components/antitracking/test/browser/browser_onModifyRequestNotificationForTrackingResources.js
toolkit/components/antitracking/test/browser/head.js
--- a/toolkit/components/antitracking/test/browser/browser_onModifyRequestNotificationForTrackingResources.js
+++ b/toolkit/components/antitracking/test/browser/browser_onModifyRequestNotificationForTrackingResources.js
@@ -12,17 +12,17 @@ ChromeUtils.import("resource://gre/modul
 
 let gExpectedResourcesSeen = 0;
 async function onModifyRequest() {
   return new Promise((resolve, reject) => {
     Services.obs.addObserver(function observer(subject, topic, data) {
       let httpChannel = subject.QueryInterface(Ci.nsIHttpChannel);
       let spec = httpChannel.URI.spec;
       info("Observed channel for " + spec);
-      if (httpChannel.URI.prePath + "/" != TEST_3RD_PARTY_DOMAIN_TP) {
+      if (httpChannel.URI.prePath != TEST_3RD_PARTY_DOMAIN_TP) {
         return;
       }
       if (spec.endsWith("empty.js")) {
         ok(true, "Correct resource observed");
         ++gExpectedResourcesSeen;
       } else if (spec.endsWith("empty.js?redirect")) {
         httpChannel.redirectTo(Services.io.newURI(spec.replace("empty.js?redirect", "head.js")));
       } else if (spec.endsWith("empty.js?redirect2")) {
--- a/toolkit/components/antitracking/test/browser/head.js
+++ b/toolkit/components/antitracking/test/browser/head.js
@@ -1,13 +1,13 @@
-const TEST_DOMAIN = "http://example.net/";
-const TEST_3RD_PARTY_DOMAIN = "https://tracking.example.org/";
-const TEST_3RD_PARTY_DOMAIN_TP = "https://tracking.example.com/";
+const TEST_DOMAIN = "http://example.net";
+const TEST_3RD_PARTY_DOMAIN = "https://tracking.example.org";
+const TEST_3RD_PARTY_DOMAIN_TP = "https://tracking.example.com";
 
-const TEST_PATH = "browser/toolkit/components/antitracking/test/browser/";
+const TEST_PATH = "/browser/toolkit/components/antitracking/test/browser/";
 
 const TEST_TOP_PAGE = TEST_DOMAIN + TEST_PATH + "page.html";
 const TEST_EMBEDDER_PAGE = TEST_DOMAIN + TEST_PATH + "embedder.html";
 const TEST_POPUP_PAGE = TEST_DOMAIN + TEST_PATH + "popup.html";
 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";
@@ -15,17 +15,17 @@ const TEST_3RD_PARTY_PAGE_WITH_SVG = TES
 const BEHAVIOR_ACCEPT         = Ci.nsICookieService.BEHAVIOR_ACCEPT;
 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", {});
 
-requestLongerTimeout(3);
+requestLongerTimeout(5);
 
 this.AntiTracking = {
   runTest(name, callbackTracking, callbackNonTracking, cleanupFunction, extraPrefs,
           windowOpenTest = true, userInteractionTest = true, expectedBlockingNotifications = true,
           runInPrivateWindow = false, iframeSandbox = null) {
     // Here we want to test that a 3rd party context is simply blocked.
     this._createTask({
       name,
@@ -536,17 +536,18 @@ this.AntiTracking = {
                                    runInPrivateWindow, iframeSandbox, extraPrefs);
         this._createCleanupTask(cleanupFunction);
       }
 
       // Phase 3: Here we want to test that a third-party context doesn't
       // get blocked with user interaction present
       if (userInteractionTest) {
         this._createUserInteractionTask(name, callbackTracking, callbackNonTracking,
-                                        runInPrivateWindow, iframeSandbox, extraPrefs);
+                                        runInPrivateWindow, iframeSandbox,
+                                        expectedBlockingNotifications, extraPrefs);
         this._createCleanupTask(cleanupFunction);
       }
     }
   },
 
   async _setupTest(win, cookieBehavior, blockingByContentBlocking,
                    blockingByContentBlockingUI, blockingByContentBlockingRTUI,
                    extraPrefs) {
@@ -591,20 +592,75 @@ this.AntiTracking = {
                                     options.blockingByContentBlocking,
                                     options.blockingByContentBlockingUI,
                                     options.blockingByContentBlockingRTUI,
                                     options.extraPrefs);
 
       let cookieBlocked = 0;
       let listener = {
         onSecurityChange(webProgress, request, oldState, state,
-                         status, contentBlockingLogJSON) {
+                         contentBlockingLogJSON) {
           if (state & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER) {
             ++cookieBlocked;
           }
+          let contentBlockingLog = {};
+          try {
+            contentBlockingLog = JSON.parse(contentBlockingLogJSON);
+          } catch (e) {
+          }
+          // If this is the first cookie to be blocked, our state should have
+          // just changed, otherwise it should have previously contained the
+          // STATE_COOKIES_BLOCKED_TRACKER bit too.
+          if (options.expectedBlockingNotifications && cookieBlocked &&
+              !options.allowList) {
+            if (cookieBlocked == 1) {
+              is(oldState & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, 0,
+                 "When blocking the first cookie, old state should not have had the " +
+                 "STATE_COOKIES_BLOCKED_TRACKER bit");
+            }
+
+            let trackerOriginCount = 0;
+            for (let trackerOrigin in contentBlockingLog) {
+              ++trackerOriginCount;
+              is(trackerOrigin, TEST_3RD_PARTY_DOMAIN, "Correct tracker origin must be reported");
+              let originLog = contentBlockingLog[trackerOrigin];
+              if (originLog.length == 1) {
+                is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[0][1], true,
+                   "Correct blocking status reported");
+                ok(originLog[0][2] >= 1,
+                   "Correct repeat count reported");
+              } else {
+                // This branch is needed here because of the tests that use the storage
+                // access API to gain storage access.
+                is(originLog.length, 2, "Correct origin log length");
+                is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[0][1], true,
+                   "Correct blocking status reported");
+                ok(originLog[0][2] >= 1,
+                   "Correct repeat count reported");
+                is(originLog[1][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[1][1], false,
+                   "Correct blocking status reported");
+                is(originLog[1][2], 1,
+                   "Correct repeat count reported");
+              }
+            }
+            is(trackerOriginCount, 1, "Should only report one tracker origin");
+          }
+          if (!options.expectedBlockingNotifications) {
+            is(oldState & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, 0,
+               "When not blocking, old state should not have had the " +
+               "STATE_COOKIES_BLOCKED_TRACKER bit");
+            is(Object.keys(contentBlockingLog).length, 0,
+               "Content blocking log JSON must be empty");
+          }
         },
       };
       win.gBrowser.addProgressListener(listener);
 
       info("Creating a new tab");
       let tab = BrowserTestUtils.addTab(win.gBrowser, TEST_TOP_PAGE);
       win.gBrowser.selectedTab = tab;
 
@@ -698,16 +754,71 @@ this.AntiTracking = {
       let win = window;
       if (runInPrivateWindow) {
         win = OpenBrowserWindow({private: true});
         await TestUtils.topicObserved("browser-delayed-startup-finished");
       }
 
       await AntiTracking._setupTest(win, BEHAVIOR_REJECT_TRACKER, true, true, true, extraPrefs);
 
+      let cookieBlocked = 0;
+      let listener = {
+        onSecurityChange(webProgress, request, oldState, state,
+                         contentBlockingLogJSON) {
+          if (state & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER) {
+            ++cookieBlocked;
+          }
+          let contentBlockingLog = {};
+          try {
+            contentBlockingLog = JSON.parse(contentBlockingLogJSON);
+          } catch (e) {
+          }
+          // If this is the first cookie to be blocked, our state should have
+          // just changed, otherwise it should have previously contained the
+          // STATE_COOKIES_BLOCKED_TRACKER bit too.
+          if (cookieBlocked) {
+            if (cookieBlocked == 1) {
+              is(oldState & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, 0,
+                 "When blocking the first cookie, old state should not have had the " +
+                 "STATE_COOKIES_BLOCKED_TRACKER bit");
+            }
+
+            for (let trackerOrigin in contentBlockingLog) {
+              is(trackerOrigin, TEST_3RD_PARTY_DOMAIN, "Correct tracker origin must be reported");
+              let originLog = contentBlockingLog[trackerOrigin];
+              if (originLog.length == 1) {
+                is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[0][1], true,
+                   "Correct blocking status reported");
+                ok(originLog[0][2] >= 1,
+                   "Correct repeat count reported");
+              } else {
+                is(originLog.length, 2, "Correct origin log length");
+                is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[0][1], true,
+                   "Correct blocking status reported");
+                ok(originLog[0][2] >= 1,
+                   "Correct repeat count reported");
+                is(originLog[1][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[1][1], false,
+                   "Correct blocking status reported");
+                is(originLog[1][2], 1,
+                   "Correct repeat count reported");
+              }
+            }
+            // Can't assert the number of tracker origins because we may get 0
+            // for web progress navigations coming from the window opening...
+          }
+        },
+      };
+      win.gBrowser.addProgressListener(listener);
+
       info("Creating a new tab");
       let tab = BrowserTestUtils.addTab(win.gBrowser, TEST_TOP_PAGE);
       win.gBrowser.selectedTab = tab;
 
       let browser = win.gBrowser.getBrowserForTab(tab);
       await BrowserTestUtils.browserLoaded(browser);
 
       let pageURL = TEST_3RD_PARTY_PAGE_WO;
@@ -753,38 +864,96 @@ this.AntiTracking = {
             ok(false, "Unknown message");
           });
 
           content.document.body.appendChild(ifr);
           ifr.src = obj.page;
         });
       });
 
+      win.gBrowser.removeProgressListener(listener);
+
       info("Removing the tab");
       BrowserTestUtils.removeTab(tab);
 
       if (runInPrivateWindow) {
         win.close();
       }
     });
   },
 
   _createUserInteractionTask(name, blockingCallback, nonBlockingCallback,
-                             runInPrivateWindow, iframeSandbox, extraPrefs) {
+                             runInPrivateWindow, iframeSandbox,
+                             expectedBlockingNotifications, extraPrefs) {
     add_task(async function() {
       info("Starting user-interaction test " + name);
 
       let win = window;
       if (runInPrivateWindow) {
         win = OpenBrowserWindow({private: true});
         await TestUtils.topicObserved("browser-delayed-startup-finished");
       }
 
       await AntiTracking._setupTest(win, BEHAVIOR_REJECT_TRACKER, true, true, true, extraPrefs);
 
+      let cookieBlocked = 0;
+      let listener = {
+        onSecurityChange(webProgress, request, oldState, state,
+                         contentBlockingLogJSON) {
+          if (state & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER) {
+            ++cookieBlocked;
+          }
+          let contentBlockingLog = {};
+          try {
+            contentBlockingLog = JSON.parse(contentBlockingLogJSON);
+          } catch (e) {
+          }
+          // If this is the first cookie to be blocked, our state should have
+          // just changed, otherwise it should have previously contained the
+          // STATE_COOKIES_BLOCKED_TRACKER bit too.
+          if (expectedBlockingNotifications && cookieBlocked) {
+            if (cookieBlocked == 1) {
+              is(oldState & Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER, 0,
+                 "When blocking the first cookie, old state should not have had the " +
+                 "STATE_COOKIES_BLOCKED_TRACKER bit");
+            }
+
+            for (let trackerOrigin in contentBlockingLog) {
+              is(trackerOrigin, TEST_3RD_PARTY_DOMAIN, "Correct tracker origin must be reported");
+              let originLog = contentBlockingLog[trackerOrigin];
+              if (originLog.length == 1) {
+                is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[0][1], true,
+                   "Correct blocking status reported");
+                ok(originLog[0][2] >= 1,
+                   "Correct repeat count reported");
+              } else {
+                is(originLog.length, 2, "Correct origin log length");
+                is(originLog[0][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[0][1], true,
+                   "Correct blocking status reported");
+                ok(originLog[0][2] >= 1,
+                   "Correct repeat count reported");
+                is(originLog[1][0], Ci.nsIWebProgressListener.STATE_COOKIES_BLOCKED_TRACKER,
+                   "Correct blocking type reported");
+                is(originLog[1][1], false,
+                   "Correct blocking status reported");
+                ok(originLog[1][2] >= 1,
+                   "Correct repeat count reported");
+              }
+            }
+            // Can't assert the number of tracker origins because we may get 0
+            // for web progress navigations coming from the window opening...
+          }
+        },
+      };
+      win.gBrowser.addProgressListener(listener);
+
       info("Creating a new tab");
       let tab = BrowserTestUtils.addTab(win.gBrowser, TEST_TOP_PAGE);
       win.gBrowser.selectedTab = tab;
 
       let browser = win.gBrowser.getBrowserForTab(tab);
       await BrowserTestUtils.browserLoaded(browser);
 
       info("Creating a 3rd party content");
@@ -864,16 +1033,18 @@ this.AntiTracking = {
             }
 
             ok(false, "Unknown message");
           });
           ifr.contentWindow.postMessage({ callback: obj.nonBlockingCallback }, "*");
         });
       });
 
+      win.gBrowser.removeProgressListener(listener);
+
       info("Removing the tab");
       BrowserTestUtils.removeTab(tab);
 
       if (runInPrivateWindow) {
         win.close();
       }
     });
   },