tor-browser

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

test_SiteDataManagerContainers.js (3917B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/
      3 */
      4 "use strict";
      5 
      6 const { SiteDataManager } = ChromeUtils.importESModule(
      7  "resource:///modules/SiteDataManager.sys.mjs"
      8 );
      9 const { SiteDataTestUtils } = ChromeUtils.importESModule(
     10  "resource://testing-common/SiteDataTestUtils.sys.mjs"
     11 );
     12 
     13 const EXAMPLE_ORIGIN = "https://www.example.com";
     14 const EXAMPLE_ORIGIN_2 = "https://example.org";
     15 
     16 add_task(function setup() {
     17  do_get_profile();
     18 });
     19 
     20 add_task(async function testGetSitesByContainers() {
     21  SiteDataTestUtils.addToCookies({
     22    origin: EXAMPLE_ORIGIN,
     23    name: "foo1",
     24    value: "bar1",
     25    originAttributes: { userContextId: "1" },
     26  });
     27  SiteDataTestUtils.addToCookies({
     28    origin: EXAMPLE_ORIGIN,
     29    name: "foo2",
     30    value: "bar2",
     31    originAttributes: { userContextId: "2" },
     32  });
     33  SiteDataTestUtils.addToCookies({
     34    origin: EXAMPLE_ORIGIN,
     35    name: "foo3",
     36    value: "bar3",
     37    originAttributes: { userContextId: "2" },
     38  });
     39  SiteDataTestUtils.addToCookies({
     40    origin: EXAMPLE_ORIGIN_2,
     41    name: "foo",
     42    value: "bar",
     43    originAttributes: { userContextId: "3" },
     44  });
     45 
     46  await SiteDataTestUtils.addToIndexedDB(
     47    EXAMPLE_ORIGIN + "^userContextId=1",
     48    4096
     49  );
     50  await SiteDataTestUtils.addToIndexedDB(
     51    EXAMPLE_ORIGIN_2 + "^userContextId=3",
     52    2048
     53  );
     54 
     55  await SiteDataManager.updateSites();
     56 
     57  let sites = await SiteDataManager.getSites();
     58 
     59  let site1Container1 = sites
     60    .find(site => site.baseDomain == "example.com")
     61    .containersData.get(1);
     62 
     63  let site1Container2 = sites
     64    .find(site => site.baseDomain == "example.com")
     65    .containersData.get(2);
     66 
     67  let site2Container3 = sites
     68    .find(site => site.baseDomain == "example.org")
     69    .containersData.get(3);
     70 
     71  Assert.equal(
     72    sites.reduce(
     73      (accumulator, site) => accumulator + site.containersData.size,
     74      0
     75    ),
     76    3,
     77    "Has the correct number of sites by containers"
     78  );
     79 
     80  Assert.equal(
     81    site1Container1.cookiesBlocked,
     82    1,
     83    "Has the correct number of cookiesBlocked by containers"
     84  );
     85 
     86  Assert.greater(
     87    site1Container1.quotaUsage,
     88    4096,
     89    "Has correct usage for example.com^userContextId=1"
     90  );
     91 
     92  Assert.equal(
     93    typeof site1Container1.lastAccessed.getDate,
     94    "function",
     95    "lastAccessed for example.com^userContextId=1 is a Date"
     96  );
     97  // Note: this is comparing a date and a number, and Assert.greater requires
     98  // inputs to be the same type. bug 1973910 covers fixing this.
     99  // eslint-disable-next-line mozilla/no-comparison-or-assignment-inside-ok
    100  Assert.ok(
    101    site1Container1.lastAccessed > Date.now() - 60 * 1000,
    102    "lastAccessed for example.com^userContextId=1 happened recently"
    103  );
    104 
    105  Assert.equal(
    106    site1Container2.cookiesBlocked,
    107    2,
    108    "Has the correct number of cookiesBlocked by containers"
    109  );
    110 
    111  Assert.equal(
    112    site1Container2.quotaUsage,
    113    0,
    114    "Has correct usage for example.org^userContextId=2"
    115  );
    116 
    117  Assert.equal(
    118    typeof site1Container2.lastAccessed.getDate,
    119    "function",
    120    "lastAccessed for example.com^userContextId=2 is a Date"
    121  );
    122 
    123  Assert.equal(
    124    site2Container3.cookiesBlocked,
    125    1,
    126    "Has the correct number of cookiesBlocked by containers"
    127  );
    128 
    129  Assert.greater(
    130    site2Container3.quotaUsage,
    131    2048,
    132    "Has correct usage for example.org^userContextId=3"
    133  );
    134 
    135  Assert.equal(
    136    typeof site2Container3.lastAccessed.getDate,
    137    "function",
    138    "lastAccessed for example.org^userContextId=3 is a Date"
    139  );
    140  // Note: this is comparing a date and a number, and Assert.greater requires
    141  // inputs to be the same type. bug 1973910 covers fixing this.
    142  // eslint-disable-next-line mozilla/no-comparison-or-assignment-inside-ok
    143  Assert.ok(
    144    site2Container3.lastAccessed > Date.now() - 60 * 1000,
    145    "lastAccessed for example.org^userContextId=3 happened recently"
    146  );
    147 
    148  await SiteDataTestUtils.clear();
    149 });