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
|
<!DOCTYPE HTML>
<html>
<head>
<title>WebExtension test</title>
<script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
<script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
<script type="text/javascript" src="head.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<script type="text/javascript">
"use strict";
add_task(function* test_cookies() {
async function background() {
function assertExpected(expected, cookie) {
for (let key of Object.keys(cookie)) {
browser.test.assertTrue(key in expected, `found property ${key}`);
browser.test.assertEq(expected[key], cookie[key], `property value for ${key} is correct`);
}
browser.test.assertEq(Object.keys(expected).length, Object.keys(cookie).length, "all expected properties found");
}
const TEST_URL = "http://example.org/";
const TEST_SECURE_URL = "https://example.org/";
const THE_FUTURE = Date.now() + 5 * 60;
const TEST_PATH = "set_path";
const TEST_URL_WITH_PATH = TEST_URL + TEST_PATH;
const TEST_COOKIE_PATH = `/${TEST_PATH}`;
const STORE_ID = "firefox-default";
const PRIVATE_STORE_ID = "firefox-private";
let expected = {
name: "name1",
value: "value1",
domain: "example.org",
hostOnly: true,
path: "/",
secure: false,
httpOnly: false,
session: false,
expirationDate: THE_FUTURE,
storeId: STORE_ID,
};
let cookie = await browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", expirationDate: THE_FUTURE});
assertExpected(expected, cookie);
cookie = await browser.cookies.get({url: TEST_URL, name: "name1"});
assertExpected(expected, cookie);
let cookies = await browser.cookies.getAll({name: "name1"});
browser.test.assertEq(cookies.length, 1, "one cookie found for matching name");
assertExpected(expected, cookies[0]);
cookies = await browser.cookies.getAll({domain: "example.org"});
browser.test.assertEq(cookies.length, 1, "one cookie found for matching domain");
assertExpected(expected, cookies[0]);
cookies = await browser.cookies.getAll({domain: "example.net"});
browser.test.assertEq(cookies.length, 0, "no cookies found for non-matching domain");
cookies = await browser.cookies.getAll({secure: false});
browser.test.assertEq(cookies.length, 1, "one non-secure cookie found");
assertExpected(expected, cookies[0]);
cookies = await browser.cookies.getAll({secure: true});
browser.test.assertEq(cookies.length, 0, "no secure cookies found");
cookies = await browser.cookies.getAll({storeId: STORE_ID});
browser.test.assertEq(cookies.length, 1, "one cookie found for valid storeId");
assertExpected(expected, cookies[0]);
cookies = await browser.cookies.getAll({storeId: "invalid_id"});
browser.test.assertEq(cookies.length, 0, "no cookies found for invalid storeId");
let details = await browser.cookies.remove({url: TEST_URL, name: "name1"});
assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
cookie = await browser.cookies.get({url: TEST_URL, name: "name1"});
browser.test.assertEq(null, cookie, "removed cookie not found");
let stores = await browser.cookies.getAllCookieStores();
browser.test.assertEq(1, stores.length, "expected number of stores returned");
browser.test.assertEq(STORE_ID, stores[0].id, "expected store id returned");
browser.test.assertEq(1, stores[0].tabIds.length, "one tab returned for store");
{
let privateWindow = await browser.windows.create({incognito: true});
let stores = await browser.cookies.getAllCookieStores();
browser.test.assertEq(2, stores.length, "expected number of stores returned");
browser.test.assertEq(STORE_ID, stores[0].id, "expected store id returned");
browser.test.assertEq(1, stores[0].tabIds.length, "one tab returned for store");
browser.test.assertEq(PRIVATE_STORE_ID, stores[1].id, "expected private store id returned");
browser.test.assertEq(1, stores[0].tabIds.length, "one tab returned for private store");
await browser.windows.remove(privateWindow.id);
}
cookie = await browser.cookies.set({url: TEST_URL, name: "name2", domain: ".example.org", expirationDate: THE_FUTURE});
browser.test.assertEq(false, cookie.hostOnly, "cookie is not a hostOnly cookie");
details = await browser.cookies.remove({url: TEST_URL, name: "name2"});
assertExpected({url: TEST_URL, name: "name2", storeId: STORE_ID}, details);
// Create a session cookie.
cookie = await browser.cookies.set({url: TEST_URL, name: "name1", value: "value1"});
browser.test.assertEq(true, cookie.session, "session cookie set");
cookie = await browser.cookies.get({url: TEST_URL, name: "name1"});
browser.test.assertEq(true, cookie.session, "got session cookie");
cookies = await browser.cookies.getAll({session: true});
browser.test.assertEq(cookies.length, 1, "one session cookie found");
browser.test.assertEq(true, cookies[0].session, "found session cookie");
cookies = await browser.cookies.getAll({session: false});
browser.test.assertEq(cookies.length, 0, "no non-session cookies found");
details = await browser.cookies.remove({url: TEST_URL, name: "name1"});
assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
cookie = await browser.cookies.get({url: TEST_URL, name: "name1"});
browser.test.assertEq(null, cookie, "removed cookie not found");
cookie = await browser.cookies.set({url: TEST_SECURE_URL, name: "name1", value: "value1", secure: true});
browser.test.assertEq(true, cookie.secure, "secure cookie set");
cookie = await browser.cookies.get({url: TEST_SECURE_URL, name: "name1"});
browser.test.assertEq(true, cookie.session, "got secure cookie");
cookies = await browser.cookies.getAll({secure: true});
browser.test.assertEq(cookies.length, 1, "one secure cookie found");
browser.test.assertEq(true, cookies[0].secure, "found secure cookie");
cookies = await browser.cookies.getAll({secure: false});
browser.test.assertEq(cookies.length, 0, "no non-secure cookies found");
details = await browser.cookies.remove({url: TEST_SECURE_URL, name: "name1"});
assertExpected({url: TEST_SECURE_URL, name: "name1", storeId: STORE_ID}, details);
cookie = await browser.cookies.get({url: TEST_SECURE_URL, name: "name1"});
browser.test.assertEq(null, cookie, "removed cookie not found");
cookie = await browser.cookies.set({url: TEST_URL_WITH_PATH, path: TEST_COOKIE_PATH, name: "name1", value: "value1", expirationDate: THE_FUTURE});
browser.test.assertEq(TEST_COOKIE_PATH, cookie.path, "created cookie with path");
cookie = await browser.cookies.get({url: TEST_URL_WITH_PATH, name: "name1"});
browser.test.assertEq(TEST_COOKIE_PATH, cookie.path, "got cookie with path");
cookies = await browser.cookies.getAll({path: TEST_COOKIE_PATH});
browser.test.assertEq(cookies.length, 1, "one cookie with path found");
browser.test.assertEq(TEST_COOKIE_PATH, cookies[0].path, "found cookie with path");
cookie = await browser.cookies.get({url: TEST_URL + "invalid_path", name: "name1"});
browser.test.assertEq(null, cookie, "get with invalid path returns null");
cookies = await browser.cookies.getAll({path: "/invalid_path"});
browser.test.assertEq(cookies.length, 0, "getAll with invalid path returns 0 cookies");
details = await browser.cookies.remove({url: TEST_URL_WITH_PATH, name: "name1"});
assertExpected({url: TEST_URL_WITH_PATH, name: "name1", storeId: STORE_ID}, details);
cookie = await browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", httpOnly: true});
browser.test.assertEq(true, cookie.httpOnly, "httpOnly cookie set");
cookie = await browser.cookies.set({url: TEST_URL, name: "name1", value: "value1", httpOnly: false});
browser.test.assertEq(false, cookie.httpOnly, "non-httpOnly cookie set");
details = await browser.cookies.remove({url: TEST_URL, name: "name1"});
assertExpected({url: TEST_URL, name: "name1", storeId: STORE_ID}, details);
cookie = await browser.cookies.set({url: TEST_URL});
browser.test.assertEq("", cookie.name, "default name set");
browser.test.assertEq("", cookie.value, "default value set");
browser.test.assertEq(true, cookie.session, "no expiry date created session cookie");
{
let privateWindow = await browser.windows.create({incognito: true});
// Hacky work-around for bugzil.la/1309637
await new Promise(resolve => setTimeout(resolve, 700));
let cookie = await browser.cookies.set({url: TEST_URL, name: "store", value: "private", expirationDate: THE_FUTURE, storeId: PRIVATE_STORE_ID});
browser.test.assertEq("private", cookie.value, "set the private cookie");
cookie = await browser.cookies.set({url: TEST_URL, name: "store", value: "default", expirationDate: THE_FUTURE, storeId: STORE_ID});
browser.test.assertEq("default", cookie.value, "set the default cookie");
cookie = await browser.cookies.get({url: TEST_URL, name: "store", storeId: PRIVATE_STORE_ID});
browser.test.assertEq("private", cookie.value, "get the private cookie");
browser.test.assertEq(PRIVATE_STORE_ID, cookie.storeId, "get the private cookie storeId");
cookie = await browser.cookies.get({url: TEST_URL, name: "store", storeId: STORE_ID});
browser.test.assertEq("default", cookie.value, "get the default cookie");
browser.test.assertEq(STORE_ID, cookie.storeId, "get the default cookie storeId");
let details = await browser.cookies.remove({url: TEST_URL, name: "store", storeId: STORE_ID});
assertExpected({url: TEST_URL, name: "store", storeId: STORE_ID}, details);
cookie = await browser.cookies.get({url: TEST_URL, name: "store", storeId: STORE_ID});
browser.test.assertEq(null, cookie, "deleted the default cookie");
details = await browser.cookies.remove({url: TEST_URL, name: "store", storeId: PRIVATE_STORE_ID});
assertExpected({url: TEST_URL, name: "store", storeId: PRIVATE_STORE_ID}, details);
cookie = await browser.cookies.get({url: TEST_URL, name: "store", storeId: PRIVATE_STORE_ID});
browser.test.assertEq(null, cookie, "deleted the private cookie");
await browser.windows.remove(privateWindow.id);
}
browser.test.notifyPass("cookies");
}
let extension = ExtensionTestUtils.loadExtension({
background,
manifest: {
permissions: ["cookies", "*://example.org/"],
},
});
yield extension.startup();
yield extension.awaitFinish("cookies");
yield extension.unload();
});
</script>
</body>
</html>
|