tor-browser

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

test_node_execute.js (3482B)


      1 // This test checks that the interaction between NodeServer.execute defined in
      2 // httpd.js and the node server that we're interacting with defined in
      3 // moz-http2.js is working properly.
      4 /* global my_defined_var */
      5 
      6 "use strict";
      7 
      8 const { NodeServer } = ChromeUtils.importESModule(
      9  "resource://testing-common/NodeServer.sys.mjs"
     10 );
     11 
     12 add_task(async function test_execute() {
     13  function f() {
     14    return "bla";
     15  }
     16  let id = await NodeServer.fork();
     17  equal(await NodeServer.execute(id, `"hello"`), "hello");
     18  equal(await NodeServer.execute(id, `(() => "hello")()`), "hello");
     19  equal(await NodeServer.execute(id, `my_defined_var = 1;`), 1);
     20  equal(await NodeServer.execute(id, `(() => my_defined_var)()`), 1);
     21  equal(await NodeServer.execute(id, `my_defined_var`), 1);
     22 
     23  await NodeServer.execute(id, `not_defined_var`)
     24    .then(() => {
     25      ok(false, "should have thrown");
     26    })
     27    .catch(e => {
     28      equal(e.message, "ReferenceError: not_defined_var is not defined");
     29      ok(
     30        e.stack.includes("moz-http2-child.js"),
     31        `stack should be coming from moz-http2-child.js - ${e.stack}`
     32      );
     33    });
     34  await NodeServer.execute("definitely_wrong_id", `"hello"`)
     35    .then(() => {
     36      ok(false, "should have thrown");
     37    })
     38    .catch(e => {
     39      equal(e.message, "Error: could not find id");
     40      ok(
     41        e.stack.includes("moz-http2.js"),
     42        `stack should be coming from moz-http2.js - ${e.stack}`
     43      );
     44    });
     45 
     46  // Defines f in the context of the node server.
     47  // The implementation of NodeServer.execute prepends `functionName =` to the
     48  // body of the function we pass so it gets attached to the global context
     49  // in the server.
     50  equal(await NodeServer.execute(id, f), undefined);
     51  equal(await NodeServer.execute(id, `f()`), "bla");
     52 
     53  class myClass {
     54    static doStuff() {
     55      return my_defined_var;
     56    }
     57  }
     58 
     59  equal(await NodeServer.execute(id, myClass), undefined);
     60  equal(await NodeServer.execute(id, `myClass.doStuff()`), 1);
     61 
     62  equal(await NodeServer.kill(id), undefined);
     63  await NodeServer.execute(id, `f()`)
     64    .then(() => ok(false, "should throw"))
     65    .catch(e => equal(e.message, "Error: could not find id"));
     66  id = await NodeServer.fork();
     67  // Check that a child process dying during a call throws an error.
     68  await NodeServer.execute(id, `process.exit()`)
     69    .then(() => ok(false, "should throw"))
     70    .catch(e =>
     71      equal(e.message, "child process exit closing code: 0 signal: null")
     72    );
     73 
     74  id = await NodeServer.fork();
     75  equal(
     76    await NodeServer.execute(
     77      id,
     78      `setTimeout(function() { sendBackResponse(undefined) }, 0); 2`
     79    ),
     80    2
     81  );
     82  await new Promise(resolve => do_timeout(10, resolve));
     83  await NodeServer.kill(id)
     84    .then(() => ok(false, "should throw"))
     85    .catch(e =>
     86      equal(
     87        e.message,
     88        `forked process without handler sent: {"error":"","errorStack":""}\n`
     89      )
     90    );
     91 });
     92 
     93 add_task(async function test_promise() {
     94  let id = await NodeServer.fork();
     95  registerCleanupFunction(async () => {
     96    await NodeServer.kill(id);
     97  });
     98 
     99  function p1() {
    100    return new Promise(resolve => resolve(10));
    101  }
    102  equal(await NodeServer.execute(id, p1), undefined);
    103  equal(await NodeServer.execute(id, `p1()`), 10);
    104 
    105  function p2() {
    106    return new Promise((resolve, reject) => reject("this is a rejection"));
    107  }
    108 
    109  equal(await NodeServer.execute(id, p2), undefined);
    110  await Assert.rejects(NodeServer.execute(id, `p2()`), /this is a rejection/);
    111 });