tor-browser

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

test_cache2-02b-open-non-existing-and-doom.js (4327B)


      1 "use strict";
      2 
      3 add_task(async function test() {
      4  do_get_profile();
      5  do_test_pending();
      6 
      7  await new Promise(resolve => {
      8    // Open non-existing for read, should fail
      9    asyncOpenCacheEntry(
     10      "http://b/",
     11      "disk",
     12      Ci.nsICacheStorage.OPEN_READONLY,
     13      null,
     14      new OpenCallback(NOTFOUND, null, null, function (entry) {
     15        resolve(entry);
     16      })
     17    );
     18  });
     19 
     20  await new Promise(resolve => {
     21    // Open the same non-existing for read again, should fail second time
     22    asyncOpenCacheEntry(
     23      "http://b/",
     24      "disk",
     25      Ci.nsICacheStorage.OPEN_READONLY,
     26      null,
     27      new OpenCallback(NOTFOUND, null, null, function (entry) {
     28        resolve(entry);
     29      })
     30    );
     31  });
     32 
     33  await new Promise(resolve => {
     34    // Try it again normally, should go
     35    asyncOpenCacheEntry(
     36      "http://b/",
     37      "disk",
     38      Ci.nsICacheStorage.OPEN_NORMALLY,
     39      null,
     40      new OpenCallback(NEW, "b1m", "b1d", function (entry) {
     41        resolve(entry);
     42      })
     43    );
     44  });
     45 
     46  await new Promise(resolve => {
     47    // ...and check
     48    asyncOpenCacheEntry(
     49      "http://b/",
     50      "disk",
     51      Ci.nsICacheStorage.OPEN_NORMALLY,
     52      null,
     53      new OpenCallback(NORMAL, "b1m", "b1d", function (entry) {
     54        resolve(entry);
     55      })
     56    );
     57  });
     58 
     59  Services.prefs.setBoolPref("network.cache.bug1708673", true);
     60  registerCleanupFunction(() => {
     61    Services.prefs.clearUserPref("network.cache.bug1708673");
     62  });
     63 
     64  let asyncDoomVisitor = new Promise(resolve => {
     65    let doomTasks = [];
     66    let visitor = {
     67      onCacheStorageInfo() {},
     68      async onCacheEntryInfo(
     69        aURI,
     70        aIdEnhance,
     71        aDataSize,
     72        aAltDataSize,
     73        aFetchCount,
     74        aLastModifiedTime,
     75        aExpirationTime,
     76        aPinned,
     77        aInfo
     78      ) {
     79        doomTasks.push(
     80          new Promise(resolve1 => {
     81            Services.cache2
     82              .diskCacheStorage(aInfo, false)
     83              .asyncDoomURI(aURI, aIdEnhance, {
     84                onCacheEntryDoomed() {
     85                  info("doomed");
     86                  resolve1();
     87                },
     88              });
     89          })
     90        );
     91      },
     92      onCacheEntryVisitCompleted() {
     93        Promise.allSettled(doomTasks).then(resolve);
     94      },
     95      QueryInterface: ChromeUtils.generateQI(["nsICacheStorageVisitor"]),
     96    };
     97    Services.cache2.asyncVisitAllStorages(visitor, true);
     98  });
     99 
    100  let asyncOpenVisitor = new Promise(resolve => {
    101    let openTasks = [];
    102    let visitor = {
    103      onCacheStorageInfo() {},
    104      async onCacheEntryInfo(
    105        aURI,
    106        aIdEnhance,
    107        aDataSize,
    108        aAltDataSize,
    109        aFetchCount,
    110        aLastModifiedTime,
    111        aExpirationTime,
    112        aPinned,
    113        aInfo
    114      ) {
    115        info(`found ${aURI.spec}`);
    116        openTasks.push(
    117          new Promise(r2 => {
    118            Services.cache2
    119              .diskCacheStorage(aInfo, false)
    120              .asyncOpenURI(
    121                aURI,
    122                "",
    123                Ci.nsICacheStorage.OPEN_READONLY |
    124                  Ci.nsICacheStorage.OPEN_SECRETLY,
    125                {
    126                  onCacheEntryCheck() {
    127                    return Ci.nsICacheEntryOpenCallback.ENTRY_WANTED;
    128                  },
    129                  onCacheEntryAvailable() {
    130                    info("opened");
    131                    r2();
    132                  },
    133                  QueryInterface: ChromeUtils.generateQI([
    134                    "nsICacheEntryOpenCallback",
    135                  ]),
    136                }
    137              );
    138          })
    139        );
    140      },
    141      onCacheEntryVisitCompleted() {
    142        Promise.all(openTasks).then(resolve);
    143      },
    144      QueryInterface: ChromeUtils.generateQI(["nsICacheStorageVisitor"]),
    145    };
    146    Services.cache2.asyncVisitAllStorages(visitor, true);
    147  });
    148 
    149  await Promise.all([asyncDoomVisitor, asyncOpenVisitor]);
    150 
    151  info("finished visiting");
    152 
    153  await new Promise(resolve => {
    154    let entryCount = 0;
    155    let visitor = {
    156      onCacheStorageInfo() {},
    157      async onCacheEntryInfo() {
    158        entryCount++;
    159      },
    160      onCacheEntryVisitCompleted() {
    161        Assert.equal(entryCount, 0);
    162        resolve();
    163      },
    164      QueryInterface: ChromeUtils.generateQI(["nsICacheStorageVisitor"]),
    165    };
    166    Services.cache2.asyncVisitAllStorages(visitor, true);
    167  });
    168 
    169  finish_cache2_test();
    170 });