tor-browser

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

test_bug399727.js (2972B)


      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 
      6 const { AppConstants } = ChromeUtils.importESModule(
      7  "resource://gre/modules/AppConstants.sys.mjs"
      8 );
      9 
     10 function BinaryComparer(file, callback) {
     11  var fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
     12    Ci.nsIFileInputStream
     13  );
     14  fstream.init(file, -1, 0, 0);
     15  this.length = file.fileSize;
     16  this.fileStream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
     17    Ci.nsIBinaryInputStream
     18  );
     19  this.fileStream.setInputStream(fstream);
     20  this.offset = 0;
     21  this.callback = callback;
     22 }
     23 
     24 BinaryComparer.prototype = {
     25  fileStream: null,
     26  offset: null,
     27  length: null,
     28  callback: null,
     29 
     30  onStartRequest() {},
     31 
     32  onStopRequest(aRequest, aStatusCode) {
     33    this.fileStream.close();
     34    Assert.equal(aStatusCode, Cr.NS_OK);
     35    Assert.equal(this.offset, this.length);
     36    this.callback();
     37  },
     38 
     39  onDataAvailable(aRequest, aInputStream, aOffset, aCount) {
     40    var stream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(
     41      Ci.nsIBinaryInputStream
     42    );
     43    stream.setInputStream(aInputStream);
     44    var source, actual;
     45    for (var i = 0; i < aCount; i++) {
     46      try {
     47        source = this.fileStream.read8();
     48      } catch (e) {
     49        do_throw("Unable to read from file at offset " + this.offset + " " + e);
     50      }
     51      try {
     52        actual = stream.read8();
     53      } catch (e) {
     54        do_throw(
     55          "Unable to read from converted stream at offset " +
     56            this.offset +
     57            " " +
     58            e
     59        );
     60      }
     61      if (source != actual) {
     62        do_throw(
     63          "Invalid value " +
     64            actual +
     65            " at offset " +
     66            this.offset +
     67            ", should have been " +
     68            source
     69        );
     70      }
     71      this.offset++;
     72    }
     73  },
     74 };
     75 
     76 function comparer_callback() {
     77  do_test_finished();
     78 }
     79 
     80 function run_test() {
     81  var source = do_get_file(DATA_DIR + "test_bug399727.html");
     82  var comparer = new BinaryComparer(
     83    do_get_file(
     84      DATA_DIR +
     85        "test_bug399727" +
     86        (AppConstants.USE_LIBZ_RS ? ".libz-rs.zlib" : ".zlib")
     87    ),
     88    comparer_callback
     89  );
     90 
     91  // Prepare the stream converter
     92  var scs = Cc["@mozilla.org/streamConverters;1"].getService(
     93    Ci.nsIStreamConverterService
     94  );
     95  var converter = scs.asyncConvertData(
     96    "uncompressed",
     97    "deflate",
     98    comparer,
     99    null
    100  );
    101 
    102  // Open the expected output file
    103  var fstream = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
    104    Ci.nsIFileInputStream
    105  );
    106  fstream.init(source, -1, 0, 0);
    107 
    108  // Set up a pump to push data from the file to the stream converter
    109  var pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance(
    110    Ci.nsIInputStreamPump
    111  );
    112  pump.init(fstream, 0, 0, true);
    113  pump.asyncRead(converter);
    114  do_test_pending();
    115 }