diff --git a/libloki/test/libloki-protocol_test.js b/libloki/test/libloki-protocol_test.js index c165b6d27..8b3318079 100644 --- a/libloki/test/libloki-protocol_test.js +++ b/libloki/test/libloki-protocol_test.js @@ -57,4 +57,57 @@ describe('ConversationCollection', () => { for (let i = 0 ; i !== testKeyArray.byteLength ; i += 1) assert.strictEqual(testKeyArray[i], signedKeyArray[i]); }); + + it('should should return the same prekey bundle after creating a contact', async () => { + const pubKey = libsignal.crypto.getRandomBytes(32); + const pubKeyString = StringView.arrayBufferToHex(pubKey); + const bundle1 = await libloki.getPreKeyBundleForNumber(pubKeyString); + const bundle2 = await libloki.getPreKeyBundleForNumber(pubKeyString); + + assert.isDefined(bundle1); + assert.isDefined(bundle1.identityKey); + assert.isDefined(bundle1.deviceId); + assert.isDefined(bundle1.preKeyId); + assert.isDefined(bundle1.signedKeyId); + assert.isDefined(bundle1.preKey); + assert.isDefined(bundle1.signedKey); + assert.isDefined(bundle1.signature); + + assert.isDefined(bundle2); + assert.isDefined(bundle2.identityKey); + assert.isDefined(bundle2.deviceId); + assert.isDefined(bundle2.preKeyId); + assert.isDefined(bundle2.signedKeyId); + assert.isDefined(bundle2.preKey); + assert.isDefined(bundle2.signedKey); + assert.isDefined(bundle2.signature); + + const identityKeyArray1 = new Uint8Array(bundle1.identityKey.toArrayBuffer()); + const identityKeyArray2 = new Uint8Array(bundle2.identityKey.toArrayBuffer()); + assert.strictEqual(identityKeyArray2.byteLength, identityKeyArray2.byteLength); + for (let i = 0 ; i !== identityKeyArray2.byteLength ; i += 1) + assert.strictEqual(identityKeyArray1[i], identityKeyArray2[i]); + + assert.strictEqual(bundle1.deviceId, bundle2.deviceId); + assert.strictEqual(bundle1.preKeyId, bundle2.preKeyId); + assert.strictEqual(bundle1.signedKeyId, bundle2.signedKeyId); + + const preKeyArray1 = new Uint8Array(bundle1.preKey.toArrayBuffer()); + const preKeyArray2 = new Uint8Array(bundle2.preKey.toArrayBuffer()); + assert.strictEqual(preKeyArray2.byteLength, preKeyArray2.byteLength); + for (let i = 0 ; i !== preKeyArray2.byteLength ; i += 1) + assert.strictEqual(preKeyArray1[i], preKeyArray2[i]); + + const signedKeyArray1 = new Uint8Array(bundle1.signedKey.toArrayBuffer()); + const signedKeyArray2 = new Uint8Array(bundle2.signedKey.toArrayBuffer()); + assert.strictEqual(signedKeyArray2.byteLength, signedKeyArray2.byteLength); + for (let i = 0 ; i !== signedKeyArray2.byteLength ; i += 1) + assert.strictEqual(signedKeyArray1[i], signedKeyArray2[i]); + + const signatureArray1 = new Uint8Array(bundle1.signature.toArrayBuffer()); + const signatureArray2 = new Uint8Array(bundle2.signature.toArrayBuffer()); + assert.strictEqual(signatureArray2.byteLength, signatureArray2.byteLength); + for (let i = 0 ; i !== signatureArray2.byteLength ; i += 1) + assert.strictEqual(signatureArray1[i], signatureArray2[i]); + }); }); diff --git a/libtextsecure/test/in_memory_signal_protocol_store.js b/libtextsecure/test/in_memory_signal_protocol_store.js index 712208853..35d5e145d 100644 --- a/libtextsecure/test/in_memory_signal_protocol_store.js +++ b/libtextsecure/test/in_memory_signal_protocol_store.js @@ -75,7 +75,18 @@ SignalProtocolStore.prototype = { resolve(res); }); }, - storePreKey(keyId, keyPair) { + storePreKey(keyId, keyPair, contactIdentityKeyString = null) { + if (contactIdentityKeyString) { + const data = { + id: keyId, + publicKey: keyPair.pubKey, + privateKey: keyPair.privKey, + recipient: contactIdentityKeyString, + }; + return new Promise(resolve => { + resolve(this.put(`25519KeypreKey${contactIdentityKeyString}`, data)); + }); + } return new Promise(resolve => { resolve(this.put(`25519KeypreKey${keyId}`, keyPair)); });