tor-browser

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

test_bulk_error.js (2811B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 function run_test() {
      7  initTestDevToolsServer();
      8  add_test_bulk_actor();
      9 
     10  add_task(async function () {
     11    await test_string_error(socket_transport);
     12    await test_string_error(local_transport);
     13    DevToolsServer.destroy();
     14  });
     15 
     16  run_next_test();
     17 }
     18 
     19 /*** Sample Bulk Actor ***/
     20 const protocol = require("resource://devtools/shared/protocol.js");
     21 const { Arg, RetVal } = protocol;
     22 const { Actor } = require("resource://devtools/shared/protocol/Actor.js");
     23 
     24 const testBulkSpec = protocol.generateActorSpec({
     25  typeName: "testBulk",
     26 
     27  methods: {
     28    jsonReply: {
     29      request: protocol.BULK_REQUEST,
     30      response: { allDone: RetVal("number") },
     31    },
     32    bulkReply: {
     33      request: { foo: Arg(0, "number") },
     34      response: protocol.BULK_RESPONSE,
     35    },
     36  },
     37 });
     38 
     39 class TestBulkActor extends Actor {
     40  constructor(conn) {
     41    super(conn, testBulkSpec);
     42  }
     43 
     44  jsonReply({ length }) {
     45    Assert.equal(length, really_long().length);
     46 
     47    return {
     48      allDone: true,
     49    };
     50  }
     51 
     52  async bulkReply({ foo }) {
     53    Assert.equal(foo, 42, "received the bulk reply request");
     54 
     55    throw new Error("actor exception");
     56  }
     57 }
     58 
     59 class TestBulkFront extends protocol.FrontClassWithSpec(testBulkSpec) {
     60  formAttributeName = "testBulk";
     61  form(form) {
     62    this.actor = form.actor;
     63  }
     64 }
     65 protocol.registerFront(TestBulkFront);
     66 
     67 function add_test_bulk_actor() {
     68  ActorRegistry.addGlobalActor(
     69    {
     70      constructorName: "TestBulkActor",
     71      constructorFun: TestBulkActor,
     72    },
     73    "testBulk"
     74  );
     75 }
     76 
     77 /*** Tests ***/
     78 
     79 var test_string_error = async function (transportFactory) {
     80  const transport = await transportFactory();
     81 
     82  const client = new DevToolsClient(transport);
     83  await client.connect();
     84  await client.mainRoot.rootForm;
     85  const front = await client.mainRoot.getFront("testBulk");
     86 
     87  try {
     88    await front.bulkReply({ foo: 42 });
     89    Assert.ok(false, "bulkReply should have thrown");
     90  } catch (e) {
     91    Assert.stringContains(e.message, "actor exception");
     92  }
     93 
     94  const reallyLong = really_long();
     95 
     96  const input = Cc["@mozilla.org/io/string-input-stream;1"].createInstance(
     97    Ci.nsIStringInputStream
     98  );
     99  input.setByteStringData(reallyLong);
    100 
    101  const { promise, resolve } = Promise.withResolvers();
    102  function bulkSendReadyCallback({ writer, done }) {
    103    try {
    104      // Send bulk data to server
    105      writer.copyFrom(input, () => {
    106        input.close();
    107        done();
    108      });
    109      do_throw(new Error("Copying should fail, the stream is not async."));
    110    } catch (e) {
    111      Assert.ok(true);
    112      resolve();
    113    }
    114  }
    115  await front.jsonReply({ length: reallyLong.length }, bulkSendReadyCallback);
    116  await promise;
    117 
    118  client.close();
    119  transport.close();
    120 };