summaryrefslogtreecommitdiffstats
path: root/remote/marionette/test/xpcshell/test_cookie.js
blob: b5ce5e9008a13facac16743839d4ebcfec9a716f (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

const { cookie } = ChromeUtils.importESModule(
  "chrome://remote/content/marionette/cookie.sys.mjs"
);

/* eslint-disable mozilla/use-chromeutils-generateqi */

cookie.manager = {
  cookies: [],

  add(
    domain,
    path,
    name,
    value,
    secure,
    httpOnly,
    session,
    expiry,
    originAttributes,
    sameSite
  ) {
    if (name === "fail") {
      throw new Error("An error occurred while adding cookie");
    }
    let newCookie = {
      host: domain,
      path,
      name,
      value,
      isSecure: secure,
      isHttpOnly: httpOnly,
      isSession: session,
      expiry,
      originAttributes,
      sameSite,
    };
    cookie.manager.cookies.push(newCookie);
  },

  remove(host, name, path) {
    for (let i = 0; i < this.cookies.length; ++i) {
      let candidate = this.cookies[i];
      if (
        candidate.host === host &&
        candidate.name === name &&
        candidate.path === path
      ) {
        return this.cookies.splice(i, 1);
      }
    }
    return false;
  },

  getCookiesFromHost(host) {
    let hostCookies = this.cookies.filter(
      c => c.host === host || c.host === "." + host
    );

    return hostCookies;
  },
};

add_task(function test_fromJSON() {
  // object
  for (let invalidType of ["foo", 42, true, [], null, undefined]) {
    Assert.throws(() => cookie.fromJSON(invalidType), /Expected cookie object/);
  }

  // name and value
  for (let invalidType of [42, true, [], {}, null, undefined]) {
    Assert.throws(
      () => cookie.fromJSON({ name: invalidType }),
      /Cookie name must be string/
    );
    Assert.throws(
      () => cookie.fromJSON({ name: "foo", value: invalidType }),
      /Cookie value must be string/
    );
  }

  // domain
  for (let invalidType of [42, true, [], {}, null]) {
    let domainTest = {
      name: "foo",
      value: "bar",
      domain: invalidType,
    };
    Assert.throws(
      () => cookie.fromJSON(domainTest),
      /Cookie domain must be string/
    );
  }
  let domainTest = {
    name: "foo",
    value: "bar",
    domain: "domain",
  };
  let parsedCookie = cookie.fromJSON(domainTest);
  equal(parsedCookie.domain, "domain");

  // path
  for (let invalidType of [42, true, [], {}, null]) {
    let pathTest = {
      name: "foo",
      value: "bar",
      path: invalidType,
    };
    Assert.throws(
      () => cookie.fromJSON(pathTest),
      /Cookie path must be string/
    );
  }

  // secure
  for (let invalidType of ["foo", 42, [], {}, null]) {
    let secureTest = {
      name: "foo",
      value: "bar",
      secure: invalidType,
    };
    Assert.throws(
      () => cookie.fromJSON(secureTest),
      /Cookie secure flag must be boolean/
    );
  }

  // httpOnly
  for (let invalidType of ["foo", 42, [], {}, null]) {
    let httpOnlyTest = {
      name: "foo",
      value: "bar",
      httpOnly: invalidType,
    };
    Assert.throws(
      () => cookie.fromJSON(httpOnlyTest),
      /Cookie httpOnly flag must be boolean/
    );
  }

  // expiry
  for (let invalidType of [
    -1,
    Number.MAX_SAFE_INTEGER + 1,
    "foo",
    true,
    [],
    {},
    null,
  ]) {
    let expiryTest = {
      name: "foo",
      value: "bar",
      expiry: invalidType,
    };
    Assert.throws(
      () => cookie.fromJSON(expiryTest),
      /Cookie expiry must be a positive integer/
    );
  }

  // sameSite
  for (let invalidType of ["foo", 42, [], {}, null]) {
    const sameSiteTest = {
      name: "foo",
      value: "bar",
      sameSite: invalidType,
    };
    Assert.throws(
      () => cookie.fromJSON(sameSiteTest),
      /Cookie SameSite flag must be one of None, Lax, or Strict/
    );
  }

  // bare requirements
  let bare = cookie.fromJSON({ name: "name", value: "value" });
  equal("name", bare.name);
  equal("value", bare.value);
  for (let missing of [
    "path",
    "secure",
    "httpOnly",
    "session",
    "expiry",
    "sameSite",
  ]) {
    ok(!bare.hasOwnProperty(missing));
  }

  // everything
  let full = cookie.fromJSON({
    name: "name",
    value: "value",
    domain: ".domain",
    path: "path",
    secure: true,
    httpOnly: true,
    expiry: 42,
    sameSite: "Lax",
  });
  equal("name", full.name);
  equal("value", full.value);
  equal(".domain", full.domain);
  equal("path", full.path);
  equal(true, full.secure);
  equal(true, full.httpOnly);
  equal(42, full.expiry);
  equal("Lax", full.sameSite);
});

add_task(function test_add() {
  cookie.manager.cookies = [];

  for (let invalidType of [42, true, [], {}, null, undefined]) {
    Assert.throws(
      () => cookie.add({ name: invalidType }),
      /Cookie name must be string/
    );
    Assert.throws(
      () => cookie.add({ name: "name", value: invalidType }),
      /Cookie value must be string/
    );
    Assert.throws(
      () => cookie.add({ name: "name", value: "value", domain: invalidType }),
      /Cookie domain must be string/
    );
  }

  cookie.add({
    name: "name",
    value: "value",
    domain: "domain",
  });
  equal(1, cookie.manager.cookies.length);
  equal("name", cookie.manager.cookies[0].name);
  equal("value", cookie.manager.cookies[0].value);
  equal(".domain", cookie.manager.cookies[0].host);
  equal("/", cookie.manager.cookies[0].path);
  ok(cookie.manager.cookies[0].expiry > new Date(Date.now()).getTime() / 1000);

  cookie.add({
    name: "name2",
    value: "value2",
    domain: "domain2",
  });
  equal(2, cookie.manager.cookies.length);

  Assert.throws(() => {
    let biscuit = { name: "name3", value: "value3", domain: "domain3" };
    cookie.add(biscuit, { restrictToHost: "other domain" });
  }, /Cookies may only be set for the current domain/);

  cookie.add({
    name: "name4",
    value: "value4",
    domain: "my.domain:1234",
  });
  equal(".my.domain", cookie.manager.cookies[2].host);

  cookie.add({
    name: "name5",
    value: "value5",
    domain: "domain5",
    path: "/foo/bar",
  });
  equal("/foo/bar", cookie.manager.cookies[3].path);

  cookie.add({
    name: "name6",
    value: "value",
    domain: ".domain",
  });
  equal(".domain", cookie.manager.cookies[4].host);

  const sameSiteMap = new Map([
    ["None", Ci.nsICookie.SAMESITE_NONE],
    ["Lax", Ci.nsICookie.SAMESITE_LAX],
    ["Strict", Ci.nsICookie.SAMESITE_STRICT],
  ]);

  Array.from(sameSiteMap.keys()).forEach((entry, index) => {
    cookie.add({
      name: "name" + index,
      value: "value",
      domain: ".domain",
      sameSite: entry,
    });
    equal(sameSiteMap.get(entry), cookie.manager.cookies[5 + index].sameSite);
  });

  Assert.throws(() => {
    cookie.add({ name: "fail", value: "value6", domain: "domain6" });
  }, /UnableToSetCookieError/);
});

add_task(function test_remove() {
  cookie.manager.cookies = [];

  let crumble = {
    name: "test_remove",
    value: "value",
    domain: "domain",
    path: "/custom/path",
  };

  equal(0, cookie.manager.cookies.length);
  cookie.add(crumble);
  equal(1, cookie.manager.cookies.length);

  cookie.remove(crumble);
  equal(0, cookie.manager.cookies.length);
  equal(undefined, cookie.manager.cookies[0]);
});

add_task(function test_iter() {
  cookie.manager.cookies = [];
  let tomorrow = new Date();
  tomorrow.setHours(tomorrow.getHours() + 24);

  cookie.add({
    expiry: tomorrow,
    name: "0",
    value: "",
    domain: "foo.example.com",
  });
  cookie.add({
    expiry: tomorrow,
    name: "1",
    value: "",
    domain: "bar.example.com",
  });

  let fooCookies = [...cookie.iter("foo.example.com")];
  equal(1, fooCookies.length);
  equal(".foo.example.com", fooCookies[0].domain);
  equal(true, fooCookies[0].hasOwnProperty("expiry"));

  cookie.add({
    name: "aSessionCookie",
    value: "",
    domain: "session.com",
  });

  let sessionCookies = [...cookie.iter("session.com")];
  equal(1, sessionCookies.length);
  equal("aSessionCookie", sessionCookies[0].name);
  equal(false, sessionCookies[0].hasOwnProperty("expiry"));

  cookie.add({
    name: "2",
    value: "",
    domain: "samesite.example.com",
    sameSite: "Lax",
  });

  let sameSiteCookies = [...cookie.iter("samesite.example.com")];
  equal(1, sameSiteCookies.length);
  equal("Lax", sameSiteCookies[0].sameSite);
});