signal-desktop/ts/test-electron/textsecure/AccountManager_test.ts

219 lines
5.7 KiB
TypeScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2017-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import { assert } from 'chai';
import { v4 as getGuid } from 'uuid';
import { getRandomBytes } from '../../Crypto';
import AccountManager from '../../textsecure/AccountManager';
import type { OuterSignedPrekeyType } from '../../textsecure/Types.d';
import { UUID } from '../../types/UUID';
/* eslint-disable @typescript-eslint/no-explicit-any */
2018-07-21 21:51:20 +00:00
describe('AccountManager', () => {
let accountManager: AccountManager;
2018-07-21 21:51:20 +00:00
beforeEach(() => {
const server: any = {};
accountManager = new AccountManager(server);
});
2018-07-21 21:51:20 +00:00
describe('#cleanSignedPreKeys', () => {
let originalGetIdentityKeyPair: any;
let originalLoadSignedPreKeys: any;
let originalRemoveSignedPreKey: any;
let originalGetUuid: any;
let signedPreKeys: Array<OuterSignedPrekeyType>;
const DAY = 1000 * 60 * 60 * 24;
const pubKey = getRandomBytes(33);
const privKey = getRandomBytes(32);
const identityKey = window.Signal.Curve.generateKeyPair();
beforeEach(async () => {
const ourUuid = new UUID(getGuid());
originalGetUuid = window.textsecure.storage.user.getUuid;
originalGetIdentityKeyPair =
window.textsecure.storage.protocol.getIdentityKeyPair;
originalLoadSignedPreKeys =
window.textsecure.storage.protocol.loadSignedPreKeys;
originalRemoveSignedPreKey =
window.textsecure.storage.protocol.removeSignedPreKey;
window.textsecure.storage.user.getUuid = () => ourUuid;
window.textsecure.storage.protocol.getIdentityKeyPair = async () =>
identityKey;
window.textsecure.storage.protocol.loadSignedPreKeys = async () =>
signedPreKeys;
});
2018-07-21 21:51:20 +00:00
afterEach(() => {
window.textsecure.storage.user.getUuid = originalGetUuid;
2021-11-11 22:43:05 +00:00
window.textsecure.storage.protocol.getIdentityKeyPair =
originalGetIdentityKeyPair;
window.textsecure.storage.protocol.loadSignedPreKeys =
originalLoadSignedPreKeys;
window.textsecure.storage.protocol.removeSignedPreKey =
originalRemoveSignedPreKey;
});
describe('encrypted device name', () => {
it('roundtrips', async () => {
const deviceName = 'v2.5.0 on Ubunto 20.04';
2021-09-24 00:49:05 +00:00
const encrypted = accountManager.encryptDeviceName(
deviceName,
identityKey
);
if (!encrypted) {
throw new Error('failed to encrypt!');
}
assert.strictEqual(typeof encrypted, 'string');
const decrypted = await accountManager.decryptDeviceName(encrypted);
assert.strictEqual(decrypted, deviceName);
});
2021-09-24 00:49:05 +00:00
it('handles falsey deviceName', () => {
const encrypted = accountManager.encryptDeviceName('', identityKey);
assert.strictEqual(encrypted, null);
});
});
it('keeps three confirmed keys even if over a month old', () => {
const now = Date.now();
2018-05-02 16:51:22 +00:00
signedPreKeys = [
{
keyId: 1,
created_at: now - DAY * 32,
2018-05-02 16:51:22 +00:00
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 2,
created_at: now - DAY * 34,
2018-05-02 16:51:22 +00:00
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 3,
created_at: now - DAY * 38,
2018-05-02 16:51:22 +00:00
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
];
// should be no calls to store.removeSignedPreKey, would cause crash
return accountManager.cleanSignedPreKeys();
});
it('eliminates oldest keys, even if recent key is unconfirmed', async () => {
const now = Date.now();
2018-05-02 16:51:22 +00:00
signedPreKeys = [
{
keyId: 1,
created_at: now - DAY * 32,
2018-05-02 16:51:22 +00:00
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 2,
created_at: now - DAY * 31,
confirmed: false,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 3,
created_at: now - DAY * 24,
2018-05-02 16:51:22 +00:00
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
// Oldest, should be dropped
2018-05-02 16:51:22 +00:00
keyId: 4,
created_at: now - DAY * 38,
2018-05-02 16:51:22 +00:00
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 5,
created_at: now - DAY,
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 6,
created_at: now - DAY * 5,
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
];
let count = 0;
window.textsecure.storage.protocol.removeSignedPreKey = async (
_,
keyId
) => {
if (keyId !== 4) {
2018-07-21 21:51:20 +00:00
throw new Error(`Wrong keys were eliminated! ${keyId}`);
}
2018-11-02 18:02:53 +00:00
count += 1;
};
2018-11-02 18:02:53 +00:00
await accountManager.cleanSignedPreKeys();
assert.strictEqual(count, 1);
});
it('Removes no keys if less than five', async () => {
const now = Date.now();
2018-05-02 16:51:22 +00:00
signedPreKeys = [
{
keyId: 1,
created_at: now - DAY * 32,
2018-05-02 16:51:22 +00:00
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 2,
created_at: now - DAY * 44,
2018-05-02 16:51:22 +00:00
confirmed: true,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 3,
created_at: now - DAY * 36,
confirmed: false,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
{
keyId: 4,
created_at: now - DAY * 20,
confirmed: false,
pubKey,
privKey,
2018-05-02 16:51:22 +00:00
},
];
window.textsecure.storage.protocol.removeSignedPreKey = async () => {
throw new Error('None should be removed!');
};
2018-11-02 18:02:53 +00:00
await accountManager.cleanSignedPreKeys();
});
});
});