signal-desktop/libtextsecure/test/message_receiver_test.js

113 lines
3.5 KiB
JavaScript
Raw Normal View History

2020-10-30 20:34:04 +00:00
// Copyright 2015-2020 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
2018-11-02 18:02:53 +00:00
/* global libsignal, textsecure, SignalProtocolStore */
2018-07-21 21:51:20 +00:00
describe('MessageReceiver', () => {
2018-05-02 16:51:22 +00:00
textsecure.storage.impl = new SignalProtocolStore();
2018-11-02 18:02:53 +00:00
const { WebSocket } = window;
2018-07-21 21:51:20 +00:00
const number = '+19999999999';
const uuid = 'AAAAAAAA-BBBB-4CCC-9DDD-EEEEEEEEEEEE';
2018-07-21 21:51:20 +00:00
const deviceId = 1;
const signalingKey = libsignal.crypto.getRandomBytes(32 + 20);
2018-11-02 18:02:53 +00:00
2018-07-21 21:51:20 +00:00
before(() => {
2018-05-02 16:51:22 +00:00
window.WebSocket = MockSocket;
textsecure.storage.user.setNumberAndDeviceId(number, deviceId, 'name');
textsecure.storage.user.setUuidAndDeviceId(uuid, deviceId);
2018-05-02 16:51:22 +00:00
textsecure.storage.put('password', 'password');
textsecure.storage.put('signaling_key', signalingKey);
});
2018-07-21 21:51:20 +00:00
after(() => {
2018-05-02 16:51:22 +00:00
window.WebSocket = WebSocket;
});
2018-07-21 21:51:20 +00:00
describe('connecting', () => {
const attrs = {
2018-05-02 16:51:22 +00:00
type: textsecure.protobuf.Envelope.Type.CIPHERTEXT,
source: number,
sourceUuid: uuid,
2018-05-02 16:51:22 +00:00
sourceDevice: deviceId,
timestamp: Date.now(),
};
2018-07-21 21:51:20 +00:00
const websocketmessage = new textsecure.protobuf.WebSocketMessage({
2018-05-02 16:51:22 +00:00
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
request: { verb: 'PUT', path: '/messages' },
});
2018-07-21 21:51:20 +00:00
before(done => {
const signal = new textsecure.protobuf.Envelope(attrs).toArrayBuffer();
2018-11-02 18:02:53 +00:00
const aesKey = signalingKey.slice(0, 32);
const macKey = signalingKey.slice(32, 32 + 20);
2018-05-02 16:51:22 +00:00
window.crypto.subtle
2018-11-02 18:02:53 +00:00
.importKey('raw', aesKey, { name: 'AES-CBC' }, false, ['encrypt'])
2018-07-21 21:51:20 +00:00
.then(key => {
const iv = libsignal.crypto.getRandomBytes(16);
2018-05-02 16:51:22 +00:00
window.crypto.subtle
.encrypt({ name: 'AES-CBC', iv: new Uint8Array(iv) }, key, signal)
2018-07-21 21:51:20 +00:00
.then(ciphertext => {
2018-05-02 16:51:22 +00:00
window.crypto.subtle
.importKey(
'raw',
2018-11-02 18:02:53 +00:00
macKey,
2018-05-02 16:51:22 +00:00
{ name: 'HMAC', hash: { name: 'SHA-256' } },
false,
['sign']
)
2018-11-02 18:02:53 +00:00
.then(innerKey => {
2018-05-02 16:51:22 +00:00
window.crypto.subtle
2018-11-02 18:02:53 +00:00
.sign({ name: 'HMAC', hash: 'SHA-256' }, innerKey, signal)
2018-07-21 21:51:20 +00:00
.then(mac => {
const version = new Uint8Array([1]);
const message = dcodeIO.ByteBuffer.concat([
2018-05-02 16:51:22 +00:00
version,
iv,
ciphertext,
mac,
]);
websocketmessage.request.body = message.toArrayBuffer();
done();
});
});
});
});
2018-05-02 16:51:22 +00:00
});
2018-07-21 21:51:20 +00:00
it('connects', done => {
const mockServer = new MockServer(
`ws://localhost:8080/v1/websocket/?login=${encodeURIComponent(
uuid
2018-07-21 21:51:20 +00:00
)}.1&password=password`
2018-05-02 16:51:22 +00:00
);
2018-07-21 21:51:20 +00:00
mockServer.on('connection', server => {
2018-05-02 16:51:22 +00:00
server.send(new Blob([websocketmessage.toArrayBuffer()]));
});
2018-07-21 21:51:20 +00:00
window.addEventListener('textsecure:message', ev => {
const signal = ev.proto;
2018-11-02 18:02:53 +00:00
const keys = Object.keys(attrs);
for (let i = 0, max = keys.length; i < max; i += 1) {
const key = keys[i];
2018-05-02 16:51:22 +00:00
assert.strictEqual(attrs[key], signal[key]);
}
assert.strictEqual(signal.message.body, 'hello');
2018-11-02 18:02:53 +00:00
mockServer.close();
2018-05-02 16:51:22 +00:00
done();
});
2018-11-02 18:02:53 +00:00
window.messageReceiver = new textsecure.MessageReceiver(
'username',
'password',
'signalingKey'
// 'ws://localhost:8080',
// window,
2018-05-02 16:51:22 +00:00
);
});
2018-05-02 16:51:22 +00:00
});
});