Skip to content

Commit d8f815e

Browse files
committed
Factor out functions for building responses
1 parent fe3dec9 commit d8f815e

File tree

1 file changed

+38
-53
lines changed

1 file changed

+38
-53
lines changed

spec/integ/crypto/verification.spec.ts

+38-53
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
162162
expectSendToDeviceMessage("m.key.verification.request"),
163163
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
164164
]);
165-
const transactionId = request.transactionId;
165+
const transactionId = request.transactionId!;
166166
expect(transactionId).toBeDefined();
167167
expect(request.phase).toEqual(VerificationPhase.Requested);
168168
expect(request.roomId).toBeUndefined();
@@ -188,32 +188,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
188188
}
189189

190190
// The dummy device replies with an m.key.verification.ready...
191-
returnToDeviceMessageFromSync({
192-
type: "m.key.verification.ready",
193-
content: {
194-
from_device: TEST_DEVICE_ID,
195-
methods: ["m.sas.v1"],
196-
transaction_id: transactionId,
197-
},
198-
});
191+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.sas.v1"]));
199192
await waitForVerificationRequestChanged(request);
200193
expect(request.phase).toEqual(VerificationPhase.Ready);
201194
expect(request.otherDeviceId).toEqual(TEST_DEVICE_ID);
202195

203196
// ... and picks a method with m.key.verification.start
204-
returnToDeviceMessageFromSync({
205-
type: "m.key.verification.start",
206-
content: {
207-
from_device: TEST_DEVICE_ID,
208-
method: "m.sas.v1",
209-
transaction_id: transactionId,
210-
hashes: ["sha256"],
211-
key_agreement_protocols: ["curve25519-hkdf-sha256"],
212-
message_authentication_codes: ["hkdf-hmac-sha256.v2"],
213-
// we have to include "decimal" per the spec.
214-
short_authentication_string: ["decimal", "emoji"],
215-
},
216-
});
197+
returnToDeviceMessageFromSync(buildSasStartMessage(transactionId));
198+
217199
// as soon as the Changed event arrives, `verifier` should be defined
218200
const verifier = await new Promise<Verifier>((resolve) => {
219201
function onChange() {
@@ -354,7 +336,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
354336
expectSendToDeviceMessage("m.key.verification.request"),
355337
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
356338
]);
357-
const transactionId = request.transactionId;
339+
const transactionId = request.transactionId!;
358340

359341
const toDeviceMessage = requestBody.messages[TEST_USER_ID][TEST_DEVICE_ID];
360342
expect(toDeviceMessage.methods).toContain("m.qr_code.show.v1");
@@ -364,14 +346,7 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
364346
expect(toDeviceMessage.transaction_id).toEqual(transactionId);
365347

366348
// The dummy device replies with an m.key.verification.ready, with an indication we can scan the QR code
367-
returnToDeviceMessageFromSync({
368-
type: "m.key.verification.ready",
369-
content: {
370-
from_device: TEST_DEVICE_ID,
371-
methods: ["m.qr_code.scan.v1"],
372-
transaction_id: transactionId,
373-
},
374-
});
349+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.qr_code.scan.v1"]));
375350
await waitForVerificationRequestChanged(request);
376351
expect(request.phase).toEqual(VerificationPhase.Ready);
377352

@@ -441,33 +416,14 @@ describe.each(Object.entries(CRYPTO_BACKENDS))("verification (%s)", (backend: st
441416
expectSendToDeviceMessage("m.key.verification.request"),
442417
aliceClient.getCrypto()!.requestDeviceVerification(TEST_USER_ID, TEST_DEVICE_ID),
443418
]);
444-
const transactionId = request.transactionId;
419+
const transactionId = request.transactionId!;
445420

446421
// The dummy device replies with an m.key.verification.ready...
447-
returnToDeviceMessageFromSync({
448-
type: "m.key.verification.ready",
449-
content: {
450-
from_device: TEST_DEVICE_ID,
451-
methods: ["m.sas.v1"],
452-
transaction_id: transactionId,
453-
},
454-
});
422+
returnToDeviceMessageFromSync(buildReadyMessage(transactionId, ["m.sas.v1"]));
455423
await waitForVerificationRequestChanged(request);
456424

457425
// ... and picks a method with m.key.verification.start
458-
returnToDeviceMessageFromSync({
459-
type: "m.key.verification.start",
460-
content: {
461-
from_device: TEST_DEVICE_ID,
462-
method: "m.sas.v1",
463-
transaction_id: transactionId,
464-
hashes: ["sha256"],
465-
key_agreement_protocols: ["curve25519-hkdf-sha256"],
466-
message_authentication_codes: ["hkdf-hmac-sha256.v2"],
467-
// we have to include "decimal" per the spec.
468-
short_authentication_string: ["decimal", "emoji"],
469-
},
470-
});
426+
returnToDeviceMessageFromSync(buildSasStartMessage(transactionId));
471427
await waitForVerificationRequestChanged(request);
472428
expect(request.phase).toEqual(VerificationPhase.Started);
473429

@@ -614,3 +570,32 @@ function calculateMAC(olmSAS: Olm.SAS, input: string, info: string): string {
614570
function encodeUnpaddedBase64(uint8Array: ArrayBuffer | Uint8Array): string {
615571
return Buffer.from(uint8Array).toString("base64").replace(/=+$/g, "");
616572
}
573+
574+
/** build an m.key.verification.ready to-device message originating from the dummy device */
575+
function buildReadyMessage(transactionId: string, methods: string[]): { type: string; content: object } {
576+
return {
577+
type: "m.key.verification.ready",
578+
content: {
579+
from_device: TEST_DEVICE_ID,
580+
methods: methods,
581+
transaction_id: transactionId,
582+
},
583+
};
584+
}
585+
586+
/** build an m.key.verification.start to-device message suitable for the SAS flow, originating from the dummy device */
587+
function buildSasStartMessage(transactionId: string): { type: string; content: object } {
588+
return {
589+
type: "m.key.verification.start",
590+
content: {
591+
from_device: TEST_DEVICE_ID,
592+
method: "m.sas.v1",
593+
transaction_id: transactionId,
594+
hashes: ["sha256"],
595+
key_agreement_protocols: ["curve25519-hkdf-sha256"],
596+
message_authentication_codes: ["hkdf-hmac-sha256.v2"],
597+
// we have to include "decimal" per the spec.
598+
short_authentication_string: ["decimal", "emoji"],
599+
},
600+
};
601+
}

0 commit comments

Comments
 (0)