tor-browser

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

test_bug654926_test_seek.js (1436B)


      1 "use strict";
      2 
      3 function gen_1MiB() {
      4  var i;
      5  var data = "x";
      6  for (i = 0; i < 20; i++) {
      7    data += data;
      8  }
      9  return data;
     10 }
     11 
     12 function write_and_check(str, data, len) {
     13  var written = str.write(data, len);
     14  if (written != len) {
     15    do_throw(
     16      "str.write has not written all data!\n" +
     17        "  Expected: " +
     18        len +
     19        "\n" +
     20        "  Actual: " +
     21        written +
     22        "\n"
     23    );
     24  }
     25 }
     26 
     27 function write_datafile(status, entry) {
     28  Assert.equal(status, Cr.NS_OK);
     29  var data = gen_1MiB();
     30  var os = entry.openOutputStream(0, data.length);
     31 
     32  write_and_check(os, data, data.length);
     33 
     34  os.close();
     35 
     36  // try to open the entry for appending
     37  asyncOpenCacheEntry(
     38    "http://data/",
     39    "disk",
     40    Ci.nsICacheStorage.OPEN_NORMALLY,
     41    null,
     42    open_for_readwrite
     43  );
     44 }
     45 
     46 function open_for_readwrite(status, entry) {
     47  Assert.equal(status, Cr.NS_OK);
     48  var os = entry.openOutputStream(entry.dataSize, -1);
     49 
     50  // Opening the entry for appending data calls nsDiskCacheStreamIO::Seek()
     51  // which initializes mFD. If no data is written then mBufDirty is false and
     52  // mFD won't be closed in nsDiskCacheStreamIO::Flush().
     53 
     54  os.close();
     55 
     56  do_test_finished();
     57 }
     58 
     59 function run_test() {
     60  do_get_profile();
     61 
     62  // clear the cache
     63  evict_cache_entries();
     64 
     65  asyncOpenCacheEntry(
     66    "http://data/",
     67    "disk",
     68    Ci.nsICacheStorage.OPEN_NORMALLY,
     69    null,
     70    write_datafile
     71  );
     72 
     73  do_test_pending();
     74 }