2018-12-13 19:12:33 +00:00
|
|
|
/* global libsignal */
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('AccountManager', () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
let accountManager;
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
beforeEach(() => {
|
2017-12-01 21:35:39 +00:00
|
|
|
accountManager = new window.textsecure.AccountManager();
|
|
|
|
});
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
describe('#cleanSignedPreKeys', () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
let originalProtocolStorage;
|
|
|
|
let signedPreKeys;
|
|
|
|
const DAY = 1000 * 60 * 60 * 24;
|
|
|
|
|
2018-12-13 19:12:33 +00:00
|
|
|
beforeEach(async () => {
|
|
|
|
const identityKey = await libsignal.KeyHelper.generateIdentityKeyPair();
|
|
|
|
|
2017-12-01 21:35:39 +00:00
|
|
|
originalProtocolStorage = window.textsecure.storage.protocol;
|
|
|
|
window.textsecure.storage.protocol = {
|
2018-12-13 19:12:33 +00:00
|
|
|
getIdentityKeyPair() {
|
|
|
|
return identityKey;
|
|
|
|
},
|
2018-07-21 21:51:20 +00:00
|
|
|
loadSignedPreKeys() {
|
2017-12-01 21:35:39 +00:00
|
|
|
return Promise.resolve(signedPreKeys);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
});
|
2018-07-21 21:51:20 +00:00
|
|
|
afterEach(() => {
|
2017-12-01 21:35:39 +00:00
|
|
|
window.textsecure.storage.protocol = originalProtocolStorage;
|
|
|
|
});
|
|
|
|
|
2018-12-13 19:12:33 +00:00
|
|
|
describe('encrypted device name', () => {
|
|
|
|
it('roundtrips', async () => {
|
|
|
|
const deviceName = 'v2.5.0 on Ubunto 20.04';
|
|
|
|
const encrypted = await accountManager.encryptDeviceName(deviceName);
|
|
|
|
assert.strictEqual(typeof encrypted, 'string');
|
|
|
|
const decrypted = await accountManager.decryptDeviceName(encrypted);
|
|
|
|
|
|
|
|
assert.strictEqual(decrypted, deviceName);
|
|
|
|
});
|
2019-01-29 17:39:55 +00:00
|
|
|
|
|
|
|
it('handles null deviceName', async () => {
|
|
|
|
const encrypted = await accountManager.encryptDeviceName(null);
|
|
|
|
assert.strictEqual(encrypted, null);
|
|
|
|
});
|
2018-12-13 19:12:33 +00:00
|
|
|
});
|
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
it('keeps three confirmed keys even if over a week old', () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
const now = Date.now();
|
2018-05-02 16:51:22 +00:00
|
|
|
signedPreKeys = [
|
|
|
|
{
|
|
|
|
keyId: 1,
|
|
|
|
created_at: now - DAY * 21,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 2,
|
|
|
|
created_at: now - DAY * 14,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 3,
|
|
|
|
created_at: now - DAY * 18,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
];
|
2017-12-01 21:35:39 +00:00
|
|
|
|
|
|
|
// should be no calls to store.removeSignedPreKey, would cause crash
|
|
|
|
return accountManager.cleanSignedPreKeys();
|
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('eliminates confirmed keys over a week old, if more than three', async () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
const now = Date.now();
|
2018-05-02 16:51:22 +00:00
|
|
|
signedPreKeys = [
|
|
|
|
{
|
|
|
|
keyId: 1,
|
|
|
|
created_at: now - DAY * 21,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 2,
|
|
|
|
created_at: now - DAY * 14,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 3,
|
|
|
|
created_at: now - DAY * 4,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 4,
|
|
|
|
created_at: now - DAY * 18,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 5,
|
|
|
|
created_at: now - DAY,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
];
|
2017-12-01 21:35:39 +00:00
|
|
|
|
|
|
|
let count = 0;
|
2018-11-02 18:02:53 +00:00
|
|
|
window.textsecure.storage.protocol.removeSignedPreKey = keyId => {
|
2017-12-01 21:35:39 +00:00
|
|
|
if (keyId !== 1 && keyId !== 4) {
|
2018-07-21 21:51:20 +00:00
|
|
|
throw new Error(`Wrong keys were eliminated! ${keyId}`);
|
2017-12-01 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
count += 1;
|
2017-12-01 21:35:39 +00:00
|
|
|
};
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
await accountManager.cleanSignedPreKeys();
|
|
|
|
assert.strictEqual(count, 2);
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('keeps at least three unconfirmed keys if no confirmed', async () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
const now = Date.now();
|
2018-05-02 16:51:22 +00:00
|
|
|
signedPreKeys = [
|
|
|
|
{
|
|
|
|
keyId: 1,
|
|
|
|
created_at: now - DAY * 14,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 2,
|
|
|
|
created_at: now - DAY * 21,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 3,
|
|
|
|
created_at: now - DAY * 18,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 4,
|
|
|
|
created_at: now - DAY,
|
|
|
|
},
|
|
|
|
];
|
2017-12-01 21:35:39 +00:00
|
|
|
|
|
|
|
let count = 0;
|
2018-11-02 18:02:53 +00:00
|
|
|
window.textsecure.storage.protocol.removeSignedPreKey = keyId => {
|
2017-12-01 21:35:39 +00:00
|
|
|
if (keyId !== 2) {
|
2018-07-21 21:51:20 +00:00
|
|
|
throw new Error(`Wrong keys were eliminated! ${keyId}`);
|
2017-12-01 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
count += 1;
|
2017-12-01 21:35:39 +00:00
|
|
|
};
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
await accountManager.cleanSignedPreKeys();
|
|
|
|
assert.strictEqual(count, 1);
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
it('if some confirmed keys, keeps unconfirmed to addd up to three total', async () => {
|
2017-12-01 21:35:39 +00:00
|
|
|
const now = Date.now();
|
2018-05-02 16:51:22 +00:00
|
|
|
signedPreKeys = [
|
|
|
|
{
|
|
|
|
keyId: 1,
|
|
|
|
created_at: now - DAY * 21,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 2,
|
|
|
|
created_at: now - DAY * 14,
|
|
|
|
confirmed: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 3,
|
|
|
|
created_at: now - DAY * 12,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
keyId: 4,
|
|
|
|
created_at: now - DAY * 8,
|
|
|
|
},
|
|
|
|
];
|
2017-12-01 21:35:39 +00:00
|
|
|
|
|
|
|
let count = 0;
|
2018-11-02 18:02:53 +00:00
|
|
|
window.textsecure.storage.protocol.removeSignedPreKey = keyId => {
|
2017-12-01 21:35:39 +00:00
|
|
|
if (keyId !== 3) {
|
2018-07-21 21:51:20 +00:00
|
|
|
throw new Error(`Wrong keys were eliminated! ${keyId}`);
|
2017-12-01 21:35:39 +00:00
|
|
|
}
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
count += 1;
|
2017-12-01 21:35:39 +00:00
|
|
|
};
|
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
await accountManager.cleanSignedPreKeys();
|
|
|
|
assert.strictEqual(count, 1);
|
2017-12-01 21:35:39 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|