tor-browser

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

test_bug470716.js (4353B)


      1 "use strict";
      2 
      3 var CC = Components.Constructor;
      4 
      5 const StreamCopier = CC(
      6  "@mozilla.org/network/async-stream-copier;1",
      7  "nsIAsyncStreamCopier",
      8  "init"
      9 );
     10 
     11 const ScriptableInputStream = CC(
     12  "@mozilla.org/scriptableinputstream;1",
     13  "nsIScriptableInputStream",
     14  "init"
     15 );
     16 
     17 const Pipe = CC("@mozilla.org/pipe;1", "nsIPipe", "init");
     18 
     19 var pipe1;
     20 var pipe2;
     21 var copier;
     22 var test_result;
     23 var test_content;
     24 var test_source_closed;
     25 var test_sink_closed;
     26 var test_nr;
     27 
     28 var copyObserver = {
     29  onStartRequest() {},
     30 
     31  onStopRequest(request, statusCode) {
     32    // check status code
     33    Assert.equal(statusCode, test_result);
     34 
     35    // check number of copied bytes
     36    Assert.equal(pipe2.inputStream.available(), test_content.length);
     37 
     38    // check content
     39    var scinp = new ScriptableInputStream(pipe2.inputStream);
     40    var content = scinp.read(scinp.available());
     41    Assert.equal(content, test_content);
     42 
     43    // check closed sink
     44    try {
     45      pipe2.outputStream.write("closedSinkTest", 14);
     46      Assert.ok(!test_sink_closed);
     47    } catch (ex) {
     48      Assert.ok(test_sink_closed);
     49    }
     50 
     51    // check closed source
     52    try {
     53      pipe1.outputStream.write("closedSourceTest", 16);
     54      Assert.ok(!test_source_closed);
     55    } catch (ex) {
     56      Assert.ok(test_source_closed);
     57    }
     58 
     59    do_timeout(0, do_test);
     60  },
     61 
     62  QueryInterface: ChromeUtils.generateQI(["nsIRequestObserver"]),
     63 };
     64 
     65 function startCopier(closeSource, closeSink) {
     66  pipe1 = new Pipe(
     67    true /* nonBlockingInput */,
     68    true /* nonBlockingOutput */,
     69    0 /* segmentSize */,
     70    0xffffffff /* segmentCount */,
     71    null /* segmentAllocator */
     72  );
     73 
     74  pipe2 = new Pipe(
     75    true /* nonBlockingInput */,
     76    true /* nonBlockingOutput */,
     77    0 /* segmentSize */,
     78    0xffffffff /* segmentCount */,
     79    null /* segmentAllocator */
     80  );
     81 
     82  copier = new StreamCopier(
     83    pipe1.inputStream /* aSource */,
     84    pipe2.outputStream /* aSink */,
     85    null /* aTarget */,
     86    true /* aSourceBuffered */,
     87    true /* aSinkBuffered */,
     88    8192 /* aChunkSize */,
     89    closeSource /* aCloseSource */,
     90    closeSink /* aCloseSink */
     91  );
     92 
     93  copier.asyncCopy(copyObserver, null);
     94 }
     95 
     96 function do_test() {
     97  test_nr++;
     98  test_content = "test" + test_nr;
     99 
    100  switch (test_nr) {
    101    case 1:
    102    case 2: // close sink
    103    case 3: // close source
    104    case 4: // close both
    105      // test canceling transfer
    106      // use some undefined error code to check if it is successfully passed
    107      // to the request observer
    108      test_result = 0x87654321;
    109 
    110      test_source_closed = (test_nr - 1) >> 1 != 0;
    111      test_sink_closed = (test_nr - 1) % 2 != 0;
    112 
    113      startCopier(test_source_closed, test_sink_closed);
    114      pipe1.outputStream.write(test_content, test_content.length);
    115      pipe1.outputStream.flush();
    116      do_timeout(20, function () {
    117        copier.cancel(test_result);
    118        pipe1.outputStream.write("a", 1);
    119      });
    120      break;
    121    case 5:
    122    case 6: // close sink
    123    case 7: // close source
    124    case 8: // close both
    125      // test copying with EOF on source
    126      test_result = 0;
    127 
    128      test_source_closed = (test_nr - 5) >> 1 != 0;
    129      test_sink_closed = (test_nr - 5) % 2 != 0;
    130 
    131      startCopier(test_source_closed, test_sink_closed);
    132      pipe1.outputStream.write(test_content, test_content.length);
    133      // we will close the source
    134      test_source_closed = true;
    135      pipe1.outputStream.close();
    136      break;
    137    case 9:
    138    case 10: // close sink
    139    case 11: // close source
    140    case 12: // close both
    141      // test copying with error on sink
    142      // use some undefined error code to check if it is successfully passed
    143      // to the request observer
    144      test_result = 0x87654321;
    145 
    146      test_source_closed = (test_nr - 9) >> 1 != 0;
    147      test_sink_closed = (test_nr - 9) % 2 != 0;
    148 
    149      startCopier(test_source_closed, test_sink_closed);
    150      pipe1.outputStream.write(test_content, test_content.length);
    151      pipe1.outputStream.flush();
    152      // we will close the sink
    153      test_sink_closed = true;
    154      do_timeout(20, function () {
    155        pipe2.outputStream
    156          .QueryInterface(Ci.nsIAsyncOutputStream)
    157          .closeWithStatus(test_result);
    158        pipe1.outputStream.write("a", 1);
    159      });
    160      break;
    161    case 13:
    162      do_test_finished();
    163      break;
    164  }
    165 }
    166 
    167 function run_test() {
    168  test_nr = 0;
    169  do_timeout(0, do_test);
    170  do_test_pending();
    171 }