summaryrefslogtreecommitdiffstats
path: root/browser/components/sessionstore/test/browser_restore_cookies_noOriginAttributes.js
blob: bdfdf7fbe33c7b136f09d665151c131dd1294aa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Bug 1267910 - The regression test case for session cookies.
 */

"use strict";

const TEST_HOST = "www.example.com";
const COOKIE = {
  name: "test1",
  value: "yes1",
  path: "/browser/browser/components/sessionstore/test/",
};
const SESSION_DATA = JSON.stringify({
  version: ["sessionrestore", 1],
  windows: [
    {
      tabs: [
        {
          entries: [],
          lastAccessed: 1463893009797,
          hidden: false,
          attributes: {},
          image: null,
        },
        {
          entries: [
            {
              url: "http://www.example.com/browser/browser/components/sessionstore/test/browser_1267910_page.html",
              triggeringPrincipal_base64,
              charset: "UTF-8",
              ID: 0,
              docshellID: 2,
              originalURI:
                "http://www.example.com/browser/browser/components/sessionstore/test/browser_1267910_page.html",
              docIdentifier: 0,
              persist: true,
            },
          ],
          lastAccessed: 1463893009321,
          hidden: false,
          attributes: {},
          userContextId: 0,
          index: 1,
          image: "http://www.example.com/favicon.ico",
        },
      ],
      selected: 1,
      _closedTabs: [],
      busy: false,
      width: 1024,
      height: 768,
      screenX: 4,
      screenY: 23,
      sizemode: "normal",
      cookies: [
        {
          host: "www.example.com",
          value: "yes1",
          path: "/browser/browser/components/sessionstore/test/",
          name: "test1",
        },
      ],
    },
  ],
  selectedWindow: 1,
  _closedWindows: [],
  session: {
    lastUpdate: 1463893009801,
    startTime: 1463893007134,
    recentCrashes: 0,
  },
  global: {},
});

const SESSION_DATA_OA = JSON.stringify({
  version: ["sessionrestore", 1],
  windows: [
    {
      tabs: [
        {
          entries: [],
          lastAccessed: 1463893009797,
          hidden: false,
          attributes: {},
          image: null,
        },
        {
          entries: [
            {
              url: "http://www.example.com/browser/browser/components/sessionstore/test/browser_1267910_page.html",
              triggeringPrincipal_base64,
              charset: "UTF-8",
              ID: 0,
              docshellID: 2,
              originalURI:
                "http://www.example.com/browser/browser/components/sessionstore/test/browser_1267910_page.html",
              docIdentifier: 0,
              persist: true,
            },
          ],
          lastAccessed: 1463893009321,
          hidden: false,
          attributes: {},
          userContextId: 0,
          index: 1,
          image: "http://www.example.com/favicon.ico",
        },
      ],
      selected: 1,
      _closedTabs: [],
      busy: false,
      width: 1024,
      height: 768,
      screenX: 4,
      screenY: 23,
      sizemode: "normal",
      cookies: [
        {
          host: "www.example.com",
          value: "yes1",
          path: "/browser/browser/components/sessionstore/test/",
          name: "test1",
          originAttributes: {
            addonId: "",
            inIsolatedMozBrowser: false,
            userContextId: 0,
          },
        },
      ],
    },
  ],
  selectedWindow: 1,
  _closedWindows: [],
  session: {
    lastUpdate: 1463893009801,
    startTime: 1463893007134,
    recentCrashes: 0,
  },
  global: {},
});

add_task(async function run_test() {
  // Wait until initialization is complete.
  await SessionStore.promiseInitialized;

  // Clear cookies.
  Services.cookies.removeAll();

  // Open a new window.
  let win = await promiseNewWindowLoaded();

  // Restore window with session cookies that have no originAttributes.
  await setWindowState(win, SESSION_DATA, true);

  let cookieCount = 0;
  for (var cookie of Services.cookies.getCookiesFromHost(TEST_HOST, {})) {
    cookieCount++;
  }

  // Check that the cookie is restored successfully.
  is(cookieCount, 1, "expected one cookie");
  is(cookie.name, COOKIE.name, "cookie name successfully restored");
  is(cookie.value, COOKIE.value, "cookie value successfully restored");
  is(cookie.path, COOKIE.path, "cookie path successfully restored");

  // Clear cookies.
  Services.cookies.removeAll();

  // In real usage, the event loop would get to spin between setWindowState
  // uses.  Without a spin, we can defer handling the STATE_STOP that
  // removes the progress listener until after the mozbrowser has been
  // destroyed, causing a window leak.
  await new Promise(resolve => win.setTimeout(resolve, 0));

  // Restore window with session cookies that have originAttributes within.
  await setWindowState(win, SESSION_DATA_OA, true);

  cookieCount = 0;
  for (cookie of Services.cookies.getCookiesFromHost(TEST_HOST, {})) {
    cookieCount++;
  }

  // Check that the cookie is restored successfully.
  is(cookieCount, 1, "expected one cookie");
  is(cookie.name, COOKIE.name, "cookie name successfully restored");
  is(cookie.value, COOKIE.value, "cookie value successfully restored");
  is(cookie.path, COOKIE.path, "cookie path successfully restored");

  // Close our window.
  await BrowserTestUtils.closeWindow(win);
});