tor-browser

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

databaseShadowing-shared.js (3553B)


      1 /* import-globals-from head.js */
      2 
      3 /* eslint-disable mozilla/no-comparison-or-assignment-inside-ok */
      4 
      5 const principalInfos = [
      6  { url: "http://example.com", attrs: {} },
      7 
      8  { url: "http://origin.test", attrs: {} },
      9 
     10  { url: "http://prefix.test", attrs: {} },
     11  { url: "http://prefix.test", attrs: { userContextId: 10 } },
     12 
     13  { url: "http://pattern.test", attrs: { userContextId: 15 } },
     14  { url: "http://pattern.test:8080", attrs: { userContextId: 15 } },
     15  { url: "https://pattern.test", attrs: { userContextId: 15 } },
     16 ];
     17 
     18 const surrogate = String.fromCharCode(0xdc00);
     19 const replacement = String.fromCharCode(0xfffd);
     20 const beginning = "beginning";
     21 const ending = "ending";
     22 const complexValue = beginning + surrogate + surrogate + ending;
     23 const corruptedValue = beginning + replacement + replacement + ending;
     24 
     25 function enableNextGenLocalStorage() {
     26  info("Setting pref");
     27 
     28  Services.prefs.setBoolPref(
     29    "dom.storage.enable_unsupported_legacy_implementation",
     30    false
     31  );
     32 }
     33 
     34 function disableNextGenLocalStorage() {
     35  info("Setting pref");
     36 
     37  Services.prefs.setBoolPref(
     38    "dom.storage.enable_unsupported_legacy_implementation",
     39    true
     40  );
     41 }
     42 
     43 function storeData() {
     44  for (let i = 0; i < principalInfos.length; i++) {
     45    let principalInfo = principalInfos[i];
     46    let principal = getPrincipal(principalInfo.url, principalInfo.attrs);
     47 
     48    info("Getting storage");
     49 
     50    let storage = getLocalStorage(principal);
     51 
     52    info("Adding data");
     53 
     54    storage.setItem("key0", "value0");
     55    storage.clear();
     56    storage.setItem("key1", "value1");
     57    storage.removeItem("key1");
     58    storage.setItem("key2", "value2");
     59    storage.setItem("complexKey", complexValue);
     60 
     61    info("Closing storage");
     62 
     63    storage.close();
     64  }
     65 }
     66 
     67 function exportShadowDatabase(name) {
     68  info("Verifying shadow database");
     69 
     70  let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
     71  let shadowDatabase = profileDir.clone();
     72  shadowDatabase.append("webappsstore.sqlite");
     73 
     74  let exists = shadowDatabase.exists();
     75  ok(exists, "Shadow database does exist");
     76 
     77  info("Copying shadow database");
     78 
     79  let currentDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
     80  shadowDatabase.copyTo(currentDir, name);
     81 }
     82 
     83 function importShadowDatabase(name) {
     84  info("Verifying shadow database");
     85 
     86  let currentDir = Services.dirsvc.get("CurWorkD", Ci.nsIFile);
     87  let shadowDatabase = currentDir.clone();
     88  shadowDatabase.append(name);
     89 
     90  let exists = shadowDatabase.exists();
     91  if (!exists) {
     92    return false;
     93  }
     94 
     95  info("Copying shadow database");
     96 
     97  let profileDir = Services.dirsvc.get("ProfD", Ci.nsIFile);
     98  shadowDatabase.copyTo(profileDir, "webappsstore.sqlite");
     99 
    100  return true;
    101 }
    102 
    103 function verifyData(clearedOrigins, migrated = false) {
    104  for (let i = 0; i < principalInfos.length; i++) {
    105    let principalInfo = principalInfos[i];
    106    let principal = getPrincipal(principalInfo.url, principalInfo.attrs);
    107 
    108    info("Getting storage");
    109 
    110    let storage = getLocalStorage(principal);
    111 
    112    info("Verifying data");
    113 
    114    if (clearedOrigins.includes(i)) {
    115      ok(storage.getItem("key2") == null, "Correct value");
    116      ok(storage.getItem("complexKey") == null, "Correct value");
    117    } else {
    118      ok(storage.getItem("key0") == null, "Correct value");
    119      ok(storage.getItem("key1") == null, "Correct value");
    120      is(storage.getItem("key2"), "value2", "Correct value");
    121      is(
    122        storage.getItem("complexKey"),
    123        migrated ? corruptedValue : complexValue,
    124        "Correct value"
    125      );
    126    }
    127 
    128    info("Closing storage");
    129 
    130    storage.close();
    131  }
    132 }