tor-browser

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

test_reopen.js (3118B)


      1 // This testcase verifies that channels can't be reopened
      2 // See https://bugzilla.mozilla.org/show_bug.cgi?id=372486
      3 
      4 "use strict";
      5 
      6 const { HttpServer } = ChromeUtils.importESModule(
      7  "resource://testing-common/httpd.sys.mjs"
      8 );
      9 
     10 const BinaryInputStream = Components.Constructor(
     11  "@mozilla.org/binaryinputstream;1",
     12  "nsIBinaryInputStream",
     13  "setInputStream"
     14 );
     15 
     16 const NS_ERROR_IN_PROGRESS = 0x804b000f;
     17 const NS_ERROR_ALREADY_OPENED = 0x804b0049;
     18 
     19 var chan = null;
     20 var httpserv = null;
     21 
     22 [test_data_channel, test_http_channel, test_file_channel, end].forEach(f =>
     23  add_test(f)
     24 );
     25 
     26 // Utility functions
     27 
     28 function makeChan(url) {
     29  return (chan = NetUtil.newChannel({
     30    uri: url,
     31    loadUsingSystemPrincipal: true,
     32  }).QueryInterface(Ci.nsIChannel));
     33 }
     34 
     35 function new_file_channel(file) {
     36  return NetUtil.newChannel({
     37    uri: Services.io.newFileURI(file),
     38    loadUsingSystemPrincipal: true,
     39  });
     40 }
     41 
     42 function check_throws(closure, error) {
     43  var thrown = false;
     44  try {
     45    closure();
     46  } catch (e) {
     47    if (error instanceof Array) {
     48      Assert.notEqual(error.indexOf(e.result), -1);
     49    } else {
     50      Assert.equal(e.result, error);
     51    }
     52    thrown = true;
     53  }
     54  Assert.ok(thrown);
     55 }
     56 
     57 function check_open_throws(error) {
     58  check_throws(function () {
     59    chan.open(listener);
     60  }, error);
     61 }
     62 
     63 function check_async_open_throws(error) {
     64  check_throws(function () {
     65    chan.asyncOpen(listener);
     66  }, error);
     67 }
     68 
     69 var listener = {
     70  onStartRequest: function test_onStartR() {
     71    check_async_open_throws(NS_ERROR_IN_PROGRESS);
     72  },
     73 
     74  onDataAvailable: function test_ODA(request, inputStream, offset, count) {
     75    new BinaryInputStream(inputStream).readByteArray(count); // required by API
     76    check_async_open_throws(NS_ERROR_IN_PROGRESS);
     77  },
     78 
     79  onStopRequest: function test_onStopR() {
     80    // Once onStopRequest is reached, the channel is marked as having been
     81    // opened
     82    check_async_open_throws(NS_ERROR_ALREADY_OPENED);
     83    do_timeout(0, after_channel_closed);
     84  },
     85 };
     86 
     87 function after_channel_closed() {
     88  check_async_open_throws(NS_ERROR_ALREADY_OPENED);
     89 
     90  run_next_test();
     91 }
     92 
     93 function test_channel(createChanClosure) {
     94  // First, synchronous reopening test
     95  chan = createChanClosure();
     96  chan.open();
     97  check_open_throws(NS_ERROR_IN_PROGRESS);
     98  check_async_open_throws([NS_ERROR_IN_PROGRESS, NS_ERROR_ALREADY_OPENED]);
     99 
    100  // Then, asynchronous one
    101  chan = createChanClosure();
    102  chan.asyncOpen(listener);
    103  check_open_throws(NS_ERROR_IN_PROGRESS);
    104  check_async_open_throws(NS_ERROR_IN_PROGRESS);
    105 }
    106 
    107 function test_data_channel() {
    108  test_channel(function () {
    109    return makeChan("data:text/plain,foo");
    110  });
    111 }
    112 
    113 function test_http_channel() {
    114  test_channel(function () {
    115    return makeChan("http://localhost:" + httpserv.identity.primaryPort + "/");
    116  });
    117 }
    118 
    119 function test_file_channel() {
    120  var file = do_get_file("data/test_readline1.txt");
    121  test_channel(function () {
    122    return new_file_channel(file);
    123  });
    124 }
    125 
    126 function end() {
    127  httpserv.stop(do_test_finished);
    128 }
    129 
    130 function run_test() {
    131  // start server
    132  httpserv = new HttpServer();
    133  httpserv.start(-1);
    134 
    135  run_next_test();
    136 }