tor-browser

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

test_multipart_streamconv_missing_lead_boundary.js (2221B)


      1 "use strict";
      2 
      3 const { HttpServer } = ChromeUtils.importESModule(
      4  "resource://testing-common/httpd.sys.mjs"
      5 );
      6 
      7 var httpserver = null;
      8 
      9 ChromeUtils.defineLazyGetter(this, "uri", function () {
     10  return "http://localhost:" + httpserver.identity.primaryPort + "/multipart";
     11 });
     12 
     13 function make_channel(url) {
     14  return NetUtil.newChannel({ uri: url, loadUsingSystemPrincipal: true });
     15 }
     16 
     17 var multipartBody =
     18  "Preamble\r\n--boundary\r\n\r\nSome text\r\n--boundary\r\n\r\n<?xml version='1.0'?><root/>\r\n--boundary--";
     19 
     20 function contentHandler(metadata, response) {
     21  response.setHeader("Content-Type", 'multipart/mixed; boundary="boundary"');
     22  response.bodyOutputStream.write(multipartBody, multipartBody.length);
     23 }
     24 
     25 var numTests = 2;
     26 var testNum = 0;
     27 
     28 var testData = [
     29  { data: "Some text", type: "text/plain" },
     30  { data: "<?xml version='1.0'?><root/>", type: "text/xml" },
     31 ];
     32 
     33 function responseHandler(request, buffer) {
     34  Assert.equal(buffer, testData[testNum].data);
     35  Assert.equal(
     36    request.QueryInterface(Ci.nsIChannel).contentType,
     37    testData[testNum].type
     38  );
     39  if (++testNum == numTests) {
     40    httpserver.stop(do_test_finished);
     41  }
     42 }
     43 
     44 var multipartListener = {
     45  _buffer: "",
     46 
     47  QueryInterface: ChromeUtils.generateQI([
     48    "nsIStreamListener",
     49    "nsIRequestObserver",
     50  ]),
     51 
     52  onStartRequest() {
     53    this._buffer = "";
     54  },
     55 
     56  onDataAvailable(request, stream, offset, count) {
     57    try {
     58      this._buffer = this._buffer.concat(read_stream(stream, count));
     59      dump("BUFFEEE: " + this._buffer + "\n\n");
     60    } catch (ex) {
     61      do_throw("Error in onDataAvailable: " + ex);
     62    }
     63  },
     64 
     65  onStopRequest(request) {
     66    try {
     67      responseHandler(request, this._buffer);
     68    } catch (ex) {
     69      do_throw("Error in closure function: " + ex);
     70    }
     71  },
     72 };
     73 
     74 function run_test() {
     75  httpserver = new HttpServer();
     76  httpserver.registerPathHandler("/multipart", contentHandler);
     77  httpserver.start(-1);
     78 
     79  var streamConv = Cc["@mozilla.org/streamConverters;1"].getService(
     80    Ci.nsIStreamConverterService
     81  );
     82  var conv = streamConv.asyncConvertData(
     83    "multipart/mixed",
     84    "*/*",
     85    multipartListener,
     86    null
     87  );
     88 
     89  var chan = make_channel(uri);
     90  chan.asyncOpen(conv);
     91  do_test_pending();
     92 }