Work on message receiver tests
Not quite working yet // FREEBIE
This commit is contained in:
parent
d9d4e281cc
commit
4c9d69094d
1 changed files with 74 additions and 34 deletions
|
@ -15,41 +15,80 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
describe('MessageReceiver', function() {
|
describe('MessageReceiver', function() {
|
||||||
|
textsecure.storage.impl = new AxolotlStore();
|
||||||
var WebSocket = window.WebSocket;
|
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; });
|
after (function() { window.WebSocket = WebSocket; });
|
||||||
it('connects', function(done) {
|
|
||||||
var mockServer = new MockServer('ws://localhost:8080');
|
describe('connecting', function() {
|
||||||
|
var blob = null;
|
||||||
var attrs = {
|
var attrs = {
|
||||||
type: textsecure.protobuf.Envelope.Type.PLAINTEXT,
|
type: textsecure.protobuf.Envelope.Type.CIPHERTEXT,
|
||||||
source: '+19999999999',
|
source: number,
|
||||||
sourceDevice: '1',
|
sourceDevice: deviceId,
|
||||||
timestamp: Date.now(),
|
timestamp: Date.now(),
|
||||||
};
|
};
|
||||||
mockServer.on('connection', function(server) {
|
var websocketmessage = new textsecure.protobuf.WebSocketMessage({
|
||||||
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,
|
type: textsecure.protobuf.WebSocketMessage.Type.REQUEST,
|
||||||
request: { verb: 'PUT', path: '/messages', body: signal }
|
request: { verb: 'PUT', path: '/messages' }
|
||||||
}).encode().toArrayBuffer()
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
window.addEventListener('signal', function(ev) {
|
before(function(done) {
|
||||||
|
var signal = new textsecure.protobuf.Envelope(attrs).toArrayBuffer();
|
||||||
|
var data = new textsecure.protobuf.DataMessage({ body: 'hello' });
|
||||||
|
|
||||||
|
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;
|
var signal = ev.proto;
|
||||||
for (var key in attrs) {
|
for (var key in attrs) {
|
||||||
assert.strictEqual(attrs[key], signal[key]);
|
assert.strictEqual(attrs[key], signal[key]);
|
||||||
}
|
}
|
||||||
assert.strictEqual(signal.message.body, 'hello');
|
assert.strictEqual(signal.message.body, 'hello');
|
||||||
|
server.close();
|
||||||
|
done();
|
||||||
|
});
|
||||||
|
var messageReceiver = new textsecure.MessageReceiver('ws://localhost:8080', window);
|
||||||
});
|
});
|
||||||
var messageReceiver = new textsecure.MessageReceiver(window);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('keepalives', function() {
|
||||||
it('sends a keepalive once a minute', function(done) {
|
it('sends a keepalive once a minute', function(done) {
|
||||||
this.timeout(60000);
|
this.timeout(60000);
|
||||||
var mockServer = new MockServer('ws://localhost:8081');
|
var mockServer = new MockServer('ws://localhost:8081/v1/websocket/?login='+ encodeURIComponent(number) +'.1&password=password');
|
||||||
mockServer.on('connection', function(server) {
|
mockServer.on('connection', function(server) {
|
||||||
server.on('message', function(data) {
|
server.on('message', function(data) {
|
||||||
var message = textsecure.protobuf.WebSocketMessage.decode(data);
|
var message = textsecure.protobuf.WebSocketMessage.decode(data);
|
||||||
|
@ -60,6 +99,7 @@ describe('MessageReceiver', function() {
|
||||||
done();
|
done();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
var messageReceiver = new textsecure.MessageReceiver(window);
|
var messageReceiver = new textsecure.MessageReceiver('ws://localhost:8081', window);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in a new issue