Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 // Test cookie database migration from version 3 (prerelease Gecko 2.0) to the
5 // current version, presently 4 (Gecko 2.0).
7 let test_generator = do_run_test();
9 function run_test() {
10 do_test_pending();
11 test_generator.next();
12 }
14 function finish_test() {
15 do_execute_soon(function() {
16 test_generator.close();
17 do_test_finished();
18 });
19 }
21 function do_run_test() {
22 // Set up a profile.
23 let profile = do_get_profile();
25 // Create a schema 3 database.
26 let schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3);
28 let now = Date.now() * 1000;
29 let futureExpiry = Math.round(now / 1e6 + 1000);
30 let pastExpiry = Math.round(now / 1e6 - 1000);
32 // Populate it, with:
33 // 1) Unexpired, unique cookies.
34 for (let i = 0; i < 20; ++i) {
35 let cookie = new Cookie("oh" + i, "hai", "foo.com", "/",
36 futureExpiry, now, now + i, false, false, false);
38 schema3db.insertCookie(cookie);
39 }
41 // 2) Expired, unique cookies.
42 for (let i = 20; i < 40; ++i) {
43 let cookie = new Cookie("oh" + i, "hai", "bar.com", "/",
44 pastExpiry, now, now + i, false, false, false);
46 schema3db.insertCookie(cookie);
47 }
49 // 3) Many copies of the same cookie, some of which have expired and
50 // some of which have not.
51 for (let i = 40; i < 45; ++i) {
52 let cookie = new Cookie("oh", "hai", "baz.com", "/",
53 futureExpiry + i, now, now + i, false, false, false);
55 schema3db.insertCookie(cookie);
56 }
57 for (let i = 45; i < 50; ++i) {
58 let cookie = new Cookie("oh", "hai", "baz.com", "/",
59 pastExpiry - i, now, now + i, false, false, false);
61 schema3db.insertCookie(cookie);
62 }
63 for (let i = 50; i < 55; ++i) {
64 let cookie = new Cookie("oh", "hai", "baz.com", "/",
65 futureExpiry - i, now, now + i, false, false, false);
67 schema3db.insertCookie(cookie);
68 }
69 for (let i = 55; i < 60; ++i) {
70 let cookie = new Cookie("oh", "hai", "baz.com", "/",
71 pastExpiry + i, now, now + i, false, false, false);
73 schema3db.insertCookie(cookie);
74 }
76 // Close it.
77 schema3db.close();
78 schema3db = null;
80 // Load the database, forcing migration to the current schema version. Then
81 // test the expected set of cookies:
82 // 1) All unexpired, unique cookies exist.
83 do_check_eq(Services.cookiemgr.countCookiesFromHost("foo.com"), 20);
85 // 2) All expired, unique cookies exist.
86 do_check_eq(Services.cookiemgr.countCookiesFromHost("bar.com"), 20);
88 // 3) Only one cookie remains, and it's the one with the highest expiration
89 // time.
90 do_check_eq(Services.cookiemgr.countCookiesFromHost("baz.com"), 1);
91 let enumerator = Services.cookiemgr.getCookiesFromHost("baz.com");
92 let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
93 do_check_eq(cookie.expiry, futureExpiry + 44);
95 do_close_profile(test_generator);
96 yield;
98 // Open the database so we can execute some more schema 3 statements on it.
99 schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3);
101 // Populate it with more cookies.
102 for (let i = 60; i < 80; ++i) {
103 let cookie = new Cookie("oh" + i, "hai", "cat.com", "/",
104 futureExpiry, now, now + i, false, false, false);
106 schema3db.insertCookie(cookie);
107 }
109 // Close it.
110 schema3db.close();
111 schema3db = null;
113 // Load the database. The cookies added immediately prior will have a NULL
114 // creationTime column.
115 do_load_profile();
117 // Test the expected set of cookies.
118 do_check_eq(Services.cookiemgr.countCookiesFromHost("cat.com"), 20);
119 let enumerator = Services.cookiemgr.getCookiesFromHost("cat.com");
120 let cookie = enumerator.getNext().QueryInterface(Ci.nsICookie2);
121 do_check_eq(cookie.creationTime, 0);
123 finish_test();
124 }