Skip to content

Commit 07fed7f

Browse files
committed
test(crypto-nodejs): Continue to test OlmMachine and add tracing support.
1 parent 0ed74d8 commit 07fed7f

File tree

6 files changed

+86
-61
lines changed

6 files changed

+86
-61
lines changed

crates/matrix-sdk-crypto-nodejs/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ crate-type = ["cdylib"]
2222
default = []
2323
qrcode = ["matrix-sdk-crypto/qrcode"]
2424
docsrs = []
25+
tracing = ["tracing-subscriber"]
2526

2627
[dependencies]
2728
matrix-sdk-crypto = { version = "0.5.0", path = "../matrix-sdk-crypto" }
@@ -32,6 +33,7 @@ napi = { git = "https://github.com/Hywan/napi-rs", branch = "feat-either-n-up-to
3233
napi-derive = { git = "https://github.com/Hywan/napi-rs", branch = "feat-either-n-up-to-26" }
3334
serde_json = "1.0.79"
3435
http = "0.2.6"
36+
tracing-subscriber = { version = "0.3", default-features = false, features = ["tracing-log", "time", "smallvec", "fmt"], optional = true }
3537

3638
[build-dependencies]
3739
napi-build = "2.0.0"
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# `matrix-sdk-crypto-nodejs`
2+
3+
This is the NodeJS bindings to [`matrix-sdk-crypto`].
4+
5+
## Installation
6+
7+
```sh
8+
$ npm install
9+
$ npm run build
10+
$ npm run test
11+
```
12+
13+
### With tracing
14+
15+
If you want to get [tracing](https://tracing.rs) (to get logs):
16+
17+
```sh
18+
$ npm run build -- --features tracing
19+
$ RUST_LOG=debug npm run test
20+
```
21+
22+
See
23+
[`tracing-subscriber`](https://tracing.rs/tracing_subscriber/index.html)
24+
to learn more about the `RUST_LOG` environment variable.
25+
26+
27+
28+
29+
[`matrix-sdk-crypto`]: https://github.com/matrix-org/matrix-rust-sdk/tree/main/crates/matrix-sdk-crypto

crates/matrix-sdk-crypto-nodejs/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,7 @@ pub mod machine;
2424
pub mod requests;
2525
pub mod responses;
2626
pub mod sync_events;
27+
#[cfg(feature = "tracing")]
28+
pub mod tracing;
2729

2830
use crate::errors::into_err;

crates/matrix-sdk-crypto-nodejs/src/machine.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,15 +232,18 @@ impl OlmMachine {
232232
/// # Arguments
233233
///
234234
/// * `users`, the list of users that we should check if we lack a session
235-
/// with one of their devices. This can be an empty array when calling
236-
/// this method between sync requests.
235+
/// with one of their devices. This can be an empty array or `null` when
236+
/// calling this method between sync requests.
237237
#[napi]
238238
pub async fn get_missing_sessions(
239239
&self,
240-
users: Vec<&identifiers::UserId>,
240+
users: Option<Vec<&identifiers::UserId>>,
241241
) -> Result<Option<requests::KeysClaimRequest>, napi::Error> {
242-
let users =
243-
users.into_iter().map(|user| user.inner.clone()).collect::<Vec<ruma::OwnedUserId>>();
242+
let users = users
243+
.unwrap_or_default()
244+
.into_iter()
245+
.map(|user| user.inner.clone())
246+
.collect::<Vec<ruma::OwnedUserId>>();
244247

245248
match self
246249
.inner
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use napi_derive::*;
2+
3+
#[napi]
4+
pub fn init_tracing() {
5+
tracing_subscriber::fmt::init();
6+
}

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

Lines changed: 39 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
const { OlmMachine, UserId, DeviceId, RoomId, DeviceLists, RequestType, KeysUploadRequest, KeysQueryRequest } = require('../');
1+
const { initTracing, OlmMachine, UserId, DeviceId, RoomId, DeviceLists, RequestType, KeysUploadRequest, KeysQueryRequest, EncryptionSettings } = require('../');
2+
3+
//initTracing();
24

35
describe(OlmMachine.name, () => {
46
test('cannot be instantiated with the constructor', () => {
@@ -80,13 +82,14 @@ describe(OlmMachine.name, () => {
8082
});
8183

8284
test('can mark requests as sent', async () => {
83-
const m = await machine();
85+
const m = await machine(new UserId('@alice:example.org'), new DeviceId('DEVICEID'));
86+
8487
const toDeviceEvents = JSON.stringify({});
8588
const changedDevices = new DeviceLists();
8689
const oneTimeKeyCounts = {};
8790
const unusedFallbackKeys = [];
8891

89-
const receiveSyncChanges = JSON.parse(await m.receiveSyncChanges(toDeviceEvents, changedDevices, oneTimeKeyCounts, unusedFallbackKeys));
92+
const receiveSyncChanges = await m.receiveSyncChanges(toDeviceEvents, changedDevices, oneTimeKeyCounts, unusedFallbackKeys);
9093
const outgoingRequests = await m.outgoingRequests();
9194

9295
expect(outgoingRequests).toHaveLength(2);
@@ -96,13 +99,13 @@ describe(OlmMachine.name, () => {
9699
expect(request).toBeInstanceOf(KeysUploadRequest);
97100

98101
// https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3keysupload
99-
const hypothetic_response = JSON.stringify({
102+
const hypothetical_response = JSON.stringify({
100103
"one_time_key_counts": {
101104
"curve25519": 10,
102105
"signed_curve25519": 20
103106
}
104107
});
105-
const marked = await m.markRequestAsSent(request.id, request.type, hypothetic_response);
108+
const marked = await m.markRequestAsSent(request.id, request.type, hypothetical_response);
106109
expect(marked).toStrictEqual(true);
107110
}
108111

@@ -111,77 +114,57 @@ describe(OlmMachine.name, () => {
111114
expect(request).toBeInstanceOf(KeysQueryRequest);
112115

113116
// https://spec.matrix.org/v1.2/client-server-api/#post_matrixclientv3keysquery
114-
const hypothetic_response = JSON.stringify({
117+
const hypothetical_response = JSON.stringify({
115118
"device_keys": {
116-
"@alice:example.com": {
119+
"@alice:example.org": {
117120
"JLAFKJWSCS": {
118121
"algorithms": [
119122
"m.olm.v1.curve25519-aes-sha2",
120123
"m.megolm.v1.aes-sha2"
121124
],
122125
"device_id": "JLAFKJWSCS",
123126
"keys": {
124-
"curve25519:JLAFKJWSCS": "3C5BFWi2Y8MaVvjM8M22DBmh24PmgR0nPvJOIArzgyI",
125-
"ed25519:JLAFKJWSCS": "lEuiRJBit0IG6nUf5pUzWTUEsRVVe/HJkoKuEww9ULI"
127+
"curve25519:JLAFKJWSCS": "wjLpTLRqbqBzLs63aYaEv2Boi6cFEbbM/sSRQ2oAKk4",
128+
"ed25519:JLAFKJWSCS": "nE6W2fCblxDcOFmeEtCHNl8/l8bXcu7GKyAswA4r3mM"
126129
},
127130
"signatures": {
128-
"@alice:example.com": {
129-
"ed25519:JLAFKJWSCS": "dSO80A01XiigH3uBiDVx/EjzaoycHcjq9lfQX0uWsqxl2giMIiSPR8a4d291W1ihKJL/a+myXS367WT6NAIcBA"
131+
"@alice:example.org": {
132+
"ed25519:JLAFKJWSCS": "m53Wkbh2HXkc3vFApZvCrfXcX3AI51GsDHustMhKwlv3TuOJMj4wistcOTM8q2+e/Ro7rWFUb9ZfnNbwptSUBA"
130133
}
131134
},
132135
"unsigned": {
133136
"device_display_name": "Alice's mobile phone"
134137
},
135-
"user_id": "@alice:example.com"
138+
"user_id": "@alice:example.org"
136139
}
137140
}
138141
},
139-
"master_keys": {
140-
"@alice:example.com": {
141-
"keys": {
142-
"ed25519:base64+master+public+key": "base64+master+public+key"
143-
},
144-
"usage": [
145-
"master"
146-
],
147-
"user_id": "@alice:example.com"
148-
}
149-
},
150-
"self_signing_keys": {
151-
"@alice:example.com": {
152-
"keys": {
153-
"ed25519:base64+self+signing+public+key": "base64+self+signing+master+public+key"
154-
},
155-
"signatures": {
156-
"@alice:example.com": {
157-
"ed25519:base64+master+public+key": "signature+of+self+signing+key"
158-
}
159-
},
160-
"usage": [
161-
"self_signing"
162-
],
163-
"user_id": "@alice:example.com"
164-
}
165-
},
166-
"user_signing_keys": {
167-
"@alice:example.com": {
168-
"keys": {
169-
"ed25519:base64+user+signing+public+key": "base64+user+signing+master+public+key"
170-
},
171-
"signatures": {
172-
"@alice:example.com": {
173-
"ed25519:base64+master+public+key": "signature+of+user+signing+key"
174-
}
175-
},
176-
"usage": [
177-
"user_signing"
178-
],
179-
"user_id": "@alice:example.com"
180-
}
181-
}
142+
"failures": {}
182143
});
183-
const marked = await m.markRequestAsSent(request.id, request.type, hypothetic_response);
144+
const marked = await m.markRequestAsSent(request.id, request.type, hypothetical_response);
184145
expect(marked).toStrictEqual(true);
146+
147+
console.log(await m.getMissingSessions([user]));
185148
}
186149
});
150+
151+
/*
152+
test('can get missing sessions', async () => {
153+
const m = await machine();
154+
155+
expect(await m.getMissingSessions([user])).toStrictEqual(null);
156+
});
157+
158+
test('can update tracked users', async () => {
159+
const m = await machine();
160+
161+
expect(await m.updateTrackedUsers([user])).toStrictEqual(undefined);
162+
});
163+
164+
test('can share a room key', async () => {
165+
const m = await machine();
166+
167+
console.log(await m.shareRoomKey(room, [new UserId('@bob:example.org')], new EncryptionSettings()));
168+
});
169+
*/
187170
});

0 commit comments

Comments
 (0)