signal-desktop/libtextsecure/test/message_receiver_test.js

99 lines
3.2 KiB
JavaScript
Raw Normal View History

2018-07-21 21:51:20 +00:00
describe('MessageReceiver', () => {
2018-05-02 16:51:22 +00:00
textsecure.storage.impl = new SignalProtocolStore();
2018-07-21 21:51:20 +00:00
const WebSocket = window.WebSocket;
const number = '+19999999999';
const deviceId = 1;
const signalingKey = libsignal.crypto.getRandomBytes(32 + 20);
before(() => {
2018-05-02 16:51:22 +00:00
window.WebSocket = MockSocket;
textsecure.storage.user.setNumberAndDeviceId(number, deviceId, 'name');
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 blob = null;
const attrs = {
2018-05-02 16:51:22 +00:00
type: textsecure.protobuf.Envelope.Type.CIPHERTEXT,
source: number,
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();
const data = new textsecure.protobuf.DataMessage({ body: 'hello' });
2018-07-21 21:51:20 +00:00
const signaling_key = signalingKey;
const aes_key = signaling_key.slice(0, 32);
const mac_key = signaling_key.slice(32, 32 + 20);
2018-05-02 16:51:22 +00:00
window.crypto.subtle
.importKey('raw', aes_key, { 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',
mac_key,
{ name: 'HMAC', hash: { name: 'SHA-256' } },
false,
['sign']
)
2018-07-21 21:51:20 +00:00
.then(key => {
2018-05-02 16:51:22 +00:00
window.crypto.subtle
.sign({ name: 'HMAC', hash: 'SHA-256' }, key, 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(
number
)}.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;
for (const key in attrs) {
2018-05-02 16:51:22 +00:00
assert.strictEqual(attrs[key], signal[key]);
}
assert.strictEqual(signal.message.body, 'hello');
server.close();
done();
});
2018-07-21 21:51:20 +00:00
const messageReceiver = new textsecure.MessageReceiver(
2018-05-02 16:51:22 +00:00
'ws://localhost:8080',
window
);
});
2018-05-02 16:51:22 +00:00
});
});