From cb6566a198f1ddcff9d5d22a6e63832a13cc8da9 Mon Sep 17 00:00:00 2001 From: Richard van der Hoff Date: Tue, 21 Jun 2016 10:11:02 +0100 Subject: [PATCH] matrix-client-crypto-spec: reorder Separate the helper functions from the tests, and order them by functionality rather than by test order. I've been struggling to find the tests among the helpers, and struggling to find the helpers among the other helpers. Hopefully this will help. --- spec/integ/matrix-client-crypto.spec.js | 477 ++++++++++++------------ 1 file changed, 238 insertions(+), 239 deletions(-) diff --git a/spec/integ/matrix-client-crypto.spec.js b/spec/integ/matrix-client-crypto.spec.js index b921ffa53..2d664d56e 100644 --- a/spec/integ/matrix-client-crypto.spec.js +++ b/spec/integ/matrix-client-crypto.spec.js @@ -18,34 +18,247 @@ MockStorageApi.prototype = { } }; +var aliHttpBackend; +var bobHttpBackend; +var aliClient; +var roomId = "!room:localhost"; +var aliUserId = "@ali:localhost"; +var aliDeviceId = "zxcvb"; +var aliAccessToken = "aseukfgwef"; +var bobClient; +var bobUserId = "@bob:localhost"; +var bobDeviceId = "bvcxz"; +var bobAccessToken = "fewgfkuesa"; +var bobOneTimeKeys; +var aliDeviceKeys; +var bobDeviceKeys; +var bobDeviceCurve25519Key; +var bobDeviceEd25519Key; +var aliLocalStore; +var aliStorage; +var bobStorage; +var aliMessages; +var bobMessages; + + +function aliUploadsKeys() { + var uploadPath = "/keys/upload/" + aliDeviceId; + aliHttpBackend.when("POST", uploadPath).respond(200, function(path, content) { + expect(content.one_time_keys).toEqual({}); + aliDeviceKeys = content.device_keys; + return {one_time_key_counts: {curve25519: 0}}; + }); + return q.all([ + aliClient.uploadKeys(0), + aliHttpBackend.flush(uploadPath, 1), + ]).then(function() { + console.log("ali uploaded keys"); + }); +} + +function bobUploadsKeys() { + var uploadPath = "/keys/upload/bvcxz"; + bobHttpBackend.when("POST", uploadPath).respond(200, function(path, content) { + expect(content.one_time_keys).toEqual({}); + bobHttpBackend.when("POST", uploadPath).respond(200, function(path, content) { + expect(content.one_time_keys).not.toEqual({}); + bobDeviceKeys = content.device_keys; + bobOneTimeKeys = content.one_time_keys; + var count = 0; + for (var key in content.one_time_keys) { + if (content.one_time_keys.hasOwnProperty(key)) { + count++; + } + } + expect(count).toEqual(5); + return {one_time_key_counts: {curve25519: count}}; + }); + return {one_time_key_counts: {}}; + }); + bobClient.uploadKeys(5).catch(utils.failTest); + return bobHttpBackend.flush().then(function() { + expect(bobDeviceKeys).toBeDefined(); + expect(bobOneTimeKeys).toBeDefined(); + bobDeviceCurve25519Key = bobDeviceKeys.keys["curve25519:bvcxz"]; + bobDeviceEd25519Key = bobDeviceKeys.keys["ed25519:bvcxz"]; + }); +} + +function aliDownloadsKeys() { + var bobKeys = {}; + bobKeys[bobDeviceId] = bobDeviceKeys; + aliHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) { + expect(content.device_keys[bobUserId]).toEqual({}); + var result = {}; + result[bobUserId] = bobKeys; + return {device_keys: result}; + }); + var p1 = aliClient.downloadKeys([bobUserId]).then(function() { + expect(aliClient.listDeviceKeys(bobUserId)).toEqual([{ + id: "bvcxz", + key: bobDeviceEd25519Key, + verified: false, + }]); + }); + var p2 = aliHttpBackend.flush(); + + return q.all([p1, p2]).then(function() { + var devices = aliStorage.getEndToEndDevicesForUser(bobUserId); + expect(devices[bobDeviceId].keys).toEqual(bobDeviceKeys.keys); + expect(devices[bobDeviceId].verified).toBe(false); + }); +} + +function bobDownloadsKeys() { + var aliKeys = {}; + aliKeys[aliDeviceId] = aliDeviceKeys; + bobHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) { + expect(content.device_keys[aliUserId]).toEqual({}); + var result = {}; + result[aliUserId] = aliKeys; + return {device_keys: result}; + }); + return q.all([ + bobClient.downloadKeys([aliUserId]), + bobHttpBackend.flush(), + ]); +} + +function aliEnablesEncryption() { + aliHttpBackend.when("POST", "/keys/claim").respond(200, function(path, content) { + expect(content.one_time_keys[bobUserId][bobDeviceId]).toEqual("curve25519"); + for (var keyId in bobOneTimeKeys) { + if (bobOneTimeKeys.hasOwnProperty(keyId)) { + if (keyId.indexOf("curve25519:") === 0) { + break; + } + } + } + var result = {}; + result[bobUserId] = {}; + result[bobUserId][bobDeviceId] = {}; + result[bobUserId][bobDeviceId][keyId] = bobOneTimeKeys[keyId]; + return {one_time_keys: result}; + }); + var p = aliClient.setRoomEncryption(roomId, { + algorithm: "m.olm.v1.curve25519-aes-sha2", + members: [aliUserId, bobUserId] + }).then(function(res) { + expect(res.missingUsers).toEqual([]); + expect(res.missingDevices).toEqual({}); + expect(aliClient.isRoomEncrypted(roomId)).toBeTruthy(); + }); + aliHttpBackend.flush(); + return p; +} + +function bobEnablesEncryption() { + return bobClient.setRoomEncryption(roomId, { + algorithm: "m.olm.v1.curve25519-aes-sha2", + members: [aliUserId, bobUserId] + }).then(function(res) { + console.log("bob enabled encryption"); + expect(res.missingUsers).toEqual([]); + expect(res.missingDevices).toEqual({}); + expect(bobClient.isRoomEncrypted(roomId)).toBeTruthy(); + }); +} + +function aliSendsMessage() { + return sendMessage(aliHttpBackend, aliClient).then(function(content) { + aliMessages.push(content); + var ciphertext = content.ciphertext[bobDeviceCurve25519Key]; + expect(ciphertext).toBeDefined(); + }); +} + +function bobSendsMessage() { + return sendMessage(bobHttpBackend, bobClient).then(function(content) { + bobMessages.push(content); + var aliKeyId = "curve25519:" + aliDeviceId; + var aliDeviceCurve25519Key = aliDeviceKeys.keys[aliKeyId]; + var ciphertext = content.ciphertext[aliDeviceCurve25519Key]; + expect(ciphertext).toBeDefined(); + return ciphertext; + }); +} + +function sendMessage(httpBackend, client) { + var path = "/send/m.room.encrypted/"; + var sent; + httpBackend.when("PUT", path).respond(200, function(path, content) { + sent = content; + return { + event_id: "asdfgh", + }; + }); + var p1 = client.sendMessage( + roomId, {msgtype: "m.text", body: "Hello, World"} + ); + var p2 = httpBackend.flush(path, 1); + return q.all([p1, p2]).then(function() { + return sent; + }); +} + +function aliRecvMessage() { + var message = bobMessages.shift(); + return recvMessage(aliHttpBackend, aliClient, message); +} + +function bobRecvMessage() { + var message = aliMessages.shift(); + return recvMessage(bobHttpBackend, bobClient, message); +} + +function recvMessage(httpBackend, client, message) { + var syncData = { + next_batch: "x", + rooms: { + join: { + + } + } + }; + syncData.rooms.join[roomId] = { + timeline: { + events: [ + utils.mkEvent({ + type: "m.room.encrypted", + room: roomId, + content: message + }) + ] + } + }; + httpBackend.when("GET", "/sync").respond(200, syncData); + var deferred = q.defer(); + client.on("event", function(event) { + expect(event.getType()).toEqual("m.room.message"); + expect(event.getContent()).toEqual({ + msgtype: "m.text", + body: "Hello, World" + }); + expect(event.isEncrypted()).toBeTruthy(); + deferred.resolve(); + }); + startClient(httpBackend, client); + httpBackend.flush(); + return deferred.promise; +} + +function startClient(httpBackend, client) { + client.startClient(); + httpBackend.when("GET", "/pushrules").respond(200, {}); + httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" }); +} + + describe("MatrixClient crypto", function() { if (!sdk.CRYPTO_ENABLED) { return; } - var baseUrl = "http://localhost.or.something"; - var aliHttpBackend; - var bobHttpBackend; - var aliClient; - var roomId = "!room:localhost"; - var aliUserId = "@ali:localhost"; - var aliDeviceId = "zxcvb"; - var aliAccessToken = "aseukfgwef"; - var bobClient; - var bobUserId = "@bob:localhost"; - var bobDeviceId = "bvcxz"; - var bobAccessToken = "fewgfkuesa"; - var bobOneTimeKeys; - var aliDeviceKeys; - var bobDeviceKeys; - var bobDeviceCurve25519Key; - var bobDeviceEd25519Key; - var aliLocalStore; - var aliStorage; - var bobStorage; - var aliMessages; - var bobMessages; - beforeEach(function() { aliLocalStore = new MockStorageApi(); aliStorage = new sdk.WebStorageSessionStore(aliLocalStore); @@ -54,7 +267,7 @@ describe("MatrixClient crypto", function() { aliHttpBackend = new HttpBackend(); aliClient = sdk.createClient({ - baseUrl: baseUrl, + baseUrl: "http://alis.server", userId: aliUserId, accessToken: aliAccessToken, deviceId: aliDeviceId, @@ -64,7 +277,7 @@ describe("MatrixClient crypto", function() { bobHttpBackend = new HttpBackend(); bobClient = sdk.createClient({ - baseUrl: baseUrl, + baseUrl: "http://bobs.server", userId: bobUserId, accessToken: bobAccessToken, deviceId: bobDeviceId, @@ -90,65 +303,12 @@ describe("MatrixClient crypto", function() { }); }); - function bobUploadsKeys() { - var uploadPath = "/keys/upload/bvcxz"; - bobHttpBackend.when("POST", uploadPath).respond(200, function(path, content) { - expect(content.one_time_keys).toEqual({}); - bobHttpBackend.when("POST", uploadPath).respond(200, function(path, content) { - expect(content.one_time_keys).not.toEqual({}); - bobDeviceKeys = content.device_keys; - bobOneTimeKeys = content.one_time_keys; - var count = 0; - for (var key in content.one_time_keys) { - if (content.one_time_keys.hasOwnProperty(key)) { - count++; - } - } - expect(count).toEqual(5); - return {one_time_key_counts: {curve25519: count}}; - }); - return {one_time_key_counts: {}}; - }); - bobClient.uploadKeys(5).catch(utils.failTest); - return bobHttpBackend.flush().then(function() { - expect(bobDeviceKeys).toBeDefined(); - expect(bobOneTimeKeys).toBeDefined(); - bobDeviceCurve25519Key = bobDeviceKeys.keys["curve25519:bvcxz"]; - bobDeviceEd25519Key = bobDeviceKeys.keys["ed25519:bvcxz"]; - }); - } - it("Bob uploads without one-time keys and with one-time keys", function(done) { q() .then(bobUploadsKeys) .catch(utils.failTest).done(done); }); - function aliDownloadsKeys() { - var bobKeys = {}; - bobKeys[bobDeviceId] = bobDeviceKeys; - aliHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) { - expect(content.device_keys[bobUserId]).toEqual({}); - var result = {}; - result[bobUserId] = bobKeys; - return {device_keys: result}; - }); - var p1 = aliClient.downloadKeys([bobUserId]).then(function() { - expect(aliClient.listDeviceKeys(bobUserId)).toEqual([{ - id: "bvcxz", - key: bobDeviceEd25519Key, - verified: false, - }]); - }); - var p2 = aliHttpBackend.flush(); - - return q.all([p1, p2]).then(function() { - var devices = aliStorage.getEndToEndDevicesForUser(bobUserId); - expect(devices[bobDeviceId].keys).toEqual(bobDeviceKeys.keys); - expect(devices[bobDeviceId].verified).toBe(false); - }); - } - it("Ali downloads Bobs keys", function(done) { q() .then(bobUploadsKeys) @@ -156,34 +316,6 @@ describe("MatrixClient crypto", function() { .catch(utils.failTest).done(done); }); - function aliEnablesEncryption() { - aliHttpBackend.when("POST", "/keys/claim").respond(200, function(path, content) { - expect(content.one_time_keys[bobUserId][bobDeviceId]).toEqual("curve25519"); - for (var keyId in bobOneTimeKeys) { - if (bobOneTimeKeys.hasOwnProperty(keyId)) { - if (keyId.indexOf("curve25519:") === 0) { - break; - } - } - } - var result = {}; - result[bobUserId] = {}; - result[bobUserId][bobDeviceId] = {}; - result[bobUserId][bobDeviceId][keyId] = bobOneTimeKeys[keyId]; - return {one_time_keys: result}; - }); - var p = aliClient.setRoomEncryption(roomId, { - algorithm: "m.olm.v1.curve25519-aes-sha2", - members: [aliUserId, bobUserId] - }).then(function(res) { - expect(res.missingUsers).toEqual([]); - expect(res.missingDevices).toEqual({}); - expect(aliClient.isRoomEncrypted(roomId)).toBeTruthy(); - }); - aliHttpBackend.flush(); - return p; - } - it("Ali enables encryption", function(done) { q() .then(bobUploadsKeys) @@ -192,32 +324,6 @@ describe("MatrixClient crypto", function() { .catch(utils.failTest).done(done); }); - function sendMessage(httpBackend, client) { - var path = "/send/m.room.encrypted/"; - var sent; - httpBackend.when("PUT", path).respond(200, function(path, content) { - sent = content; - return { - event_id: "asdfgh", - }; - }); - var p1 = client.sendMessage( - roomId, {msgtype: "m.text", body: "Hello, World"} - ); - var p2 = httpBackend.flush(path, 1); - return q.all([p1, p2]).then(function() { - return sent; - }); - } - - function aliSendsMessage() { - return sendMessage(aliHttpBackend, aliClient).then(function(content) { - aliMessages.push(content); - var ciphertext = content.ciphertext[bobDeviceCurve25519Key]; - expect(ciphertext).toBeDefined(); - }); - } - it("Ali sends a message", function(done) { q() .then(bobUploadsKeys) @@ -227,53 +333,6 @@ describe("MatrixClient crypto", function() { .catch(utils.failTest).done(done); }); - function startClient(httpBackend, client) { - client.startClient(); - httpBackend.when("GET", "/pushrules").respond(200, {}); - httpBackend.when("POST", "/filter").respond(200, { filter_id: "fid" }); - } - - function recvMessage(httpBackend, client, message) { - var syncData = { - next_batch: "x", - rooms: { - join: { - - } - } - }; - syncData.rooms.join[roomId] = { - timeline: { - events: [ - utils.mkEvent({ - type: "m.room.encrypted", - room: roomId, - content: message - }) - ] - } - }; - httpBackend.when("GET", "/sync").respond(200, syncData); - var deferred = q.defer(); - client.on("event", function(event) { - expect(event.getType()).toEqual("m.room.message"); - expect(event.getContent()).toEqual({ - msgtype: "m.text", - body: "Hello, World" - }); - expect(event.isEncrypted()).toBeTruthy(); - deferred.resolve(); - }); - startClient(httpBackend, client); - httpBackend.flush(); - return deferred.promise; - } - - function bobRecvMessage() { - var message = aliMessages.shift(); - return recvMessage(bobHttpBackend, bobClient, message); - } - it("Bob receives a message", function(done) { q() .then(bobUploadsKeys) @@ -296,66 +355,6 @@ describe("MatrixClient crypto", function() { .catch(utils.failTest).done(done); }); - - function aliUploadsKeys() { - var uploadPath = "/keys/upload/" + aliDeviceId; - aliHttpBackend.when("POST", uploadPath).respond(200, function(path, content) { - expect(content.one_time_keys).toEqual({}); - aliDeviceKeys = content.device_keys; - return {one_time_key_counts: {curve25519: 0}}; - }); - return q.all([ - aliClient.uploadKeys(0), - aliHttpBackend.flush(uploadPath, 1), - ]).then(function() { - console.log("ali uploaded keys"); - }); - } - - function bobDownloadsKeys() { - var aliKeys = {}; - aliKeys[aliDeviceId] = aliDeviceKeys; - bobHttpBackend.when("POST", "/keys/query").respond(200, function(path, content) { - expect(content.device_keys[aliUserId]).toEqual({}); - var result = {}; - result[aliUserId] = aliKeys; - return {device_keys: result}; - }); - return q.all([ - bobClient.downloadKeys([aliUserId]), - bobHttpBackend.flush(), - ]); - } - - function bobEnablesEncryption() { - return bobClient.setRoomEncryption(roomId, { - algorithm: "m.olm.v1.curve25519-aes-sha2", - members: [aliUserId, bobUserId] - }).then(function(res) { - console.log("bob enabled encryption"); - expect(res.missingUsers).toEqual([]); - expect(res.missingDevices).toEqual({}); - expect(bobClient.isRoomEncrypted(roomId)).toBeTruthy(); - }); - } - - function bobSendsMessage() { - return sendMessage(bobHttpBackend, bobClient).then(function(content) { - bobMessages.push(content); - var aliKeyId = "curve25519:" + aliDeviceId; - var aliDeviceCurve25519Key = aliDeviceKeys.keys[aliKeyId]; - var ciphertext = content.ciphertext[aliDeviceCurve25519Key]; - expect(ciphertext).toBeDefined(); - return ciphertext; - }); - } - - function aliRecvMessage() { - var message = bobMessages.shift(); - return recvMessage(aliHttpBackend, aliClient, message); - } - - it("Bob replies to the message", function(done) { q() .then(bobUploadsKeys)