tor-browser

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

test_bug248970_cache.js (3436B)


      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 "use strict";
      6 
      7 // names for cache devices
      8 const kDiskDevice = "disk";
      9 const kMemoryDevice = "memory";
     10 
     11 const kCacheA = "http://cache/A";
     12 const kCacheA2 = "http://cache/A2";
     13 const kCacheB = "http://cache/B";
     14 const kTestContent = "test content";
     15 
     16 const entries = [
     17  // key       content       device          should exist after leaving PB
     18  [kCacheA, kTestContent, kMemoryDevice, true],
     19  [kCacheA2, kTestContent, kDiskDevice, false],
     20  [kCacheB, kTestContent, kDiskDevice, true],
     21 ];
     22 
     23 var store_idx;
     24 var store_cb = null;
     25 
     26 function store_entries(cb) {
     27  if (cb) {
     28    store_cb = cb;
     29    store_idx = 0;
     30  }
     31 
     32  if (store_idx == entries.length) {
     33    executeSoon(store_cb);
     34    return;
     35  }
     36 
     37  asyncOpenCacheEntry(
     38    entries[store_idx][0],
     39    entries[store_idx][2],
     40    Ci.nsICacheStorage.OPEN_TRUNCATE,
     41    Services.loadContextInfo.custom(false, {
     42      privateBrowsingId: entries[store_idx][3] ? 0 : 1,
     43    }),
     44    store_data
     45  );
     46 }
     47 
     48 var store_data = function (status, entry) {
     49  Assert.equal(status, Cr.NS_OK);
     50  var os = entry.openOutputStream(0, entries[store_idx][1].length);
     51 
     52  var written = os.write(entries[store_idx][1], entries[store_idx][1].length);
     53  if (written != entries[store_idx][1].length) {
     54    do_throw(
     55      "os.write has not written all data!\n" +
     56        "  Expected: " +
     57        entries[store_idx][1].length +
     58        "\n" +
     59        "  Actual: " +
     60        written +
     61        "\n"
     62    );
     63  }
     64  os.close();
     65  store_idx++;
     66  executeSoon(store_entries);
     67 };
     68 
     69 var check_idx;
     70 var check_cb = null;
     71 var check_pb_exited;
     72 function check_entries(cb, pbExited) {
     73  if (cb) {
     74    check_cb = cb;
     75    check_idx = 0;
     76    check_pb_exited = pbExited;
     77  }
     78 
     79  if (check_idx == entries.length) {
     80    executeSoon(check_cb);
     81    return;
     82  }
     83 
     84  asyncOpenCacheEntry(
     85    entries[check_idx][0],
     86    entries[check_idx][2],
     87    Ci.nsICacheStorage.OPEN_READONLY,
     88    Services.loadContextInfo.custom(false, {
     89      privateBrowsingId: entries[check_idx][3] ? 0 : 1,
     90    }),
     91    check_data
     92  );
     93 }
     94 
     95 var check_data = function (status, entry) {
     96  var cont = function () {
     97    check_idx++;
     98    executeSoon(check_entries);
     99  };
    100 
    101  if (!check_pb_exited || entries[check_idx][3]) {
    102    Assert.equal(status, Cr.NS_OK);
    103    var is = entry.openInputStream(0);
    104    pumpReadStream(is, function (read) {
    105      Assert.equal(read, entries[check_idx][1]);
    106      cont();
    107    });
    108  } else {
    109    Assert.equal(status, Cr.NS_ERROR_CACHE_KEY_NOT_FOUND);
    110    cont();
    111  }
    112 };
    113 
    114 function run_test() {
    115  // Simulate a profile dir for xpcshell
    116  do_get_profile();
    117 
    118  // Start off with an empty cache
    119  evict_cache_entries();
    120 
    121  // Store cache-A, cache-A2, cache-B and cache-C
    122  store_entries(run_test2);
    123 
    124  do_test_pending();
    125 }
    126 
    127 function run_test2() {
    128  // Check if cache-A, cache-A2, cache-B and cache-C are available
    129  check_entries(run_test3, false);
    130 }
    131 
    132 function run_test3() {
    133  // Simulate all private browsing instances being closed
    134  Services.obs.notifyObservers(null, "last-pb-context-exited");
    135 
    136  // Make sure the memory device is not empty
    137  get_device_entry_count(kMemoryDevice, null, function (count) {
    138    Assert.equal(count, 1);
    139    // Check if cache-A is gone, and cache-B and cache-C are still available
    140    check_entries(do_test_finished, true);
    141  });
    142 }