tor-browser

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

test_alt-data_too_big.js (2455B)


      1 /**
      2 * Test for handling too big alternative data
      3 *
      4 *  - first we try to open an output stream for too big alt-data which must fail
      5 *    and leave original data intact
      6 *
      7 *  - then we open the output stream without passing predicted data size which
      8 *    succeeds but writing must fail later at the size limit and the original
      9 *    data must be kept
     10 */
     11 
     12 "use strict";
     13 
     14 var data = "data    ";
     15 var altData = "alt-data";
     16 
     17 function run_test() {
     18  do_get_profile();
     19 
     20  // Expand both data to 1MB
     21  for (let i = 0; i < 17; i++) {
     22    data += data;
     23    altData += altData;
     24  }
     25 
     26  // Set the limit so that the data fits but alt-data doesn't.
     27  Services.prefs.setIntPref("browser.cache.disk.max_entry_size", 1800);
     28 
     29  write_data();
     30 
     31  do_test_pending();
     32 }
     33 
     34 function write_data() {
     35  asyncOpenCacheEntry(
     36    "http://data/",
     37    "disk",
     38    Ci.nsICacheStorage.OPEN_NORMALLY,
     39    null,
     40    function (status, entry) {
     41      Assert.equal(status, Cr.NS_OK);
     42 
     43      var os = entry.openOutputStream(0, -1);
     44      var written = os.write(data, data.length);
     45      Assert.equal(written, data.length);
     46      os.close();
     47 
     48      open_big_altdata_output(entry);
     49    }
     50  );
     51 }
     52 
     53 function open_big_altdata_output(entry) {
     54  try {
     55    entry.openAlternativeOutputStream("text/binary", altData.length);
     56  } catch (e) {
     57    Assert.equal(e.result, Cr.NS_ERROR_FILE_TOO_BIG);
     58  }
     59 
     60  check_entry(write_big_altdata);
     61 }
     62 
     63 function write_big_altdata() {
     64  asyncOpenCacheEntry(
     65    "http://data/",
     66    "disk",
     67    Ci.nsICacheStorage.OPEN_NORMALLY,
     68    null,
     69    function (status, entry) {
     70      Assert.equal(status, Cr.NS_OK);
     71 
     72      var os = entry.openAlternativeOutputStream("text/binary", -1);
     73      try {
     74        os.write(altData, altData.length);
     75      } catch (e) {
     76        Assert.equal(e.result, Cr.NS_ERROR_FILE_TOO_BIG);
     77      }
     78      os.close();
     79 
     80      check_entry(do_test_finished);
     81    }
     82  );
     83 }
     84 
     85 function check_entry(cb) {
     86  asyncOpenCacheEntry(
     87    "http://data/",
     88    "disk",
     89    Ci.nsICacheStorage.OPEN_NORMALLY,
     90    null,
     91    function (status, entry) {
     92      Assert.equal(status, Cr.NS_OK);
     93 
     94      var is = null;
     95      try {
     96        is = entry.openAlternativeInputStream("text/binary");
     97      } catch (e) {
     98        Assert.equal(e.result, Cr.NS_ERROR_NOT_AVAILABLE);
     99      }
    100 
    101      is = entry.openInputStream(0);
    102      pumpReadStream(is, function (read) {
    103        Assert.equal(read.length, data.length);
    104        is.close();
    105 
    106        executeSoon(cb);
    107      });
    108    }
    109  );
    110 }