Skip to content

Commit 12c53bb

Browse files
committed
test(crypto-nodejs): Add more test cases.
1 parent 07fed7f commit 12c53bb

File tree

1 file changed

+87
-73
lines changed

1 file changed

+87
-73
lines changed

crates/matrix-sdk-crypto-nodejs/tests/machine.test.js

Lines changed: 87 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { initTracing, OlmMachine, UserId, DeviceId, RoomId, DeviceLists, RequestType, KeysUploadRequest, KeysQueryRequest, EncryptionSettings } = require('../');
1+
const { initTracing, OlmMachine, UserId, DeviceId, RoomId, DeviceLists, RequestType, KeysUploadRequest, KeysQueryRequest, KeysClaimRequest, EncryptionSettings } = require('../');
22

33
//initTracing();
44

@@ -61,98 +61,111 @@ describe(OlmMachine.name, () => {
6161

6262
expect(outgoingRequests).toHaveLength(2);
6363

64-
expect(outgoingRequests[0]).toBeInstanceOf(KeysUploadRequest);
65-
expect(outgoingRequests[0].id).toBeDefined();
66-
expect(outgoingRequests[0].type).toStrictEqual(RequestType.KeysUpload);
6764
{
65+
expect(outgoingRequests[0]).toBeInstanceOf(KeysUploadRequest);
66+
expect(outgoingRequests[0].id).toBeDefined();
67+
expect(outgoingRequests[0].type).toStrictEqual(RequestType.KeysUpload);
68+
6869
const body = JSON.parse(outgoingRequests[0].body);
6970
expect(body.device_keys).toBeDefined();
7071
expect(body.one_time_keys).toBeDefined();
7172
}
7273

73-
expect(outgoingRequests[1]).toBeInstanceOf(KeysQueryRequest);
74-
expect(outgoingRequests[1].id).toBeDefined();
75-
expect(outgoingRequests[1].type).toStrictEqual(RequestType.KeysQuery);
7674
{
75+
expect(outgoingRequests[1]).toBeInstanceOf(KeysQueryRequest);
76+
expect(outgoingRequests[1].id).toBeDefined();
77+
expect(outgoingRequests[1].type).toStrictEqual(RequestType.KeysQuery);
78+
7779
const body = JSON.parse(outgoingRequests[1].body);
7880
expect(body.timeout).toBeDefined();
7981
expect(body.device_keys).toBeDefined();
8082
expect(body.token).toBeDefined();
8183
}
8284
});
8385

84-
test('can mark requests as sent', async () => {
85-
const m = await machine(new UserId('@alice:example.org'), new DeviceId('DEVICEID'));
86-
87-
const toDeviceEvents = JSON.stringify({});
88-
const changedDevices = new DeviceLists();
89-
const oneTimeKeyCounts = {};
90-
const unusedFallbackKeys = [];
91-
92-
const receiveSyncChanges = await m.receiveSyncChanges(toDeviceEvents, changedDevices, oneTimeKeyCounts, unusedFallbackKeys);
93-
const outgoingRequests = await m.outgoingRequests();
86+
describe('can mark requests as sent and get missing sessions', () => {
87+
let m;
88+
let ougoingRequests;
9489

95-
expect(outgoingRequests).toHaveLength(2);
90+
beforeAll(async () => {
91+
m = await machine(new UserId('@alice:example.org'), new DeviceId('DEVICEID'));
9692

97-
{
98-
const request = outgoingRequests[0];
99-
expect(request).toBeInstanceOf(KeysUploadRequest);
100-
101-
// https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3keysupload
102-
const hypothetical_response = JSON.stringify({
103-
"one_time_key_counts": {
104-
"curve25519": 10,
105-
"signed_curve25519": 20
106-
}
107-
});
108-
const marked = await m.markRequestAsSent(request.id, request.type, hypothetical_response);
109-
expect(marked).toStrictEqual(true);
110-
}
93+
const toDeviceEvents = JSON.stringify({});
94+
const changedDevices = new DeviceLists();
95+
const oneTimeKeyCounts = {};
96+
const unusedFallbackKeys = [];
11197

112-
{
113-
const request = outgoingRequests[1];
114-
expect(request).toBeInstanceOf(KeysQueryRequest);
115-
116-
// https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3keysquery
117-
const hypothetical_response = JSON.stringify({
118-
"device_keys": {
119-
"@alice:example.org": {
120-
"JLAFKJWSCS": {
121-
"algorithms": [
122-
"m.olm.v1.curve25519-aes-sha2",
123-
"m.megolm.v1.aes-sha2"
124-
],
125-
"device_id": "JLAFKJWSCS",
126-
"keys": {
127-
"curve25519:JLAFKJWSCS": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4",
128-
"ed25519:JLAFKJWSCS": "nE6W2fCblxDcOFmeEtCHNl8/l8bXcu7GKyAswA4r3mM"
129-
},
130-
"signatures": {
131-
"@alice:example.org": {
132-
"ed25519:JLAFKJWSCS": "m53Wkbh2HXkc3vFApZvCrfXcX3AI51GsDHustMhKwlv3TuOJMj4wistcOTM8q2+e/Ro7rWFUb9ZfnNbwptSUBA"
133-
}
134-
},
135-
"unsigned": {
136-
"device_display_name": "Alice's mobile phone"
137-
},
138-
"user_id": "@alice:example.org"
139-
}
140-
}
141-
},
142-
"failures": {}
143-
});
144-
const marked = await m.markRequestAsSent(request.id, request.type, hypothetical_response);
145-
expect(marked).toStrictEqual(true);
98+
const receiveSyncChanges = await m.receiveSyncChanges(toDeviceEvents, changedDevices, oneTimeKeyCounts, unusedFallbackKeys);
99+
outgoingRequests = await m.outgoingRequests();
146100

147-
console.log(await m.getMissingSessions([user]));
148-
}
149-
});
101+
expect(outgoingRequests).toHaveLength(2);
102+
});
150103

151-
/*
152-
test('can get missing sessions', async () => {
153-
const m = await machine();
104+
test('can mark requests as sent', async () => {
105+
{
106+
const request = outgoingRequests[0];
107+
expect(request).toBeInstanceOf(KeysUploadRequest);
154108

155-
expect(await m.getMissingSessions([user])).toStrictEqual(null);
109+
// https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3keysupload
110+
const hypothetical_response = JSON.stringify({
111+
"one_time_key_counts": {
112+
"curve25519": 10,
113+
"signed_curve25519": 20
114+
}
115+
});
116+
const marked = await m.markRequestAsSent(request.id, request.type, hypothetical_response);
117+
expect(marked).toStrictEqual(true);
118+
}
119+
120+
{
121+
const request = outgoingRequests[1];
122+
expect(request).toBeInstanceOf(KeysQueryRequest);
123+
124+
// https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3keysquery
125+
const hypothetical_response = JSON.stringify({
126+
"device_keys": {
127+
"@alice:example.org": {
128+
"JLAFKJWSCS": {
129+
"algorithms": [
130+
"m.olm.v1.curve25519-aes-sha2",
131+
"m.megolm.v1.aes-sha2"
132+
],
133+
"device_id": "JLAFKJWSCS",
134+
"keys": {
135+
"curve25519:JLAFKJWSCS": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4",
136+
"ed25519:JLAFKJWSCS": "nE6W2fCblxDcOFmeEtCHNl8/l8bXcu7GKyAswA4r3mM"
137+
},
138+
"signatures": {
139+
"@alice:example.org": {
140+
"ed25519:JLAFKJWSCS": "m53Wkbh2HXkc3vFApZvCrfXcX3AI51GsDHustMhKwlv3TuOJMj4wistcOTM8q2+e/Ro7rWFUb9ZfnNbwptSUBA"
141+
}
142+
},
143+
"unsigned": {
144+
"device_display_name": "Alice's mobile phone"
145+
},
146+
"user_id": "@alice:example.org"
147+
}
148+
}
149+
},
150+
"failures": {}
151+
});
152+
const marked = await m.markRequestAsSent(request.id, request.type, hypothetical_response);
153+
expect(marked).toStrictEqual(true);
154+
}
155+
});
156+
157+
test('can get missing sessions', async () => {
158+
const request = await m.getMissingSessions([user]);
159+
160+
expect(request).toBeInstanceOf(KeysClaimRequest);
161+
expect(request.id).toBeDefined();
162+
expect(request.type).toStrictEqual(RequestType.KeysClaim);
163+
164+
const body = JSON.parse(request.body);
165+
expect(body.one_time_keys).toBeDefined();
166+
expect(body.one_time_keys[user.toString()]).toBeDefined();
167+
expect(body.timeout).toBeDefined();
168+
});
156169
});
157170

158171
test('can update tracked users', async () => {
@@ -161,6 +174,7 @@ describe(OlmMachine.name, () => {
161174
expect(await m.updateTrackedUsers([user])).toStrictEqual(undefined);
162175
});
163176

177+
/*
164178
test('can share a room key', async () => {
165179
const m = await machine();
166180

0 commit comments

Comments
 (0)