tor-browser

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

test_usageAfterMigration.js (3923B)


      1 /**
      2 * Any copyright is dedicated to the Public Domain.
      3 * http://creativecommons.org/publicdomain/zero/1.0/
      4 */
      5 
      6 add_task(async function testSteps() {
      7  const principal = getPrincipal("http://example.com");
      8 
      9  const dataFile = getRelativeFile(
     10    "storage/default/http+++example.com/ls/data.sqlite"
     11  );
     12 
     13  const usageJournalFile = getRelativeFile(
     14    "storage/default/http+++example.com/ls/usage-journal"
     15  );
     16 
     17  const usageFile = getRelativeFile(
     18    "storage/default/http+++example.com/ls/usage"
     19  );
     20 
     21  const data = {};
     22  data.key = "foo";
     23  data.value = "bar";
     24  data.usage = data.key.length + data.value.length;
     25 
     26  async function createStorageForMigration(createUsageDir) {
     27    info("Clearing");
     28 
     29    let request = clear();
     30    await requestFinished(request);
     31 
     32    info("Installing package");
     33 
     34    // The profile contains storage.sqlite and webappsstore.sqlite. The file
     35    // create_db.js in the package was run locally, specifically it was
     36    // temporarily added to xpcshell.toml and then executed:
     37    // mach xpcshell-test --interactive dom/localstorage/test/unit/create_db.js
     38    installPackage("usageAfterMigration_profile");
     39 
     40    if (createUsageDir) {
     41      // Origin must be initialized before the usage dir is created.
     42 
     43      info("Initializing storage");
     44 
     45      request = initStorage();
     46      await requestFinished(request);
     47 
     48      info("Initializing temporary storage");
     49 
     50      request = initTemporaryStorage();
     51      await requestFinished(request);
     52 
     53      info("Initializing origin");
     54 
     55      request = initTemporaryOrigin("default", principal);
     56      await requestFinished(request);
     57 
     58      info("Creating usage as a directory");
     59 
     60      // This will cause a failure during migration.
     61      usageFile.create(Ci.nsIFile.DIRECTORY_TYPE, parseInt("0755", 8));
     62    }
     63  }
     64 
     65  function verifyData() {
     66    ok(dataFile.exists(), "Data file does exist");
     67  }
     68 
     69  async function verifyUsage(success) {
     70    info("Verifying usage in memory");
     71 
     72    let request = getCachedOriginUsage(principal);
     73    await requestFinished(request);
     74 
     75    if (success) {
     76      is(request.result, data.usage, "Correct usage");
     77    } else {
     78      is(request.result, 0, "Zero usage");
     79    }
     80 
     81    info("Verifying usage on disk");
     82 
     83    if (success) {
     84      ok(!usageJournalFile.exists(), "Usage journal file doesn't exist");
     85      ok(usageFile.exists(), "Usage file does exist");
     86      let usage = await readUsageFromUsageFile(usageFile);
     87      is(usage, data.usage, "Correct usage");
     88    } else {
     89      ok(usageJournalFile.exists(), "Usage journal file does exist");
     90      ok(usageFile.exists(), "Usage file does exist");
     91    }
     92  }
     93 
     94  info("Setting prefs");
     95 
     96  Services.prefs.setBoolPref(
     97    "dom.storage.enable_unsupported_legacy_implementation",
     98    false
     99  );
    100 
    101  info("Stage 1 - Testing usage after successful data migration");
    102 
    103  await createStorageForMigration(/* createUsageDir */ false);
    104 
    105  info("Getting storage");
    106 
    107  let storage = getLocalStorage(principal);
    108 
    109  info("Opening");
    110 
    111  storage.open();
    112 
    113  verifyData();
    114 
    115  await verifyUsage(/* success */ true);
    116 
    117  info("Stage 2 - Testing usage after unsuccessful data migration");
    118 
    119  await createStorageForMigration(/* createUsageDir */ true);
    120 
    121  info("Getting storage");
    122 
    123  storage = getLocalStorage(principal);
    124 
    125  info("Opening");
    126 
    127  try {
    128    storage.open();
    129    ok(false, "Should have thrown");
    130  } catch (ex) {
    131    ok(true, "Did throw");
    132  }
    133 
    134  verifyData();
    135 
    136  await verifyUsage(/* success */ false);
    137 
    138  info("Stage 3 - Testing usage after unsuccessful/successful data migration");
    139 
    140  await createStorageForMigration(/* createUsageDir */ true);
    141 
    142  info("Getting storage");
    143 
    144  storage = getLocalStorage(principal);
    145 
    146  info("Opening");
    147 
    148  try {
    149    storage.open();
    150    ok(false, "Should have thrown");
    151  } catch (ex) {
    152    ok(true, "Did throw");
    153  }
    154 
    155  usageFile.remove(true);
    156 
    157  info("Opening");
    158 
    159  storage.open();
    160 
    161  verifyData();
    162 
    163  await verifyUsage(/* success */ true);
    164 });