1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/services/sync/tests/unit/test_service_wipeClient.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,112 @@ 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://services-sync/identity.js"); 1.8 +Cu.import("resource://services-sync/engines.js"); 1.9 +Cu.import("resource://services-sync/record.js"); 1.10 +Cu.import("resource://services-sync/service.js"); 1.11 +Cu.import("resource://services-sync/util.js"); 1.12 +Cu.import("resource://testing-common/services/sync/utils.js"); 1.13 + 1.14 +Service.engineManager.clear(); 1.15 + 1.16 +function CanDecryptEngine() { 1.17 + SyncEngine.call(this, "CanDecrypt", Service); 1.18 +} 1.19 +CanDecryptEngine.prototype = { 1.20 + __proto__: SyncEngine.prototype, 1.21 + 1.22 + // Override these methods with mocks for the test 1.23 + canDecrypt: function canDecrypt() { 1.24 + return true; 1.25 + }, 1.26 + 1.27 + wasWiped: false, 1.28 + wipeClient: function wipeClient() { 1.29 + this.wasWiped = true; 1.30 + } 1.31 +}; 1.32 +Service.engineManager.register(CanDecryptEngine); 1.33 + 1.34 + 1.35 +function CannotDecryptEngine() { 1.36 + SyncEngine.call(this, "CannotDecrypt", Service); 1.37 +} 1.38 +CannotDecryptEngine.prototype = { 1.39 + __proto__: SyncEngine.prototype, 1.40 + 1.41 + // Override these methods with mocks for the test 1.42 + canDecrypt: function canDecrypt() { 1.43 + return false; 1.44 + }, 1.45 + 1.46 + wasWiped: false, 1.47 + wipeClient: function wipeClient() { 1.48 + this.wasWiped = true; 1.49 + } 1.50 +}; 1.51 +Service.engineManager.register(CannotDecryptEngine); 1.52 + 1.53 + 1.54 +add_test(function test_withEngineList() { 1.55 + try { 1.56 + _("Ensure initial scenario."); 1.57 + do_check_false(Service.engineManager.get("candecrypt").wasWiped); 1.58 + do_check_false(Service.engineManager.get("cannotdecrypt").wasWiped); 1.59 + 1.60 + _("Wipe local engine data."); 1.61 + Service.wipeClient(["candecrypt", "cannotdecrypt"]); 1.62 + 1.63 + _("Ensure only the engine that can decrypt was wiped."); 1.64 + do_check_true(Service.engineManager.get("candecrypt").wasWiped); 1.65 + do_check_false(Service.engineManager.get("cannotdecrypt").wasWiped); 1.66 + } finally { 1.67 + Service.engineManager.get("candecrypt").wasWiped = false; 1.68 + Service.engineManager.get("cannotdecrypt").wasWiped = false; 1.69 + Service.startOver(); 1.70 + } 1.71 + 1.72 + run_next_test(); 1.73 +}); 1.74 + 1.75 +add_test(function test_startOver_clears_keys() { 1.76 + generateNewKeys(Service.collectionKeys); 1.77 + do_check_true(!!Service.collectionKeys.keyForCollection()); 1.78 + Service.startOver(); 1.79 + do_check_false(!!Service.collectionKeys.keyForCollection()); 1.80 + 1.81 + run_next_test(); 1.82 +}); 1.83 + 1.84 +add_test(function test_credentials_preserved() { 1.85 + _("Ensure that credentials are preserved if client is wiped."); 1.86 + 1.87 + // Required for wipeClient(). 1.88 + ensureLegacyIdentityManager(); 1.89 + Service.identity.account = "testaccount"; 1.90 + Service.identity.basicPassword = "testpassword"; 1.91 + Service.clusterURL = "http://dummy:9000/"; 1.92 + let key = Utils.generatePassphrase(); 1.93 + Service.identity.syncKey = key; 1.94 + Service.identity.persistCredentials(); 1.95 + 1.96 + // Simulate passwords engine wipe without all the overhead. To do this 1.97 + // properly would require extra test infrastructure. 1.98 + Services.logins.removeAllLogins(); 1.99 + Service.wipeClient(); 1.100 + 1.101 + let id = new IdentityManager(); 1.102 + do_check_eq(id.account, "testaccount"); 1.103 + do_check_eq(id.basicPassword, "testpassword"); 1.104 + do_check_eq(id.syncKey, key); 1.105 + 1.106 + Service.startOver(); 1.107 + 1.108 + run_next_test(); 1.109 +}); 1.110 + 1.111 +function run_test() { 1.112 + initTestLogging(); 1.113 + 1.114 + run_next_test(); 1.115 +}