tor-browser

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

test_IPProxyManager.js (9259B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const { IPPEnrollAndEntitleManager } = ChromeUtils.importESModule(
      8  "moz-src:///browser/components/ipprotection/IPPEnrollAndEntitleManager.sys.mjs"
      9 );
     10 
     11 add_setup(async function () {
     12  await putServerInRemoteSettings();
     13 });
     14 
     15 /**
     16 * Tests that starting the service gets a state changed event.
     17 */
     18 add_task(async function test_IPPProxyManager_start() {
     19  let sandbox = sinon.createSandbox();
     20  setupStubs(sandbox);
     21 
     22  IPProtectionService.init();
     23 
     24  await waitForEvent(
     25    IPProtectionService,
     26    "IPProtectionService:StateChanged",
     27    () => IPProtectionService.state === IPProtectionStates.READY
     28  );
     29 
     30  Assert.ok(
     31    !IPPProxyManager.activatedAt,
     32    "IP Protection service should not be active initially"
     33  );
     34 
     35  let startedEventPromise = waitForEvent(
     36    IPPProxyManager,
     37    "IPPProxyManager:StateChanged",
     38    () => IPPProxyManager.state === IPPProxyStates.ACTIVE
     39  );
     40 
     41  IPPProxyManager.start();
     42 
     43  Assert.equal(
     44    IPPProxyManager.state,
     45    IPPProxyStates.ACTIVATING,
     46    "Proxy activation"
     47  );
     48 
     49  await startedEventPromise;
     50 
     51  Assert.equal(
     52    IPPProxyManager.state,
     53    IPPProxyStates.ACTIVE,
     54    "IP Protection service should be active after starting"
     55  );
     56  Assert.ok(
     57    !!IPPProxyManager.activatedAt,
     58    "IP Protection service should have an activation timestamp"
     59  );
     60  Assert.ok(
     61    IPPProxyManager.active,
     62    "IP Protection service should have an active connection"
     63  );
     64 
     65  IPProtectionService.uninit();
     66  sandbox.restore();
     67 });
     68 
     69 /**
     70 * Tests that stopping the service gets stop events.
     71 */
     72 add_task(async function test_IPPProxyManager_stop() {
     73  let sandbox = sinon.createSandbox();
     74  setupStubs(sandbox);
     75 
     76  const waitForReady = waitForEvent(
     77    IPProtectionService,
     78    "IPProtectionService:StateChanged",
     79    () => IPProtectionService.state === IPProtectionStates.READY
     80  );
     81 
     82  IPProtectionService.init();
     83  await waitForReady;
     84 
     85  await IPPProxyManager.start();
     86 
     87  let stoppedEventPromise = waitForEvent(
     88    IPPProxyManager,
     89    "IPPProxyManager:StateChanged",
     90    () => IPPProxyManager.state === IPPProxyStates.READY
     91  );
     92  await IPPProxyManager.stop();
     93 
     94  await stoppedEventPromise;
     95  Assert.equal(
     96    IPPProxyManager.state,
     97    IPPProxyStates.READY,
     98    "IP Protection service should not be active after stopping"
     99  );
    100  Assert.ok(
    101    !IPPProxyManager.activatedAt,
    102    "IP Protection service should not have an activation timestamp after stopping"
    103  );
    104  Assert.ok(
    105    !IPProtectionService.connection,
    106    "IP Protection service should not have an active connection"
    107  );
    108 
    109  IPProtectionService.uninit();
    110  sandbox.restore();
    111 });
    112 
    113 /**
    114 * Tests that the proxy manager gets proxy pass and connection on starting
    115 * and removes the connection after after stop.
    116 */
    117 add_task(async function test_IPPProxyManager_start_stop_reset() {
    118  const sandbox = sinon.createSandbox();
    119  setupStubs(sandbox);
    120 
    121  let readyEvent = waitForEvent(
    122    IPProtectionService,
    123    "IPProtectionService:StateChanged",
    124    () => IPProtectionService.state === IPProtectionStates.READY
    125  );
    126 
    127  IPProtectionService.init();
    128  await readyEvent;
    129 
    130  await IPPProxyManager.start();
    131 
    132  Assert.ok(IPPProxyManager.active, "Should be active after starting");
    133 
    134  Assert.ok(
    135    IPPProxyManager.isolationKey,
    136    "Should have an isolationKey after starting"
    137  );
    138 
    139  Assert.ok(
    140    IPPProxyManager.hasValidProxyPass,
    141    "Should have a valid proxy pass after starting"
    142  );
    143 
    144  await IPPProxyManager.stop();
    145 
    146  Assert.ok(!IPPProxyManager.active, "Should not be active after starting");
    147 
    148  Assert.ok(
    149    !IPPProxyManager.isolationKey,
    150    "Should not have an isolationKey after stopping"
    151  );
    152 
    153  sandbox.restore();
    154 });
    155 
    156 /**
    157 * Tests that the proxy manager gets proxy pass and connection on starting
    158 * and removes them after stop / reset.
    159 */
    160 add_task(async function test_IPPProxyManager_reset() {
    161  let sandbox = sinon.createSandbox();
    162  sandbox.stub(IPProtectionService.guardian, "fetchProxyPass").returns({
    163    status: 200,
    164    error: undefined,
    165    pass: new ProxyPass(createProxyPassToken()),
    166  });
    167 
    168  await IPPProxyManager.start();
    169 
    170  Assert.ok(IPPProxyManager.active, "Should be active after starting");
    171 
    172  Assert.ok(
    173    IPPProxyManager.isolationKey,
    174    "Should have an isolationKey after starting"
    175  );
    176 
    177  Assert.ok(
    178    IPPProxyManager.hasValidProxyPass,
    179    "Should have a valid proxy pass after starting"
    180  );
    181 
    182  await IPPProxyManager.reset();
    183 
    184  Assert.ok(!IPPProxyManager.active, "Should not be active after starting");
    185 
    186  Assert.ok(
    187    !IPPProxyManager.isolationKey,
    188    "Should not have an isolationKey after stopping"
    189  );
    190 
    191  Assert.ok(
    192    !IPPProxyManager.hasValidProxyPass,
    193    "Should not have a proxy pass after stopping"
    194  );
    195 
    196  sandbox.restore();
    197 });
    198 
    199 /**
    200 * Tests the error state.
    201 */
    202 add_task(async function test_IPPProxyStates_error() {
    203  let sandbox = sinon.createSandbox();
    204  sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => true);
    205  sandbox
    206    .stub(IPProtectionService.guardian, "isLinkedToGuardian")
    207    .resolves(true);
    208  sandbox.stub(IPProtectionService.guardian, "fetchUserInfo").resolves({
    209    status: 200,
    210    error: undefined,
    211    entitlement: { uid: 42 },
    212  });
    213  sandbox
    214    .stub(IPPEnrollAndEntitleManager, "maybeEnrollAndEntitle")
    215    .resolves({ isEnrolledAndEntitled: false });
    216 
    217  await IPProtectionService.init();
    218 
    219  Assert.equal(
    220    IPProtectionService.state,
    221    IPProtectionStates.READY,
    222    "IP Protection service should be ready"
    223  );
    224 
    225  await IPPProxyManager.start(false);
    226 
    227  Assert.equal(
    228    IPPProxyManager.state,
    229    IPPProxyStates.ERROR,
    230    "IP Protection service should be active"
    231  );
    232 
    233  IPProtectionService.uninit();
    234  sandbox.restore();
    235 });
    236 
    237 /**
    238 * Tests the active state.
    239 */
    240 add_task(async function test_IPPProxytates_active() {
    241  let sandbox = sinon.createSandbox();
    242  sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => true);
    243  sandbox
    244    .stub(IPProtectionService.guardian, "isLinkedToGuardian")
    245    .resolves(true);
    246  sandbox.stub(IPProtectionService.guardian, "fetchUserInfo").resolves({
    247    status: 200,
    248    error: undefined,
    249    entitlement: { uid: 42 },
    250  });
    251  sandbox.stub(IPProtectionService.guardian, "fetchProxyPass").resolves({
    252    status: 200,
    253    error: undefined,
    254    pass: new ProxyPass(
    255      options.validProxyPass
    256        ? createProxyPassToken()
    257        : createExpiredProxyPassToken()
    258    ),
    259  });
    260 
    261  const waitForReady = waitForEvent(
    262    IPProtectionService,
    263    "IPProtectionService:StateChanged",
    264    () => IPProtectionService.state === IPProtectionStates.READY
    265  );
    266 
    267  IPProtectionService.init();
    268 
    269  await waitForReady;
    270 
    271  Assert.equal(
    272    IPProtectionService.state,
    273    IPProtectionStates.READY,
    274    "IP Protection service should be ready"
    275  );
    276 
    277  const startPromise = IPPProxyManager.start(false);
    278 
    279  Assert.equal(
    280    IPPProxyManager.state,
    281    IPPProxyStates.ACTIVATING,
    282    "Proxy activation"
    283  );
    284 
    285  await startPromise;
    286 
    287  Assert.equal(
    288    IPProtectionService.state,
    289    IPProtectionStates.READY,
    290    "IP Protection service should be in ready state"
    291  );
    292 
    293  Assert.equal(
    294    IPPProxyManager.state,
    295    IPPProxyStates.ACTIVE,
    296    "IP Protection service should be active"
    297  );
    298 
    299  await IPPProxyManager.stop(false);
    300 
    301  Assert.equal(
    302    IPProtectionService.state,
    303    IPProtectionStates.READY,
    304    "IP Protection service should be ready again"
    305  );
    306 
    307  IPProtectionService.uninit();
    308  sandbox.restore();
    309 });
    310 
    311 /**
    312 * Tests the quick start/stop calls.
    313 */
    314 add_task(async function test_IPPProxytates_start_stop() {
    315  let sandbox = sinon.createSandbox();
    316  sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => true);
    317  sandbox
    318    .stub(IPProtectionService.guardian, "isLinkedToGuardian")
    319    .resolves(true);
    320  sandbox.stub(IPProtectionService.guardian, "fetchUserInfo").resolves({
    321    status: 200,
    322    error: undefined,
    323    entitlement: { uid: 42 },
    324  });
    325  sandbox.stub(IPProtectionService.guardian, "fetchProxyPass").resolves({
    326    status: 200,
    327    error: undefined,
    328    pass: new ProxyPass(
    329      options.validProxyPass
    330        ? createProxyPassToken()
    331        : createExpiredProxyPassToken()
    332    ),
    333  });
    334 
    335  const waitForReady = waitForEvent(
    336    IPProtectionService,
    337    "IPProtectionService:StateChanged",
    338    () => IPProtectionService.state === IPProtectionStates.READY
    339  );
    340 
    341  IPProtectionService.init();
    342 
    343  await waitForReady;
    344 
    345  Assert.equal(
    346    IPProtectionService.state,
    347    IPProtectionStates.READY,
    348    "IP Protection service should be ready"
    349  );
    350 
    351  IPPProxyManager.start(false);
    352  IPPProxyManager.start(false);
    353  IPPProxyManager.start(false);
    354 
    355  IPPProxyManager.stop(false);
    356  IPPProxyManager.stop(false);
    357  IPPProxyManager.stop(false);
    358  IPPProxyManager.stop(false);
    359 
    360  Assert.equal(
    361    IPPProxyManager.state,
    362    IPPProxyStates.ACTIVATING,
    363    "Proxy activation"
    364  );
    365 
    366  await waitForEvent(
    367    IPPProxyManager,
    368    "IPPProxyManager:StateChanged",
    369    () => IPPProxyManager.state === IPPProxyStates.ACTIVE
    370  );
    371 
    372  await waitForEvent(
    373    IPPProxyManager,
    374    "IPPProxyManager:StateChanged",
    375    () => IPPProxyManager.state === IPPProxyStates.READY
    376  );
    377 
    378  IPProtectionService.uninit();
    379  sandbox.restore();
    380 });