tor-browser

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

browser_restore_cookies_noOriginAttributes.js (5354B)


      1 /*
      2 * Bug 1267910 - The regression test case for session cookies.
      3 */
      4 
      5 "use strict";
      6 
      7 const TEST_HOST = "www.example.com";
      8 const COOKIE = {
      9  name: "test1",
     10  value: "yes1",
     11  path: "/browser/browser/components/sessionstore/test/",
     12  sameSite: Ci.nsICookie.SAMESITE_UNSET,
     13 };
     14 const SESSION_DATA = JSON.stringify({
     15  version: ["sessionrestore", 1],
     16  windows: [
     17    {
     18      tabs: [
     19        {
     20          entries: [],
     21          lastAccessed: 1463893009797,
     22          hidden: false,
     23          attributes: {},
     24          image: null,
     25        },
     26        {
     27          entries: [
     28            {
     29              url: "http://www.example.com/browser/browser/components/sessionstore/test/browser_1267910_page.html",
     30              triggeringPrincipal_base64,
     31              charset: "UTF-8",
     32              ID: 0,
     33              docshellID: 2,
     34              originalURI:
     35                "http://www.example.com/browser/browser/components/sessionstore/test/browser_1267910_page.html",
     36              docIdentifier: 0,
     37              persist: true,
     38            },
     39          ],
     40          lastAccessed: 1463893009321,
     41          hidden: false,
     42          attributes: {},
     43          userContextId: 0,
     44          index: 1,
     45          image: "http://www.example.com/favicon.ico",
     46        },
     47      ],
     48      selected: 1,
     49      _closedTabs: [],
     50      busy: false,
     51      width: 1024,
     52      height: 768,
     53      screenX: 4,
     54      screenY: 23,
     55      sizemode: "normal",
     56      cookies: [
     57        {
     58          host: "www.example.com",
     59          value: "yes1",
     60          path: "/browser/browser/components/sessionstore/test/",
     61          name: "test1",
     62          sameSite: Ci.nsICookie.SAMESITE_UNSET,
     63        },
     64      ],
     65    },
     66  ],
     67  selectedWindow: 1,
     68  _closedWindows: [],
     69  session: {
     70    lastUpdate: 1463893009801,
     71    startTime: 1463893007134,
     72    recentCrashes: 0,
     73  },
     74  global: {},
     75 });
     76 
     77 const SESSION_DATA_OA = JSON.stringify({
     78  version: ["sessionrestore", 1],
     79  windows: [
     80    {
     81      tabs: [
     82        {
     83          entries: [],
     84          lastAccessed: 1463893009797,
     85          hidden: false,
     86          attributes: {},
     87          image: null,
     88        },
     89        {
     90          entries: [
     91            {
     92              url: "http://www.example.com/browser/browser/components/sessionstore/test/browser_1267910_page.html",
     93              triggeringPrincipal_base64,
     94              charset: "UTF-8",
     95              ID: 0,
     96              docshellID: 2,
     97              originalURI:
     98                "http://www.example.com/browser/browser/components/sessionstore/test/browser_1267910_page.html",
     99              docIdentifier: 0,
    100              persist: true,
    101            },
    102          ],
    103          lastAccessed: 1463893009321,
    104          hidden: false,
    105          attributes: {},
    106          userContextId: 0,
    107          index: 1,
    108          image: "http://www.example.com/favicon.ico",
    109        },
    110      ],
    111      selected: 1,
    112      _closedTabs: [],
    113      busy: false,
    114      width: 1024,
    115      height: 768,
    116      screenX: 4,
    117      screenY: 23,
    118      sizemode: "normal",
    119      cookies: [
    120        {
    121          host: "www.example.com",
    122          value: "yes1",
    123          path: "/browser/browser/components/sessionstore/test/",
    124          name: "test1",
    125          sameSite: Ci.nsICookie.SAMESITE_UNSET,
    126          originAttributes: {
    127            addonId: "",
    128            inIsolatedMozBrowser: false,
    129            userContextId: 0,
    130          },
    131        },
    132      ],
    133    },
    134  ],
    135  selectedWindow: 1,
    136  _closedWindows: [],
    137  session: {
    138    lastUpdate: 1463893009801,
    139    startTime: 1463893007134,
    140    recentCrashes: 0,
    141  },
    142  global: {},
    143 });
    144 
    145 add_task(async function run_test() {
    146  // Wait until initialization is complete.
    147  await SessionStore.promiseInitialized;
    148 
    149  // Clear cookies.
    150  Services.cookies.removeAll();
    151 
    152  // Open a new window.
    153  let win = await promiseNewWindowLoaded();
    154 
    155  // Restore window with session cookies that have no originAttributes.
    156  await setWindowState(win, SESSION_DATA, true);
    157 
    158  let cookieCount = 0;
    159  for (var cookie of Services.cookies.getCookiesFromHost(TEST_HOST, {})) {
    160    cookieCount++;
    161  }
    162 
    163  // Check that the cookie is restored successfully.
    164  is(cookieCount, 1, "expected one cookie");
    165  is(cookie.name, COOKIE.name, "cookie name successfully restored");
    166  is(cookie.value, COOKIE.value, "cookie value successfully restored");
    167  is(cookie.path, COOKIE.path, "cookie path successfully restored");
    168 
    169  // Clear cookies.
    170  Services.cookies.removeAll();
    171 
    172  // In real usage, the event loop would get to spin between setWindowState
    173  // uses.  Without a spin, we can defer handling the STATE_STOP that
    174  // removes the progress listener until after the mozbrowser has been
    175  // destroyed, causing a window leak.
    176  await new Promise(resolve => win.setTimeout(resolve, 0));
    177 
    178  // Restore window with session cookies that have originAttributes within.
    179  await setWindowState(win, SESSION_DATA_OA, true);
    180 
    181  cookieCount = 0;
    182  for (cookie of Services.cookies.getCookiesFromHost(TEST_HOST, {})) {
    183    cookieCount++;
    184  }
    185 
    186  // Check that the cookie is restored successfully.
    187  is(cookieCount, 1, "expected one cookie");
    188  is(cookie.name, COOKIE.name, "cookie name successfully restored");
    189  is(cookie.value, COOKIE.value, "cookie value successfully restored");
    190  is(cookie.path, COOKIE.path, "cookie path successfully restored");
    191 
    192  // Close our window.
    193  await BrowserTestUtils.closeWindow(win);
    194 });