tor-browser

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

test_streamcopier.js (1543B)


      1 "use strict";
      2 
      3 var testStr = "This is a test. ";
      4 for (var i = 0; i < 10; ++i) {
      5  testStr += testStr;
      6 }
      7 
      8 function run_test() {
      9  // Set up our stream to copy
     10  var inStr = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
     11    Ci.nsIStringInputStream
     12  );
     13  inStr.setByteStringData(testStr);
     14 
     15  // Set up our destination stream.  Make sure to use segments a good
     16  // bit smaller than our data length.
     17  Assert.greater(testStr.length, 1024 * 10);
     18  var pipe = Cc["@mozilla.org/pipe;1"].createInstance(Ci.nsIPipe);
     19  pipe.init(true, true, 1024, 0xffffffff, null);
     20 
     21  var streamCopier = Cc[
     22    "@mozilla.org/network/async-stream-copier;1"
     23  ].createInstance(Ci.nsIAsyncStreamCopier);
     24  streamCopier.init(
     25    inStr,
     26    pipe.outputStream,
     27    null,
     28    true,
     29    true,
     30    1024,
     31    true,
     32    true
     33  );
     34 
     35  var ctx = {};
     36  ctx.wrappedJSObject = ctx;
     37 
     38  var observer = {
     39    onStartRequest() {},
     40    onStopRequest(aRequest, aStatusCode) {
     41      Assert.equal(aStatusCode, 0);
     42      var sis = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(
     43        Ci.nsIScriptableInputStream
     44      );
     45      sis.init(pipe.inputStream);
     46      var result = "";
     47      var temp;
     48      try {
     49        // Need this because read() can throw at EOF
     50        while ((temp = sis.read(1024))) {
     51          result += temp;
     52        }
     53      } catch (e) {
     54        Assert.equal(e.result, Cr.NS_BASE_STREAM_CLOSED);
     55      }
     56      Assert.equal(result, testStr);
     57      do_test_finished();
     58    },
     59  };
     60 
     61  streamCopier.asyncCopy(observer, ctx);
     62  do_test_pending();
     63 }