services/sync/tests/unit/test_password_mpenabled.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/services/sync/tests/unit/test_password_mpenabled.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,137 @@
     1.4 +/* Any copyright is dedicated to the Public Domain.
     1.5 +   http://creativecommons.org/publicdomain/zero/1.0/ */
     1.6 +
     1.7 +Cu.import("resource://gre/modules/Log.jsm");
     1.8 +Cu.import("resource://services-sync/stages/enginesync.js");
     1.9 +Cu.import("resource://services-sync/util.js");
    1.10 +Cu.import("resource://services-sync/engines/passwords.js");
    1.11 +Cu.import("resource://services-sync/service.js");
    1.12 +Cu.import("resource://testing-common/services/sync/utils.js");
    1.13 +
    1.14 +function run_test() {
    1.15 +  initTestLogging("Trace");
    1.16 +  run_next_test();
    1.17 +}
    1.18 +
    1.19 +add_test(function test_simple() {
    1.20 +  ensureLegacyIdentityManager();
    1.21 +  // Stub fxAccountsEnabled
    1.22 +  let xpcs = Cc["@mozilla.org/weave/service;1"]
    1.23 +             .getService(Components.interfaces.nsISupports)
    1.24 +             .wrappedJSObject;
    1.25 +  let fxaEnabledGetter = xpcs.__lookupGetter__("fxAccountsEnabled");
    1.26 +  xpcs.__defineGetter__("fxAccountsEnabled", () => true);
    1.27 +
    1.28 +  // Stub mpEnabled.
    1.29 +  let mpEnabledF = Utils.mpEnabled;
    1.30 +  let mpEnabled = false;
    1.31 +  Utils.mpEnabled = function() mpEnabled;
    1.32 +
    1.33 +  let manager = Service.engineManager;
    1.34 +
    1.35 +  Service.engineManager.register(PasswordEngine);
    1.36 +  let engine = Service.engineManager.get("passwords");
    1.37 +  let wipeCount = 0;
    1.38 +  let engineWipeServerF = engine.wipeServer;
    1.39 +  engine.wipeServer = function() {
    1.40 +    ++wipeCount;
    1.41 +  }
    1.42 +
    1.43 +  // A server for the metadata.
    1.44 +  let server  = new SyncServer();
    1.45 +  let johndoe = server.registerUser("johndoe", "password");
    1.46 +  johndoe.createContents({
    1.47 +    meta: {global: {engines: {passwords: {version: engine.version,
    1.48 +                                          syncID: engine.syncID}}}},
    1.49 +    crypto: {},
    1.50 +    clients: {}
    1.51 +  });
    1.52 +  server.start();
    1.53 +  setBasicCredentials("johndoe", "password", "abcdeabcdeabcdeabcdeabcdea");
    1.54 +  Service.serverURL = server.baseURI;
    1.55 +  Service.clusterURL = server.baseURI;
    1.56 +
    1.57 +  let engineSync = new EngineSynchronizer(Service);
    1.58 +  engineSync._log.level = Log.Level.Trace;
    1.59 +
    1.60 +  function assertEnabled(expected, message) {
    1.61 +    Assert.strictEqual(engine.enabled, expected, message);
    1.62 +    // The preference *must* reflect the actual state.
    1.63 +    Assert.strictEqual(Svc.Prefs.get("engine." + engine.prefName), expected,
    1.64 +                       message + " (pref should match enabled state)");
    1.65 +  }
    1.66 +
    1.67 +  try {
    1.68 +    assertEnabled(true, "password engine should be enabled by default")
    1.69 +    let engineMeta = Service.recordManager.get(engine.metaURL);
    1.70 +    // This engine should be in the meta/global
    1.71 +    Assert.notStrictEqual(engineMeta.payload.engines[engine.name], undefined,
    1.72 +                          "The engine should appear in the metadata");
    1.73 +    Assert.ok(!engineMeta.changed, "the metadata for the password engine hasn't changed");
    1.74 +
    1.75 +    // (pretend to) enable a master-password
    1.76 +    mpEnabled = true;
    1.77 +    // The password engine should be locally disabled...
    1.78 +    assertEnabled(false, "if mp is locked the engine should be disabled");
    1.79 +    // ...but not declined.
    1.80 +    Assert.ok(!manager.isDeclined("passwords"), "password engine is not declined");
    1.81 +    // Next time a sync would happen, we call _updateEnabledEngines(), which
    1.82 +    // would remove the engine from the metadata - call that now.
    1.83 +    engineSync._updateEnabledEngines();
    1.84 +    // The global meta should no longer list the engine.
    1.85 +    engineMeta = Service.recordManager.get(engine.metaURL);
    1.86 +    Assert.strictEqual(engineMeta.payload.engines[engine.name], undefined,
    1.87 +                       "The engine should have vanished");
    1.88 +    // And we should have wiped the server data.
    1.89 +    Assert.strictEqual(wipeCount, 1, "wipeServer should have been called");
    1.90 +
    1.91 +    // Now simulate an incoming meta/global indicating the engine should be
    1.92 +    // enabled.  We should fail to actually enable it - the pref should remain
    1.93 +    // false and we wipe the server for anything another device might have
    1.94 +    // stored.
    1.95 +    let meta = {
    1.96 +      payload: {
    1.97 +        engines: {
    1.98 +          "passwords": {"version":1,"syncID":"yfBi2v7PpFO2"},
    1.99 +        },
   1.100 +      },
   1.101 +    };
   1.102 +    engineSync._updateEnabledFromMeta(meta, 3, manager);
   1.103 +    Assert.strictEqual(wipeCount, 2, "wipeServer should have been called");
   1.104 +    Assert.ok(!manager.isDeclined("passwords"), "password engine is not declined");
   1.105 +    assertEnabled(false, "engine still not enabled locally");
   1.106 +
   1.107 +    // Let's turn the MP off - but *not* re-enable it locally.
   1.108 +    mpEnabled = false;
   1.109 +    // Just disabling the MP isn't enough to force it back to enabled.
   1.110 +    assertEnabled(false, "engine still not enabled locally");
   1.111 +    // Another incoming metadata record with the engine enabled should cause
   1.112 +    // it to be enabled locally.
   1.113 +    meta = {
   1.114 +      payload: {
   1.115 +        engines: {
   1.116 +          "passwords": 1,
   1.117 +        },
   1.118 +      },
   1.119 +    };
   1.120 +    engineSync._updateEnabledFromMeta(meta, 3, manager);
   1.121 +    Assert.strictEqual(wipeCount, 2, "wipeServer should *not* have been called again");
   1.122 +    Assert.ok(!manager.isDeclined("passwords"), "password engine is not declined");
   1.123 +    // It should be enabled locally.
   1.124 +    assertEnabled(true, "engine now enabled locally");
   1.125 +    // Next time a sync starts it should magically re-appear in our meta/global
   1.126 +    engine._syncStartup();
   1.127 +    //engineSync._updateEnabledEngines();
   1.128 +    engineMeta = Service.recordManager.get(engine.metaURL);
   1.129 +    Assert.equal(engineMeta.payload.engines[engine.name].version, engine.version,
   1.130 +                 "The engine should re-appear in the metadata");
   1.131 +  } finally {
   1.132 +    // restore the damage we did above...
   1.133 +    engine.wipeServer = engineWipeServerF;
   1.134 +    engine._store.wipe();
   1.135 +    // Un-stub mpEnabled and fxAccountsEnabled
   1.136 +    Utils.mpEnabled = mpEnabledF;
   1.137 +    xpcs.__defineGetter__("fxAccountsEnabled", fxaEnabledGetter);
   1.138 +    server.stop(run_next_test);
   1.139 +  }
   1.140 +});

mercurial