From 4c9d69094dff8d1a8a761a9bcd6d04a64476b73d Mon Sep 17 00:00:00 2001 From: lilia Date: Mon, 27 Jul 2015 18:03:28 -0700 Subject: [PATCH] Work on message receiver tests Not quite working yet // FREEBIE --- libtextsecure/test/message_receiver_test.js | 108 ++++++++++++++------ 1 file changed, 74 insertions(+), 34 deletions(-) diff --git a/libtextsecure/test/message_receiver_test.js b/libtextsecure/test/message_receiver_test.js index fa38969bf13c..1bdd6711e7a8 100644 --- a/libtextsecure/test/message_receiver_test.js +++ b/libtextsecure/test/message_receiver_test.js @@ -15,51 +15,91 @@ */ describe('MessageReceiver', function() { + textsecure.storage.impl = new AxolotlStore(); var WebSocket = window.WebSocket; - before(function() { window.WebSocket = MockSocket; }); + var number = '+19999999999'; + var deviceId = 1; + before(function() { + window.WebSocket = MockSocket; + textsecure.storage.user.setNumberAndDeviceId(number, deviceId, 'name'); + textsecure.storage.put("password", "password"); + var signalingKey = textsecure.crypto.getRandomBytes(32 + 20); + textsecure.storage.put("signaling_key", signalingKey); + }); after (function() { window.WebSocket = WebSocket; }); - it('connects', function(done) { - var mockServer = new MockServer('ws://localhost:8080'); + + describe('connecting', function() { + var blob = null; var attrs = { - type: textsecure.protobuf.Envelope.Type.PLAINTEXT, - source: '+19999999999', - sourceDevice: '1', + type: textsecure.protobuf.Envelope.Type.CIPHERTEXT, + source: number, + sourceDevice: deviceId, timestamp: Date.now(), }; - mockServer.on('connection', function(server) { - var signal = new textsecure.protobuf.Envelope(attrs); - signal.message = new textsecure.protobuf.DataMessage({ body: 'hello' }); - server.send( - new textsecure.protobuf.WebSocketMessage({ - type: textsecure.protobuf.WebSocketMessage.Type.REQUEST, - request: { verb: 'PUT', path: '/messages', body: signal } - }).encode().toArrayBuffer() - ); + var websocketmessage = new textsecure.protobuf.WebSocketMessage({ + type: textsecure.protobuf.WebSocketMessage.Type.REQUEST, + request: { verb: 'PUT', path: '/messages' } }); - window.addEventListener('signal', function(ev) { - var signal = ev.proto; - for (var key in attrs) { - assert.strictEqual(attrs[key], signal[key]); - } - assert.strictEqual(signal.message.body, 'hello'); - }); - var messageReceiver = new textsecure.MessageReceiver(window); - }); + before(function(done) { + var signal = new textsecure.protobuf.Envelope(attrs).toArrayBuffer(); + var data = new textsecure.protobuf.DataMessage({ body: 'hello' }); - it('sends a keepalive once a minute', function(done) { - this.timeout(60000); - var mockServer = new MockServer('ws://localhost:8081'); - mockServer.on('connection', function(server) { - server.on('message', function(data) { - var message = textsecure.protobuf.WebSocketMessage.decode(data); - assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.REQUEST); - assert.strictEqual(message.request.verb, 'GET'); - assert.strictEqual(message.request.path, '/v1/keepalive'); + var signaling_key = toArrayBuffer(textsecure.storage.get("signaling_key")); //TODO: in crypto_storage + var aes_key = signaling_key.slice(0, 32); + var mac_key = signaling_key.slice(32, 32 + 20); + + window.crypto.subtle.importKey('raw', aes_key, {name: 'AES-CBC'}, false, ['encrypt']).then(function(key) { + var iv = textsecure.crypto.getRandomBytes(16); + window.crypto.subtle.encrypt({name: 'AES-CBC', iv: new Uint8Array(iv)}, key, signal).then(function(ciphertext) { + window.crypto.subtle.importKey('raw', mac_key, {name: 'HMAC', hash: {name: 'SHA-256'}}, false, ['sign']).then(function(key) { + window.crypto.subtle.sign( {name: 'HMAC', hash: 'SHA-256'}, key, signal).then(function(mac) { + var version = new Uint8Array([1]); + var message = dcodeIO.ByteBuffer.concat([version, iv, ciphertext, mac ]); + websocketmessage.request.body = message.toArrayBuffer(); + console.log(new Uint8Array(message.toArrayBuffer())); + done(); + }); + }); + }); + }); + }); + + it('connects', function(done) { + var mockServer = new MockServer('ws://localhost:8080/v1/websocket/?login='+ encodeURIComponent(number) +'.1&password=password'); + + mockServer.on('connection', function(server) { + server.send(new Blob([ websocketmessage.toArrayBuffer() ])); + }); + + window.addEventListener('textsecure:message', function(ev) { + var signal = ev.proto; + for (var key in attrs) { + assert.strictEqual(attrs[key], signal[key]); + } + assert.strictEqual(signal.message.body, 'hello'); server.close(); done(); }); + var messageReceiver = new textsecure.MessageReceiver('ws://localhost:8080', window); + }); + }); + + describe('keepalives', function() { + it('sends a keepalive once a minute', function(done) { + this.timeout(60000); + var mockServer = new MockServer('ws://localhost:8081/v1/websocket/?login='+ encodeURIComponent(number) +'.1&password=password'); + mockServer.on('connection', function(server) { + server.on('message', function(data) { + var message = textsecure.protobuf.WebSocketMessage.decode(data); + assert.strictEqual(message.type, textsecure.protobuf.WebSocketMessage.Type.REQUEST); + assert.strictEqual(message.request.verb, 'GET'); + assert.strictEqual(message.request.path, '/v1/keepalive'); + server.close(); + done(); + }); + }); + var messageReceiver = new textsecure.MessageReceiver('ws://localhost:8081', window); }); - var messageReceiver = new textsecure.MessageReceiver(window); }); });