tor-browser

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

browser_storage_updates.js (7337B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Ensure that storage updates are detected and that the correct information is
      5 // contained inside the storage actors.
      6 
      7 "use strict";
      8 
      9 Services.scriptloader.loadSubScript(
     10  "chrome://mochitests/content/browser/devtools/server/tests/browser/storage-helpers.js",
     11  this
     12 );
     13 
     14 const l10n = new Localization(["devtools/client/storage.ftl"], true);
     15 const sessionString = l10n.formatValueSync("storage-expires-session");
     16 
     17 const TESTS = [
     18  // index 0
     19  {
     20    async action() {
     21      await addCookie("c1", "foobar1");
     22      await addCookie("c2", "foobar2");
     23      await localStorageSetItem("l1", "foobar1");
     24    },
     25    snapshot: {
     26      cookies: [
     27        {
     28          name: "c1",
     29          value: "foobar1",
     30        },
     31        {
     32          name: "c2",
     33          value: "foobar2",
     34        },
     35      ],
     36      "local-storage": [
     37        {
     38          name: "l1",
     39          value: "foobar1",
     40        },
     41      ],
     42    },
     43  },
     44 
     45  // index 1
     46  {
     47    async action() {
     48      await addCookie("c1", "new_foobar1");
     49      await localStorageSetItem("l2", "foobar2");
     50    },
     51    snapshot: {
     52      cookies: [
     53        {
     54          name: "c1",
     55          value: "new_foobar1",
     56        },
     57        {
     58          name: "c2",
     59          value: "foobar2",
     60        },
     61      ],
     62      "local-storage": [
     63        {
     64          name: "l1",
     65          value: "foobar1",
     66        },
     67        {
     68          name: "l2",
     69          value: "foobar2",
     70        },
     71      ],
     72    },
     73  },
     74 
     75  // index 2
     76  {
     77    async action() {
     78      await removeCookie("c2");
     79      await localStorageRemoveItem("l1");
     80      await localStorageSetItem("l3", "foobar3");
     81    },
     82    snapshot: {
     83      cookies: [
     84        {
     85          name: "c1",
     86          value: "new_foobar1",
     87        },
     88      ],
     89      "local-storage": [
     90        {
     91          name: "l2",
     92          value: "foobar2",
     93        },
     94        {
     95          name: "l3",
     96          value: "foobar3",
     97        },
     98      ],
     99    },
    100  },
    101 
    102  // index 3
    103  {
    104    async action() {
    105      await removeCookie("c1");
    106      await addCookie("c3", "foobar3");
    107      await localStorageRemoveItem("l2");
    108      await sessionStorageSetItem("s1", "foobar1");
    109      await sessionStorageSetItem("s2", "foobar2");
    110      await localStorageSetItem("l3", "new_foobar3");
    111    },
    112    snapshot: {
    113      cookies: [
    114        {
    115          name: "c3",
    116          value: "foobar3",
    117        },
    118      ],
    119      "local-storage": [
    120        {
    121          name: "l3",
    122          value: "new_foobar3",
    123        },
    124      ],
    125      "session-storage": [
    126        {
    127          name: "s1",
    128          value: "foobar1",
    129        },
    130        {
    131          name: "s2",
    132          value: "foobar2",
    133        },
    134      ],
    135    },
    136  },
    137 
    138  // index 4
    139  {
    140    async action() {
    141      await sessionStorageRemoveItem("s1");
    142    },
    143    snapshot: {
    144      cookies: [
    145        {
    146          name: "c3",
    147          value: "foobar3",
    148        },
    149      ],
    150      "local-storage": [
    151        {
    152          name: "l3",
    153          value: "new_foobar3",
    154        },
    155      ],
    156      "session-storage": [
    157        {
    158          name: "s2",
    159          value: "foobar2",
    160        },
    161      ],
    162    },
    163  },
    164 
    165  // index 5
    166  {
    167    async action() {
    168      await clearCookies();
    169    },
    170    snapshot: {
    171      cookies: [],
    172      "local-storage": [
    173        {
    174          name: "l3",
    175          value: "new_foobar3",
    176        },
    177      ],
    178      "session-storage": [
    179        {
    180          name: "s2",
    181          value: "foobar2",
    182        },
    183      ],
    184    },
    185  },
    186 
    187  // index 6
    188  {
    189    async action() {
    190      await clearLocalAndSessionStores();
    191    },
    192    snapshot: {
    193      cookies: [],
    194      "local-storage": [],
    195      "session-storage": [],
    196    },
    197  },
    198 ];
    199 
    200 add_task(async function () {
    201  const { commands } = await openTabAndSetupStorage(
    202    MAIN_DOMAIN + "storage-updates.html"
    203  );
    204 
    205  for (let i = 0; i < TESTS.length; i++) {
    206    const test = TESTS[i];
    207    await runTest(test, commands, i);
    208  }
    209 
    210  await commands.destroy();
    211 });
    212 
    213 async function runTest({ action, snapshot }, commands, index) {
    214  info("Running test at index " + index);
    215  await action();
    216  await checkStores(commands, snapshot);
    217 }
    218 
    219 async function checkStores(commands, snapshot) {
    220  const { resourceCommand } = commands;
    221  const { TYPES } = resourceCommand;
    222  const actual = {};
    223  await resourceCommand.watchResources(
    224    [TYPES.COOKIE, TYPES.LOCAL_STORAGE, TYPES.SESSION_STORAGE],
    225    {
    226      async onAvailable(resources) {
    227        for (const resource of resources) {
    228          actual[resource.resourceType] = await resource.getStoreObjects(
    229            TEST_DOMAIN,
    230            null,
    231            {
    232              sessionString,
    233            }
    234          );
    235        }
    236      },
    237    }
    238  );
    239 
    240  for (const [type, entries] of Object.entries(snapshot)) {
    241    const store = actual[type].data;
    242 
    243    is(
    244      store.length,
    245      entries.length,
    246      `The number of entries in ${type} is correct`
    247    );
    248 
    249    for (const entry of entries) {
    250      checkStoreValue(entry.name, entry.value, store);
    251    }
    252  }
    253 }
    254 
    255 function checkStoreValue(name, value, store) {
    256  for (const entry of store) {
    257    if (entry.name === name) {
    258      ok(true, `There is an entry for "${name}"`);
    259 
    260      // entry.value is a longStringActor so we need to read it's value using
    261      // entry.value.str.
    262      is(entry.value.str, value, `Value for ${name} is correct`);
    263      return;
    264    }
    265  }
    266  ok(false, `There is an entry for "${name}"`);
    267 }
    268 
    269 async function addCookie(name, value) {
    270  info(`addCookie("${name}", "${value}")`);
    271 
    272  await SpecialPowers.spawn(
    273    gBrowser.selectedBrowser,
    274    [[name, value]],
    275    ([iName, iValue]) => {
    276      content.wrappedJSObject.window.addCookie(iName, iValue);
    277    }
    278  );
    279 }
    280 
    281 async function removeCookie(name) {
    282  info(`removeCookie("${name}")`);
    283 
    284  await SpecialPowers.spawn(gBrowser.selectedBrowser, [name], iName => {
    285    content.wrappedJSObject.window.removeCookie(iName);
    286  });
    287 }
    288 
    289 async function localStorageSetItem(name, value) {
    290  info(`localStorageSetItem("${name}", "${value}")`);
    291 
    292  await SpecialPowers.spawn(
    293    gBrowser.selectedBrowser,
    294    [[name, value]],
    295    ([iName, iValue]) => {
    296      content.window.localStorage.setItem(iName, iValue);
    297    }
    298  );
    299 }
    300 
    301 async function localStorageRemoveItem(name) {
    302  info(`localStorageRemoveItem("${name}")`);
    303 
    304  await SpecialPowers.spawn(gBrowser.selectedBrowser, [name], iName => {
    305    content.window.localStorage.removeItem(iName);
    306  });
    307 }
    308 
    309 async function sessionStorageSetItem(name, value) {
    310  info(`sessionStorageSetItem("${name}", "${value}")`);
    311 
    312  await SpecialPowers.spawn(
    313    gBrowser.selectedBrowser,
    314    [[name, value]],
    315    ([iName, iValue]) => {
    316      content.window.sessionStorage.setItem(iName, iValue);
    317    }
    318  );
    319 }
    320 
    321 async function sessionStorageRemoveItem(name) {
    322  info(`sessionStorageRemoveItem("${name}")`);
    323 
    324  await SpecialPowers.spawn(gBrowser.selectedBrowser, [name], iName => {
    325    content.window.sessionStorage.removeItem(iName);
    326  });
    327 }
    328 
    329 async function clearCookies() {
    330  info(`clearCookies()`);
    331 
    332  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    333    content.wrappedJSObject.window.clearCookies();
    334  });
    335 }
    336 
    337 async function clearLocalAndSessionStores() {
    338  info(`clearLocalAndSessionStores()`);
    339 
    340  await SpecialPowers.spawn(gBrowser.selectedBrowser, [], () => {
    341    content.wrappedJSObject.window.clearLocalAndSessionStores();
    342  });
    343 }