tor-browser

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

head.js (2440B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { require } = ChromeUtils.importESModule(
      7  "resource://devtools/shared/loader/Loader.sys.mjs"
      8 );
      9 const {
     10  DevToolsServer,
     11 } = require("resource://devtools/server/devtools-server.js");
     12 const {
     13  DevToolsClient,
     14 } = require("resource://devtools/client/devtools-client.js");
     15 
     16 function dumpn(msg) {
     17  dump("DBG-TEST: " + msg + "\n");
     18 }
     19 
     20 function connectPipeTracing() {
     21  return new TracingTransport(DevToolsServer.connectPipe());
     22 }
     23 
     24 /**
     25 * Mock the `Transport` class in order to intercept all the packet
     26 * getting in and out and then being able to assert them and dump them.
     27 */
     28 class TracingTransport {
     29  constructor(childTransport) {
     30    this.hooks = null;
     31    this.child = childTransport;
     32    this.child.hooks = this;
     33 
     34    this.expectations = [];
     35    this.packets = [];
     36    this.checkIndex = 0;
     37  }
     38  // Remove actor names
     39  normalize(packet) {
     40    return JSON.parse(
     41      JSON.stringify(packet, (key, value) => {
     42        if (key === "to" || key === "from" || key === "actor") {
     43          return "<actorid>";
     44        }
     45        return value;
     46      })
     47    );
     48  }
     49  send(packet) {
     50    this.packets.push({
     51      type: "sent",
     52      packet: this.normalize(packet),
     53    });
     54    return this.child.send(packet);
     55  }
     56  close() {
     57    return this.child.close();
     58  }
     59  ready() {
     60    return this.child.ready();
     61  }
     62  onPacket(packet) {
     63    this.packets.push({
     64      type: "received",
     65      packet: this.normalize(packet),
     66    });
     67    this.hooks.onPacket(packet);
     68  }
     69  onTransportClosed() {
     70    if (this.hooks.onTransportClosed) {
     71      this.hooks.onTransportClosed();
     72    }
     73  }
     74 
     75  expectSend(expected) {
     76    const packet = this.packets[this.checkIndex++];
     77    Assert.equal(packet.type, "sent");
     78    deepEqual(packet.packet, this.normalize(expected));
     79  }
     80 
     81  expectReceive(expected) {
     82    const packet = this.packets[this.checkIndex++];
     83    Assert.equal(packet.type, "received");
     84    deepEqual(packet.packet, this.normalize(expected));
     85  }
     86 
     87  // Write your tests, call dumpLog at the end, inspect the output,
     88  // then sprinkle the calls through the right places in your test.
     89  dumpLog() {
     90    for (const entry of this.packets) {
     91      if (entry.type === "sent") {
     92        dumpn("trace.expectSend(" + entry.packet + ");");
     93      } else {
     94        dumpn("trace.expectReceive(" + entry.packet + ");");
     95      }
     96    }
     97  }
     98 }