Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_schema_3_migration.js
blob7b5c639950bca84574f333ed8931b45f65cd8f36
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).
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 3 database.
38 let schema3db = new CookieDatabaseConnection(do_get_cookie_file(profile), 3);
40 let now = Date.now() * 1000;
41 let futureExpiry = Math.round(now / 1e6 + 1000);
42 let pastExpiry = Math.round(now / 1e6 - 1000);
44 // Populate it, with:
45 // 1) Unexpired, unique cookies.
46 for (let i = 0; i < 20; ++i) {
47 let cookie = new Cookie(
48 "oh" + i,
49 "hai",
50 "foo.com",
51 "/",
52 futureExpiry,
53 now,
54 now + i,
55 false,
56 false,
57 false
60 schema3db.insertCookie(cookie);
63 // 2) Expired, unique cookies.
64 for (let i = 20; i < 40; ++i) {
65 let cookie = new Cookie(
66 "oh" + i,
67 "hai",
68 "bar.com",
69 "/",
70 pastExpiry,
71 now,
72 now + i,
73 false,
74 false,
75 false
78 schema3db.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(
85 "oh",
86 "hai",
87 "baz.com",
88 "/",
89 futureExpiry + i,
90 now,
91 now + i,
92 false,
93 false,
94 false
97 schema3db.insertCookie(cookie);
99 for (let i = 45; i < 50; ++i) {
100 let cookie = new Cookie(
101 "oh",
102 "hai",
103 "baz.com",
104 "/",
105 pastExpiry - i,
106 now,
107 now + i,
108 false,
109 false,
110 false
113 schema3db.insertCookie(cookie);
115 for (let i = 50; i < 55; ++i) {
116 let cookie = new Cookie(
117 "oh",
118 "hai",
119 "baz.com",
120 "/",
121 futureExpiry - i,
122 now,
123 now + i,
124 false,
125 false,
126 false
129 schema3db.insertCookie(cookie);
131 for (let i = 55; i < 60; ++i) {
132 let cookie = new Cookie(
133 "oh",
134 "hai",
135 "baz.com",
136 "/",
137 pastExpiry + i,
138 now,
139 now + i,
140 false,
141 false,
142 false
145 schema3db.insertCookie(cookie);
148 // Close it.
149 schema3db.close();
150 schema3db = null;
152 // Load the database, forcing migration to the current schema version. Then
153 // test the expected set of cookies:
154 do_load_profile();
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
163 // time.
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 finish_test();