summaryrefslogtreecommitdiffstats
path: root/toolkit/components/satchel/test/unit/test_db_corrupt.js
blob: b53b5cd6d0a535830b768c3fb903f98a9fc39b5f (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
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var bakFile;

function run_test() {
  // ===== test init =====
  let testfile = do_get_file("formhistory_CORRUPT.sqlite");
  let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);

  // Cleanup from any previous tests or failures.
  let destFile = profileDir.clone();
  destFile.append("formhistory.sqlite");
  if (destFile.exists()) {
    destFile.remove(false);
  }

  bakFile = profileDir.clone();
  bakFile.append("formhistory.sqlite.corrupt");
  if (bakFile.exists()) {
    bakFile.remove(false);
  }

  testfile.copyTo(profileDir, "formhistory.sqlite");
  run_next_test();
}

add_test(function test_corruptFormHistoryDB_lazyCorruptInit1() {
  do_log_info("ensure FormHistory backs up a corrupt DB on initialization.");

  // DB init is done lazily so the DB shouldn't be created yet.
  Assert.ok(!bakFile.exists());
  // Doing any request to the DB should create it.
  countEntries(null, null, run_next_test);
});

add_test(function test_corruptFormHistoryDB_lazyCorruptInit2() {
  Assert.ok(bakFile.exists());
  bakFile.remove(false);
  run_next_test();
});

add_test(function test_corruptFormHistoryDB_emptyInit() {
  do_log_info(
    "test that FormHistory initializes an empty DB in place of corrupt DB."
  );

  (async function () {
    let count = await FormHistory.count({});
    Assert.equal(count, 0);
    count = await FormHistory.count({ fieldname: "name-A", value: "value-A" });
    Assert.equal(count, 0);
    run_next_test();
  })().catch(error => {
    do_throw("DB initialized after reading a corrupt DB file is not empty.");
  });
});

add_test(function test_corruptFormHistoryDB_addEntry() {
  do_log_info("test adding an entry to the empty DB.");

  updateEntry("add", "name-A", "value-A", function () {
    countEntries("name-A", "value-A", function (count) {
      Assert.ok(count == 1);
      run_next_test();
    });
  });
});

add_test(function test_corruptFormHistoryDB_removeEntry() {
  do_log_info("test removing an entry to the empty DB.");

  updateEntry("remove", "name-A", "value-A", function () {
    countEntries("name-A", "value-A", function (count) {
      Assert.ok(count == 0);
      run_next_test();
    });
  });
});