tor-browser

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

test_persist_journal.js (2355B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 // Tests the journal persists on close.
      5 
      6 async function check_journal_persists(db, journal) {
      7  let path = db.databaseFile.path;
      8  info(`testing ${path}`);
      9  await new Promise(resolve => {
     10    db.executeSimpleSQLAsync(`PRAGMA journal_mode = ${journal}`, {
     11      handleCompletion: resolve,
     12    });
     13  });
     14 
     15  await new Promise(resolve => {
     16    db.executeSimpleSQLAsync("CREATE TABLE test (id INTEGER PRIMARY KEY)", {
     17      handleCompletion: resolve,
     18    });
     19  });
     20 
     21  if (journal == "wal") {
     22    Assert.ok(await IOUtils.exists(path + "-wal"), "-wal exists before close");
     23    Assert.greater(
     24      (await IOUtils.stat(path + "-wal")).size,
     25      0,
     26      "-wal size is non-zero"
     27    );
     28  } else {
     29    Assert.ok(
     30      await IOUtils.exists(path + "-journal"),
     31      "-journal exists before close"
     32    );
     33    Assert.equal(
     34      (await IOUtils.stat(path + "-journal")).size,
     35      0,
     36      "-journal is truncated after every transaction"
     37    );
     38  }
     39 
     40  await new Promise(resolve => db.asyncClose(resolve));
     41 
     42  if (journal == "wal") {
     43    Assert.ok(await IOUtils.exists(path + "-wal"), "-wal persists after close");
     44    Assert.equal(
     45      (await IOUtils.stat(path + "-wal")).size,
     46      0,
     47      "-wal has been truncated"
     48    );
     49  } else {
     50    Assert.ok(
     51      await IOUtils.exists(path + "-journal"),
     52      "-journal persists after close"
     53    );
     54    Assert.equal(
     55      (await IOUtils.stat(path + "-journal")).size,
     56      0,
     57      "-journal has been truncated"
     58    );
     59  }
     60 }
     61 
     62 async function getDbPath(name) {
     63  let path = PathUtils.join(PathUtils.profileDir, name + ".sqlite");
     64  Assert.ok(!(await IOUtils.exists(path)), "database should not exist");
     65  return path;
     66 }
     67 
     68 add_task(async function () {
     69  for (let journal of ["truncate", "wal"]) {
     70    await check_journal_persists(
     71      Services.storage.openDatabase(
     72        new FileUtils.File(await getDbPath(`shared-${journal}`))
     73      ),
     74      journal
     75    );
     76    await check_journal_persists(
     77      Services.storage.openUnsharedDatabase(
     78        new FileUtils.File(await getDbPath(`unshared-${journal}`))
     79      ),
     80      journal
     81    );
     82    await check_journal_persists(
     83      await openAsyncDatabase(
     84        new FileUtils.File(await getDbPath(`async-${journal}`))
     85      ),
     86      journal
     87    );
     88  }
     89 });