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

83 lines
2.5 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';
import { IncomingWebSocketRequest } from '../textsecure/WebsocketResources';
import type { WebAPIType } from '../textsecure/WebAPI';
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();
await window.storage.user.setUuidAndDeviceId(generateAci(), 2);
await window.storage.protocol.hydrateCaches();
});
afterEach(async () => {
if (oldAci !== undefined && oldDeviceId !== undefined) {
await window.storage.user.setUuidAndDeviceId(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({
server: {} as WebAPIType,
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(
new IncomingWebSocketRequest(
{
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
});
});
});