2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2015 Signal Messenger, LLC
|
2021-07-09 19:36:10 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
2021-11-04 21:04:51 +00:00
|
|
|
|
|
|
|
/* eslint-disable @typescript-eslint/no-empty-function */
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
import { assert } from 'chai';
|
2022-03-23 20:49:27 +00:00
|
|
|
import Long from 'long';
|
2021-07-09 19:36:10 +00:00
|
|
|
|
|
|
|
import MessageReceiver from '../textsecure/MessageReceiver';
|
2024-08-06 21:21:15 +00:00
|
|
|
import { IncomingWebSocketRequestLegacy } from '../textsecure/WebsocketResources';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { DecryptionErrorEvent } from '../textsecure/messageReceiverEvents';
|
2023-08-10 16:43:33 +00:00
|
|
|
import { generateAci } from '../types/ServiceId';
|
|
|
|
import type { AciString } from '../types/ServiceId';
|
2021-07-09 19:36:10 +00:00
|
|
|
import { SignalService as Proto } from '../protobuf';
|
|
|
|
import * as Crypto from '../Crypto';
|
|
|
|
|
|
|
|
describe('MessageReceiver', () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
const someAci = generateAci();
|
2021-07-09 19:36:10 +00:00
|
|
|
const deviceId = 1;
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
let oldAci: AciString | undefined;
|
2021-09-10 02:38:11 +00:00
|
|
|
let oldDeviceId: number | undefined;
|
|
|
|
|
2021-08-25 23:10:09 +00:00
|
|
|
beforeEach(async () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
oldAci = window.storage.user.getAci();
|
2021-09-10 02:38:11 +00:00
|
|
|
oldDeviceId = window.storage.user.getDeviceId();
|
2023-08-16 20:54:39 +00:00
|
|
|
await window.storage.user.setAciAndDeviceId(generateAci(), 2);
|
2021-08-25 23:10:09 +00:00
|
|
|
await window.storage.protocol.hydrateCaches();
|
|
|
|
});
|
|
|
|
|
|
|
|
afterEach(async () => {
|
2023-08-10 16:43:33 +00:00
|
|
|
if (oldAci !== undefined && oldDeviceId !== undefined) {
|
2023-08-16 20:54:39 +00:00
|
|
|
await window.storage.user.setAciAndDeviceId(oldAci, oldDeviceId);
|
2021-09-10 02:38:11 +00:00
|
|
|
}
|
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({
|
|
|
|
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,
|
2023-08-10 16:43:33 +00:00
|
|
|
sourceServiceId: someAci,
|
2021-07-09 19:36:10 +00:00
|
|
|
sourceDevice: deviceId,
|
2022-03-23 20:49:27 +00:00
|
|
|
timestamp: Long.fromNumber(Date.now()),
|
2021-09-24 00:49:05 +00:00
|
|
|
content: Crypto.getRandomBytes(200),
|
2021-07-09 19:36:10 +00:00
|
|
|
}).finish();
|
|
|
|
|
2021-07-28 21:37:09 +00:00
|
|
|
messageReceiver.handleRequest(
|
2024-08-06 21:21:15 +00:00
|
|
|
new IncomingWebSocketRequestLegacy(
|
2021-07-28 21:37:09 +00:00
|
|
|
{
|
2022-03-23 20:49:27 +00:00
|
|
|
id: Long.fromNumber(1),
|
2021-07-28 21:37:09 +00:00
|
|
|
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) => {
|
2023-08-10 16:43:33 +00:00
|
|
|
assert.strictEqual(error.decryptionError.senderAci, someAci);
|
2021-08-25 23:10:09 +00:00
|
|
|
assert.strictEqual(error.decryptionError.senderDevice, deviceId);
|
|
|
|
resolve();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
await messageReceiver.drain();
|
2021-07-09 19:36:10 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|