tor-browser

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

test_adb.js (6357B)


      1 /* Any copyright is dedicated to the Public Domain.
      2   http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { ExtensionTestUtils } = ChromeUtils.importESModule(
      7  "resource://testing-common/ExtensionXPCShellUtils.sys.mjs"
      8 );
      9 const { NetUtil } = ChromeUtils.importESModule(
     10  "resource://gre/modules/NetUtil.sys.mjs"
     11 );
     12 const {
     13  getFileForBinary,
     14 } = require("resource://devtools/client/shared/remote-debugging/adb/adb-binary.js");
     15 const {
     16  check,
     17 } = require("resource://devtools/client/shared/remote-debugging/adb/adb-running-checker.js");
     18 const {
     19  adbProcess,
     20 } = require("resource://devtools/client/shared/remote-debugging/adb/adb-process.js");
     21 const {
     22  TrackDevicesCommand,
     23 } = require("resource://devtools/client/shared/remote-debugging/adb/commands/index.js");
     24 
     25 const ADB_JSON = {
     26  Linux: {
     27    x86: ["linux/adb"],
     28    x86_64: ["linux64/adb"],
     29  },
     30  Darwin: {
     31    x86_64: ["mac64/adb"],
     32  },
     33  WINNT: {
     34    x86: ["win32/adb.exe", "win32/AdbWinApi.dll", "win32/AdbWinUsbApi.dll"],
     35    x86_64: ["win32/adb.exe", "win32/AdbWinApi.dll", "win32/AdbWinUsbApi.dll"],
     36  },
     37 };
     38 let extension_version = 1.0;
     39 
     40 ExtensionTestUtils.init(this);
     41 
     42 function readAdbMockContent() {
     43  const adbMockFile = do_get_file("adb.py", false);
     44  const s = Cc["@mozilla.org/network/file-input-stream;1"].createInstance(
     45    Ci.nsIFileInputStream
     46  );
     47  s.init(adbMockFile, -1, -1, false);
     48  try {
     49    return NetUtil.readInputStreamToString(s, s.available());
     50  } finally {
     51    s.close();
     52  }
     53 }
     54 
     55 const adbMock = readAdbMockContent();
     56 
     57 add_task(async function setup() {
     58  // Prepare the profile directory where the adb extension will be installed.
     59  do_get_profile();
     60 });
     61 
     62 add_task(async function testAdbIsNotRunningInitially() {
     63  const isAdbRunning = await check();
     64  // Assume that no adb server running.
     65  ok(!isAdbRunning, "adb is not running initially");
     66 });
     67 
     68 add_task(async function testNoAdbExtension() {
     69  const extension = ExtensionTestUtils.loadExtension({
     70    manifest: {
     71      version: (extension_version++).toString(),
     72      browser_specific_settings: {
     73        gecko: { id: "not-adb@mozilla.org" },
     74      },
     75    },
     76  });
     77 
     78  await extension.startup();
     79 
     80  const adbBinary = await getFileForBinary();
     81  equal(adbBinary, null);
     82 
     83  await extension.unload();
     84 });
     85 
     86 add_task(async function testNoAdbJSON() {
     87  const extension = ExtensionTestUtils.loadExtension({
     88    manifest: {
     89      version: (extension_version++).toString(),
     90      browser_specific_settings: {
     91        // The extension id here and in later test cases should match the
     92        // corresponding prefrece value.
     93        gecko: { id: "adb@mozilla.org" },
     94      },
     95    },
     96  });
     97 
     98  await extension.startup();
     99 
    100  const adbBinary = await getFileForBinary();
    101  equal(adbBinary, null);
    102 
    103  await extension.unload();
    104 });
    105 
    106 add_task(async function testNoTargetBinaries() {
    107  const extension = ExtensionTestUtils.loadExtension({
    108    manifest: {
    109      version: (extension_version++).toString(),
    110      browser_specific_settings: {
    111        gecko: { id: "adb@mozilla.org" },
    112      },
    113    },
    114    files: {
    115      "adb.json": JSON.stringify(ADB_JSON),
    116    },
    117  });
    118 
    119  await extension.startup();
    120 
    121  const adbBinary = await getFileForBinary();
    122  equal(adbBinary, null);
    123 
    124  await extension.unload();
    125 });
    126 
    127 add_task(async function testExtract() {
    128  const extension = ExtensionTestUtils.loadExtension({
    129    manifest: {
    130      version: (extension_version++).toString(),
    131      browser_specific_settings: {
    132        gecko: { id: "adb@mozilla.org" },
    133      },
    134    },
    135    files: {
    136      "adb.json": JSON.stringify(ADB_JSON),
    137      "linux/adb": "adb",
    138      "linux64/adb": "adb",
    139      "mac64/adb": "adb",
    140      "win32/adb.exe": "adb.exe",
    141      "win32/AdbWinApi.dll": "AdbWinApi.dll",
    142      "win32/AdbWinUsbApi.dll": "AdbWinUsbApi.dll",
    143    },
    144  });
    145 
    146  await extension.startup();
    147 
    148  const adbBinary = await getFileForBinary();
    149  ok(await adbBinary.exists());
    150 
    151  await extension.unload();
    152 });
    153 
    154 add_task(
    155  {
    156    skip_if: () => mozinfo.os == "win", // bug 1482008
    157  },
    158  async function testStartAndStop() {
    159    const extension = ExtensionTestUtils.loadExtension({
    160      manifest: {
    161        version: (extension_version++).toString(),
    162        browser_specific_settings: {
    163          gecko: { id: "adb@mozilla.org" },
    164        },
    165      },
    166      files: {
    167        "adb.json": JSON.stringify(ADB_JSON),
    168        "linux/adb": adbMock,
    169        "linux64/adb": adbMock,
    170        "mac64/adb": adbMock,
    171        "win32/adb.exe": adbMock,
    172        "win32/AdbWinApi.dll": "dummy",
    173        "win32/AdbWinUsbApi.dll": "dummy",
    174      },
    175    });
    176 
    177    await extension.startup();
    178 
    179    // Call start() once and call stop() afterwards.
    180    await adbProcess.start();
    181    ok(adbProcess.ready);
    182    ok(await check(), "adb is now running");
    183 
    184    await adbProcess.stop();
    185    ok(!adbProcess.ready);
    186    ok(!(await check()), "adb is no longer running");
    187 
    188    // Call start() twice and call stop() afterwards.
    189    await adbProcess.start();
    190    await adbProcess.start();
    191    ok(adbProcess.ready);
    192    ok(await check(), "adb is now running");
    193 
    194    await adbProcess.stop();
    195    ok(!adbProcess.ready);
    196    ok(!(await check()), "adb is no longer running");
    197 
    198    await extension.unload();
    199  }
    200 );
    201 
    202 add_task(
    203  {
    204    skip_if: () => mozinfo.os == "win", // bug 1482008
    205  },
    206  async function testTrackDevices() {
    207    const extension = ExtensionTestUtils.loadExtension({
    208      manifest: {
    209        version: (extension_version++).toString(),
    210        browser_specific_settings: {
    211          gecko: { id: "adb@mozilla.org" },
    212        },
    213      },
    214      files: {
    215        "adb.json": JSON.stringify(ADB_JSON),
    216        "linux/adb": adbMock,
    217        "linux64/adb": adbMock,
    218        "mac64/adb": adbMock,
    219        "win32/adb.exe": adbMock,
    220        "win32/AdbWinApi.dll": "dummy",
    221        "win32/AdbWinUsbApi.dll": "dummy",
    222      },
    223    });
    224 
    225    await extension.startup();
    226 
    227    await adbProcess.start();
    228    ok(adbProcess.ready);
    229 
    230    ok(await check(), "adb is now running");
    231 
    232    const receivedDeviceId = await new Promise(resolve => {
    233      const trackDevicesCommand = new TrackDevicesCommand();
    234      trackDevicesCommand.on("device-connected", deviceId => {
    235        resolve(deviceId);
    236      });
    237      trackDevicesCommand.run();
    238    });
    239 
    240    equal(receivedDeviceId, "1234567890");
    241 
    242    await adbProcess.stop();
    243    ok(!adbProcess.ready);
    244 
    245    await extension.unload();
    246  }
    247 );