tor-browser

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

test_clearHistory_shutdown.js (5069B)


      1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
      2 /* vim:set ts=2 sw=2 sts=2 et: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 /**
      8 * Tests that requesting clear history at shutdown will really clear history.
      9 */
     10 
     11 const URIS = [
     12  "http://a.example1.com/",
     13  "http://b.example1.com/",
     14  "http://b.example2.com/",
     15  "http://c.example3.com/",
     16 ];
     17 
     18 const FTP_URL = "ftp://localhost/clearHistoryOnShutdown/";
     19 
     20 const { Sanitizer } = ChromeUtils.importESModule(
     21  "resource:///modules/Sanitizer.sys.mjs"
     22 );
     23 
     24 // Send the profile-after-change notification to the form history component to ensure
     25 // that it has been initialized.
     26 var formHistoryStartup = Cc[
     27  "@mozilla.org/satchel/form-history-startup;1"
     28 ].getService(Ci.nsIObserver);
     29 formHistoryStartup.observe(null, "profile-after-change", null);
     30 ChromeUtils.defineESModuleGetters(this, {
     31  FormHistory: "resource://gre/modules/FormHistory.sys.mjs",
     32 });
     33 
     34 var timeInMicroseconds = Date.now() * 1000;
     35 
     36 add_task(async function test_execute() {
     37  info("Avoiding full places initialization importing default bookmarks.");
     38  let { PlacesBrowserStartup } = ChromeUtils.importESModule(
     39    "moz-src:///browser/components/places/PlacesBrowserStartup.sys.mjs"
     40  );
     41  PlacesBrowserStartup.willImportDefaultBookmarks();
     42 
     43  Sanitizer.onStartup();
     44 
     45  Services.prefs.setBoolPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "cache", true);
     46  Services.prefs.setBoolPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "cookies", true);
     47  Services.prefs.setBoolPref(
     48    Sanitizer.PREF_SHUTDOWN_BRANCH + "offlineApps",
     49    true
     50  );
     51  Services.prefs.setBoolPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "history", true);
     52  Services.prefs.setBoolPref(
     53    Sanitizer.PREF_SHUTDOWN_BRANCH + "downloads",
     54    true
     55  );
     56  Services.prefs.setBoolPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "cookies", true);
     57  Services.prefs.setBoolPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "formdata", true);
     58  Services.prefs.setBoolPref(Sanitizer.PREF_SHUTDOWN_BRANCH + "sessions", true);
     59  Services.prefs.setBoolPref(
     60    Sanitizer.PREF_SHUTDOWN_BRANCH + "siteSettings",
     61    true
     62  );
     63 
     64  Services.prefs.setBoolPref(Sanitizer.PREF_SANITIZE_ON_SHUTDOWN, true);
     65 
     66  info("Add visits.");
     67  for (let aUrl of URIS) {
     68    await PlacesTestUtils.addVisits({
     69      uri: uri(aUrl),
     70      visitDate: timeInMicroseconds++,
     71      transition: PlacesUtils.history.TRANSITION_TYPED,
     72    });
     73  }
     74  info("Add cache.");
     75  await storeCache(FTP_URL, "testData");
     76  info("Add form history.");
     77  await addFormHistory();
     78  Assert.equal(await getFormHistoryCount(), 1, "Added form history");
     79 
     80  info("Simulate and wait shutdown.");
     81  await shutdownPlaces();
     82 
     83  Assert.equal(await getFormHistoryCount(), 0, "Form history cleared");
     84 
     85  let stmt = DBConn(true).createStatement(
     86    "SELECT id FROM moz_places WHERE url = :page_url "
     87  );
     88 
     89  try {
     90    URIS.forEach(function (aUrl) {
     91      stmt.params.page_url = aUrl;
     92      Assert.ok(!stmt.executeStep());
     93      stmt.reset();
     94    });
     95  } finally {
     96    stmt.finalize();
     97  }
     98 
     99  info("Check cache");
    100  // Check cache.
    101  await checkCache(FTP_URL);
    102 });
    103 
    104 function addFormHistory() {
    105  let now = Date.now() * 1000;
    106  return FormHistory.update({
    107    op: "add",
    108    fieldname: "testfield",
    109    value: "test",
    110    timesUsed: 1,
    111    firstUsed: now,
    112    lastUsed: now,
    113  });
    114 }
    115 
    116 async function getFormHistoryCount() {
    117  return FormHistory.count({ fieldname: "testfield" });
    118 }
    119 
    120 function storeCache(aURL, aContent) {
    121  let cache = Services.cache2;
    122  let storage = cache.diskCacheStorage(Services.loadContextInfo.default);
    123 
    124  return new Promise(resolve => {
    125    let storeCacheListener = {
    126      onCacheEntryCheck() {
    127        return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
    128      },
    129 
    130      onCacheEntryAvailable(entry, isnew, status) {
    131        Assert.equal(status, Cr.NS_OK);
    132 
    133        entry.setMetaDataElement("servertype", "0");
    134        var os = entry.openOutputStream(0, -1);
    135 
    136        var written = os.write(aContent, aContent.length);
    137        if (written != aContent.length) {
    138          do_throw(
    139            "os.write has not written all data!\n" +
    140              "  Expected: " +
    141              written +
    142              "\n" +
    143              "  Actual: " +
    144              aContent.length +
    145              "\n"
    146          );
    147        }
    148        os.close();
    149        resolve();
    150      },
    151    };
    152 
    153    storage.asyncOpenURI(
    154      Services.io.newURI(aURL),
    155      "",
    156      Ci.nsICacheStorage.OPEN_NORMALLY,
    157      storeCacheListener
    158    );
    159  });
    160 }
    161 
    162 function checkCache(aURL) {
    163  let cache = Services.cache2;
    164  let storage = cache.diskCacheStorage(Services.loadContextInfo.default);
    165 
    166  return new Promise(resolve => {
    167    let checkCacheListener = {
    168      onCacheEntryAvailable(entry, isnew, status) {
    169        Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
    170        resolve();
    171      },
    172    };
    173 
    174    storage.asyncOpenURI(
    175      Services.io.newURI(aURL),
    176      "",
    177      Ci.nsICacheStorage.OPEN_READONLY,
    178      checkCacheListener
    179    );
    180  });
    181 }