tor-browser

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

test_remote_client_manager.js (4639B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const {
      7  remoteClientManager,
      8 } = require("resource://devtools/client/shared/remote-debugging/remote-client-manager.js");
      9 const {
     10  CONNECTION_TYPES,
     11 } = require("resource://devtools/client/shared/remote-debugging/constants.js");
     12 
     13 add_task(async function testRemoteClientManager() {
     14  for (const type of Object.values(CONNECTION_TYPES)) {
     15    const fakeClient = createFakeClient();
     16    const runtimeInfo = {};
     17    const clientId = "clientId";
     18    const remoteId = remoteClientManager.getRemoteId(clientId, type);
     19 
     20    const connectionType =
     21      remoteClientManager.getConnectionTypeByRemoteId(remoteId);
     22    equal(
     23      connectionType,
     24      type,
     25      `[${type}]: Correct connection type was returned by getConnectionTypeByRemoteId`
     26    );
     27 
     28    equal(
     29      remoteClientManager.hasClient(clientId, type),
     30      false,
     31      `[${type}]: hasClient returns false if no client was set`
     32    );
     33    equal(
     34      remoteClientManager.getClient(clientId, type),
     35      null,
     36      `[${type}]: getClient returns null if no client was set`
     37    );
     38    equal(
     39      remoteClientManager.getClientByRemoteId(remoteId),
     40      null,
     41      `[${type}]: getClientByRemoteId returns null if no client was set`
     42    );
     43    equal(
     44      remoteClientManager.getRuntimeInfoByRemoteId(remoteId),
     45      null,
     46      `[${type}]: getRuntimeInfoByRemoteId returns null if no client was set`
     47    );
     48 
     49    remoteClientManager.setClient(clientId, type, fakeClient, runtimeInfo);
     50    equal(
     51      remoteClientManager.hasClient(clientId, type),
     52      true,
     53      `[${type}]: hasClient returns true`
     54    );
     55    equal(
     56      remoteClientManager.getClient(clientId, type),
     57      fakeClient,
     58      `[${type}]: getClient returns the correct client`
     59    );
     60    equal(
     61      remoteClientManager.getClientByRemoteId(remoteId),
     62      fakeClient,
     63      `[${type}]: getClientByRemoteId returns the correct client`
     64    );
     65    equal(
     66      remoteClientManager.getRuntimeInfoByRemoteId(remoteId),
     67      runtimeInfo,
     68      `[${type}]: getRuntimeInfoByRemoteId returns the correct object`
     69    );
     70 
     71    remoteClientManager.removeClient(clientId, type);
     72    equal(
     73      remoteClientManager.hasClient(clientId, type),
     74      false,
     75      `[${type}]: hasClient returns false after removing the client`
     76    );
     77    equal(
     78      remoteClientManager.getClient(clientId, type),
     79      null,
     80      `[${type}]: getClient returns null after removing the client`
     81    );
     82    equal(
     83      remoteClientManager.getClientByRemoteId(remoteId),
     84      null,
     85      `[${type}]: getClientByRemoteId returns null after removing the client`
     86    );
     87    equal(
     88      remoteClientManager.getRuntimeInfoByRemoteId(),
     89      null,
     90      `[${type}]: getRuntimeInfoByRemoteId returns null after removing the client`
     91    );
     92  }
     93 
     94  // Test various fallback scenarios for APIs relying on remoteId, when called without a
     95  // remoteId, we expect to get the information for the local this-firefox runtime.
     96  const { THIS_FIREFOX } = CONNECTION_TYPES;
     97  const thisFirefoxClient = createFakeClient();
     98  const thisFirefoxInfo = {};
     99  remoteClientManager.setClient(
    100    THIS_FIREFOX,
    101    THIS_FIREFOX,
    102    thisFirefoxClient,
    103    thisFirefoxInfo
    104  );
    105 
    106  equal(
    107    remoteClientManager.getClientByRemoteId(),
    108    thisFirefoxClient,
    109    `[fallback]: getClientByRemoteId returns this-firefox if remoteId is null`
    110  );
    111  equal(
    112    remoteClientManager.getRuntimeInfoByRemoteId(),
    113    thisFirefoxInfo,
    114    `[fallback]: getRuntimeInfoByRemoteId returns this-firefox if remoteId is null`
    115  );
    116 
    117  const otherRemoteId = remoteClientManager.getRemoteId(
    118    "clientId",
    119    CONNECTION_TYPES.USB
    120  );
    121  equal(
    122    remoteClientManager.getClientByRemoteId(otherRemoteId),
    123    null,
    124    `[fallback]: getClientByRemoteId does not fallback if remoteId is non-null`
    125  );
    126  equal(
    127    remoteClientManager.getRuntimeInfoByRemoteId(otherRemoteId),
    128    null,
    129    `[fallback]: getRuntimeInfoByRemoteId does not fallback if remoteId is non-null`
    130  );
    131 });
    132 
    133 add_task(async function testRemoteClientManagerWithUnknownType() {
    134  const remoteId = remoteClientManager.getRemoteId(
    135    "someClientId",
    136    "NotARealType"
    137  );
    138  const connectionType =
    139    remoteClientManager.getConnectionTypeByRemoteId(remoteId);
    140  equal(
    141    connectionType,
    142    CONNECTION_TYPES.UNKNOWN,
    143    `Connection type UNKNOWN was returned by getConnectionTypeByRemoteId`
    144  );
    145 });
    146 
    147 function createFakeClient() {
    148  const EventEmitter = require("resource://devtools/shared/event-emitter.js");
    149 
    150  const client = {};
    151  EventEmitter.decorate(client);
    152  return client;
    153 }