summaryrefslogtreecommitdiffstats
path: root/netwerk/test/unit/test_schema_2_migration.js
blob: 03588c01306912127a4fbf38e34972e16c4f2572 (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test cookie database migration from version 2 (Gecko 1.9.3) to the current
// version, presently 4 (Gecko 2.0).
"use strict";

var test_generator = do_run_test();

function run_test() {
  do_test_pending();
  test_generator.next();
}

function finish_test() {
  executeSoon(function () {
    test_generator.return();
    do_test_finished();
  });
}

function* do_run_test() {
  // Set up a profile.
  let profile = do_get_profile();

  // Start the cookieservice, to force creation of a database.
  // Get the sessionCookies to join the initialization in cookie thread
  Services.cookies.sessionCookies;

  // Close the profile.
  do_close_profile(test_generator);
  yield;

  // Remove the cookie file in order to create another database file.
  do_get_cookie_file(profile).remove(false);

  // Create a schema 2 database.
  let schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);

  let now = Date.now() * 1000;
  let futureExpiry = Math.round(now / 1e6 + 1000);
  let pastExpiry = Math.round(now / 1e6 - 1000);

  // Populate it, with:
  // 1) Unexpired, unique cookies.
  for (let i = 0; i < 20; ++i) {
    let cookie = new Cookie(
      "oh" + i,
      "hai",
      "foo.com",
      "/",
      futureExpiry,
      now,
      now + i,
      false,
      false,
      false
    );

    schema2db.insertCookie(cookie);
  }

  // 2) Expired, unique cookies.
  for (let i = 20; i < 40; ++i) {
    let cookie = new Cookie(
      "oh" + i,
      "hai",
      "bar.com",
      "/",
      pastExpiry,
      now,
      now + i,
      false,
      false,
      false
    );

    schema2db.insertCookie(cookie);
  }

  // 3) Many copies of the same cookie, some of which have expired and
  // some of which have not.
  for (let i = 40; i < 45; ++i) {
    let cookie = new Cookie(
      "oh",
      "hai",
      "baz.com",
      "/",
      futureExpiry + i,
      now,
      now + i,
      false,
      false,
      false
    );

    schema2db.insertCookie(cookie);
  }
  for (let i = 45; i < 50; ++i) {
    let cookie = new Cookie(
      "oh",
      "hai",
      "baz.com",
      "/",
      pastExpiry - i,
      now,
      now + i,
      false,
      false,
      false
    );

    schema2db.insertCookie(cookie);
  }
  for (let i = 50; i < 55; ++i) {
    let cookie = new Cookie(
      "oh",
      "hai",
      "baz.com",
      "/",
      futureExpiry - i,
      now,
      now + i,
      false,
      false,
      false
    );

    schema2db.insertCookie(cookie);
  }
  for (let i = 55; i < 60; ++i) {
    let cookie = new Cookie(
      "oh",
      "hai",
      "baz.com",
      "/",
      pastExpiry + i,
      now,
      now + i,
      false,
      false,
      false
    );

    schema2db.insertCookie(cookie);
  }

  // Close it.
  schema2db.close();
  schema2db = null;

  // Load the database, forcing migration to the current schema version. Then
  // test the expected set of cookies:
  do_load_profile();

  // 1) All unexpired, unique cookies exist.
  Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 20);

  // 2) All expired, unique cookies exist.
  Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20);

  // 3) Only one cookie remains, and it's the one with the highest expiration
  // time.
  Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1);
  let cookies = Services.cookies.getCookiesFromHost("baz.com", {});
  let cookie = cookies[0];
  Assert.equal(cookie.expiry, futureExpiry + 44);

  do_close_profile(test_generator);
  yield;

  // Open the database so we can execute some more schema 2 statements on it.
  schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);

  // Populate it with more cookies.
  for (let i = 60; i < 80; ++i) {
    schema2db.insertCookie(
      new Cookie(
        "oh" + i,
        "hai",
        "foo.com",
        "/",
        futureExpiry,
        now,
        now + i,
        false,
        false,
        false
      )
    );
  }
  for (let i = 80; i < 100; ++i) {
    schema2db.insertCookie(
      new Cookie(
        "oh" + i,
        "hai",
        "cat.com",
        "/",
        futureExpiry,
        now,
        now + i,
        false,
        false,
        false
      )
    );
  }

  // Attempt to add a cookie with the same (name, host, path) values as another
  // cookie. This should succeed since we have a REPLACE clause for conflict on
  // the unique index.
  cookie = new Cookie(
    "oh",
    "hai",
    "baz.com",
    "/",
    futureExpiry,
    now,
    now + 100,
    false,
    false,
    false
  );

  schema2db.insertCookie(cookie);

  // Check that there is, indeed, a singular cookie for baz.com.
  Assert.equal(do_count_cookies_in_db(schema2db.db, "baz.com"), 1);

  // Close it.
  schema2db.close();
  schema2db = null;

  // Back up the database, so we can test both asynchronous and synchronous
  // loading separately.
  let file = do_get_cookie_file(profile);
  let copy = profile.clone();
  copy.append("cookies.sqlite.copy");
  file.copyTo(null, copy.leafName);

  // Load the database asynchronously, forcing a purge of the newly-added
  // cookies. (Their baseDomain column will be NULL.)
  do_load_profile(test_generator);
  yield;

  // Test the expected set of cookies.
  Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 40);
  Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20);
  Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1);
  Assert.equal(Services.cookies.countCookiesFromHost("cat.com"), 20);

  do_close_profile(test_generator);
  yield;

  // Copy the database back.
  file.remove(false);
  copy.copyTo(null, file.leafName);

  // Load the database host-at-a-time.
  do_load_profile();

  // Test the expected set of cookies.
  Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 40);
  Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20);
  Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1);
  Assert.equal(Services.cookies.countCookiesFromHost("cat.com"), 20);

  do_close_profile(test_generator);
  yield;

  // Open the database and prove that they were deleted.
  schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
  Assert.equal(do_count_cookies_in_db(schema2db.db), 81);
  Assert.equal(do_count_cookies_in_db(schema2db.db, "foo.com"), 40);
  Assert.equal(do_count_cookies_in_db(schema2db.db, "bar.com"), 20);
  schema2db.close();

  // Copy the database back.
  file.remove(false);
  copy.copyTo(null, file.leafName);

  // Load the database synchronously, in its entirety.
  do_load_profile();
  Assert.equal(do_count_cookies(), 81);

  // Test the expected set of cookies.
  Assert.equal(Services.cookies.countCookiesFromHost("foo.com"), 40);
  Assert.equal(Services.cookies.countCookiesFromHost("bar.com"), 20);
  Assert.equal(Services.cookies.countCookiesFromHost("baz.com"), 1);
  Assert.equal(Services.cookies.countCookiesFromHost("cat.com"), 20);

  do_close_profile(test_generator);
  yield;

  // Open the database and prove that they were deleted.
  schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
  Assert.equal(do_count_cookies_in_db(schema2db.db), 81);
  Assert.equal(do_count_cookies_in_db(schema2db.db, "foo.com"), 40);
  Assert.equal(do_count_cookies_in_db(schema2db.db, "bar.com"), 20);
  schema2db.close();

  finish_test();
}