2021-07-09 19:36:10 +00:00
|
|
|
// Copyright 2015-2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
/* eslint-disable
|
|
|
|
class-methods-use-this,
|
|
|
|
@typescript-eslint/no-empty-function
|
|
|
|
*/
|
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2021-09-10 02:38:11 +00:00
|
|
|
import { v4 as getGuid } from 'uuid';
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
import MessageReceiver from '../textsecure/MessageReceiver';
|
2021-07-28 21:37:09 +00:00
|
|
|
import { IncomingWebSocketRequest } from '../textsecure/WebsocketResources';
|
2021-07-23 17:23:50 +00:00
|
|
|
import { WebAPIType } from '../textsecure/WebAPI';
|
2021-07-28 21:37:09 +00:00
|
|
|
import { DecryptionErrorEvent } from '../textsecure/messageReceiverEvents';
|
2021-07-09 19:36:10 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
|
|
|
import * as Crypto from '../Crypto';
|
|
|
|
|
|
|
|
// TODO: remove once we move away from ArrayBuffers
|
|
|
|
const FIXMEU8 = Uint8Array;
|
|
|
|
|
|
|
|
describe('MessageReceiver', () => {
|
|
|
|
const number = '+19999999999';
|
|
|
|
const uuid = 'aaaaaaaa-bbbb-4ccc-9ddd-eeeeeeeeeeee';
|
|
|
|
const deviceId = 1;
|
|
|
|
|
2021-09-10 02:38:11 +00:00
|
|
|
let oldUuid: string | undefined;
|
|
|
|
let oldDeviceId: number | undefined;
|
|
|
|
|
2021-08-25 23:10:09 +00:00
|
|
|
beforeEach(async () => {
|
2021-09-10 02:38:11 +00:00
|
|
|
oldUuid = window.storage.user.getUuid()?.toString();
|
|
|
|
oldDeviceId = window.storage.user.getDeviceId();
|
|
|
|
await window.storage.user.setUuidAndDeviceId(getGuid(), 2);
|
2021-08-25 23:10:09 +00:00
|
|
|
await window.storage.protocol.hydrateCaches();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2021-09-10 02:38:11 +00:00
|
|
|
if (oldUuid !== undefined && oldDeviceId !== undefined) {
|
|
|
|
await window.storage.user.setUuidAndDeviceId(oldUuid, oldDeviceId);
|
|
|
|
}
|
2021-08-25 23:10:09 +00:00
|
|
|
await window.storage.protocol.removeAllUnprocessed();
|
|
|
|
});
|
|
|
|
|
2021-07-09 19:36:10 +00:00
|
|
|
describe('connecting', () => {
|
2021-08-25 23:10:09 +00:00
|
|
|
it('generates decryption-error event when it cannot decrypt', async () => {
|
2021-07-28 21:37:09 +00:00
|
|
|
const messageReceiver = new MessageReceiver({
|
|
|
|
server: {} as WebAPIType,
|
|
|
|
storage: window.storage,
|
2021-07-23 17:23:50 +00:00
|
|
|
serverTrustRoot: 'AAAAAAAA',
|
|
|
|
});
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
const body = Proto.Envelope.encode({
|
|
|
|
type: Proto.Envelope.Type.CIPHERTEXT,
|
|
|
|
source: number,
|
|
|
|
sourceUuid: uuid,
|
|
|
|
sourceDevice: deviceId,
|
|
|
|
timestamp: Date.now(),
|
|
|
|
content: new FIXMEU8(Crypto.getRandomBytes(200)),
|
|
|
|
}).finish();
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
messageReceiver.handleRequest(
|
|
|
|
new IncomingWebSocketRequest(
|
|
|
|
{
|
|
|
|
id: 1,
|
|
|
|
verb: 'PUT',
|
|
|
|
path: '/api/v1/message',
|
|
|
|
body,
|
|
|
|
headers: [],
|
|
|
|
},
|
|
|
|
(_: Buffer): void => {}
|
|
|
|
)
|
|
|
|
);
|
2021-07-09 19:36:10 +00:00
|
|
|
|
2021-08-25 23:10:09 +00:00
|
|
|
await new Promise<void>(resolve => {
|
|
|
|
messageReceiver.addEventListener(
|
|
|
|
'decryption-error',
|
|
|
|
(error: DecryptionErrorEvent) => {
|
|
|
|
assert.strictEqual(error.decryptionError.senderUuid, uuid);
|
|
|
|
assert.strictEqual(error.decryptionError.senderDevice, deviceId);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
await messageReceiver.drain();
|
2021-07-09 19:36:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|