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';
|
2020-03-05 21:14:58 +00:00
|
|
|
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');
|
2020-03-05 21:14:58 +00:00
|
|
|
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;
|
|
|
|
});
|
2015-07-28 01:03:28 +00:00
|
|
|
|
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,
|
2020-03-05 21:14:58 +00:00
|
|
|
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' },
|
|
|
|
});
|
2015-06-01 21:08:21 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
before(done => {
|
|
|
|
const signal = new textsecure.protobuf.Envelope(attrs).toArrayBuffer();
|
2015-07-28 01:03:28 +00:00
|
|
|
|
2018-11-02 18:02:53 +00:00
|
|
|
const aesKey = signalingKey.slice(0, 32);
|
|
|
|
const macKey = signalingKey.slice(32, 32 + 20);
|
2015-07-28 01:03:28 +00:00
|
|
|
|
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();
|
2015-07-28 01:03:28 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2015-06-01 21:08:21 +00:00
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2015-07-16 20:16:50 +00:00
|
|
|
|
2018-07-21 21:51:20 +00:00
|
|
|
it('connects', done => {
|
|
|
|
const mockServer = new MockServer(
|
|
|
|
`ws://localhost:8080/v1/websocket/?login=${encodeURIComponent(
|
2020-03-05 21:14:58 +00:00
|
|
|
uuid
|
2018-07-21 21:51:20 +00:00
|
|
|
)}.1&password=password`
|
2018-05-02 16:51:22 +00:00
|
|
|
);
|
2015-07-28 01:03:28 +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()]));
|
|
|
|
});
|
2015-07-28 01:03:28 +00:00
|
|
|
|
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(
|
2018-10-18 01:01:21 +00:00
|
|
|
'username',
|
|
|
|
'password',
|
|
|
|
'signalingKey'
|
|
|
|
// 'ws://localhost:8080',
|
|
|
|
// window,
|
2018-05-02 16:51:22 +00:00
|
|
|
);
|
2015-07-28 01:03:28 +00:00
|
|
|
});
|
2018-05-02 16:51:22 +00:00
|
|
|
});
|
2015-06-01 21:08:21 +00:00
|
|
|
});
|