tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_protectionsUI_icon_state.js (7930B)


      1 /* eslint-disable mozilla/no-arbitrary-setTimeout */
      2 /*
      3 * Test that the Content Blocking icon state is properly updated in the identity
      4 * block when loading tabs and switching between tabs.
      5 * See also Bug 1175858.
      6 */
      7 
      8 const TP_PREF = "privacy.trackingprotection.enabled";
      9 const TP_PB_PREF = "privacy.trackingprotection.pbmode.enabled";
     10 const NCB_PREF = "network.cookie.cookieBehavior";
     11 const BENIGN_PAGE =
     12  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     13  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/benignPage.html";
     14 const TRACKING_PAGE =
     15  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     16  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/trackingPage.html";
     17 const COOKIE_PAGE =
     18  // eslint-disable-next-line @microsoft/sdl/no-insecure-url
     19  "http://tracking.example.org/browser/browser/base/content/test/protectionsUI/cookiePage.html";
     20 
     21 registerCleanupFunction(function () {
     22  UrlClassifierTestUtils.cleanupTestTrackers();
     23  Services.prefs.clearUserPref(TP_PREF);
     24  Services.prefs.clearUserPref(TP_PB_PREF);
     25  Services.prefs.clearUserPref(NCB_PREF);
     26 });
     27 
     28 async function testTrackingProtectionIconState(tabbrowser) {
     29  info("Load a test page not containing tracking elements");
     30  let benignTab = await BrowserTestUtils.openNewForegroundTab(
     31    tabbrowser,
     32    BENIGN_PAGE
     33  );
     34  let gProtectionsHandler = tabbrowser.ownerGlobal.gProtectionsHandler;
     35 
     36  ok(!gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox not active");
     37 
     38  info("Load a test page containing tracking elements");
     39  let trackingTab = await BrowserTestUtils.openNewForegroundTab(
     40    tabbrowser,
     41    TRACKING_PAGE
     42  );
     43 
     44  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
     45 
     46  info("Load a test page containing tracking cookies");
     47  let trackingCookiesTab = await BrowserTestUtils.openNewForegroundTab(
     48    tabbrowser,
     49    COOKIE_PAGE
     50  );
     51 
     52  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
     53 
     54  info("Switch from tracking cookie -> benign tab");
     55  let securityChanged = waitForSecurityChange(1, tabbrowser.ownerGlobal);
     56  tabbrowser.selectedTab = benignTab;
     57  await securityChanged;
     58 
     59  ok(!gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox not active");
     60 
     61  info("Switch from benign -> tracking tab");
     62  securityChanged = waitForSecurityChange(1, tabbrowser.ownerGlobal);
     63  tabbrowser.selectedTab = trackingTab;
     64  await securityChanged;
     65 
     66  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
     67 
     68  info("Switch from tracking -> tracking cookies tab");
     69  securityChanged = waitForSecurityChange(1, tabbrowser.ownerGlobal);
     70  tabbrowser.selectedTab = trackingCookiesTab;
     71  await securityChanged;
     72 
     73  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
     74 
     75  info("Reload tracking cookies tab");
     76  securityChanged = waitForSecurityChange(1, tabbrowser.ownerGlobal);
     77  let contentBlockingEvent = waitForContentBlockingEvent(
     78    2,
     79    tabbrowser.ownerGlobal
     80  );
     81  tabbrowser.reload();
     82  await Promise.all([securityChanged, contentBlockingEvent]);
     83 
     84  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
     85 
     86  info("Reload tracking tab");
     87  securityChanged = waitForSecurityChange(2, tabbrowser.ownerGlobal);
     88  contentBlockingEvent = waitForContentBlockingEvent(3, tabbrowser.ownerGlobal);
     89  tabbrowser.selectedTab = trackingTab;
     90  tabbrowser.reload();
     91  await Promise.all([securityChanged, contentBlockingEvent]);
     92 
     93  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
     94 
     95  info("Inject tracking cookie inside tracking tab");
     96  securityChanged = waitForSecurityChange(1, tabbrowser.ownerGlobal);
     97  let timeoutPromise = new Promise(resolve => setTimeout(resolve, 500));
     98  await SpecialPowers.spawn(tabbrowser.selectedBrowser, [], function () {
     99    content.postMessage("cookie", "*");
    100  });
    101  let result = await Promise.race([securityChanged, timeoutPromise]);
    102  is(result, undefined, "No securityChange events should be received");
    103 
    104  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
    105 
    106  info("Inject tracking element inside tracking tab");
    107  securityChanged = waitForSecurityChange(1, tabbrowser.ownerGlobal);
    108  timeoutPromise = new Promise(resolve => setTimeout(resolve, 500));
    109  await SpecialPowers.spawn(tabbrowser.selectedBrowser, [], function () {
    110    content.postMessage("tracking", "*");
    111  });
    112  result = await Promise.race([securityChanged, timeoutPromise]);
    113  is(result, undefined, "No securityChange events should be received");
    114 
    115  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
    116 
    117  tabbrowser.selectedTab = trackingCookiesTab;
    118 
    119  info("Inject tracking cookie inside tracking cookies tab");
    120  securityChanged = waitForSecurityChange(1, tabbrowser.ownerGlobal);
    121  timeoutPromise = new Promise(resolve => setTimeout(resolve, 500));
    122  await SpecialPowers.spawn(tabbrowser.selectedBrowser, [], function () {
    123    content.postMessage("cookie", "*");
    124  });
    125  result = await Promise.race([securityChanged, timeoutPromise]);
    126  is(result, undefined, "No securityChange events should be received");
    127 
    128  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
    129 
    130  info("Inject tracking element inside tracking cookies tab");
    131  securityChanged = waitForSecurityChange(1, tabbrowser.ownerGlobal);
    132  timeoutPromise = new Promise(resolve => setTimeout(resolve, 500));
    133  await SpecialPowers.spawn(tabbrowser.selectedBrowser, [], function () {
    134    content.postMessage("tracking", "*");
    135  });
    136  result = await Promise.race([securityChanged, timeoutPromise]);
    137  is(result, undefined, "No securityChange events should be received");
    138 
    139  ok(gProtectionsHandler.iconBox.hasAttribute("active"), "iconBox active");
    140 
    141  while (tabbrowser.tabs.length > 1) {
    142    tabbrowser.removeCurrentTab();
    143  }
    144 }
    145 
    146 add_task(async function testNormalBrowsing() {
    147  await UrlClassifierTestUtils.addTestTrackers();
    148 
    149  let gProtectionsHandler = gBrowser.ownerGlobal.gProtectionsHandler;
    150  ok(
    151    gProtectionsHandler,
    152    "gProtectionsHandler is attached to the browser window"
    153  );
    154 
    155  let { TrackingProtection } =
    156    gBrowser.ownerGlobal.gProtectionsHandler.blockers;
    157  ok(TrackingProtection, "TP is attached to the browser window");
    158 
    159  let { ThirdPartyCookies } = gBrowser.ownerGlobal.gProtectionsHandler.blockers;
    160  ok(ThirdPartyCookies, "TPC is attached to the browser window");
    161 
    162  Services.prefs.setBoolPref(TP_PREF, true);
    163  ok(TrackingProtection.enabled, "TP is enabled after setting the pref");
    164  Services.prefs.setIntPref(
    165    NCB_PREF,
    166    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
    167  );
    168  ok(
    169    ThirdPartyCookies.enabled,
    170    "ThirdPartyCookies is enabled after setting the pref"
    171  );
    172 
    173  await testTrackingProtectionIconState(gBrowser);
    174 });
    175 
    176 add_task(async function testPrivateBrowsing() {
    177  await SpecialPowers.pushPrefEnv({
    178    set: [["dom.security.https_first_pbm", false]],
    179  });
    180 
    181  let privateWin = await BrowserTestUtils.openNewBrowserWindow({
    182    private: true,
    183  });
    184  let tabbrowser = privateWin.gBrowser;
    185 
    186  let gProtectionsHandler = tabbrowser.ownerGlobal.gProtectionsHandler;
    187  ok(
    188    gProtectionsHandler,
    189    "gProtectionsHandler is attached to the private window"
    190  );
    191  let { TrackingProtection } =
    192    tabbrowser.ownerGlobal.gProtectionsHandler.blockers;
    193  ok(TrackingProtection, "TP is attached to the private window");
    194  let { ThirdPartyCookies } =
    195    tabbrowser.ownerGlobal.gProtectionsHandler.blockers;
    196  ok(ThirdPartyCookies, "TPC is attached to the browser window");
    197 
    198  Services.prefs.setBoolPref(TP_PB_PREF, true);
    199  ok(TrackingProtection.enabled, "TP is enabled after setting the pref");
    200  Services.prefs.setIntPref(
    201    NCB_PREF,
    202    Ci.nsICookieService.BEHAVIOR_REJECT_TRACKER
    203  );
    204  ok(
    205    ThirdPartyCookies.enabled,
    206    "ThirdPartyCookies is enabled after setting the pref"
    207  );
    208 
    209  await testTrackingProtectionIconState(tabbrowser);
    210 
    211  privateWin.close();
    212 });