Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_schema_10_migration.js
blobaf50c967fdda65952b86f2adfe213cc73edce2b2
1 /* Any copyright is dedicated to the Public Domain.
2 http://creativecommons.org/publicdomain/zero/1.0/ */
4 // Test cookie database migration from version 10 (prerelease Gecko 2.0) to the
5 // current version, presently 12.
6 "use strict";
8 var test_generator = do_run_test();
10 function run_test() {
11 do_test_pending();
12 test_generator.next();
15 function finish_test() {
16 executeSoon(function () {
17 test_generator.return();
18 do_test_finished();
19 });
22 function* do_run_test() {
23 // Set up a profile.
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;
30 // Close the profile.
31 do_close_profile(test_generator);
32 yield;
34 // Remove the cookie file in order to create another database file.
35 do_get_cookie_file(profile).remove(false);
37 // Create a schema 10 database.
38 let schema10db = new CookieDatabaseConnection(
39 do_get_cookie_file(profile),
43 let now = Date.now() * 1000;
44 let futureExpiry = Math.round(now / 1e6 + 1000);
45 let pastExpiry = Math.round(now / 1e6 - 1000);
47 // Populate it, with:
48 // 1) Unexpired, unique cookies.
49 for (let i = 0; i < 20; ++i) {
50 let cookie = new Cookie(
51 "oh" + i,
52 "hai",
53 "foo.com",
54 "/",
55 futureExpiry,
56 now,
57 now + i,
58 false,
59 false,
60 false
63 schema10db.insertCookie(cookie);
66 // 2) Expired, unique cookies.
67 for (let i = 20; i < 40; ++i) {
68 let cookie = new Cookie(
69 "oh" + i,
70 "hai",
71 "bar.com",
72 "/",
73 pastExpiry,
74 now,
75 now + i,
76 false,
77 false,
78 false
81 schema10db.insertCookie(cookie);
84 // 3) Many copies of the same cookie, some of which have expired and
85 // some of which have not.
86 for (let i = 40; i < 45; ++i) {
87 let cookie = new Cookie(
88 "oh",
89 "hai",
90 "baz.com",
91 "/",
92 futureExpiry + i,
93 now,
94 now + i,
95 false,
96 false,
97 false
100 try {
101 schema10db.insertCookie(cookie);
102 } catch (e) {}
104 for (let i = 45; i < 50; ++i) {
105 let cookie = new Cookie(
106 "oh",
107 "hai",
108 "baz.com",
109 "/",
110 pastExpiry - i,
111 now,
112 now + i,
113 false,
114 false,
115 false
118 try {
119 schema10db.insertCookie(cookie);
120 } catch (e) {}
122 for (let i = 50; i < 55; ++i) {
123 let cookie = new Cookie(
124 "oh",
125 "hai",
126 "baz.com",
127 "/",
128 futureExpiry - i,
129 now,
130 now + i,
131 false,
132 false,
133 false
136 try {
137 schema10db.insertCookie(cookie);
138 } catch (e) {}
140 for (let i = 55; i < 60; ++i) {
141 let cookie = new Cookie(
142 "oh",
143 "hai",
144 "baz.com",
145 "/",
146 pastExpiry + i,
147 now,
148 now + i,
149 false,
150 false,
151 false
154 try {
155 schema10db.insertCookie(cookie);
156 } catch (e) {}
159 // Close it.
160 schema10db.close();
161 schema10db = null;
163 // Load the database, forcing migration to the current schema version. Then
164 // test the expected set of cookies:
165 do_load_profile();
167 // 1) All unexpired, unique cookies exist.
168 Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 20);
170 // 2) All expired, unique cookies exist.
171 Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20);
173 // 3) Only one cookie remains, and it's the one with the highest expiration
174 // time.
175 Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1);
176 let cookies = Services.cookies.getCookiesFromHost("baz.com", {});
177 let cookie = cookies[0];
178 Assert.equal(cookie.expiry, futureExpiry + 40);
180 finish_test();