2021-04-16 23:13:13 +00:00
|
|
|
// Copyright 2015-2021 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
2015-04-01 20:08:09 +00:00
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
import { assert } from 'chai';
|
2021-05-14 01:18:43 +00:00
|
|
|
import {
|
|
|
|
Direction,
|
|
|
|
SenderKeyRecord,
|
|
|
|
SessionRecord,
|
|
|
|
} from '@signalapp/signal-client';
|
2021-04-16 23:13:13 +00:00
|
|
|
|
|
|
|
import { signal } from '../protobuf/compiled';
|
|
|
|
import { sessionStructureToArrayBuffer } from '../util/sessionTranslation';
|
|
|
|
|
|
|
|
import { getRandomBytes, constantTimeEqual } from '../Crypto';
|
|
|
|
import { clampPrivateKey, setPublicKeyTypeByte } from '../Curve';
|
|
|
|
import { SignalProtocolStore } from '../SignalProtocolStore';
|
|
|
|
import { IdentityKeyType, KeyPairType } from '../textsecure/Types.d';
|
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
const {
|
|
|
|
RecordStructure,
|
|
|
|
SessionStructure,
|
|
|
|
SenderKeyRecordStructure,
|
|
|
|
SenderKeyStateStructure,
|
|
|
|
} = signal.proto.storage;
|
2017-06-29 02:39:55 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('SignalProtocolStore', () => {
|
|
|
|
const number = '+5558675309';
|
2021-04-16 23:13:13 +00:00
|
|
|
let store: SignalProtocolStore;
|
|
|
|
let identityKey: KeyPairType;
|
|
|
|
let testKey: KeyPairType;
|
|
|
|
|
|
|
|
function getSessionRecord(isOpen?: boolean): SessionRecord {
|
|
|
|
const proto = new RecordStructure();
|
|
|
|
|
|
|
|
proto.previousSessions = [];
|
|
|
|
|
|
|
|
if (isOpen) {
|
|
|
|
proto.currentSession = new SessionStructure();
|
|
|
|
|
|
|
|
proto.currentSession.aliceBaseKey = toUint8Array(getPublicKey());
|
|
|
|
proto.currentSession.localIdentityPublic = toUint8Array(getPublicKey());
|
|
|
|
proto.currentSession.localRegistrationId = 435;
|
|
|
|
|
|
|
|
proto.currentSession.previousCounter = 1;
|
|
|
|
proto.currentSession.remoteIdentityPublic = toUint8Array(getPublicKey());
|
|
|
|
proto.currentSession.remoteRegistrationId = 243;
|
|
|
|
|
|
|
|
proto.currentSession.rootKey = toUint8Array(getPrivateKey());
|
|
|
|
proto.currentSession.sessionVersion = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
return SessionRecord.deserialize(
|
|
|
|
Buffer.from(sessionStructureToArrayBuffer(proto))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
function getSenderKeyRecord(): SenderKeyRecord {
|
|
|
|
const proto = new SenderKeyRecordStructure();
|
|
|
|
|
|
|
|
const state = new SenderKeyStateStructure();
|
|
|
|
|
|
|
|
state.senderKeyId = 4;
|
|
|
|
|
|
|
|
const senderChainKey = new SenderKeyStateStructure.SenderChainKey();
|
|
|
|
|
|
|
|
senderChainKey.iteration = 10;
|
|
|
|
senderChainKey.seed = toUint8Array(getPublicKey());
|
|
|
|
state.senderChainKey = senderChainKey;
|
|
|
|
|
|
|
|
const senderSigningKey = new SenderKeyStateStructure.SenderSigningKey();
|
|
|
|
senderSigningKey.public = toUint8Array(getPublicKey());
|
|
|
|
senderSigningKey.private = toUint8Array(getPrivateKey());
|
|
|
|
|
|
|
|
state.senderSigningKey = senderSigningKey;
|
|
|
|
|
|
|
|
state.senderMessageKeys = [];
|
|
|
|
const messageKey = new SenderKeyStateStructure.SenderMessageKey();
|
|
|
|
messageKey.iteration = 234;
|
|
|
|
messageKey.seed = toUint8Array(getPublicKey());
|
|
|
|
state.senderMessageKeys.push(messageKey);
|
|
|
|
|
|
|
|
proto.senderKeyStates = [];
|
|
|
|
proto.senderKeyStates.push(state);
|
|
|
|
|
|
|
|
return SenderKeyRecord.deserialize(
|
|
|
|
Buffer.from(
|
|
|
|
signal.proto.storage.SenderKeyRecordStructure.encode(proto).finish()
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
function toUint8Array(buffer: ArrayBuffer): Uint8Array {
|
|
|
|
return new Uint8Array(buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
function getPrivateKey() {
|
|
|
|
const key = getRandomBytes(32);
|
|
|
|
clampPrivateKey(key);
|
|
|
|
return key;
|
|
|
|
}
|
|
|
|
function getPublicKey() {
|
|
|
|
const key = getRandomBytes(33);
|
|
|
|
setPublicKeyTypeByte(key);
|
|
|
|
return key;
|
|
|
|
}
|
2017-07-25 18:33:02 +00:00
|
|
|
|
2020-03-05 21:14:58 +00:00
|
|
|
before(async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
store = window.textsecure.storage.protocol;
|
2019-02-04 23:54:37 +00:00
|
|
|
store.hydrateCaches();
|
2017-06-29 02:39:55 +00:00
|
|
|
identityKey = {
|
2021-04-16 23:13:13 +00:00
|
|
|
pubKey: getPublicKey(),
|
|
|
|
privKey: getPrivateKey(),
|
2017-06-29 02:39:55 +00:00
|
|
|
};
|
|
|
|
testKey = {
|
2021-04-16 23:13:13 +00:00
|
|
|
pubKey: getPublicKey(),
|
|
|
|
privKey: getPrivateKey(),
|
2017-06-29 02:39:55 +00:00
|
|
|
};
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
setPublicKeyTypeByte(identityKey.pubKey);
|
|
|
|
setPublicKeyTypeByte(testKey.pubKey);
|
|
|
|
|
|
|
|
clampPrivateKey(identityKey.privKey);
|
|
|
|
clampPrivateKey(testKey.privKey);
|
|
|
|
|
|
|
|
window.storage.put('registrationId', 1337);
|
|
|
|
window.storage.put('identityKey', identityKey);
|
|
|
|
await window.storage.fetch();
|
|
|
|
|
|
|
|
window.ConversationController.reset();
|
|
|
|
await window.ConversationController.load();
|
|
|
|
await window.ConversationController.getOrCreateAndWait(number, 'private');
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2017-06-29 02:39:55 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('getLocalRegistrationId', () => {
|
|
|
|
it('retrieves my registration id', async () => {
|
2019-09-26 19:56:31 +00:00
|
|
|
await store.hydrateCaches();
|
2018-10-18 01:01:21 +00:00
|
|
|
const id = await store.getLocalRegistrationId();
|
|
|
|
assert.strictEqual(id, 1337);
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('getIdentityKeyPair', () => {
|
|
|
|
it('retrieves my identity key', async () => {
|
2019-09-26 19:56:31 +00:00
|
|
|
await store.hydrateCaches();
|
2018-10-18 01:01:21 +00:00
|
|
|
const key = await store.getIdentityKeyPair();
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!key) {
|
|
|
|
throw new Error('Missing key!');
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.isTrue(constantTimeEqual(key.pubKey, identityKey.pubKey));
|
|
|
|
assert.isTrue(constantTimeEqual(key.privKey, identityKey.privKey));
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-13 20:57:46 +00:00
|
|
|
|
2021-05-14 01:18:43 +00:00
|
|
|
describe('senderKeys', () => {
|
|
|
|
it('roundtrips in memory', async () => {
|
|
|
|
const distributionId = window.getGuid();
|
|
|
|
const expected = getSenderKeyRecord();
|
|
|
|
|
|
|
|
const deviceId = 1;
|
|
|
|
const encodedAddress = `${number}.${deviceId}`;
|
|
|
|
|
|
|
|
await store.saveSenderKey(encodedAddress, distributionId, expected);
|
|
|
|
|
|
|
|
const actual = await store.getSenderKey(encodedAddress, distributionId);
|
|
|
|
if (!actual) {
|
|
|
|
throw new Error('getSenderKey returned nothing!');
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.isTrue(
|
|
|
|
constantTimeEqual(expected.serialize(), actual.serialize())
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
it('roundtrips through database', async () => {
|
|
|
|
const distributionId = window.getGuid();
|
|
|
|
const expected = getSenderKeyRecord();
|
|
|
|
|
|
|
|
const deviceId = 1;
|
|
|
|
const encodedAddress = `${number}.${deviceId}`;
|
|
|
|
|
|
|
|
await store.saveSenderKey(encodedAddress, distributionId, expected);
|
|
|
|
|
|
|
|
// Re-fetch from the database to ensure we get the latest database value
|
|
|
|
await store.hydrateCaches();
|
|
|
|
|
|
|
|
const actual = await store.getSenderKey(encodedAddress, distributionId);
|
|
|
|
if (!actual) {
|
|
|
|
throw new Error('getSenderKey returned nothing!');
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.isTrue(
|
|
|
|
constantTimeEqual(expected.serialize(), actual.serialize())
|
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('saveIdentity', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const identifier = `${number}.1`;
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('stores identity keys', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, testKey.pubKey);
|
|
|
|
const key = await store.loadIdentityKey(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!key) {
|
|
|
|
throw new Error('Missing key!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(constantTimeEqual(key, testKey.pubKey));
|
2015-07-22 19:48:08 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('allows key changes', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = getPublicKey();
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, testKey.pubKey);
|
|
|
|
await store.saveIdentity(identifier, newIdentity);
|
2016-05-04 07:09:44 +00:00
|
|
|
});
|
2017-05-31 01:04:03 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When there is no existing key (first use)', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.removeIdentityKey(number);
|
|
|
|
await store.saveIdentity(identifier, testKey.pubKey);
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('marks the key firstUse', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert(identity.firstUse);
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('sets the timestamp', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert(identity.timestamp);
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('sets the verified status to DEFAULT', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.verified, store.VerifiedStatus.DEFAULT);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When there is a different existing key (non first use)', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = getPublicKey();
|
2018-10-18 01:01:21 +00:00
|
|
|
const oldTimestamp = Date.now();
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: identifier,
|
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: oldTimestamp,
|
|
|
|
nonblockingApproval: false,
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
});
|
|
|
|
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, newIdentity);
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('marks the key not firstUse', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert(!identity.firstUse);
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('updates the timestamp', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.notEqual(identity.timestamp, oldTimestamp);
|
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('The previous verified status was DEFAULT', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: oldTimestamp,
|
|
|
|
nonblockingApproval: false,
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-13 20:57:46 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, newIdentity);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('sets the new key to default', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.verified, store.VerifiedStatus.DEFAULT);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('The previous verified status was VERIFIED', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: oldTimestamp,
|
|
|
|
nonblockingApproval: false,
|
|
|
|
verified: store.VerifiedStatus.VERIFIED,
|
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
|
|
|
|
await store.hydrateCaches();
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, newIdentity);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('sets the new key to unverified', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
assert.strictEqual(
|
2018-10-18 01:01:21 +00:00
|
|
|
identity.verified,
|
2018-04-27 21:25:04 +00:00
|
|
|
store.VerifiedStatus.UNVERIFIED
|
|
|
|
);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('The previous verified status was UNVERIFIED', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: oldTimestamp,
|
|
|
|
nonblockingApproval: false,
|
|
|
|
verified: store.VerifiedStatus.UNVERIFIED,
|
|
|
|
});
|
|
|
|
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, newIdentity);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('sets the new key to unverified', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
assert.strictEqual(
|
2018-10-18 01:01:21 +00:00
|
|
|
identity.verified,
|
2018-04-27 21:25:04 +00:00
|
|
|
store.VerifiedStatus.UNVERIFIED
|
|
|
|
);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When the key has not changed', () => {
|
|
|
|
const oldTimestamp = Date.now();
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
timestamp: oldTimestamp,
|
|
|
|
nonblockingApproval: false,
|
2021-04-16 23:13:13 +00:00
|
|
|
firstUse: false,
|
2018-10-18 01:01:21 +00:00
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('If it is marked firstUse', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
identity.firstUse = true;
|
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey(identity);
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('nothing changes', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, testKey.pubKey, true);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert(!identity.nonblockingApproval);
|
|
|
|
assert.strictEqual(identity.timestamp, oldTimestamp);
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('If it is not marked firstUse', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
identity.firstUse = false;
|
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey(identity);
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('If nonblocking approval is required', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
let now: number;
|
2018-11-02 18:02:53 +00:00
|
|
|
before(async () => {
|
2017-07-03 22:16:52 +00:00
|
|
|
now = Date.now();
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(
|
|
|
|
number
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
identity.timestamp = now;
|
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey(identity);
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-17 22:41:42 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('sets non-blocking approval', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, testKey.pubKey, true);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(
|
|
|
|
number
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(identity.nonblockingApproval, true);
|
|
|
|
assert.strictEqual(identity.timestamp, now);
|
|
|
|
assert.strictEqual(identity.firstUse, false);
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('saveIdentityWithAttributes', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
let now: number;
|
|
|
|
let validAttributes: IdentityKeyType;
|
2017-06-29 02:39:55 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
before(async () => {
|
2017-06-29 02:39:55 +00:00
|
|
|
now = Date.now();
|
|
|
|
validAttributes = {
|
2021-04-16 23:13:13 +00:00
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: now,
|
|
|
|
verified: store.VerifiedStatus.VERIFIED,
|
|
|
|
nonblockingApproval: false,
|
2017-06-29 02:39:55 +00:00
|
|
|
};
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.removeIdentityKey(number);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('with valid attributes', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentityWithAttributes(number, validAttributes);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('publicKey is saved', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
|
|
|
assert.isTrue(constantTimeEqual(identity.publicKey, testKey.pubKey));
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('firstUse is saved', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.firstUse, true);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('timestamp is saved', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.timestamp, now);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('verified is saved', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.verified, store.VerifiedStatus.VERIFIED);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('nonblockingApproval is saved', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.nonblockingApproval, false);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('with invalid attributes', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
let attributes: IdentityKeyType;
|
2018-11-02 18:02:53 +00:00
|
|
|
beforeEach(() => {
|
2021-04-16 23:13:13 +00:00
|
|
|
attributes = window._.clone(validAttributes);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
async function testInvalidAttributes() {
|
|
|
|
try {
|
|
|
|
await store.saveIdentityWithAttributes(number, attributes);
|
|
|
|
throw new Error('saveIdentityWithAttributes should have failed');
|
|
|
|
} catch (error) {
|
|
|
|
// good. we expect to fail with invalid attributes.
|
|
|
|
}
|
2017-06-13 20:57:46 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('rejects an invalid publicKey', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
attributes.publicKey = 'a string' as any;
|
2018-10-18 01:01:21 +00:00
|
|
|
await testInvalidAttributes();
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('rejects invalid firstUse', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
attributes.firstUse = 0 as any;
|
2018-10-18 01:01:21 +00:00
|
|
|
await testInvalidAttributes();
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('rejects invalid timestamp', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
attributes.timestamp = NaN as any;
|
2018-10-18 01:01:21 +00:00
|
|
|
await testInvalidAttributes();
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('rejects invalid verified', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
attributes.verified = null as any;
|
2018-10-18 01:01:21 +00:00
|
|
|
await testInvalidAttributes();
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('rejects invalid nonblockingApproval', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
attributes.nonblockingApproval = 0 as any;
|
2018-10-18 01:01:21 +00:00
|
|
|
await testInvalidAttributes();
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('setApproval', () => {
|
|
|
|
it('sets nonblockingApproval', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.setApproval(number, true);
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(identity.nonblockingApproval, true);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('setVerified', () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
async function saveRecordDefault() {
|
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
nonblockingApproval: false,
|
2017-06-17 03:21:16 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-13 20:57:46 +00:00
|
|
|
}
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('with no public key argument', () => {
|
2017-06-17 03:21:16 +00:00
|
|
|
before(saveRecordDefault);
|
2018-11-02 18:02:53 +00:00
|
|
|
it('updates the verified status', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.setVerified(number, store.VerifiedStatus.VERIFIED);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.verified, store.VerifiedStatus.VERIFIED);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(constantTimeEqual(identity.publicKey, testKey.pubKey));
|
2017-06-17 03:21:16 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('with the current public key', () => {
|
2017-06-17 03:21:16 +00:00
|
|
|
before(saveRecordDefault);
|
2018-11-02 18:02:53 +00:00
|
|
|
it('updates the verified status', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.setVerified(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.VERIFIED,
|
|
|
|
testKey.pubKey
|
|
|
|
);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.verified, store.VerifiedStatus.VERIFIED);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(constantTimeEqual(identity.publicKey, testKey.pubKey));
|
2017-06-17 03:21:16 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('with a mismatching public key', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = getPublicKey();
|
2017-06-17 03:21:16 +00:00
|
|
|
before(saveRecordDefault);
|
2018-11-02 18:02:53 +00:00
|
|
|
it('does not change the record.', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.setVerified(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.VERIFIED,
|
|
|
|
newIdentity
|
|
|
|
);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(identity.verified, store.VerifiedStatus.DEFAULT);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(constantTimeEqual(identity.publicKey, testKey.pubKey));
|
2017-06-17 03:21:16 +00:00
|
|
|
});
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('processContactSyncVerificationState', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = getPublicKey();
|
|
|
|
let keychangeTriggered: number;
|
2017-06-29 02:39:55 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
beforeEach(() => {
|
2017-06-29 02:39:55 +00:00
|
|
|
keychangeTriggered = 0;
|
2018-11-02 18:02:53 +00:00
|
|
|
store.bind('keychange', () => {
|
|
|
|
keychangeTriggered += 1;
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
afterEach(() => {
|
2017-06-29 02:39:55 +00:00
|
|
|
store.unbind('keychange');
|
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the new verified status is DEFAULT', () => {
|
|
|
|
describe('when there is no existing record', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.removeIdentityKeyById(number);
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-19 23:34:00 +00:00
|
|
|
});
|
2017-06-17 03:56:06 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('does nothing', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.DEFAULT,
|
|
|
|
newIdentity
|
|
|
|
);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
|
|
|
|
|
|
|
if (identity) {
|
|
|
|
// fetchRecord resolved so there is a record.
|
|
|
|
// Bad.
|
|
|
|
throw new Error(
|
|
|
|
'processContactSyncVerificationState should not save new records'
|
2018-04-27 21:25:04 +00:00
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the record exists', () => {
|
|
|
|
describe('when the existing key is different', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.VERIFIED,
|
|
|
|
nonblockingApproval: false,
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2017-06-19 23:34:00 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('does not save the new identity (because this is a less secure state)', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.DEFAULT,
|
|
|
|
newIdentity
|
|
|
|
);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(
|
|
|
|
number
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
identity.verified,
|
|
|
|
store.VerifiedStatus.VERIFIED
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(
|
|
|
|
constantTimeEqual(identity.publicKey, testKey.pubKey)
|
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-19 23:34:00 +00:00
|
|
|
});
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the existing key is the same but VERIFIED', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.VERIFIED,
|
|
|
|
nonblockingApproval: false,
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
2017-06-29 02:39:55 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('updates the verified status', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.DEFAULT,
|
|
|
|
testKey.pubKey
|
|
|
|
);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(
|
|
|
|
number
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(identity.verified, store.VerifiedStatus.DEFAULT);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(
|
|
|
|
constantTimeEqual(identity.publicKey, testKey.pubKey)
|
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the existing key is the same and already DEFAULT', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
nonblockingApproval: false,
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('does not hang', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.DEFAULT,
|
|
|
|
testKey.pubKey
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the new verified status is UNVERIFIED', () => {
|
|
|
|
describe('when there is no existing record', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.removeIdentityKeyById(number);
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-19 23:34:00 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('saves the new identity and marks it verified', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.UNVERIFIED,
|
|
|
|
newIdentity
|
|
|
|
);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
identity.verified,
|
|
|
|
store.VerifiedStatus.UNVERIFIED
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(constantTimeEqual(identity.publicKey, newIdentity));
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the record exists', () => {
|
|
|
|
describe('when the existing key is different', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.VERIFIED,
|
|
|
|
nonblockingApproval: false,
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('saves the new identity and marks it UNVERIFIED', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.UNVERIFIED,
|
|
|
|
newIdentity
|
|
|
|
);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(
|
|
|
|
number
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
identity.verified,
|
|
|
|
store.VerifiedStatus.UNVERIFIED
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(constantTimeEqual(identity.publicKey, newIdentity));
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(keychangeTriggered, 1);
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the key exists and is DEFAULT', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
nonblockingApproval: false,
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('updates the verified status', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.UNVERIFIED,
|
|
|
|
testKey.pubKey
|
|
|
|
);
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(
|
|
|
|
number
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
identity.verified,
|
|
|
|
store.VerifiedStatus.UNVERIFIED
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(
|
|
|
|
constantTimeEqual(identity.publicKey, testKey.pubKey)
|
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the key exists and is already UNVERIFIED', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.UNVERIFIED,
|
|
|
|
nonblockingApproval: false,
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('does not hang', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.UNVERIFIED,
|
|
|
|
testKey.pubKey
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the new verified status is VERIFIED', () => {
|
|
|
|
describe('when there is no existing record', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.removeIdentityKeyById(number);
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('saves the new identity and marks it verified', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.VERIFIED,
|
|
|
|
newIdentity
|
|
|
|
);
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(number);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(identity.verified, store.VerifiedStatus.VERIFIED);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(constantTimeEqual(identity.publicKey, newIdentity));
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the record exists', () => {
|
|
|
|
describe('when the existing key is different', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.VERIFIED,
|
|
|
|
nonblockingApproval: false,
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-19 23:34:00 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('saves the new identity and marks it VERIFIED', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.VERIFIED,
|
|
|
|
newIdentity
|
|
|
|
);
|
|
|
|
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(
|
|
|
|
number
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
identity.verified,
|
|
|
|
store.VerifiedStatus.VERIFIED
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(constantTimeEqual(identity.publicKey, newIdentity));
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(keychangeTriggered, 1);
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the existing key is the same but UNVERIFIED', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.UNVERIFIED,
|
|
|
|
nonblockingApproval: false,
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-19 23:34:00 +00:00
|
|
|
});
|
2017-06-17 03:56:06 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('saves the identity and marks it verified', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.VERIFIED,
|
|
|
|
testKey.pubKey
|
|
|
|
);
|
|
|
|
const identity = await window.Signal.Data.getIdentityKeyById(
|
|
|
|
number
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!identity) {
|
|
|
|
throw new Error('Missing identity!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
assert.strictEqual(
|
|
|
|
identity.verified,
|
|
|
|
store.VerifiedStatus.VERIFIED
|
|
|
|
);
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.isTrue(
|
|
|
|
constantTimeEqual(identity.publicKey, testKey.pubKey)
|
|
|
|
);
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('when the existing key is the same and already VERIFIED', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
firstUse: true,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.VERIFIED,
|
|
|
|
nonblockingApproval: false,
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('does not hang', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.processContactSyncVerificationState(
|
|
|
|
number,
|
|
|
|
store.VerifiedStatus.VERIFIED,
|
|
|
|
testKey.pubKey
|
|
|
|
);
|
|
|
|
|
|
|
|
assert.strictEqual(keychangeTriggered, 0);
|
2017-06-29 02:39:55 +00:00
|
|
|
});
|
|
|
|
});
|
2017-06-17 03:56:06 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-07-25 18:33:02 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('isUntrusted', () => {
|
|
|
|
it('returns false if identity key old enough', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
timestamp: Date.now() - 10 * 1000 * 60,
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
firstUse: false,
|
|
|
|
nonblockingApproval: false,
|
2017-07-25 18:33:02 +00:00
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2018-10-18 01:01:21 +00:00
|
|
|
const untrusted = await store.isUntrusted(number);
|
|
|
|
assert.strictEqual(untrusted, false);
|
2017-07-25 18:33:02 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns false if new but nonblockingApproval is true', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
firstUse: false,
|
|
|
|
nonblockingApproval: true,
|
2017-07-25 18:33:02 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
const untrusted = await store.isUntrusted(number);
|
|
|
|
assert.strictEqual(untrusted, false);
|
2017-07-25 18:33:02 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns false if new but firstUse is true', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
firstUse: true,
|
|
|
|
nonblockingApproval: false,
|
2017-07-25 18:33:02 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
const untrusted = await store.isUntrusted(number);
|
|
|
|
assert.strictEqual(untrusted, false);
|
2017-07-25 18:33:02 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns true if new, and no flags are set', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await window.Signal.Data.createOrUpdateIdentityKey({
|
|
|
|
id: number,
|
2018-04-27 21:25:04 +00:00
|
|
|
publicKey: testKey.pubKey,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
verified: store.VerifiedStatus.DEFAULT,
|
|
|
|
firstUse: false,
|
|
|
|
nonblockingApproval: false,
|
2017-07-25 18:33:02 +00:00
|
|
|
});
|
2019-02-04 23:54:37 +00:00
|
|
|
await store.hydrateCaches();
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const untrusted = await store.isUntrusted(number);
|
|
|
|
assert.strictEqual(untrusted, true);
|
2017-07-25 18:33:02 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('getVerified', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.setVerified(number, store.VerifiedStatus.VERIFIED);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('resolves to the verified status', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const result = await store.getVerified(number);
|
|
|
|
assert.strictEqual(result, store.VerifiedStatus.VERIFIED);
|
2017-06-13 20:57:46 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('isTrustedIdentity', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const identifier = `${number}.1`;
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When invalid direction is given', () => {
|
|
|
|
it('should fail', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
try {
|
2021-04-16 23:13:13 +00:00
|
|
|
await store.isTrustedIdentity(number, testKey.pubKey, 'dir' as any);
|
2018-10-18 01:01:21 +00:00
|
|
|
throw new Error('isTrustedIdentity should have failed');
|
|
|
|
} catch (error) {
|
|
|
|
// good
|
|
|
|
}
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
2016-05-04 07:09:44 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When direction is RECEIVING', () => {
|
|
|
|
it('always returns true', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = getPublicKey();
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, testKey.pubKey);
|
|
|
|
|
|
|
|
const trusted = await store.isTrustedIdentity(
|
|
|
|
identifier,
|
|
|
|
newIdentity,
|
2021-04-16 23:13:13 +00:00
|
|
|
Direction.Receiving
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (!trusted) {
|
|
|
|
throw new Error('isTrusted returned false when receiving');
|
|
|
|
}
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When direction is SENDING', () => {
|
|
|
|
describe('When there is no existing key (first use)', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.removeIdentityKey(number);
|
2017-05-30 22:21:56 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns true', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = getPublicKey();
|
2018-10-18 01:01:21 +00:00
|
|
|
const trusted = await store.isTrustedIdentity(
|
|
|
|
identifier,
|
|
|
|
newIdentity,
|
2021-04-16 23:13:13 +00:00
|
|
|
Direction.Sending
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
if (!trusted) {
|
|
|
|
throw new Error('isTrusted returned false on first use');
|
|
|
|
}
|
2017-05-30 22:21:56 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When there is an existing key', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, testKey.pubKey);
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When the existing key is different', () => {
|
|
|
|
it('returns false', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = getPublicKey();
|
2018-10-18 01:01:21 +00:00
|
|
|
const trusted = await store.isTrustedIdentity(
|
|
|
|
identifier,
|
|
|
|
newIdentity,
|
2021-04-16 23:13:13 +00:00
|
|
|
Direction.Sending
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
if (trusted) {
|
|
|
|
throw new Error('isTrusted returned true on untrusted key');
|
|
|
|
}
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
2016-05-04 07:30:42 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('When the existing key matches the new key', () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const newIdentity = getPublicKey();
|
2018-11-02 18:02:53 +00:00
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, newIdentity);
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns false if keys match but we just received this new identiy', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const trusted = await store.isTrustedIdentity(
|
|
|
|
identifier,
|
|
|
|
newIdentity,
|
2021-04-16 23:13:13 +00:00
|
|
|
Direction.Sending
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
if (trusted) {
|
|
|
|
throw new Error('isTrusted returned true on untrusted key');
|
|
|
|
}
|
2018-04-27 21:25:04 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns true if we have already approved identity', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.saveIdentity(identifier, newIdentity, true);
|
|
|
|
|
|
|
|
const trusted = await store.isTrustedIdentity(
|
|
|
|
identifier,
|
|
|
|
newIdentity,
|
2021-04-16 23:13:13 +00:00
|
|
|
Direction.Sending
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
if (!trusted) {
|
|
|
|
throw new Error('isTrusted returned false on an approved key');
|
|
|
|
}
|
2017-05-31 01:04:03 +00:00
|
|
|
});
|
2017-05-30 22:21:56 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('storePreKey', () => {
|
|
|
|
it('stores prekeys', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.storePreKey(1, testKey);
|
|
|
|
const key = await store.loadPreKey(1);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!key) {
|
|
|
|
throw new Error('Missing key!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyPair = {
|
|
|
|
pubKey: window.Signal.Crypto.typedArrayToArrayBuffer(
|
|
|
|
key.publicKey().serialize()
|
|
|
|
),
|
|
|
|
privKey: window.Signal.Crypto.typedArrayToArrayBuffer(
|
|
|
|
key.privateKey().serialize()
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.isTrue(constantTimeEqual(keyPair.pubKey, testKey.pubKey));
|
|
|
|
assert.isTrue(constantTimeEqual(keyPair.privKey, testKey.privKey));
|
2016-05-04 07:30:42 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('removePreKey', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.storePreKey(2, testKey);
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('deletes prekeys', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
await store.removePreKey(2);
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
const key = await store.loadPreKey(2);
|
|
|
|
assert.isUndefined(key);
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('storeSignedPreKey', () => {
|
|
|
|
it('stores signed prekeys', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.storeSignedPreKey(3, testKey);
|
|
|
|
const key = await store.loadSignedPreKey(3);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!key) {
|
|
|
|
throw new Error('Missing key!');
|
|
|
|
}
|
|
|
|
|
|
|
|
const keyPair = {
|
|
|
|
pubKey: window.Signal.Crypto.typedArrayToArrayBuffer(
|
|
|
|
key.publicKey().serialize()
|
|
|
|
),
|
|
|
|
privKey: window.Signal.Crypto.typedArrayToArrayBuffer(
|
|
|
|
key.privateKey().serialize()
|
|
|
|
),
|
|
|
|
};
|
|
|
|
|
|
|
|
assert.isTrue(constantTimeEqual(keyPair.pubKey, testKey.pubKey));
|
|
|
|
assert.isTrue(constantTimeEqual(keyPair.privKey, testKey.privKey));
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('removeSignedPreKey', () => {
|
|
|
|
before(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.storeSignedPreKey(4, testKey);
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
it('deletes signed prekeys', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
await store.removeSignedPreKey(4);
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
const key = await store.loadSignedPreKey(4);
|
|
|
|
assert.isUndefined(key);
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('storeSession', () => {
|
|
|
|
it('stores sessions', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const testRecord = getSessionRecord();
|
2018-11-02 18:02:53 +00:00
|
|
|
await store.storeSession(`${number}.1`, testRecord);
|
|
|
|
const record = await store.loadSession(`${number}.1`);
|
2021-04-16 23:13:13 +00:00
|
|
|
if (!record) {
|
|
|
|
throw new Error('Missing record!');
|
|
|
|
}
|
2018-10-18 01:01:21 +00:00
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
assert.equal(record, testRecord);
|
2016-05-04 07:30:42 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('removeAllSessions', () => {
|
|
|
|
it('removes all sessions for a number', async () => {
|
|
|
|
const devices = [1, 2, 3].map(deviceId => {
|
2018-10-18 01:01:21 +00:00
|
|
|
return [number, deviceId].join('.');
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
2018-11-02 18:02:53 +00:00
|
|
|
devices.map(async encodedNumber => {
|
2021-04-16 23:13:13 +00:00
|
|
|
await store.storeSession(encodedNumber, getSessionRecord());
|
2018-04-27 21:25:04 +00:00
|
|
|
})
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
await store.removeAllSessions(number);
|
|
|
|
|
|
|
|
const records = await Promise.all(
|
|
|
|
devices.map(store.loadSession.bind(store))
|
|
|
|
);
|
2018-11-02 18:02:53 +00:00
|
|
|
|
|
|
|
for (let i = 0, max = records.length; i < max; i += 1) {
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.isUndefined(records[i]);
|
|
|
|
}
|
2015-04-22 02:13:13 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('clearSessionStore', () => {
|
|
|
|
it('clears the session store', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const testRecord = getSessionRecord();
|
2018-11-02 18:02:53 +00:00
|
|
|
await store.storeSession(`${number}.1`, testRecord);
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.clearSessionStore();
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
const record = await store.loadSession(`${number}.1`);
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.isUndefined(record);
|
2016-05-04 07:30:42 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('getDeviceIds', () => {
|
|
|
|
it('returns deviceIds for a number', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const openRecord = getSessionRecord(true);
|
2020-12-09 22:05:11 +00:00
|
|
|
const openDevices = [1, 2, 3, 10].map(deviceId => {
|
2018-10-18 01:01:21 +00:00
|
|
|
return [number, deviceId].join('.');
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
await Promise.all(
|
2020-12-09 22:05:11 +00:00
|
|
|
openDevices.map(async encodedNumber => {
|
|
|
|
await store.storeSession(encodedNumber, openRecord);
|
2018-04-27 21:25:04 +00:00
|
|
|
})
|
2018-10-18 01:01:21 +00:00
|
|
|
);
|
|
|
|
|
2021-04-16 23:13:13 +00:00
|
|
|
const closedRecord = getSessionRecord(false);
|
2020-12-09 22:05:11 +00:00
|
|
|
await store.storeSession([number, 11].join('.'), closedRecord);
|
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const deviceIds = await store.getDeviceIds(number);
|
2018-11-20 01:38:55 +00:00
|
|
|
assert.sameMembers(deviceIds, [1, 2, 3, 10]);
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2021-04-16 23:13:13 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('returns empty array for a number with no device ids', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
const deviceIds = await store.getDeviceIds('foo');
|
|
|
|
assert.sameMembers(deviceIds, []);
|
2015-04-22 02:13:13 +00:00
|
|
|
});
|
2017-06-14 17:40:21 +00:00
|
|
|
});
|
2017-07-17 22:46:00 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
describe('Not yet processed messages', () => {
|
|
|
|
beforeEach(async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.removeAllUnprocessed();
|
|
|
|
const items = await store.getAllUnprocessed();
|
|
|
|
assert.strictEqual(items.length, 0);
|
2017-07-17 22:46:00 +00:00
|
|
|
});
|
|
|
|
|
2019-02-05 01:23:50 +00:00
|
|
|
it('adds three and gets them back', async () => {
|
2018-10-18 01:01:21 +00:00
|
|
|
await Promise.all([
|
2021-04-16 23:13:13 +00:00
|
|
|
store.addUnprocessed({
|
|
|
|
id: '2-two',
|
|
|
|
envelope: 'second',
|
|
|
|
timestamp: 2,
|
|
|
|
version: 2,
|
|
|
|
attempts: 0,
|
|
|
|
}),
|
|
|
|
store.addUnprocessed({
|
|
|
|
id: '3-three',
|
|
|
|
envelope: 'third',
|
|
|
|
timestamp: 3,
|
|
|
|
version: 2,
|
|
|
|
attempts: 0,
|
|
|
|
}),
|
|
|
|
store.addUnprocessed({
|
|
|
|
id: '1-one',
|
|
|
|
envelope: 'first',
|
|
|
|
timestamp: 1,
|
|
|
|
version: 2,
|
|
|
|
attempts: 0,
|
|
|
|
}),
|
2018-10-18 01:01:21 +00:00
|
|
|
]);
|
2017-07-17 22:46:00 +00:00
|
|
|
|
2018-10-18 01:01:21 +00:00
|
|
|
const items = await store.getAllUnprocessed();
|
|
|
|
assert.strictEqual(items.length, 3);
|
|
|
|
|
|
|
|
// they are in the proper order because the collection comparator is 'timestamp'
|
2019-02-05 01:23:50 +00:00
|
|
|
assert.strictEqual(items[0].envelope, 'first');
|
|
|
|
assert.strictEqual(items[1].envelope, 'second');
|
|
|
|
assert.strictEqual(items[2].envelope, 'third');
|
2017-07-17 22:46:00 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('saveUnprocessed successfully updates item', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const id = '1-one';
|
|
|
|
await store.addUnprocessed({
|
|
|
|
id,
|
|
|
|
envelope: 'first',
|
|
|
|
timestamp: 1,
|
|
|
|
version: 2,
|
|
|
|
attempts: 0,
|
|
|
|
});
|
2019-02-05 01:23:50 +00:00
|
|
|
await store.updateUnprocessedWithData(id, { decrypted: 'updated' });
|
2018-10-18 01:01:21 +00:00
|
|
|
|
|
|
|
const items = await store.getAllUnprocessed();
|
|
|
|
assert.strictEqual(items.length, 1);
|
2019-02-05 01:23:50 +00:00
|
|
|
assert.strictEqual(items[0].decrypted, 'updated');
|
2018-10-18 01:01:21 +00:00
|
|
|
assert.strictEqual(items[0].timestamp, 1);
|
2017-07-17 22:46:00 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('removeUnprocessed successfully deletes item', async () => {
|
2021-04-16 23:13:13 +00:00
|
|
|
const id = '1-one';
|
|
|
|
await store.addUnprocessed({
|
|
|
|
id,
|
|
|
|
envelope: 'first',
|
|
|
|
timestamp: 1,
|
|
|
|
version: 2,
|
|
|
|
attempts: 0,
|
|
|
|
});
|
2018-10-18 01:01:21 +00:00
|
|
|
await store.removeUnprocessed(id);
|
|
|
|
|
|
|
|
const items = await store.getAllUnprocessed();
|
|
|
|
assert.strictEqual(items.length, 0);
|
2017-07-17 22:46:00 +00:00
|
|
|
});
|
|
|
|
});
|
2015-04-01 20:08:09 +00:00
|
|
|
});
|