summaryrefslogtreecommitdiffstats
path: root/extensions/cookie/test/unit/test_cookies_sync_failure.js
blob: 2de1de628d4c4aa72a2ae8ba1c8509454779352c (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
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

// Test the various ways opening a cookie database can fail in a synchronous
// (i.e. immediate) manner, and that the database is renamed and recreated
// under each circumstance. These circumstances are, in no particular order:
//
// 1) A corrupt database, such that opening the connection fails.
// 2) The 'moz_cookies' table doesn't exist.
// 3) Not all of the expected columns exist, and statement creation fails when:
//    a) The schema version is larger than the current version.
//    b) The schema version is less than or equal to the current version.
// 4) Migration fails. This will have different modes depending on the initial
//    version:
//    a) Schema 1: the 'lastAccessed' column already exists.
//    b) Schema 2: the 'baseDomain' column already exists; or 'baseDomain'
//       cannot be computed for a particular host.
//    c) Schema 3: the 'creationTime' column already exists; or the
//       'moz_uniqueid' index already exists.

var COOKIE_DATABASE_SCHEMA_CURRENT = 7;

var test_generator = do_run_test();

function run_test() {
  do_test_pending();
  do_run_generator(test_generator);
}

function finish_test() {
  do_execute_soon(function() {
    test_generator.close();
    do_test_finished();
  });
}

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

  // Allow all cookies.
  Services.prefs.setIntPref("network.cookie.cookieBehavior", 0);

  // Get the cookie file and the backup file.
  this.cookieFile = profile.clone();
  cookieFile.append("cookies.sqlite");
  this.backupFile = profile.clone();
  backupFile.append("cookies.sqlite.bak");
  do_check_false(cookieFile.exists());
  do_check_false(backupFile.exists());

  // Create a cookie object for testing.
  this.now = Date.now() * 1000;
  this.futureExpiry = Math.round(this.now / 1e6 + 1000);
  this.cookie = new Cookie("oh", "hai", "bar.com", "/", this.futureExpiry,
    this.now, this.now, false, false, false);

  this.sub_generator = run_test_1(test_generator);
  sub_generator.next();
  yield;

  this.sub_generator = run_test_2(test_generator);
  sub_generator.next();
  yield;

  this.sub_generator = run_test_3(test_generator, 99);
  sub_generator.next();
  yield;

  this.sub_generator = run_test_3(test_generator, COOKIE_DATABASE_SCHEMA_CURRENT);
  sub_generator.next();
  yield;

  this.sub_generator = run_test_3(test_generator, 4);
  sub_generator.next();
  yield;

  this.sub_generator = run_test_3(test_generator, 3);
  sub_generator.next();
  yield;

  this.sub_generator = run_test_4_exists(test_generator, 1,
    "ALTER TABLE moz_cookies ADD lastAccessed INTEGER");
  sub_generator.next();
  yield;

  this.sub_generator = run_test_4_exists(test_generator, 2,
    "ALTER TABLE moz_cookies ADD baseDomain TEXT");
  sub_generator.next();
  yield;

  this.sub_generator = run_test_4_baseDomain(test_generator);
  sub_generator.next();
  yield;

  this.sub_generator = run_test_4_exists(test_generator, 3,
    "ALTER TABLE moz_cookies ADD creationTime INTEGER");
  sub_generator.next();
  yield;

  this.sub_generator = run_test_4_exists(test_generator, 3,
    "CREATE UNIQUE INDEX moz_uniqueid ON moz_cookies (name, host, path)");
  sub_generator.next();
  yield;

  finish_test();
  return;
}

const garbage = "hello thar!";

function create_garbage_file(file)
{
  // Create an empty database file.
  file.create(Ci.nsIFile.NORMAL_FILE_TYPE, -1);
  do_check_true(file.exists());
  do_check_eq(file.fileSize, 0);

  // Write some garbage to it.
  let ostream = Cc["@mozilla.org/network/file-output-stream;1"].
                createInstance(Ci.nsIFileOutputStream);
  ostream.init(file, -1, -1, 0);
  ostream.write(garbage, garbage.length);
  ostream.flush();
  ostream.close();

  file = file.clone(); // Windows maintains a stat cache. It's lame.
  do_check_eq(file.fileSize, garbage.length);
}

function check_garbage_file(file)
{
  do_check_true(file.exists());
  do_check_eq(file.fileSize, garbage.length);
  file.remove(false);
  do_check_false(file.exists());
}

function run_test_1(generator)
{
  // Create a garbage database file.
  create_garbage_file(cookieFile);

  // Load the profile and populate it.
  let uri = NetUtil.newURI("http://foo.com/");
  Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);

  // Fake a profile change.
  do_close_profile(sub_generator);
  yield;
  do_load_profile();

  // Check that the new database contains the cookie, and the old file was
  // renamed.
  do_check_eq(do_count_cookies(), 1);
  check_garbage_file(backupFile);

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

  // Clean up.
  cookieFile.remove(false);
  do_check_false(cookieFile.exists());
  do_run_generator(generator);
}

function run_test_2(generator)
{
  // Load the profile and populate it.
  do_load_profile();
  let uri = NetUtil.newURI("http://foo.com/");
  Services.cookies.setCookieString(uri, null, "oh=hai; max-age=1000", null);

  // Fake a profile change.
  do_close_profile(sub_generator);
  yield;

  // Drop the table.
  let db = Services.storage.openDatabase(cookieFile);
  db.executeSimpleSQL("DROP TABLE moz_cookies");
  db.close();

  // Load the profile and check that the table is recreated in-place.
  do_load_profile();
  do_check_eq(do_count_cookies(), 0);
  do_check_false(backupFile.exists());

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

  // Clean up.
  cookieFile.remove(false);
  do_check_false(cookieFile.exists());
  do_run_generator(generator);
}

function run_test_3(generator, schema)
{
  // Manually create a schema 2 database, populate it, and set the schema
  // version to the desired number.
  let schema2db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
  schema2db.insertCookie(cookie);
  schema2db.db.schemaVersion = schema;
  schema2db.close();

  // Load the profile and check that the column existence test fails.
  do_load_profile();
  do_check_eq(do_count_cookies(), 0);

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

  // Check that the schema version has been reset.
  let db = Services.storage.openDatabase(cookieFile);
  do_check_eq(db.schemaVersion, COOKIE_DATABASE_SCHEMA_CURRENT);
  db.close();

  // Clean up.
  cookieFile.remove(false);
  do_check_false(cookieFile.exists());
  do_run_generator(generator);
}

function run_test_4_exists(generator, schema, stmt)
{
  // Manually create a database, populate it, and add the desired column.
  let db = new CookieDatabaseConnection(do_get_cookie_file(profile), schema);
  db.insertCookie(cookie);
  db.db.executeSimpleSQL(stmt);
  db.close();

  // Load the profile and check that migration fails.
  do_load_profile();
  do_check_eq(do_count_cookies(), 0);

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

  // Check that the schema version has been reset and the backup file exists.
  db = Services.storage.openDatabase(cookieFile);
  do_check_eq(db.schemaVersion, COOKIE_DATABASE_SCHEMA_CURRENT);
  db.close();
  do_check_true(backupFile.exists());

  // Clean up.
  cookieFile.remove(false);
  backupFile.remove(false);
  do_check_false(cookieFile.exists());
  do_check_false(backupFile.exists());
  do_run_generator(generator);
}

function run_test_4_baseDomain(generator)
{
  // Manually create a database and populate it with a bad host.
  let db = new CookieDatabaseConnection(do_get_cookie_file(profile), 2);
  let badCookie = new Cookie("oh", "hai", ".", "/", this.futureExpiry, this.now,
    this.now, false, false, false);
  db.insertCookie(badCookie);
  db.close();

  // Load the profile and check that migration fails.
  do_load_profile();
  do_check_eq(do_count_cookies(), 0);

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

  // Check that the schema version has been reset and the backup file exists.
  db = Services.storage.openDatabase(cookieFile);
  do_check_eq(db.schemaVersion, COOKIE_DATABASE_SCHEMA_CURRENT);
  db.close();
  do_check_true(backupFile.exists());

  // Clean up.
  cookieFile.remove(false);
  backupFile.remove(false);
  do_check_false(cookieFile.exists());
  do_check_false(backupFile.exists());
  do_run_generator(generator);
}