2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2015-2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
/* global textsecure, storage, ConversationController */
|
2018-11-02 18:02:53 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('SignalProtocolStore', () => {
|
|
|
|
const store = textsecure.storage.protocol;
|
|
|
|
const identifier = '+5558675309';
|
|
|
|
const identityKey = {
|
2021-04-16 23:13:13 +00:00
|
|
|
pubKey: window.Signal.Crypto.getRandomBytes(33),
|
|
|
|
privKey: window.Signal.Crypto.getRandomBytes(32),
|
2018-05-02 16:51:22 +00:00
|
|
|
};
|
2018-07-21 21:51:20 +00:00
|
|
|
const testKey = {
|
2021-04-16 23:13:13 +00:00
|
|
|
pubKey: window.Signal.Crypto.getRandomBytes(33),
|
|
|
|
privKey: window.Signal.Crypto.getRandomBytes(32),
|
2018-05-02 16:51:22 +00:00
|
|
|
};
|
2020-03-05 21:14:58 +00:00
|
|
|
before(async () => {
|
|
|
|
localStorage.clear();
|
|
|
|
ConversationController.reset();
|
|
|
|
// store.hydrateCaches();
|
|
|
|
await storage.fetch();
|
|
|
|
await ConversationController.load();
|
|
|
|
await ConversationController.getOrCreateAndWait(identifier, 'private');
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('retrieves my registration id', async () => {
|
2018-05-02 16:51:22 +00:00
|
|
|
store.put('registrationId', 1337);
|
2018-11-02 18:02:53 +00:00
|
|
|
|
|
|
|
const reg = await store.getLocalRegistrationId();
|
|
|
|
assert.strictEqual(reg, 1337);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('retrieves my identity key', async () => {
|
2018-05-02 16:51:22 +00:00
|
|
|
store.put('identityKey', identityKey);
|
2018-11-02 18:02:53 +00:00
|
|
|
const key = await store.getIdentityKeyPair();
|
|
|
|
assertEqualArrayBuffers(key.pubKey, identityKey.pubKey);
|
|
|
|
assertEqualArrayBuffers(key.privKey, identityKey.privKey);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('stores identity keys', async () => {
|
|
|
|
await store.saveIdentity(identifier, testKey.pubKey);
|
|
|
|
const key = await store.loadIdentityKey(identifier);
|
|
|
|
assertEqualArrayBuffers(key, testKey.pubKey);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns whether a key is trusted', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = window.Signal.Crypto.getRandomBytes(33);
|
2018-11-02 18:02:53 +00:00
|
|
|
await store.saveIdentity(identifier, testKey.pubKey);
|
|
|
|
|
|
|
|
const trusted = await store.isTrustedIdentity(identifier, newIdentity);
|
|
|
|
if (trusted) {
|
|
|
|
throw new Error('Allowed to overwrite identity key');
|
|
|
|
}
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns whether a key is untrusted', async () => {
|
|
|
|
await store.saveIdentity(identifier, testKey.pubKey);
|
|
|
|
const trusted = await store.isTrustedIdentity(identifier, testKey.pubKey);
|
|
|
|
|
|
|
|
if (!trusted) {
|
|
|
|
throw new Error('Allowed to overwrite identity key');
|
|
|
|
}
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('stores prekeys', async () => {
|
|
|
|
await store.storePreKey(1, testKey);
|
|
|
|
|
|
|
|
const key = await store.loadPreKey(1);
|
|
|
|
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
|
|
|
|
assertEqualArrayBuffers(key.privKey, testKey.privKey);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('deletes prekeys', async () => {
|
|
|
|
await store.storePreKey(2, testKey);
|
|
|
|
await store.removePreKey(2, testKey);
|
|
|
|
|
|
|
|
const key = await store.loadPreKey(2);
|
|
|
|
assert.isUndefined(key);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('stores signed prekeys', async () => {
|
|
|
|
await store.storeSignedPreKey(3, testKey);
|
|
|
|
|
|
|
|
const key = await store.loadSignedPreKey(3);
|
|
|
|
assertEqualArrayBuffers(key.pubKey, testKey.pubKey);
|
|
|
|
assertEqualArrayBuffers(key.privKey, testKey.privKey);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('deletes signed prekeys', async () => {
|
|
|
|
await store.storeSignedPreKey(4, testKey);
|
|
|
|
await store.removeSignedPreKey(4, testKey);
|
|
|
|
|
|
|
|
const key = await store.loadSignedPreKey(4);
|
|
|
|
assert.isUndefined(key);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('stores sessions', async () => {
|
2018-07-21 21:51:20 +00:00
|
|
|
const testRecord = 'an opaque string';
|
|
|
|
const devices = [1, 2, 3].map(deviceId => [identifier, deviceId].join('.'));
|
2018-11-02 18:02:53 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
devices.map(async encodedNumber => {
|
|
|
|
await store.storeSession(encodedNumber, testRecord + encodedNumber);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const records = await Promise.all(
|
|
|
|
devices.map(store.loadSession.bind(store))
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 0, max = records.length; i < max; i += 1) {
|
|
|
|
assert.strictEqual(records[i], testRecord + devices[i]);
|
|
|
|
}
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('removes all sessions for a number', async () => {
|
2018-07-21 21:51:20 +00:00
|
|
|
const testRecord = 'an opaque string';
|
|
|
|
const devices = [1, 2, 3].map(deviceId => [identifier, deviceId].join('.'));
|
2018-11-02 18:02:53 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
devices.map(async encodedNumber => {
|
|
|
|
await store.storeSession(encodedNumber, testRecord + encodedNumber);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
await store.removeAllSessions(identifier);
|
|
|
|
|
|
|
|
const records = await Promise.all(
|
|
|
|
devices.map(store.loadSession.bind(store))
|
|
|
|
);
|
|
|
|
|
|
|
|
for (let i = 0, max = records.length; i < max; i += 1) {
|
|
|
|
assert.isUndefined(records[i]);
|
|
|
|
}
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns deviceIds for a number', async () => {
|
2018-07-21 21:51:20 +00:00
|
|
|
const testRecord = 'an opaque string';
|
|
|
|
const devices = [1, 2, 3].map(deviceId => [identifier, deviceId].join('.'));
|
2018-11-02 18:02:53 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
devices.map(async encodedNumber => {
|
|
|
|
await store.storeSession(encodedNumber, testRecord + encodedNumber);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
|
|
|
|
const deviceIds = await store.getDeviceIds(identifier);
|
|
|
|
assert.sameMembers(deviceIds, [1, 2, 3]);
|
|
|
|
});
|
|
|
|
it('returns empty array for a number with no device ids', async () => {
|
|
|
|
const deviceIds = await store.getDeviceIds('foo');
|
|
|
|
assert.sameMembers(deviceIds, []);
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|