tor-browser

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

test_protocol_longstring.js (7726B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 /* eslint-disable max-nested-callbacks */
      4 
      5 "use strict";
      6 
      7 /**
      8 * Test simple requests using the protocol helpers.
      9 */
     10 var protocol = require("resource://devtools/shared/protocol.js");
     11 var { RetVal, Arg } = protocol;
     12 var {
     13  LongStringActor,
     14 } = require("resource://devtools/server/actors/string.js");
     15 
     16 // The test implicitly relies on this.
     17 require("resource://devtools/client/fronts/string.js");
     18 
     19 DevToolsServer.LONG_STRING_LENGTH =
     20  DevToolsServer.LONG_STRING_INITIAL_LENGTH =
     21  DevToolsServer.LONG_STRING_READ_LENGTH =
     22    5;
     23 
     24 var SHORT_STR = "abc";
     25 var LONG_STR = "abcdefghijklmnop";
     26 
     27 var rootActor = null;
     28 
     29 const rootSpec = protocol.generateActorSpec({
     30  typeName: "root",
     31 
     32  events: {
     33    "string-event": {
     34      str: Arg(0, "longstring"),
     35    },
     36  },
     37 
     38  methods: {
     39    shortString: {
     40      response: { value: RetVal("longstring") },
     41    },
     42    longString: {
     43      response: { value: RetVal("longstring") },
     44    },
     45    emitShortString: {
     46      oneway: true,
     47    },
     48    emitLongString: {
     49      oneway: true,
     50    },
     51  },
     52 });
     53 
     54 class RootActor extends protocol.Actor {
     55  constructor(conn) {
     56    super(conn, rootSpec);
     57 
     58    rootActor = this;
     59    // Root actor owns itself.
     60    this.manage(this);
     61    this.actorID = "root";
     62  }
     63 
     64  sayHello() {
     65    return {
     66      from: "root",
     67      applicationType: "xpcshell-tests",
     68      traits: [],
     69    };
     70  }
     71 
     72  shortString() {
     73    return new LongStringActor(this.conn, SHORT_STR);
     74  }
     75 
     76  longString() {
     77    return new LongStringActor(this.conn, LONG_STR);
     78  }
     79 
     80  emitShortString() {
     81    this.emit("string-event", new LongStringActor(this.conn, SHORT_STR));
     82  }
     83 
     84  emitLongString() {
     85    this.emit("string-event", new LongStringActor(this.conn, LONG_STR));
     86  }
     87 }
     88 
     89 class RootFront extends protocol.FrontClassWithSpec(rootSpec) {
     90  constructor(client) {
     91    super(client);
     92    this.actorID = "root";
     93 
     94    // Root owns itself.
     95    this.manage(this);
     96  }
     97 
     98  connect() {}
     99 }
    100 protocol.registerFront(RootFront);
    101 
    102 function run_test() {
    103  DevToolsServer.createRootActor = conn => {
    104    return new RootActor(conn);
    105  };
    106 
    107  DevToolsServer.init();
    108 
    109  const trace = connectPipeTracing();
    110  const client = new DevToolsClient(trace);
    111  let rootFront;
    112 
    113  let strfront = null;
    114 
    115  const expectRootChildren = function (size) {
    116    Assert.equal(rootActor.__poolMap.size, size + 1);
    117    Assert.equal(rootFront.__poolMap.size, size + 1);
    118  };
    119 
    120  client.connect().then(([applicationType]) => {
    121    rootFront = client.mainRoot;
    122 
    123    // Root actor has no children yet.
    124    expectRootChildren(0);
    125 
    126    trace.expectReceive({
    127      from: "<actorid>",
    128      applicationType: "xpcshell-tests",
    129      traits: [],
    130    });
    131    Assert.equal(applicationType, "xpcshell-tests");
    132    rootFront
    133      .shortString()
    134      .then(ret => {
    135        trace.expectSend({ type: "shortString", to: "<actorid>" });
    136        trace.expectReceive({ value: "abc", from: "<actorid>" });
    137 
    138        // Should only own the one reference (itself) at this point.
    139        expectRootChildren(0);
    140        strfront = ret;
    141      })
    142      .then(() => {
    143        return strfront.string();
    144      })
    145      .then(ret => {
    146        Assert.equal(ret, SHORT_STR);
    147      })
    148      .then(() => {
    149        return rootFront.longString();
    150      })
    151      .then(ret => {
    152        trace.expectSend({ type: "longString", to: "<actorid>" });
    153        trace.expectReceive({
    154          value: {
    155            type: "longString",
    156            actor: "<actorid>",
    157            length: 16,
    158            initial: "abcde",
    159          },
    160          from: "<actorid>",
    161        });
    162 
    163        strfront = ret;
    164        // Should own a reference to itself and an extra string now.
    165        expectRootChildren(1);
    166      })
    167      .then(() => {
    168        return strfront.string();
    169      })
    170      .then(ret => {
    171        trace.expectSend({
    172          type: "substring",
    173          start: 5,
    174          end: 10,
    175          to: "<actorid>",
    176        });
    177        trace.expectReceive({ substring: "fghij", from: "<actorid>" });
    178        trace.expectSend({
    179          type: "substring",
    180          start: 10,
    181          end: 15,
    182          to: "<actorid>",
    183        });
    184        trace.expectReceive({ substring: "klmno", from: "<actorid>" });
    185        trace.expectSend({
    186          type: "substring",
    187          start: 15,
    188          end: 20,
    189          to: "<actorid>",
    190        });
    191        trace.expectReceive({ substring: "p", from: "<actorid>" });
    192 
    193        Assert.equal(ret, LONG_STR);
    194      })
    195      .then(() => {
    196        return strfront.release();
    197      })
    198      .then(() => {
    199        trace.expectSend({ type: "release", to: "<actorid>" });
    200        trace.expectReceive({ from: "<actorid>" });
    201 
    202        // That reference should be removed now.
    203        expectRootChildren(0);
    204      })
    205      .then(() => {
    206        return new Promise(resolve => {
    207          rootFront.once("string-event", str => {
    208            trace.expectSend({ type: "emitShortString", to: "<actorid>" });
    209            trace.expectReceive({
    210              type: "string-event",
    211              str: "abc",
    212              from: "<actorid>",
    213            });
    214 
    215            Assert.ok(!!str);
    216            strfront = str;
    217            // Shouldn't generate any new references
    218            expectRootChildren(0);
    219            // will generate no packets.
    220            strfront.string().then(value => {
    221              resolve(value);
    222            });
    223          });
    224          rootFront.emitShortString();
    225        });
    226      })
    227      .then(value => {
    228        Assert.equal(value, SHORT_STR);
    229      })
    230      .then(() => {
    231        // Will generate no packets
    232        return strfront.release();
    233      })
    234      .then(() => {
    235        return new Promise(resolve => {
    236          rootFront.once("string-event", str => {
    237            trace.expectSend({ type: "emitLongString", to: "<actorid>" });
    238            trace.expectReceive({
    239              type: "string-event",
    240              str: {
    241                type: "longString",
    242                actor: "<actorid>",
    243                length: 16,
    244                initial: "abcde",
    245              },
    246              from: "<actorid>",
    247            });
    248 
    249            Assert.ok(!!str);
    250            // Should generate one new reference
    251            expectRootChildren(1);
    252            strfront = str;
    253            strfront.string().then(value => {
    254              trace.expectSend({
    255                type: "substring",
    256                start: 5,
    257                end: 10,
    258                to: "<actorid>",
    259              });
    260              trace.expectReceive({ substring: "fghij", from: "<actorid>" });
    261              trace.expectSend({
    262                type: "substring",
    263                start: 10,
    264                end: 15,
    265                to: "<actorid>",
    266              });
    267              trace.expectReceive({ substring: "klmno", from: "<actorid>" });
    268              trace.expectSend({
    269                type: "substring",
    270                start: 15,
    271                end: 20,
    272                to: "<actorid>",
    273              });
    274              trace.expectReceive({ substring: "p", from: "<actorid>" });
    275 
    276              resolve(value);
    277            });
    278          });
    279          rootFront.emitLongString();
    280        });
    281      })
    282      .then(value => {
    283        Assert.equal(value, LONG_STR);
    284      })
    285      .then(() => {
    286        return strfront.release();
    287      })
    288      .then(() => {
    289        trace.expectSend({ type: "release", to: "<actorid>" });
    290        trace.expectReceive({ from: "<actorid>" });
    291        expectRootChildren(0);
    292      })
    293      .then(() => {
    294        client.close().then(() => {
    295          do_test_finished();
    296        });
    297      })
    298      .catch(err => {
    299        do_report_unexpected_exception(err, "Failure executing test");
    300      });
    301  });
    302  do_test_pending();
    303 }