tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

test_storage_service.js (5135B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 // This file tests the functions of mozIStorageService except for
      6 // openSpecialDatabase, which is tested by test_storage_service_special.js and
      7 // openUnsharedDatabase, which is tested by test_storage_service_unshared.js.
      8 
      9 function test_openDatabase_null_file() {
     10  try {
     11    Services.storage.openDatabase(null);
     12    do_throw("We should not get here!");
     13  } catch (e) {
     14    print(e);
     15    print("e.result is " + e.result);
     16    Assert.equal(Cr.NS_ERROR_INVALID_ARG, e.result);
     17  }
     18 }
     19 
     20 function test_openDatabase_file_DNE() {
     21  // the file should be created after calling
     22  var db = getTestDB();
     23  Assert.ok(!db.exists());
     24  Services.storage.openDatabase(db);
     25  Assert.ok(db.exists());
     26 }
     27 
     28 function test_openDatabase_file_exists() {
     29  // it should already exist from our last test
     30  var db = getTestDB();
     31  Assert.ok(db.exists());
     32  Services.storage.openDatabase(db);
     33  Assert.ok(db.exists());
     34 }
     35 
     36 function test_corrupt_db_throws_with_openDatabase() {
     37  let db = getCorruptDB();
     38 
     39  try {
     40    getDatabase(db);
     41    do_throw("should not be here");
     42  } catch (e) {
     43    Assert.equal(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
     44  }
     45 
     46  Assert.equal(
     47    Glean.sqliteStore.query.get(db.leafName, "corrupt").testGetValue(),
     48    1
     49  );
     50 }
     51 
     52 function test_fake_db_throws_with_openDatabase() {
     53  let db = getFakeDB();
     54 
     55  try {
     56    getDatabase(db);
     57    do_throw("should not be here");
     58  } catch (e) {
     59    Assert.equal(Cr.NS_ERROR_FILE_CORRUPTED, e.result);
     60  }
     61 
     62  Assert.equal(
     63    Glean.sqliteStore.query.get(db.leafName, "corrupt").testGetValue(),
     64    1
     65  );
     66 }
     67 
     68 function test_openDatabase_directory() {
     69  let dir = getTestDB().parent;
     70  dir.append("test_storage_temp");
     71  if (dir.exists()) {
     72    dir.remove(true);
     73  }
     74  dir.create(Ci.nsIFile.DIRECTORY_TYPE, 0o755);
     75  Assert.ok(dir.exists());
     76 
     77  try {
     78    getDatabase(dir);
     79    do_throw("should not be here");
     80  } catch (e) {
     81    Assert.equal(Cr.NS_ERROR_FILE_ACCESS_DENIED, e.result);
     82  }
     83 
     84  Assert.equal(
     85    Glean.sqliteStore.open.get("test_storage_temp", "access").testGetValue(),
     86    1
     87  );
     88 
     89  dir.remove(true);
     90 }
     91 
     92 function test_read_gooddb() {
     93  let file = do_get_file("goodDB.sqlite");
     94  let db = getDatabase(file);
     95 
     96  db.executeSimpleSQL("SELECT * FROM Foo;");
     97 
     98  Assert.equal(
     99    Glean.sqliteStore.query.get(file.leafName, "failure").testGetValue(),
    100    null
    101  );
    102 
    103  let stmt = db.createStatement("SELECT id from Foo");
    104 
    105  while (true) {
    106    if (!stmt.executeStep()) {
    107      break;
    108    }
    109  }
    110 
    111  stmt.finalize();
    112 
    113  Assert.equal(
    114    Glean.sqliteStore.query.get(file.leafName, "failure").testGetValue(),
    115    null
    116  );
    117 
    118  Assert.throws(
    119    () => db.executeSimpleSQL("INSERT INTO Foo (rowid) VALUES ('test');"),
    120    /NS_ERROR_FAILURE/,
    121    "Executing sql should fail."
    122  );
    123  Assert.equal(
    124    Glean.sqliteStore.query.get(file.leafName, "misuse").testGetValue(),
    125    1
    126  );
    127 }
    128 
    129 function test_read_baddb() {
    130  let file = do_get_file("baddataDB.sqlite");
    131  let db = getDatabase(file);
    132 
    133  Assert.throws(
    134    () => db.executeSimpleSQL("SELECT * FROM Foo"),
    135    /NS_ERROR_FILE_CORRUPTED/,
    136    "Executing sql should fail."
    137  );
    138 
    139  Assert.equal(
    140    Glean.sqliteStore.query.get(file.leafName, "corrupt").testGetValue(),
    141    1
    142  );
    143 
    144  let stmt = db.createStatement("SELECT * FROM Foo");
    145  Assert.throws(
    146    () => stmt.executeStep(),
    147    /NS_ERROR_FILE_CORRUPTED/,
    148    "Executing a statement should fail."
    149  );
    150 
    151  Assert.equal(
    152    Glean.sqliteStore.query.get(file.leafName, "corrupt").testGetValue(),
    153    2
    154  );
    155 }
    156 
    157 function test_busy_telemetry() {
    158  // Thunderbird doesn't have one or more of the probes used in this test.
    159  // Ensure the data is collected anyway.
    160  Services.prefs.setBoolPref(
    161    "toolkit.telemetry.testing.overrideProductsCheck",
    162    true
    163  );
    164 
    165  let file = do_get_file("goodDB.sqlite");
    166  let conn1 = Services.storage.openUnsharedDatabase(file);
    167  let conn2 = Services.storage.openUnsharedDatabase(file);
    168 
    169  conn1.beginTransaction();
    170  conn1.executeSimpleSQL("CREATE TABLE test (id INTEGER PRIMARY KEY)");
    171 
    172  Assert.throws(
    173    () =>
    174      conn2.executeSimpleSQL("CREATE TABLE test_busy (id INTEGER PRIMARY KEY)"),
    175    /NS_ERROR_STORAGE_BUSY/,
    176    "Nested transaction on second connection should fail"
    177  );
    178  Assert.equal(
    179    Glean.sqliteStore.query.get(file.leafName, "busy").testGetValue(),
    180    1
    181  );
    182 
    183  conn1.rollbackTransaction();
    184 }
    185 
    186 var tests = [
    187  test_openDatabase_null_file,
    188  test_openDatabase_file_DNE,
    189  test_openDatabase_file_exists,
    190  test_corrupt_db_throws_with_openDatabase,
    191  test_fake_db_throws_with_openDatabase,
    192  test_openDatabase_directory,
    193  test_read_gooddb,
    194  test_read_baddb,
    195  test_busy_telemetry,
    196 ];
    197 
    198 function run_test() {
    199  // Thunderbird doesn't have one or more of the probes used in this test.
    200  // Ensure the data is collected anyway.
    201  Services.prefs.setBoolPref(
    202    "toolkit.telemetry.testing.overrideProductsCheck",
    203    true
    204  );
    205 
    206  for (var i = 0; i < tests.length; i++) {
    207    tests[i]();
    208    Services.fog.testResetFOG();
    209  }
    210 
    211  cleanup();
    212 }