tor-browser

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

test_service_attributes.js (6770B)


      1 /* Any copyright is dedicated to the Public Domain.
      2 * http://creativecommons.org/publicdomain/zero/1.0/ */
      3 
      4 const { Service } = ChromeUtils.importESModule(
      5  "resource://services-sync/service.sys.mjs"
      6 );
      7 const { FakeGUIDService } = ChromeUtils.importESModule(
      8  "resource://testing-common/services/sync/fakeservices.sys.mjs"
      9 );
     10 
     11 add_task(async function test_urls() {
     12  _("URL related Service properties correspond to preference settings.");
     13  try {
     14    Assert.equal(Service.clusterURL, "");
     15    Assert.ok(!Service.userBaseURL);
     16    Assert.equal(Service.infoURL, undefined);
     17    Assert.equal(Service.storageURL, undefined);
     18    Assert.equal(Service.metaURL, undefined);
     19 
     20    _("The 'clusterURL' attribute updates preferences and cached URLs.");
     21 
     22    // Since we don't have a cluster URL yet, these will still not be defined.
     23    Assert.equal(Service.infoURL, undefined);
     24    Assert.ok(!Service.userBaseURL);
     25    Assert.equal(Service.storageURL, undefined);
     26    Assert.equal(Service.metaURL, undefined);
     27 
     28    Service.clusterURL = "http://weave.cluster/1.1/johndoe/";
     29 
     30    Assert.equal(Service.userBaseURL, "http://weave.cluster/1.1/johndoe/");
     31    Assert.equal(
     32      Service.infoURL,
     33      "http://weave.cluster/1.1/johndoe/info/collections"
     34    );
     35    Assert.equal(
     36      Service.storageURL,
     37      "http://weave.cluster/1.1/johndoe/storage/"
     38    );
     39    Assert.equal(
     40      Service.metaURL,
     41      "http://weave.cluster/1.1/johndoe/storage/meta/global"
     42    );
     43  } finally {
     44    for (const pref of Svc.PrefBranch.getChildList("")) {
     45      Svc.PrefBranch.clearUserPref(pref);
     46    }
     47  }
     48 });
     49 
     50 add_test(function test_syncID() {
     51  _("Service.syncID is auto-generated, corresponds to preference.");
     52  new FakeGUIDService();
     53 
     54  try {
     55    // Ensure pristine environment
     56    Assert.equal(
     57      Svc.PrefBranch.getPrefType("client.syncID"),
     58      Ci.nsIPrefBranch.PREF_INVALID
     59    );
     60 
     61    // Performing the first get on the attribute will generate a new GUID.
     62    Assert.equal(Service.syncID, "fake-guid-00");
     63    Assert.equal(Svc.PrefBranch.getStringPref("client.syncID"), "fake-guid-00");
     64 
     65    Svc.PrefBranch.setStringPref("client.syncID", Utils.makeGUID());
     66    Assert.equal(Svc.PrefBranch.getStringPref("client.syncID"), "fake-guid-01");
     67    Assert.equal(Service.syncID, "fake-guid-01");
     68  } finally {
     69    for (const pref of Svc.PrefBranch.getChildList("")) {
     70      Svc.PrefBranch.clearUserPref(pref);
     71    }
     72    new FakeGUIDService();
     73    run_next_test();
     74  }
     75 });
     76 
     77 add_test(function test_locked() {
     78  _("The 'locked' attribute can be toggled with lock() and unlock()");
     79 
     80  // Defaults to false
     81  Assert.equal(Service.locked, false);
     82 
     83  Assert.equal(Service.lock(), true);
     84  Assert.equal(Service.locked, true);
     85 
     86  // Locking again will return false
     87  Assert.equal(Service.lock(), false);
     88 
     89  Service.unlock();
     90  Assert.equal(Service.locked, false);
     91  run_next_test();
     92 });
     93 
     94 add_task(async function test_configure_throws_no_user() {
     95  _("configure() throws when no user is signed in");
     96  const { getFxAccountsSingleton } = ChromeUtils.importESModule(
     97    "resource://gre/modules/FxAccounts.sys.mjs"
     98  );
     99  const fxAccounts = getFxAccountsSingleton();
    100 
    101  const originalGetSignedInUser = fxAccounts.getSignedInUser;
    102  fxAccounts.getSignedInUser = () => Promise.resolve(null);
    103 
    104  try {
    105    await Assert.rejects(
    106      Service.configure(),
    107      /No FxA user is signed in/,
    108      "configure() should throw when no user is signed in"
    109    );
    110  } finally {
    111    fxAccounts.getSignedInUser = originalGetSignedInUser;
    112  }
    113 });
    114 
    115 add_task(async function test_configure_throws_without_keys() {
    116  _("configure() throws when user has no sync keys");
    117  const { getFxAccountsSingleton } = ChromeUtils.importESModule(
    118    "resource://gre/modules/FxAccounts.sys.mjs"
    119  );
    120  const fxAccounts = getFxAccountsSingleton();
    121 
    122  // Mock getSignedInUser to return a user
    123  const originalGetSignedInUser = fxAccounts.getSignedInUser;
    124  fxAccounts.getSignedInUser = () =>
    125    Promise.resolve({ email: "test@example.com", uid: "12345" });
    126 
    127  // Mock hasKeysForScope to return false (no keys)
    128  const originalHasKeysForScope = fxAccounts.keys.hasKeysForScope;
    129  fxAccounts.keys.hasKeysForScope = () => Promise.resolve(false);
    130 
    131  try {
    132    await Assert.rejects(
    133      Service.configure(),
    134      /User does not have sync keys/,
    135      "configure() should throw when no sync keys"
    136    );
    137  } finally {
    138    fxAccounts.getSignedInUser = originalGetSignedInUser;
    139    fxAccounts.keys.hasKeysForScope = originalHasKeysForScope;
    140  }
    141 });
    142 
    143 add_task(async function test_configure_succeeds_with_keys() {
    144  _("configure() succeeds when user has sync keys");
    145  const { getFxAccountsSingleton } = ChromeUtils.importESModule(
    146    "resource://gre/modules/FxAccounts.sys.mjs"
    147  );
    148  const fxAccounts = getFxAccountsSingleton();
    149 
    150  // Mock getSignedInUser to return a user
    151  const originalGetSignedInUser = fxAccounts.getSignedInUser;
    152  fxAccounts.getSignedInUser = () =>
    153    Promise.resolve({ email: "test@example.com", uid: "12345" });
    154 
    155  // Mock hasKeysForScope to return true (has keys)
    156  const originalHasKeysForScope = fxAccounts.keys.hasKeysForScope;
    157  fxAccounts.keys.hasKeysForScope = () => Promise.resolve(true);
    158 
    159  try {
    160    await Service.configure();
    161    // Should set the username pref
    162    Assert.equal(Svc.PrefBranch.getStringPref("username"), "test@example.com");
    163  } finally {
    164    fxAccounts.getSignedInUser = originalGetSignedInUser;
    165    fxAccounts.keys.hasKeysForScope = originalHasKeysForScope;
    166    Svc.PrefBranch.clearUserPref("username");
    167  }
    168 });
    169 
    170 add_task(async function test_third_party_to_sync_complete_flow() {
    171  _("End-to-end: third-party auth (no keys) -> receive keys -> configure sync");
    172  const { getFxAccountsSingleton } = ChromeUtils.importESModule(
    173    "resource://gre/modules/FxAccounts.sys.mjs"
    174  );
    175  const fxAccounts = getFxAccountsSingleton();
    176 
    177  const originalGetSignedInUser = fxAccounts.getSignedInUser;
    178  const originalHasKeysForScope = fxAccounts.keys.hasKeysForScope;
    179 
    180  // 1. User signs in without keys (third-party)
    181  const user = { email: "foo@example.com", uid: "uid12345" };
    182  fxAccounts.getSignedInUser = () => Promise.resolve(user);
    183 
    184  // 2. Initially no keys - hasKeysForScope returns false
    185  fxAccounts.keys.hasKeysForScope = () => Promise.resolve(false);
    186 
    187  try {
    188    await Assert.rejects(
    189      Service.configure(),
    190      /User does not have sync keys/,
    191      "configure() should throw when no sync keys"
    192    );
    193 
    194    fxAccounts.keys.hasKeysForScope = () => Promise.resolve(true);
    195 
    196    await Service.configure();
    197    Assert.equal(Svc.PrefBranch.getStringPref("username"), "foo@example.com");
    198  } finally {
    199    fxAccounts.getSignedInUser = originalGetSignedInUser;
    200    fxAccounts.keys.hasKeysForScope = originalHasKeysForScope;
    201    Svc.PrefBranch.clearUserPref("username");
    202  }
    203 });