summaryrefslogtreecommitdiffstats
path: root/comm/mailnews/db/gloda/test/unit/test_corrupt_database.js
blob: ff186e871a15f5288b164ca58f46a83124d8620e (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
/* 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/. */

/*
 * This test does not use glodaTestHelper because:
 * 1) We need to do things as part of the test without gloda having remotely
 *    thought about opening the database.
 * 2) We expect and desire that the logger produce a warning and glodaTestHelper
 *    takes the view that warnings = death.
 *
 * We do use the rest of the test infrastructure though.
 */

// -- Do configure the gloda prefs though...
// Yes to indexing.
Services.prefs.setBoolPref("mailnews.database.global.indexer.enabled", true);
// No to a sweep we don't control.
Services.prefs.setBoolPref(
  "mailnews.database.global.indexer.perform_initial_sweep",
  false
);

// We'll start with this datastore ID, and make sure it gets overwritten
// when the index is rebuilt.
var kDatastoreIDPref = "mailnews.database.global.datastore.id";
var kOriginalDatastoreID = "47e4bad6-fedc-4931-bf3f-d2f4146ac63e";
Services.prefs.setCharPref(kDatastoreIDPref, kOriginalDatastoreID);

/**
 * Create an illegal=corrupt database and make sure that we log a message and
 * still end up happy.
 */
add_task(function test_corrupt_databases_get_reported_and_blown_away() {
  // - Get the file path.
  let dbFile = Services.dirsvc.get("ProfD", Ci.nsIFile);
  dbFile.append("global-messages-db.sqlite");

  // - Protect dangerous people from themselves.
  // (There should not be a database at this point; if there is one, we are
  // not in the sandbox profile we expect.  I wouldn't bother except we're
  // going out of our way to write gibberish whereas gloda accidentally
  // opening a valid database is bad but not horrible.)
  if (dbFile.exists()) {
    do_throw("There should not be a database at this point.");
  }

  // - Create the file.
  dump("Creating gibberish file\n");
  let ostream = Cc["@mozilla.org/network/file-output-stream;1"].createInstance(
    Ci.nsIFileOutputStream
  );
  ostream.init(dbFile, -1, -1, 0);
  let fileContents = "I'm in ur database not being a database.\n";
  ostream.write(fileContents, fileContents.length);
  ostream.close();

  // - Init gloda, get warnings.
  dump("Init gloda\n");
  var { Gloda } = ChromeUtils.import(
    "resource:///modules/gloda/GlodaPublic.jsm"
  );
  dump("Gloda inited, checking\n");

  // - Make sure the datastore has an actual database.
  let { GlodaDatastore } = ChromeUtils.import(
    "resource:///modules/gloda/GlodaDatastore.jsm"
  );

  // Make sure that the datastoreID was overwritten
  Assert.notEqual(Gloda.datastoreID, kOriginalDatastoreID);
  // And for good measure, make sure that the pref was also overwritten
  let currentDatastoreID = Services.prefs.getCharPref(kDatastoreIDPref);
  Assert.notEqual(currentDatastoreID, kOriginalDatastoreID);
  // We'll also ensure that the Gloda.datastoreID matches the one stashed
  // in prefs...
  Assert.equal(currentDatastoreID, Gloda.datastoreID);
  // And finally, we'll make sure that the datastoreID is a string with length
  // greater than 0.
  Assert.equal(typeof Gloda.datastoreID, "string");
  Assert.ok(Gloda.datastoreID.length > 0);

  if (!GlodaDatastore.asyncConnection) {
    do_throw("No database connection suggests no database!");
  }
});