1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 // Test cookie database migration from version 2 (Gecko 1.9.3) to the current
5 // version, presently 4 (Gecko 2.0).
8 var test_generator
= do_run_test();
12 test_generator
.next();
15 function finish_test() {
16 executeSoon(function () {
17 test_generator
.return();
22 function* do_run_test() {
24 let profile
= do_get_profile();
26 // Start the cookieservice, to force creation of a database.
27 // Get the sessionCookies to join the initialization in cookie thread
28 Services
.cookies
.sessionCookies
;
31 do_close_profile(test_generator
);
34 // Remove the cookie file in order to create another database file.
35 do_get_cookie_file(profile
).remove(false);
37 // Create a schema 2 database.
38 let schema2db
= new CookieDatabaseConnection(do_get_cookie_file(profile
), 2);
40 let now
= Date
.now() * 1000;
41 let futureExpiry
= Math
.round(now
/ 1e6
+ 1000);
42 let pastExpiry
= Math
.round(now
/ 1e6
- 1000);
45 // 1) Unexpired, unique cookies.
46 for (let i
= 0; i
< 20; ++i
) {
47 let cookie
= new Cookie(
60 schema2db
.insertCookie(cookie
);
63 // 2) Expired, unique cookies.
64 for (let i
= 20; i
< 40; ++i
) {
65 let cookie
= new Cookie(
78 schema2db
.insertCookie(cookie
);
81 // 3) Many copies of the same cookie, some of which have expired and
82 // some of which have not.
83 for (let i
= 40; i
< 45; ++i
) {
84 let cookie
= new Cookie(
97 schema2db
.insertCookie(cookie
);
99 for (let i
= 45; i
< 50; ++i
) {
100 let cookie
= new Cookie(
113 schema2db
.insertCookie(cookie
);
115 for (let i
= 50; i
< 55; ++i
) {
116 let cookie
= new Cookie(
129 schema2db
.insertCookie(cookie
);
131 for (let i
= 55; i
< 60; ++i
) {
132 let cookie
= new Cookie(
145 schema2db
.insertCookie(cookie
);
152 // Load the database, forcing migration to the current schema version. Then
153 // test the expected set of cookies:
156 // 1) All unexpired, unique cookies exist.
157 Assert
.equal(Services
.cookies
.countCookiesFromHost("foo.com"), 20);
159 // 2) All expired, unique cookies exist.
160 Assert
.equal(Services
.cookies
.countCookiesFromHost("bar.com"), 20);
162 // 3) Only one cookie remains, and it's the one with the highest expiration
164 Assert
.equal(Services
.cookies
.countCookiesFromHost("baz.com"), 1);
165 let cookies
= Services
.cookies
.getCookiesFromHost("baz.com", {});
166 let cookie
= cookies
[0];
167 Assert
.equal(cookie
.expiry
, futureExpiry
+ 44);
169 do_close_profile(test_generator
);
172 // Open the database so we can execute some more schema 2 statements on it.
173 schema2db
= new CookieDatabaseConnection(do_get_cookie_file(profile
), 2);
175 // Populate it with more cookies.
176 for (let i
= 60; i
< 80; ++i
) {
177 schema2db
.insertCookie(
192 for (let i
= 80; i
< 100; ++i
) {
193 schema2db
.insertCookie(
209 // Attempt to add a cookie with the same (name, host, path) values as another
210 // cookie. This should succeed since we have a REPLACE clause for conflict on
225 schema2db
.insertCookie(cookie
);
227 // Check that there is, indeed, a singular cookie for baz.com.
228 Assert
.equal(do_count_cookies_in_db(schema2db
.db
, "baz.com"), 1);
234 // Back up the database, so we can test both asynchronous and synchronous
235 // loading separately.
236 let file
= do_get_cookie_file(profile
);
237 let copy
= profile
.clone();
238 copy
.append("cookies.sqlite.copy");
239 file
.copyTo(null, copy
.leafName
);
241 // Load the database asynchronously, forcing a purge of the newly-added
242 // cookies. (Their baseDomain column will be NULL.)
243 do_load_profile(test_generator
);
246 // Test the expected set of cookies.
247 Assert
.equal(Services
.cookies
.countCookiesFromHost("foo.com"), 40);
248 Assert
.equal(Services
.cookies
.countCookiesFromHost("bar.com"), 20);
249 Assert
.equal(Services
.cookies
.countCookiesFromHost("baz.com"), 1);
250 Assert
.equal(Services
.cookies
.countCookiesFromHost("cat.com"), 20);
252 do_close_profile(test_generator
);
255 // Copy the database back.
257 copy
.copyTo(null, file
.leafName
);
259 // Load the database host-at-a-time.
262 // Test the expected set of cookies.
263 Assert
.equal(Services
.cookies
.countCookiesFromHost("foo.com"), 40);
264 Assert
.equal(Services
.cookies
.countCookiesFromHost("bar.com"), 20);
265 Assert
.equal(Services
.cookies
.countCookiesFromHost("baz.com"), 1);
266 Assert
.equal(Services
.cookies
.countCookiesFromHost("cat.com"), 20);
268 do_close_profile(test_generator
);
271 // Open the database and prove that they were deleted.
272 schema2db
= new CookieDatabaseConnection(do_get_cookie_file(profile
), 2);
273 Assert
.equal(do_count_cookies_in_db(schema2db
.db
), 81);
274 Assert
.equal(do_count_cookies_in_db(schema2db
.db
, "foo.com"), 40);
275 Assert
.equal(do_count_cookies_in_db(schema2db
.db
, "bar.com"), 20);
278 // Copy the database back.
280 copy
.copyTo(null, file
.leafName
);
282 // Load the database synchronously, in its entirety.
284 Assert
.equal(do_count_cookies(), 81);
286 // Test the expected set of cookies.
287 Assert
.equal(Services
.cookies
.countCookiesFromHost("foo.com"), 40);
288 Assert
.equal(Services
.cookies
.countCookiesFromHost("bar.com"), 20);
289 Assert
.equal(Services
.cookies
.countCookiesFromHost("baz.com"), 1);
290 Assert
.equal(Services
.cookies
.countCookiesFromHost("cat.com"), 20);
292 do_close_profile(test_generator
);
295 // Open the database and prove that they were deleted.
296 schema2db
= new CookieDatabaseConnection(do_get_cookie_file(profile
), 2);
297 Assert
.equal(do_count_cookies_in_db(schema2db
.db
), 81);
298 Assert
.equal(do_count_cookies_in_db(schema2db
.db
, "foo.com"), 40);
299 Assert
.equal(do_count_cookies_in_db(schema2db
.db
, "bar.com"), 20);