From 17e515f8860d694b9091cdc0fe9c151e13bd351e Mon Sep 17 00:00:00 2001 From: lilia Date: Wed, 22 Jul 2015 12:48:08 -0700 Subject: [PATCH] Add identity key conflict tests --- js/axolotl_store.js | 4 +- libtextsecure/test/device_storage_test.js | 50 +++++++++++++++++++++++ libtextsecure/test/index.html | 1 + test/storage_test.js | 11 +++++ 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 libtextsecure/test/device_storage_test.js diff --git a/js/axolotl_store.js b/js/axolotl_store.js index bfcdee07..4c3ac503 100644 --- a/js/axolotl_store.js +++ b/js/axolotl_store.js @@ -259,7 +259,7 @@ publicKey = convertToArrayBuffer(publicKey); } var number = textsecure.utils.unencodeNumber(identifier)[0]; - return new Promise(function(resolve) { + return new Promise(function(resolve, reject) { var identityKey = new IdentityKey({id: number}); identityKey.fetch().always(function() { var oldpublicKey = identityKey.get('publicKey'); @@ -271,7 +271,7 @@ if (equalArrayBuffers(oldpublicKey, publicKey)) { resolve(); } else { - throw new Error("Attempted to overwrite a different identity key"); + reject(new Error("Attempted to overwrite a different identity key")); } } }); diff --git a/libtextsecure/test/device_storage_test.js b/libtextsecure/test/device_storage_test.js new file mode 100644 index 00000000..084039bf --- /dev/null +++ b/libtextsecure/test/device_storage_test.js @@ -0,0 +1,50 @@ +/* vim: ts=4:sw=4 + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program. If not, see . + */ + +'use strict'; + +describe('Device storage', function() { + before(function() { localStorage.clear(); }); + var store = textsecure.storage.axolotl; + var identifier = '+5558675309'; + var another_identifier = '+5555590210'; + var identityKey = { + pubKey: textsecure.crypto.getRandomBytes(33), + privKey: textsecure.crypto.getRandomBytes(32), + }; + var testKey = { + pubKey: textsecure.crypto.getRandomBytes(33), + privKey: textsecure.crypto.getRandomBytes(32), + }; + describe('saveKeysToDeviceObject', function() { + it('rejects if the identity key changes', function(done) { + return textsecure.storage.devices.saveKeysToDeviceObject({ + identityKey: identityKey.pubKey, + encodedNumber: identifier + '.1' + }).then(function() { + textsecure.storage.devices.saveKeysToDeviceObject({ + identityKey: testKey.pubKey, + encodedNumber: identifier + '.1' + }).then(function() { + done(new Error('Allowed to overwrite identity key')); + }).catch(function(e) { + assert.strictEqual(e.message, 'Identity key changed'); + done(); + }); + }); + }); + }); +}); diff --git a/libtextsecure/test/index.html b/libtextsecure/test/index.html index 312d959b..8090f0c9 100644 --- a/libtextsecure/test/index.html +++ b/libtextsecure/test/index.html @@ -52,6 +52,7 @@ + diff --git a/test/storage_test.js b/test/storage_test.js index 37c77fea..c7346e45 100644 --- a/test/storage_test.js +++ b/test/storage_test.js @@ -50,6 +50,17 @@ describe("AxolotlStore", function() { }); }).then(done,done); }); + it('rejects on key change', function(done) { + var newIdentity = textsecure.crypto.getRandomBytes(33); + store.putIdentityKey(identifier, testKey.pubKey).then(function() { + store.putIdentityKey(identifier, newIdentity).then(function() { + done(new Error('Allowed to overwrite identity key')); + }).catch(function(e) { + assert(e instanceof Error); + done(); + }); + }); + }); it('stores prekeys', function(done) { store.putPreKey(1, testKey).then(function() { return store.getPreKey(1).then(function(key) {