Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | Cu.import("resource://gre/modules/Log.jsm"); |
michael@0 | 5 | Cu.import("resource://services-sync/constants.js"); |
michael@0 | 6 | Cu.import("resource://services-sync/keys.js"); |
michael@0 | 7 | Cu.import("resource://services-sync/service.js"); |
michael@0 | 8 | Cu.import("resource://services-sync/util.js"); |
michael@0 | 9 | Cu.import("resource://testing-common/services/sync/fakeservices.js"); |
michael@0 | 10 | Cu.import("resource://testing-common/services/sync/utils.js"); |
michael@0 | 11 | |
michael@0 | 12 | function run_test() { |
michael@0 | 13 | let logger = Log.repository.rootLogger; |
michael@0 | 14 | Log.repository.rootLogger.addAppender(new Log.DumpAppender()); |
michael@0 | 15 | |
michael@0 | 16 | let guidSvc = new FakeGUIDService(); |
michael@0 | 17 | let clients = new ServerCollection(); |
michael@0 | 18 | let meta_global = new ServerWBO('global'); |
michael@0 | 19 | |
michael@0 | 20 | let collectionsHelper = track_collections_helper(); |
michael@0 | 21 | let upd = collectionsHelper.with_updated_collection; |
michael@0 | 22 | let collections = collectionsHelper.collections; |
michael@0 | 23 | |
michael@0 | 24 | function wasCalledHandler(wbo) { |
michael@0 | 25 | let handler = wbo.handler(); |
michael@0 | 26 | return function() { |
michael@0 | 27 | wbo.wasCalled = true; |
michael@0 | 28 | handler.apply(this, arguments); |
michael@0 | 29 | }; |
michael@0 | 30 | } |
michael@0 | 31 | |
michael@0 | 32 | let keysWBO = new ServerWBO("keys"); |
michael@0 | 33 | let cryptoColl = new ServerCollection({keys: keysWBO}); |
michael@0 | 34 | let metaColl = new ServerCollection({global: meta_global}); |
michael@0 | 35 | do_test_pending(); |
michael@0 | 36 | |
michael@0 | 37 | /** |
michael@0 | 38 | * Handle the bulk DELETE request sent by wipeServer. |
michael@0 | 39 | */ |
michael@0 | 40 | function storageHandler(request, response) { |
michael@0 | 41 | do_check_eq("DELETE", request.method); |
michael@0 | 42 | do_check_true(request.hasHeader("X-Confirm-Delete")); |
michael@0 | 43 | |
michael@0 | 44 | _("Wiping out all collections."); |
michael@0 | 45 | cryptoColl.delete({}); |
michael@0 | 46 | clients.delete({}); |
michael@0 | 47 | metaColl.delete({}); |
michael@0 | 48 | |
michael@0 | 49 | let ts = new_timestamp(); |
michael@0 | 50 | collectionsHelper.update_collection("crypto", ts); |
michael@0 | 51 | collectionsHelper.update_collection("clients", ts); |
michael@0 | 52 | collectionsHelper.update_collection("meta", ts); |
michael@0 | 53 | return_timestamp(request, response, ts); |
michael@0 | 54 | } |
michael@0 | 55 | |
michael@0 | 56 | let server = httpd_setup({ |
michael@0 | 57 | "/1.1/johndoe/storage": storageHandler, |
michael@0 | 58 | "/1.1/johndoe/storage/crypto/keys": upd("crypto", keysWBO.handler()), |
michael@0 | 59 | "/1.1/johndoe/storage/crypto": upd("crypto", cryptoColl.handler()), |
michael@0 | 60 | "/1.1/johndoe/storage/clients": upd("clients", clients.handler()), |
michael@0 | 61 | "/1.1/johndoe/storage/meta/global": upd("meta", wasCalledHandler(meta_global)), |
michael@0 | 62 | "/1.1/johndoe/storage/meta": upd("meta", wasCalledHandler(metaColl)), |
michael@0 | 63 | "/1.1/johndoe/info/collections": collectionsHelper.handler |
michael@0 | 64 | }); |
michael@0 | 65 | |
michael@0 | 66 | try { |
michael@0 | 67 | _("Log in."); |
michael@0 | 68 | ensureLegacyIdentityManager(); |
michael@0 | 69 | Service.serverURL = server.baseURI; |
michael@0 | 70 | |
michael@0 | 71 | _("Checking Status.sync with no credentials."); |
michael@0 | 72 | Service.verifyAndFetchSymmetricKeys(); |
michael@0 | 73 | do_check_eq(Service.status.sync, CREDENTIALS_CHANGED); |
michael@0 | 74 | do_check_eq(Service.status.login, LOGIN_FAILED_NO_PASSPHRASE); |
michael@0 | 75 | |
michael@0 | 76 | _("Log in with an old secret phrase, is upgraded to Sync Key."); |
michael@0 | 77 | Service.login("johndoe", "ilovejane", "my old secret phrase!!1!"); |
michael@0 | 78 | _("End of login"); |
michael@0 | 79 | do_check_true(Service.isLoggedIn); |
michael@0 | 80 | do_check_true(Utils.isPassphrase(Service.identity.syncKey)); |
michael@0 | 81 | let syncKey = Service.identity.syncKey; |
michael@0 | 82 | Service.startOver(); |
michael@0 | 83 | |
michael@0 | 84 | Service.serverURL = server.baseURI; |
michael@0 | 85 | Service.login("johndoe", "ilovejane", syncKey); |
michael@0 | 86 | do_check_true(Service.isLoggedIn); |
michael@0 | 87 | |
michael@0 | 88 | _("Checking that remoteSetup returns true when credentials have changed."); |
michael@0 | 89 | Service.recordManager.get(Service.metaURL).payload.syncID = "foobar"; |
michael@0 | 90 | do_check_true(Service._remoteSetup()); |
michael@0 | 91 | |
michael@0 | 92 | _("Do an initial sync."); |
michael@0 | 93 | let beforeSync = Date.now()/1000; |
michael@0 | 94 | Service.sync(); |
michael@0 | 95 | |
michael@0 | 96 | _("Checking that remoteSetup returns true."); |
michael@0 | 97 | do_check_true(Service._remoteSetup()); |
michael@0 | 98 | |
michael@0 | 99 | _("Verify that the meta record was uploaded."); |
michael@0 | 100 | do_check_eq(meta_global.data.syncID, Service.syncID); |
michael@0 | 101 | do_check_eq(meta_global.data.storageVersion, STORAGE_VERSION); |
michael@0 | 102 | do_check_eq(meta_global.data.engines.clients.version, Service.clientsEngine.version); |
michael@0 | 103 | do_check_eq(meta_global.data.engines.clients.syncID, Service.clientsEngine.syncID); |
michael@0 | 104 | |
michael@0 | 105 | _("Set the collection info hash so that sync() will remember the modified times for future runs."); |
michael@0 | 106 | collections.meta = Service.clientsEngine.lastSync; |
michael@0 | 107 | collections.clients = Service.clientsEngine.lastSync; |
michael@0 | 108 | Service.sync(); |
michael@0 | 109 | |
michael@0 | 110 | _("Sync again and verify that meta/global wasn't downloaded again"); |
michael@0 | 111 | meta_global.wasCalled = false; |
michael@0 | 112 | Service.sync(); |
michael@0 | 113 | do_check_false(meta_global.wasCalled); |
michael@0 | 114 | |
michael@0 | 115 | _("Fake modified records. This will cause a redownload, but not reupload since it hasn't changed."); |
michael@0 | 116 | collections.meta += 42; |
michael@0 | 117 | meta_global.wasCalled = false; |
michael@0 | 118 | |
michael@0 | 119 | let metaModified = meta_global.modified; |
michael@0 | 120 | |
michael@0 | 121 | Service.sync(); |
michael@0 | 122 | do_check_true(meta_global.wasCalled); |
michael@0 | 123 | do_check_eq(metaModified, meta_global.modified); |
michael@0 | 124 | |
michael@0 | 125 | _("Checking bad passphrases."); |
michael@0 | 126 | let pp = Service.identity.syncKey; |
michael@0 | 127 | Service.identity.syncKey = "notvalid"; |
michael@0 | 128 | do_check_false(Service.verifyAndFetchSymmetricKeys()); |
michael@0 | 129 | do_check_eq(Service.status.sync, CREDENTIALS_CHANGED); |
michael@0 | 130 | do_check_eq(Service.status.login, LOGIN_FAILED_INVALID_PASSPHRASE); |
michael@0 | 131 | Service.identity.syncKey = pp; |
michael@0 | 132 | do_check_true(Service.verifyAndFetchSymmetricKeys()); |
michael@0 | 133 | |
michael@0 | 134 | // changePassphrase wipes our keys, and they're regenerated on next sync. |
michael@0 | 135 | _("Checking changed passphrase."); |
michael@0 | 136 | let existingDefault = Service.collectionKeys.keyForCollection(); |
michael@0 | 137 | let existingKeysPayload = keysWBO.payload; |
michael@0 | 138 | let newPassphrase = "bbbbbabcdeabcdeabcdeabcdea"; |
michael@0 | 139 | Service.changePassphrase(newPassphrase); |
michael@0 | 140 | |
michael@0 | 141 | _("Local key cache is full, but different."); |
michael@0 | 142 | do_check_true(!!Service.collectionKeys._default); |
michael@0 | 143 | do_check_false(Service.collectionKeys._default.equals(existingDefault)); |
michael@0 | 144 | |
michael@0 | 145 | _("Server has new keys."); |
michael@0 | 146 | do_check_true(!!keysWBO.payload); |
michael@0 | 147 | do_check_true(!!keysWBO.modified); |
michael@0 | 148 | do_check_neq(keysWBO.payload, existingKeysPayload); |
michael@0 | 149 | |
michael@0 | 150 | // Try to screw up HMAC calculation. |
michael@0 | 151 | // Re-encrypt keys with a new random keybundle, and upload them to the |
michael@0 | 152 | // server, just as might happen with a second client. |
michael@0 | 153 | _("Attempting to screw up HMAC by re-encrypting keys."); |
michael@0 | 154 | let keys = Service.collectionKeys.asWBO(); |
michael@0 | 155 | let b = new BulkKeyBundle("hmacerror"); |
michael@0 | 156 | b.generateRandom(); |
michael@0 | 157 | collections.crypto = keys.modified = 100 + (Date.now()/1000); // Future modification time. |
michael@0 | 158 | keys.encrypt(b); |
michael@0 | 159 | keys.upload(Service.resource(Service.cryptoKeysURL)); |
michael@0 | 160 | |
michael@0 | 161 | do_check_false(Service.verifyAndFetchSymmetricKeys()); |
michael@0 | 162 | do_check_eq(Service.status.login, LOGIN_FAILED_INVALID_PASSPHRASE); |
michael@0 | 163 | |
michael@0 | 164 | } finally { |
michael@0 | 165 | Svc.Prefs.resetBranch(""); |
michael@0 | 166 | server.stop(do_test_finished); |
michael@0 | 167 | } |
michael@0 | 168 | } |