From b2afaabb8cf3e58432e5e2accd96e2660bed9f3a Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 23 Jan 2020 15:52:07 +0100 Subject: [PATCH 1/3] add unit tests for verifying your own device over to_device messages --- .../verification/verification_request.spec.js | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/spec/unit/crypto/verification/verification_request.spec.js b/spec/unit/crypto/verification/verification_request.spec.js index 4b36107d2..d1aaa24f7 100644 --- a/spec/unit/crypto/verification/verification_request.spec.js +++ b/spec/unit/crypto/verification/verification_request.spec.js @@ -16,14 +16,17 @@ limitations under the License. import {VerificationRequest, READY_TYPE, START_TYPE, DONE_TYPE} from "../../../../src/crypto/verification/request/VerificationRequest"; import {InRoomChannel} from "../../../../src/crypto/verification/request/InRoomChannel"; +import {ToDeviceChannel} from "../../../../src/crypto/verification/request/ToDeviceChannel"; import {MatrixEvent} from "../../../../src/models/event"; function makeMockClient(userId, deviceId) { let counter = 1; let events = []; + const deviceEvents = {}; return { getUserId() { return userId; }, getDeviceId() { return deviceId; }, + sendEvent(roomId, type, content) { counter = counter + 1; const eventId = `$${userId}-${deviceId}-${counter}`; @@ -37,11 +40,36 @@ function makeMockClient(userId, deviceId) { })); return Promise.resolve({event_id: eventId}); }, + + sendToDevice(type, msgMap) { + for (const userId of Object.keys(msgMap)) { + const deviceMap = msgMap[userId]; + for (const deviceId of Object.keys(deviceMap)) { + const content = deviceMap[deviceId]; + const event = new MatrixEvent({content, type}); + deviceEvents[userId] = deviceEvents[userId] || {}; + deviceEvents[userId][deviceId] = deviceEvents[userId][deviceId] || []; + deviceEvents[userId][deviceId].push(event); + } + } + return Promise.resolve(); + }, + popEvents() { const e = events; events = []; return e; }, + + popDeviceEvents(userId, deviceId) { + const forDevice = deviceEvents[userId]; + const events = forDevice && forDevice[deviceId]; + const result = events || []; + if (events) { + delete forDevice[deviceId]; + } + return result; + }, }; } @@ -174,4 +202,34 @@ describe("verification request unit tests", function() { await bob2Request.channel.handleEvent(readyEvent, bob2Request, true); expect(bob2Request.observeOnly).toBe(true); }); + + it("verify own device with to_device messages", async function() { + const bob1 = makeMockClient("@bob:matrix.tld", "device1"); + const bob2 = makeMockClient("@bob:matrix.tld", "device2"); + const bob1Request = new VerificationRequest( + new ToDeviceChannel(bob1, bob1.getUserId(), ["device1", "device2"], + ToDeviceChannel.makeTransactionId(), "device2"), + new Map([[MOCK_METHOD, MockVerifier]]), bob1); + const to = {userId: "@bob:matrix.tld", deviceId: "device2"}; + const verifier = bob1Request.beginKeyVerification(MOCK_METHOD, to); + expect(verifier).toBeInstanceOf(MockVerifier); + await verifier.start(); + const [startEvent] = bob1.popDeviceEvents(to.userId, to.deviceId); + expect(startEvent.getType()).toBe(START_TYPE); + const bob2Request = new VerificationRequest( + new ToDeviceChannel(bob2, bob2.getUserId(), ["device1"]), + new Map([[MOCK_METHOD, MockVerifier]]), bob2); + + await bob2Request.channel.handleEvent(startEvent, bob2Request, true); + await bob2Request.verifier.start(); + const [doneEvent1] = bob2.popDeviceEvents("@bob:matrix.tld", "device1"); + expect(doneEvent1.getType()).toBe(DONE_TYPE); + await bob1Request.channel.handleEvent(doneEvent1, bob1Request, true); + const [doneEvent2] = bob1.popDeviceEvents("@bob:matrix.tld", "device2"); + expect(doneEvent2.getType()).toBe(DONE_TYPE); + await bob2Request.channel.handleEvent(doneEvent2, bob2Request, true); + + expect(bob1Request.done).toBe(true); + expect(bob2Request.done).toBe(true); + }); }); From 7d56ee5084e3ba30a500757b1fd5c29a987c8f56 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 23 Jan 2020 15:52:23 +0100 Subject: [PATCH 2/3] with the change in the linked react-sdk PR, `event` isn't used anymore --- src/crypto/verification/request/VerificationRequest.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/crypto/verification/request/VerificationRequest.js b/src/crypto/verification/request/VerificationRequest.js index 6dbce5dbb..3957511c3 100644 --- a/src/crypto/verification/request/VerificationRequest.js +++ b/src/crypto/verification/request/VerificationRequest.js @@ -157,11 +157,6 @@ export class VerificationRequest extends EventEmitter { return 0; } - /** the m.key.verification.request event that started this request, provided for compatibility with previous verification code */ - get event() { - return this._getEventByEither(REQUEST_TYPE) || this._getEventByEither(START_TYPE); - } - /** current phase of the request. Some properties might only be defined in a current phase. */ get phase() { return this._phase; From 3faeec4add9781ebadc2dc42e7653ab03a137a18 Mon Sep 17 00:00:00 2001 From: Bruno Windels Date: Thu, 23 Jan 2020 15:59:47 +0100 Subject: [PATCH 3/3] fix lint --- spec/unit/crypto/verification/verification_request.spec.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/spec/unit/crypto/verification/verification_request.spec.js b/spec/unit/crypto/verification/verification_request.spec.js index d1aaa24f7..34b15d444 100644 --- a/spec/unit/crypto/verification/verification_request.spec.js +++ b/spec/unit/crypto/verification/verification_request.spec.js @@ -16,7 +16,8 @@ limitations under the License. import {VerificationRequest, READY_TYPE, START_TYPE, DONE_TYPE} from "../../../../src/crypto/verification/request/VerificationRequest"; import {InRoomChannel} from "../../../../src/crypto/verification/request/InRoomChannel"; -import {ToDeviceChannel} from "../../../../src/crypto/verification/request/ToDeviceChannel"; +import {ToDeviceChannel} from + "../../../../src/crypto/verification/request/ToDeviceChannel"; import {MatrixEvent} from "../../../../src/models/event"; function makeMockClient(userId, deviceId) {