diff --git a/js/axolotl_store.js b/js/axolotl_store.js
index bfcdee07d..4c3ac5033 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 000000000..084039bf3
--- /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 312d959bb..8090f0c92 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 37c77fead..c7346e458 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) {