Skip to content

Commit 9f0c515

Browse files
committed
Factor out functions for building responses
1 parent a7446de commit 9f0c515

File tree

1 file changed

+38
-53
lines changed

1 file changed

+38
-53
lines changed

spec/integ/crypto/verification.spec.ts

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
156156
expectSendToDeviceMessage("m.key.verification.request"),
157157
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
158158
]);
159-
const transactionId = request.transactionId;
159+
const transactionId = request.transactionId!;
160160
expect(transactionId).toBeDefined();
161161
expect(request.phase).toEqual(VerificationPhase.Requested);
162162
expect(request.roomId).toBeUndefined();
@@ -175,32 +175,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
175175
}
176176

177177
// The dummy device replies with an m.key.verification.ready...
178-
returnToDeviceMessageFromSync({
179-
type: "m.key.verification.ready",
180-
content: {
181-
from_device: TEST_DEVICE_ID,
182-
methods: ["m.sas.v1"],
183-
transaction_id: transactionId,
184-
},
185-
});
178+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.sas.v1"]));
186179
await waitForVerificationRequestChanged(request);
187180
expect(request.phase).toEqual(VerificationPhase.Ready);
188181
expect(request.otherDeviceId).toEqual(TEST_DEVICE_ID);
189182

190183
// ... and picks a method with m.key.verification.start
191-
returnToDeviceMessageFromSync({
192-
type: "m.key.verification.start",
193-
content: {
194-
from_device: TEST_DEVICE_ID,
195-
method: "m.sas.v1",
196-
transaction_id: transactionId,
197-
hashes: ["sha256"],
198-
key_agreement_protocols: ["curve25519-hkdf-sha256"],
199-
message_authentication_codes: ["hkdf-hmac-sha256.v2"],
200-
// we have to include "decimal" per the spec.
201-
short_authentication_string: ["decimal", "emoji"],
202-
},
203-
});
184+
returnToDeviceMessageFromSync(buildSasStartMessage(transactionId));
185+
204186
// as soon as the Changed event arrives, `verifier` should be defined
205187
const verifier = await new Promise<Verifier>((resolve) => {
206188
function onChange() {
@@ -341,7 +323,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
341323
expectSendToDeviceMessage("m.key.verification.request"),
342324
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
343325
]);
344-
const transactionId = request.transactionId;
326+
const transactionId = request.transactionId!;
345327

346328
const toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
347329
expect(toDeviceMessage.methods).toContain("m.qr_code.show.v1");
@@ -351,14 +333,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
351333
expect(toDeviceMessage.transaction_id).toEqual(transactionId);
352334

353335
// The dummy device replies with an m.key.verification.ready, with an indication we can scan the QR code
354-
returnToDeviceMessageFromSync({
355-
type: "m.key.verification.ready",
356-
content: {
357-
from_device: TEST_DEVICE_ID,
358-
methods: ["m.qr_code.scan.v1"],
359-
transaction_id: transactionId,
360-
},
361-
});
336+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.qr_code.scan.v1"]));
362337
await waitForVerificationRequestChanged(request);
363338
expect(request.phase).toEqual(VerificationPhase.Ready);
364339

@@ -428,33 +403,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
428403
expectSendToDeviceMessage("m.key.verification.request"),
429404
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
430405
]);
431-
const transactionId = request.transactionId;
406+
const transactionId = request.transactionId!;
432407

433408
// The dummy device replies with an m.key.verification.ready...
434-
returnToDeviceMessageFromSync({
435-
type: "m.key.verification.ready",
436-
content: {
437-
from_device: TEST_DEVICE_ID,
438-
methods: ["m.sas.v1"],
439-
transaction_id: transactionId,
440-
},
441-
});
409+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.sas.v1"]));
442410
await waitForVerificationRequestChanged(request);
443411

444412
// ... and picks a method with m.key.verification.start
445-
returnToDeviceMessageFromSync({
446-
type: "m.key.verification.start",
447-
content: {
448-
from_device: TEST_DEVICE_ID,
449-
method: "m.sas.v1",
450-
transaction_id: transactionId,
451-
hashes: ["sha256"],
452-
key_agreement_protocols: ["curve25519-hkdf-sha256"],
453-
message_authentication_codes: ["hkdf-hmac-sha256.v2"],
454-
// we have to include "decimal" per the spec.
455-
short_authentication_string: ["decimal", "emoji"],
456-
},
457-
});
413+
returnToDeviceMessageFromSync(buildSasStartMessage(transactionId));
458414
await waitForVerificationRequestChanged(request);
459415
expect(request.phase).toEqual(VerificationPhase.Started);
460416

@@ -601,3 +557,32 @@ function calculateMAC(olmSAS: Olm.SAS, input: string, info: string): string {
601557
function encodeUnpaddedBase64(uint8Array: ArrayBuffer | Uint8Array): string {
602558
return Buffer.from(uint8Array).toString("base64").replace(/=+$/g, "");
603559
}
560+
561+
/** build an m.key.verification.ready to-device message originating from the dummy device */
562+
function buildReadyMessage(transactionId: string, methods: string[]): { type: string; content: object } {
563+
return {
564+
type: "m.key.verification.ready",
565+
content: {
566+
from_device: TEST_DEVICE_ID,
567+
methods: methods,
568+
transaction_id: transactionId,
569+
},
570+
};
571+
}
572+
573+
/** build an m.key.verification.start to-device message suitable for the SAS flow, originating from the dummy device */
574+
function buildSasStartMessage(transactionId: string): { type: string; content: object } {
575+
return {
576+
type: "m.key.verification.start",
577+
content: {
578+
from_device: TEST_DEVICE_ID,
579+
method: "m.sas.v1",
580+
transaction_id: transactionId,
581+
hashes: ["sha256"],
582+
key_agreement_protocols: ["curve25519-hkdf-sha256"],
583+
message_authentication_codes: ["hkdf-hmac-sha256.v2"],
584+
// we have to include "decimal" per the spec.
585+
short_authentication_string: ["decimal", "emoji"],
586+
},
587+
};
588+
}

0 commit comments

Comments
 (0)