summaryrefslogtreecommitdiffstats
path: root/browser/components/search/test/unit/test_domain_to_categories_store.js
blob: e3af0c8de586368b1c7f02043f72016f8cbb1a61 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
/* Any copyright is dedicated to the Public Domain.
  http://creativecommons.org/publicdomain/zero/1.0/ */

/**
 * Ensure that the domain to categories store public methods work as expected
 * and it handles all error cases as expected.
 */

ChromeUtils.defineESModuleGetters(this, {
  CATEGORIZATION_SETTINGS: "resource:///modules/SearchSERPTelemetry.sys.mjs",
  DomainToCategoriesStore: "resource:///modules/SearchSERPTelemetry.sys.mjs",
  sinon: "resource://testing-common/Sinon.sys.mjs",
  Sqlite: "resource://gre/modules/Sqlite.sys.mjs",
});

let store = new DomainToCategoriesStore();
let defaultStorePath;
let fileContents = [convertToBuffer({ foo: [0, 1] })];

async function createCorruptedStore() {
  info("Create a corrupted store.");
  let storePath = PathUtils.join(
    PathUtils.profileDir,
    CATEGORIZATION_SETTINGS.STORE_FILE
  );
  let src = PathUtils.join(do_get_cwd().path, "corruptDB.sqlite");
  await IOUtils.copy(src, storePath);
  Assert.ok(await IOUtils.exists(storePath), "Store exists.");
  return storePath;
}

function convertToBuffer(obj) {
  return new TextEncoder().encode(JSON.stringify(obj)).buffer;
}

/**
 * Deletes data from the store and removes any files that were generated due
 * to them.
 */
async function cleanup() {
  info("Clean up store.");

  // In these tests, we sometimes use read-only files to test permission error
  // handling. On Windows, we have to change it to writable to allow for their
  // deletion so that subsequent tests aren't affected.
  if (
    (await IOUtils.exists(defaultStorePath)) &&
    Services.appinfo.OS == "WINNT"
  ) {
    await IOUtils.setPermissions(defaultStorePath, 0o600);
  }

  await store.testDelete();
  Assert.equal(store.empty, true, "Store should be empty.");
  Assert.equal(await IOUtils.exists(defaultStorePath), false, "Store exists.");
  Assert.equal(
    await store.getVersion(),
    0,
    "Version number should be 0 when store is empty."
  );

  await store.uninit();
}

async function createReadOnlyStore() {
  info("Create a store that can't be read.");
  let storePath = PathUtils.join(
    PathUtils.profileDir,
    CATEGORIZATION_SETTINGS.STORE_FILE
  );

  let conn = await Sqlite.openConnection({ path: storePath });
  await conn.execute("CREATE TABLE test (id INTEGER PRIMARY KEY)");
  await conn.close();

  await changeStoreToReadOnly();
}

async function changeStoreToReadOnly() {
  info("Change store to read only.");
  let storePath = PathUtils.join(
    PathUtils.profileDir,
    CATEGORIZATION_SETTINGS.STORE_FILE
  );
  let stat = await IOUtils.stat(storePath);
  await IOUtils.setPermissions(storePath, 0o444);
  stat = await IOUtils.stat(storePath);
  Assert.equal(stat.permissions, 0o444, "Permissions should be read only.");
  Assert.ok(await IOUtils.exists(storePath), "Store exists.");
}

add_setup(async function () {
  // We need a profile directory to create the store and open a connection.
  do_get_profile();
  defaultStorePath = PathUtils.join(
    PathUtils.profileDir,
    CATEGORIZATION_SETTINGS.STORE_FILE
  );
  registerCleanupFunction(async () => {
    await cleanup();
  });
});

// Ensure the test only function deletes the store.
add_task(async function delete_store() {
  let storePath = await createCorruptedStore();
  await store.testDelete();
  Assert.ok(!(await IOUtils.exists(storePath)), "Store doesn't exist.");
});

/**
 * These tests check common no fail scenarios.
 */

add_task(async function init_insert_uninit() {
  await store.init();
  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [], "foo should not have a result.");
  Assert.equal(
    await store.getVersion(),
    0,
    "Version number should not be set."
  );
  Assert.equal(store.empty, true, "Store should be empty.");

  info("Try inserting after init.");
  await store.insertFileContents(fileContents, 1);

  result = await store.getCategories("foo");
  Assert.deepEqual(result, [0, 1], "foo should have a matching result.");
  Assert.equal(await store.getVersion(), 1, "Version number should be set.");
  Assert.equal(store.empty, false, "Store should not be empty.");

  info("Un-init store.");
  await store.uninit();

  result = await store.getCategories("foo");
  Assert.deepEqual(result, [], "foo should be removed from store.");
  Assert.equal(store.empty, true, "Store should be empty.");
  Assert.equal(await store.getVersion(), 0, "Version should be reset.");

  await cleanup();
});

add_task(async function insert_and_re_init() {
  await store.init();
  await store.insertFileContents(fileContents, 20240202);

  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [0, 1], "foo should have a matching result.");
  Assert.equal(
    await store.getVersion(),
    20240202,
    "Version number should be set."
  );
  Assert.equal(store.empty, false, "Is store empty.");

  info("Simulate a restart.");
  await store.uninit();
  await store.init();

  result = await store.getCategories("foo");
  Assert.deepEqual(
    result,
    [0, 1],
    "After restart, foo should still be in the store."
  );
  Assert.equal(
    await store.getVersion(),
    20240202,
    "Version number should still be in the store."
  );
  Assert.equal(store.empty, false, "Is store empty.");

  await cleanup();
});

// Simulate consecutive updates.
add_task(async function insert_multiple_times() {
  await store.init();
  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [], "foo should not have a result.");
  Assert.equal(
    await store.getVersion(),
    0,
    "Version number should not be set."
  );
  Assert.equal(store.empty, true, "Is store empty.");

  for (let i = 0; i < 3; ++i) {
    info("Try inserting after init.");
    await store.insertFileContents(fileContents, 1);

    result = await store.getCategories("foo");
    Assert.deepEqual(result, [0, 1], "foo should have a matching result.");
    Assert.equal(store.empty, false, "Is store empty.");
    Assert.equal(await store.getVersion(), 1, "Version number is set.");

    await store.dropData();
    result = await store.getCategories("foo");
    Assert.deepEqual(
      result,
      [],
      "After dropping data, foo should no longer have a matching result."
    );
    Assert.equal(await store.getVersion(), 0, "Version should be reset.");
    Assert.equal(store.empty, true, "Is store empty.");
  }

  await cleanup();
});

/**
 * The following tests check failures on store initialization.
 */

add_task(async function init_with_corrupted_store() {
  await createCorruptedStore();

  info("Initialize the store.");
  await store.init();

  info("Try inserting after the corrupted store was replaced.");
  await store.insertFileContents(fileContents, 1);

  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [0, 1], "foo should have a matching result.");
  Assert.equal(await store.getVersion(), 1, "Version number is set.");
  Assert.equal(store.empty, false, "Is store empty.");

  await cleanup();
});

add_task(async function init_with_unfixable_store() {
  let sandbox = sinon.createSandbox();
  sandbox.stub(Sqlite, "openConnection").throws();

  info("Initialize the store.");
  await store.init();

  info("Try inserting content even if the connection is impossible to fix.");
  await store.dropData();
  await store.insertFileContents(fileContents, 20240202);

  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [], "foo should not have a result.");
  Assert.equal(await store.getVersion(), 0, "Version should be reset.");
  Assert.equal(store.empty, true, "Store should be empty.");

  sandbox.restore();
  await cleanup();
});

add_task(async function init_read_only_store() {
  await createReadOnlyStore();
  await store.init();

  info("Insert contents into the store.");
  await store.insertFileContents(fileContents, 20240202);
  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [], "foo should not have a result.");
  Assert.equal(
    await store.getVersion(),
    0,
    "Version number should not be set."
  );
  Assert.equal(store.empty, true, "Store should be empty.");

  await cleanup();
});

add_task(async function init_close_to_shutdown() {
  let sandbox = sinon.createSandbox();
  sandbox.stub(Sqlite.shutdown, "addBlocker").throws(new Error());
  await store.init();

  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [], "foo should not have a result.");
  Assert.equal(
    await store.getVersion(),
    0,
    "Version number should not be set."
  );
  Assert.equal(store.empty, true, "Store should be empty.");

  sandbox.restore();
  await cleanup();
});

/**
 * The following tests check error handling when inserting data into the store.
 */

add_task(async function insert_broken_file() {
  await store.init();

  Assert.equal(
    await store.getVersion(),
    0,
    "Version number should not be set."
  );

  info("Try inserting one valid file and an invalid file.");
  let contents = [...fileContents, new ArrayBuffer(0).buffer];
  await store.insertFileContents(contents, 20240202);

  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [], "foo should not have a result.");
  Assert.equal(await store.getVersion(), 0, "Version should remain unset.");
  Assert.equal(store.empty, true, "Store should remain empty.");

  await cleanup();
});

add_task(async function insert_into_read_only_store() {
  await createReadOnlyStore();
  await store.init();

  await store.dropData();
  await store.insertFileContents(fileContents, 20240202);
  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [], "foo should not have a result.");
  Assert.equal(await store.getVersion(), 0, "Version should remain unset.");
  Assert.equal(store.empty, true, "Store should remain empty.");

  await cleanup();
});

// If the store becomes read only with content already inside of it,
// the next time we try opening it, we'll encounter an error trying to write to
// it. Since we are no longer able to manipulate it, the results should always
// be empty.
add_task(async function restart_with_read_only_store() {
  await store.init();
  await store.insertFileContents(fileContents, 20240202);

  info("Check store has content.");
  let result = await store.getCategories("foo");
  Assert.deepEqual(result, [0, 1], "foo should have a matching result.");
  Assert.equal(
    await store.getVersion(),
    20240202,
    "Version number should be set."
  );
  Assert.equal(store.empty, false, "Store should not be empty.");

  await changeStoreToReadOnly();
  await store.uninit();
  await store.init();

  result = await store.getCategories("foo");
  Assert.deepEqual(
    result,
    [],
    "foo should no longer have a matching value from the store."
  );
  Assert.equal(await store.getVersion(), 0, "Version number should be unset.");
  Assert.equal(store.empty, true, "Store should be empty.");

  await cleanup();
});