tor-browser

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

test_IPProtectionPanel.js (9040B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 https://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 "use strict";
      5 
      6 const { IPProtectionPanel } = ChromeUtils.importESModule(
      7  "moz-src:///browser/components/ipprotection/IPProtectionPanel.sys.mjs"
      8 );
      9 const { IPPEnrollAndEntitleManager } = ChromeUtils.importESModule(
     10  "moz-src:///browser/components/ipprotection/IPPEnrollAndEntitleManager.sys.mjs"
     11 );
     12 
     13 /**
     14 * A class that mocks the IP Protection panel.
     15 */
     16 class FakeIPProtectionPanelElement {
     17  constructor() {
     18    this.state = {
     19      isSignedOut: true,
     20      isProtectionEnabled: false,
     21    };
     22    this.isConnected = false;
     23  }
     24 
     25  requestUpdate() {
     26    /* NOOP */
     27  }
     28 
     29  closest() {
     30    return {
     31      state: "open",
     32    };
     33  }
     34 }
     35 
     36 add_setup(async function () {
     37  // FxAccountsStorage.sys.mjs requires a profile directory.
     38  do_get_profile();
     39  await putServerInRemoteSettings();
     40 
     41  await IPProtectionService.init();
     42 
     43  registerCleanupFunction(async () => {
     44    IPProtectionService.uninit();
     45  });
     46 });
     47 
     48 /**
     49 * Tests that we can set a state and pass it to a fake element.
     50 */
     51 add_task(async function test_setState() {
     52  let ipProtectionPanel = new IPProtectionPanel();
     53  let fakeElement = new FakeIPProtectionPanelElement();
     54  ipProtectionPanel.panel = fakeElement;
     55 
     56  ipProtectionPanel.state = {};
     57  fakeElement.state = {};
     58 
     59  ipProtectionPanel.setState({
     60    foo: "bar",
     61  });
     62 
     63  Assert.deepEqual(
     64    ipProtectionPanel.state,
     65    { foo: "bar" },
     66    "The state should be set on the IPProtectionPanel instance"
     67  );
     68 
     69  Assert.deepEqual(
     70    fakeElement.state,
     71    {},
     72    "The state should not be set on the fake element, as it is not connected"
     73  );
     74 
     75  fakeElement.isConnected = true;
     76 
     77  ipProtectionPanel.setState({
     78    isFoo: true,
     79  });
     80 
     81  Assert.deepEqual(
     82    ipProtectionPanel.state,
     83    { foo: "bar", isFoo: true },
     84    "The state should be set on the IPProtectionPanel instance"
     85  );
     86 
     87  Assert.deepEqual(
     88    fakeElement.state,
     89    { foo: "bar", isFoo: true },
     90    "The state should be set on the fake element"
     91  );
     92 });
     93 
     94 /**
     95 * Tests that the whole state will be updated when calling updateState directly.
     96 */
     97 add_task(async function test_updateState() {
     98  let ipProtectionPanel = new IPProtectionPanel();
     99  let fakeElement = new FakeIPProtectionPanelElement();
    100  ipProtectionPanel.panel = fakeElement;
    101 
    102  ipProtectionPanel.state = {};
    103  fakeElement.state = {};
    104 
    105  ipProtectionPanel.setState({
    106    foo: "bar",
    107  });
    108 
    109  Assert.deepEqual(
    110    fakeElement.state,
    111    {},
    112    "The state should not be set on the fake element, as it is not connected"
    113  );
    114 
    115  fakeElement.isConnected = true;
    116  ipProtectionPanel.updateState();
    117 
    118  Assert.deepEqual(
    119    fakeElement.state,
    120    { foo: "bar" },
    121    "The state should be set on the fake element"
    122  );
    123 });
    124 
    125 /**
    126 * Tests that IPProtectionService ready state event updates the state.
    127 */
    128 add_task(async function test_IPProtectionPanel_signedIn() {
    129  let sandbox = sinon.createSandbox();
    130  sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => true);
    131  sandbox
    132    .stub(IPPEnrollAndEntitleManager, "isEnrolledAndEntitled")
    133    .get(() => true);
    134  sandbox
    135    .stub(IPProtectionService.guardian, "isLinkedToGuardian")
    136    .resolves(true);
    137  sandbox.stub(IPProtectionService.guardian, "fetchUserInfo").resolves({
    138    status: 200,
    139    error: null,
    140    entitlement: {
    141      subscribed: true,
    142      uid: 42,
    143      created_at: "2023-01-01T12:00:00.000Z",
    144    },
    145  });
    146 
    147  let ipProtectionPanel = new IPProtectionPanel();
    148  let fakeElement = new FakeIPProtectionPanelElement();
    149  ipProtectionPanel.panel = fakeElement;
    150  fakeElement.isConnected = true;
    151 
    152  let signedInEventPromise = waitForEvent(
    153    IPProtectionService,
    154    "IPProtectionService:StateChanged",
    155    () => IPProtectionService.state === IPProtectionStates.READY
    156  );
    157  IPProtectionService.updateState();
    158 
    159  await signedInEventPromise;
    160 
    161  Assert.equal(
    162    ipProtectionPanel.state.isSignedOut,
    163    false,
    164    "isSignedOut should be false in the IPProtectionPanel state"
    165  );
    166 
    167  Assert.equal(
    168    fakeElement.state.isSignedOut,
    169    false,
    170    "isSignedOut should be false in the fake elements state"
    171  );
    172 
    173  sandbox.restore();
    174 });
    175 
    176 /**
    177 * Tests that IPProtectionService unavailable state event updates the state.
    178 */
    179 add_task(async function test_IPProtectionPanel_signedOut() {
    180  let sandbox = sinon.createSandbox();
    181  sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => false);
    182 
    183  let ipProtectionPanel = new IPProtectionPanel();
    184  let fakeElement = new FakeIPProtectionPanelElement();
    185  ipProtectionPanel.panel = fakeElement;
    186  fakeElement.isConnected = true;
    187 
    188  IPProtectionService.setState(IPProtectionStates.READY);
    189  let signedOutEventPromise = waitForEvent(
    190    IPProtectionService,
    191    "IPProtectionService:StateChanged",
    192    () => IPProtectionService.state === IPProtectionStates.UNAVAILABLE
    193  );
    194  IPProtectionService.updateState();
    195 
    196  await signedOutEventPromise;
    197 
    198  Assert.equal(
    199    ipProtectionPanel.state.isSignedOut,
    200    true,
    201    "isSignedOut should be true in the IPProtectionPanel state"
    202  );
    203 
    204  Assert.equal(
    205    fakeElement.state.isSignedOut,
    206    true,
    207    "isSignedOut should be true in the fake elements state"
    208  );
    209 
    210  sandbox.restore();
    211 });
    212 
    213 /**
    214 * Tests that start and stopping the IPProtectionService updates the state.
    215 */
    216 add_task(async function test_IPProtectionPanel_started_stopped() {
    217  let ipProtectionPanel = new IPProtectionPanel();
    218  let fakeElement = new FakeIPProtectionPanelElement();
    219  ipProtectionPanel.panel = fakeElement;
    220  fakeElement.isConnected = true;
    221 
    222  let sandbox = sinon.createSandbox();
    223  sandbox.stub(IPPSignInWatcher, "isSignedIn").get(() => true);
    224  sandbox
    225    .stub(IPPEnrollAndEntitleManager, "isEnrolledAndEntitled")
    226    .get(() => true);
    227  sandbox
    228    .stub(IPProtectionService.guardian, "isLinkedToGuardian")
    229    .resolves(true);
    230  sandbox.stub(IPProtectionService.guardian, "fetchUserInfo").resolves({
    231    status: 200,
    232    error: null,
    233    entitlement: {
    234      subscribed: true,
    235      uid: 42,
    236      created_at: "2023-01-01T12:00:00.000Z",
    237    },
    238  });
    239  sandbox.stub(IPProtectionService.guardian, "fetchProxyPass").resolves({
    240    status: 200,
    241    error: undefined,
    242    pass: new ProxyPass(createProxyPassToken()),
    243  });
    244 
    245  IPProtectionService.updateState();
    246 
    247  let startedEventPromise = waitForEvent(
    248    IPPProxyManager,
    249    "IPPProxyManager:StateChanged",
    250    () => IPPProxyManager.state === IPPProxyStates.ACTIVE
    251  );
    252 
    253  IPPProxyManager.start();
    254 
    255  await startedEventPromise;
    256 
    257  Assert.equal(
    258    ipProtectionPanel.state.isProtectionEnabled,
    259    true,
    260    "isProtectionEnabled should be true in the IPProtectionPanel state"
    261  );
    262 
    263  Assert.equal(
    264    fakeElement.state.isProtectionEnabled,
    265    true,
    266    "isProtectionEnabled should be true in the fake elements state"
    267  );
    268 
    269  let stoppedEventPromise = waitForEvent(
    270    IPPProxyManager,
    271    "IPPProxyManager:StateChanged",
    272    () => IPPProxyManager.state !== IPPProxyStates.ACTIVE
    273  );
    274 
    275  await IPPProxyManager.stop();
    276 
    277  await stoppedEventPromise;
    278 
    279  Assert.equal(
    280    ipProtectionPanel.state.isProtectionEnabled,
    281    false,
    282    "isProtectionEnabled should be false in the IPProtectionPanel state"
    283  );
    284 
    285  Assert.equal(
    286    fakeElement.state.isProtectionEnabled,
    287    false,
    288    "isProtectionEnabled should be false in the fake elements state"
    289  );
    290  sandbox.restore();
    291 });
    292 
    293 /**
    294 * Tests that IPProtectionPanel state isAlpha property is correct
    295 * when IPPEnrollAndEntitleManager.isAlpha is true.
    296 */
    297 add_task(async function test_IPProtectionPanel_isAlpha_true() {
    298  let sandbox = sinon.createSandbox();
    299 
    300  sandbox
    301    .stub(IPPEnrollAndEntitleManager, "isEnrolledAndEntitled")
    302    .get(() => true);
    303  sandbox.stub(IPPEnrollAndEntitleManager, "isAlpha").get(() => true);
    304  sandbox
    305    .stub(IPProtectionService.guardian, "isLinkedToGuardian")
    306    .resolves(true);
    307 
    308  let ipProtectionPanel = new IPProtectionPanel();
    309  let fakeElement = new FakeIPProtectionPanelElement();
    310  ipProtectionPanel.panel = fakeElement;
    311  fakeElement.isConnected = true;
    312 
    313  IPProtectionService.updateState();
    314 
    315  Assert.equal(
    316    ipProtectionPanel.state.isAlpha,
    317    true,
    318    "isAlpha should be true in the IPProtectionPanel state"
    319  );
    320 
    321  sandbox.restore();
    322 });
    323 
    324 /**
    325 * Tests that IPProtectionPanel state isAlpha property is correct
    326 * when IPPEnrollAndEntitleManager.isAlpha is false.
    327 */
    328 add_task(async function test_IPProtectionPanel_isAlpha_false() {
    329  let sandbox = sinon.createSandbox();
    330 
    331  sandbox
    332    .stub(IPPEnrollAndEntitleManager, "isEnrolledAndEntitled")
    333    .get(() => true);
    334  sandbox.stub(IPPEnrollAndEntitleManager, "isAlpha").get(() => false);
    335  sandbox
    336    .stub(IPProtectionService.guardian, "isLinkedToGuardian")
    337    .resolves(true);
    338 
    339  let ipProtectionPanel = new IPProtectionPanel();
    340  let fakeElement = new FakeIPProtectionPanelElement();
    341  ipProtectionPanel.panel = fakeElement;
    342  fakeElement.isConnected = true;
    343 
    344  IPProtectionService.updateState();
    345 
    346  Assert.equal(
    347    ipProtectionPanel.state.isAlpha,
    348    false,
    349    "isAlpha should be false in the IPProtectionPanel state"
    350  );
    351 
    352  sandbox.restore();
    353 });