signal-desktop/ts/test-electron/MessageReceiver_test.ts

81 lines
2.4 KiB
TypeScript
Raw Normal View History

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
/* 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';
import type { DecryptionErrorEvent } from '../textsecure/messageReceiverEvents';
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', () => {
const someAci = generateAci();
2021-07-09 19:36:10 +00:00
const deviceId = 1;
let oldAci: AciString | undefined;
let oldDeviceId: number | undefined;
beforeEach(async () => {
oldAci = window.storage.user.getAci();
oldDeviceId = window.storage.user.getDeviceId();
2023-08-16 20:54:39 +00:00
await window.storage.user.setAciAndDeviceId(generateAci(), 2);
await window.storage.protocol.hydrateCaches();
});
afterEach(async () => {
if (oldAci !== undefined && oldDeviceId !== undefined) {
2023-08-16 20:54:39 +00:00
await window.storage.user.setAciAndDeviceId(oldAci, oldDeviceId);
}
await window.storage.protocol.removeAllUnprocessed();
});
2021-07-09 19:36:10 +00:00
describe('connecting', () => {
it('generates decryption-error event when it cannot decrypt', async () => {
const messageReceiver = new MessageReceiver({
storage: window.storage,
serverTrustRoot: 'AAAAAAAA',
});
2021-07-09 19:36:10 +00:00
const body = Proto.Envelope.encode({
type: Proto.Envelope.Type.CIPHERTEXT,
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();
messageReceiver.handleRequest(
2024-08-06 21:21:15 +00:00
new IncomingWebSocketRequestLegacy(
{
2022-03-23 20:49:27 +00:00
id: Long.fromNumber(1),
verb: 'PUT',
path: '/api/v1/message',
body,
headers: [],
},
(_: Buffer): void => {}
)
);
2021-07-09 19:36:10 +00:00
await new Promise<void>(resolve => {
messageReceiver.addEventListener(
'decryption-error',
(error: DecryptionErrorEvent) => {
assert.strictEqual(error.decryptionError.senderAci, someAci);
assert.strictEqual(error.decryptionError.senderDevice, deviceId);
resolve();
}
);
});
await messageReceiver.drain();
2021-07-09 19:36:10 +00:00
});
});
});