services/sync/tests/unit/test_password_mpenabled.js

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

     1 /* Any copyright is dedicated to the Public Domain.
     2    http://creativecommons.org/publicdomain/zero/1.0/ */
     4 Cu.import("resource://gre/modules/Log.jsm");
     5 Cu.import("resource://services-sync/stages/enginesync.js");
     6 Cu.import("resource://services-sync/util.js");
     7 Cu.import("resource://services-sync/engines/passwords.js");
     8 Cu.import("resource://services-sync/service.js");
     9 Cu.import("resource://testing-common/services/sync/utils.js");
    11 function run_test() {
    12   initTestLogging("Trace");
    13   run_next_test();
    14 }
    16 add_test(function test_simple() {
    17   ensureLegacyIdentityManager();
    18   // Stub fxAccountsEnabled
    19   let xpcs = Cc["@mozilla.org/weave/service;1"]
    20              .getService(Components.interfaces.nsISupports)
    21              .wrappedJSObject;
    22   let fxaEnabledGetter = xpcs.__lookupGetter__("fxAccountsEnabled");
    23   xpcs.__defineGetter__("fxAccountsEnabled", () => true);
    25   // Stub mpEnabled.
    26   let mpEnabledF = Utils.mpEnabled;
    27   let mpEnabled = false;
    28   Utils.mpEnabled = function() mpEnabled;
    30   let manager = Service.engineManager;
    32   Service.engineManager.register(PasswordEngine);
    33   let engine = Service.engineManager.get("passwords");
    34   let wipeCount = 0;
    35   let engineWipeServerF = engine.wipeServer;
    36   engine.wipeServer = function() {
    37     ++wipeCount;
    38   }
    40   // A server for the metadata.
    41   let server  = new SyncServer();
    42   let johndoe = server.registerUser("johndoe", "password");
    43   johndoe.createContents({
    44     meta: {global: {engines: {passwords: {version: engine.version,
    45                                           syncID: engine.syncID}}}},
    46     crypto: {},
    47     clients: {}
    48   });
    49   server.start();
    50   setBasicCredentials("johndoe", "password", "abcdeabcdeabcdeabcdeabcdea");
    51   Service.serverURL = server.baseURI;
    52   Service.clusterURL = server.baseURI;
    54   let engineSync = new EngineSynchronizer(Service);
    55   engineSync._log.level = Log.Level.Trace;
    57   function assertEnabled(expected, message) {
    58     Assert.strictEqual(engine.enabled, expected, message);
    59     // The preference *must* reflect the actual state.
    60     Assert.strictEqual(Svc.Prefs.get("engine." + engine.prefName), expected,
    61                        message + " (pref should match enabled state)");
    62   }
    64   try {
    65     assertEnabled(true, "password engine should be enabled by default")
    66     let engineMeta = Service.recordManager.get(engine.metaURL);
    67     // This engine should be in the meta/global
    68     Assert.notStrictEqual(engineMeta.payload.engines[engine.name], undefined,
    69                           "The engine should appear in the metadata");
    70     Assert.ok(!engineMeta.changed, "the metadata for the password engine hasn't changed");
    72     // (pretend to) enable a master-password
    73     mpEnabled = true;
    74     // The password engine should be locally disabled...
    75     assertEnabled(false, "if mp is locked the engine should be disabled");
    76     // ...but not declined.
    77     Assert.ok(!manager.isDeclined("passwords"), "password engine is not declined");
    78     // Next time a sync would happen, we call _updateEnabledEngines(), which
    79     // would remove the engine from the metadata - call that now.
    80     engineSync._updateEnabledEngines();
    81     // The global meta should no longer list the engine.
    82     engineMeta = Service.recordManager.get(engine.metaURL);
    83     Assert.strictEqual(engineMeta.payload.engines[engine.name], undefined,
    84                        "The engine should have vanished");
    85     // And we should have wiped the server data.
    86     Assert.strictEqual(wipeCount, 1, "wipeServer should have been called");
    88     // Now simulate an incoming meta/global indicating the engine should be
    89     // enabled.  We should fail to actually enable it - the pref should remain
    90     // false and we wipe the server for anything another device might have
    91     // stored.
    92     let meta = {
    93       payload: {
    94         engines: {
    95           "passwords": {"version":1,"syncID":"yfBi2v7PpFO2"},
    96         },
    97       },
    98     };
    99     engineSync._updateEnabledFromMeta(meta, 3, manager);
   100     Assert.strictEqual(wipeCount, 2, "wipeServer should have been called");
   101     Assert.ok(!manager.isDeclined("passwords"), "password engine is not declined");
   102     assertEnabled(false, "engine still not enabled locally");
   104     // Let's turn the MP off - but *not* re-enable it locally.
   105     mpEnabled = false;
   106     // Just disabling the MP isn't enough to force it back to enabled.
   107     assertEnabled(false, "engine still not enabled locally");
   108     // Another incoming metadata record with the engine enabled should cause
   109     // it to be enabled locally.
   110     meta = {
   111       payload: {
   112         engines: {
   113           "passwords": 1,
   114         },
   115       },
   116     };
   117     engineSync._updateEnabledFromMeta(meta, 3, manager);
   118     Assert.strictEqual(wipeCount, 2, "wipeServer should *not* have been called again");
   119     Assert.ok(!manager.isDeclined("passwords"), "password engine is not declined");
   120     // It should be enabled locally.
   121     assertEnabled(true, "engine now enabled locally");
   122     // Next time a sync starts it should magically re-appear in our meta/global
   123     engine._syncStartup();
   124     //engineSync._updateEnabledEngines();
   125     engineMeta = Service.recordManager.get(engine.metaURL);
   126     Assert.equal(engineMeta.payload.engines[engine.name].version, engine.version,
   127                  "The engine should re-appear in the metadata");
   128   } finally {
   129     // restore the damage we did above...
   130     engine.wipeServer = engineWipeServerF;
   131     engine._store.wipe();
   132     // Un-stub mpEnabled and fxAccountsEnabled
   133     Utils.mpEnabled = mpEnabledF;
   134     xpcs.__defineGetter__("fxAccountsEnabled", fxaEnabledGetter);
   135     server.stop(run_next_test);
   136   }
   137 });

mercurial