Skip to content

Commit b1501fc

Browse files
committed
Add test for verification started from a request
1 parent b877d15 commit b1501fc

File tree

2 files changed

+178
-0
lines changed

2 files changed

+178
-0
lines changed

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

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1572,6 +1572,7 @@ pub(crate) mod test {
15721572
event_id,
15731573
events::{
15741574
dummy::ToDeviceDummyEventContent,
1575+
key::verification::VerificationMethod,
15751576
room::{
15761577
encrypted::ToDeviceRoomEncryptedEventContent,
15771578
message::{MessageType, RoomMessageEventContent},
@@ -2170,4 +2171,172 @@ pub(crate) mod test {
21702171
assert!(bob_sas.is_done());
21712172
assert!(alice_device.verified());
21722173
}
2174+
2175+
#[tokio::test]
2176+
async fn interactive_verification_started_from_request() {
2177+
let (alice, bob) = get_machine_pair_with_setup_sessions().await;
2178+
2179+
// ----------------------------------------------------------------------------
2180+
// On Alice's device:
2181+
let bob_device = alice.get_device(bob.user_id(), bob.device_id()).await.unwrap().unwrap();
2182+
2183+
assert!(!bob_device.verified());
2184+
2185+
// Alice sends a verification request with her desired methods to Bob
2186+
let (alice_ver_req, request) =
2187+
bob_device.request_verification_with_methods(vec![VerificationMethod::SasV1]).await;
2188+
2189+
// ----------------------------------------------------------------------------
2190+
// On Bobs's device:
2191+
let event = request_to_event(alice.user_id(), &request);
2192+
bob.handle_verification_event(&event).await;
2193+
let flow_id = alice_ver_req.flow_id().as_str();
2194+
2195+
let verification_request = bob.get_verification_request(alice.user_id(), flow_id).unwrap();
2196+
2197+
// Bob accepts the request, sending a Ready request
2198+
let accept_request =
2199+
verification_request.accept_with_methods(vec![VerificationMethod::SasV1]).unwrap();
2200+
// And also immediately sends a start request
2201+
let (_, start_request_from_bob) = verification_request.start_sas().await.unwrap().unwrap();
2202+
2203+
// ----------------------------------------------------------------------------
2204+
// On Alice's device:
2205+
2206+
// Alice receives the Ready
2207+
let event = request_to_event(bob.user_id(), &accept_request);
2208+
alice.handle_verification_event(&event).await;
2209+
2210+
let verification_request = alice.get_verification_request(bob.user_id(), flow_id).unwrap();
2211+
2212+
// And also immediately sends a start request
2213+
let (alice_sas, start_request_from_alice) =
2214+
verification_request.start_sas().await.unwrap().unwrap();
2215+
2216+
// Now alice receives Bob's start:
2217+
let event = request_to_event(bob.user_id(), &start_request_from_bob);
2218+
alice.handle_verification_event(&event).await;
2219+
2220+
// Since Alice's user id is lexicographically smaller than Bob's, Alice does not
2221+
// do anything with the request, however.
2222+
assert!(alice.user_id() < bob.user_id());
2223+
2224+
// ----------------------------------------------------------------------------
2225+
// On Bob's device:
2226+
2227+
// Bob receives Alice's start:
2228+
let event = request_to_event(alice.user_id(), &start_request_from_alice);
2229+
bob.handle_verification_event(&event).await;
2230+
2231+
let bob_sas = bob
2232+
.get_verification(alice.user_id(), alice_sas.flow_id().as_str())
2233+
.unwrap()
2234+
.sas_v1()
2235+
.unwrap();
2236+
2237+
assert!(alice_sas.emoji().is_none());
2238+
assert!(bob_sas.emoji().is_none());
2239+
2240+
// ... and accepts it
2241+
let event = bob_sas.accept().map(|r| request_to_event(bob.user_id(), &r)).unwrap();
2242+
2243+
// ----------------------------------------------------------------------------
2244+
// On Alice's device:
2245+
2246+
// Alice receives the Accept request:
2247+
alice.handle_verification_event(&event).await;
2248+
2249+
// Alice sends a key
2250+
let msgs = alice.verification_machine.outgoing_messages();
2251+
assert!(msgs.len() == 1);
2252+
let msg = msgs.first().unwrap();
2253+
let event = outgoing_request_to_event(alice.user_id(), msg);
2254+
alice.verification_machine.mark_request_as_sent(&msg.request_id);
2255+
2256+
// ----------------------------------------------------------------------------
2257+
// On Bob's device:
2258+
2259+
// And bob receive's it:
2260+
bob.handle_verification_event(&event).await;
2261+
2262+
// Now bob sends a key
2263+
let msgs = bob.verification_machine.outgoing_messages();
2264+
assert!(msgs.len() == 1);
2265+
let msg = msgs.first().unwrap();
2266+
let event = outgoing_request_to_event(bob.user_id(), msg);
2267+
bob.verification_machine.mark_request_as_sent(&msg.request_id);
2268+
2269+
// ----------------------------------------------------------------------------
2270+
// On Alice's device:
2271+
2272+
// And alice receives it
2273+
alice.handle_verification_event(&event).await;
2274+
2275+
// As a result, both devices now can show emojis/decimals
2276+
assert!(alice_sas.emoji().is_some());
2277+
assert!(bob_sas.emoji().is_some());
2278+
2279+
// ----------------------------------------------------------------------------
2280+
// On Bob's device:
2281+
2282+
assert_eq!(alice_sas.emoji(), bob_sas.emoji());
2283+
assert_eq!(alice_sas.decimals(), bob_sas.decimals());
2284+
2285+
// Bob first confirms that the emojis match and sends the MAC...
2286+
let contents = bob_sas.confirm().await.unwrap().0;
2287+
assert!(contents.len() == 1);
2288+
let event = request_to_event(bob.user_id(), &contents[0]);
2289+
2290+
// ----------------------------------------------------------------------------
2291+
// On Alice's device:
2292+
2293+
// ...which alice receives
2294+
alice.handle_verification_event(&event).await;
2295+
2296+
assert!(!alice_sas.is_done());
2297+
assert!(!bob_sas.is_done());
2298+
2299+
// Now alice confirms that the emojis match and sends...
2300+
let contents = alice_sas.confirm().await.unwrap().0;
2301+
assert!(contents.len() == 2);
2302+
// ... her own MAC...
2303+
let event_mac = request_to_event(alice.user_id(), &contents[0]);
2304+
// ... and a Done message
2305+
let event_done = request_to_event(alice.user_id(), &contents[1]);
2306+
2307+
// ----------------------------------------------------------------------------
2308+
// On Bob's device:
2309+
2310+
// Bob receives the MAC message
2311+
bob.handle_verification_event(&event_mac).await;
2312+
2313+
// Bob verifies that the MAC is valid and also sends a "done" message.
2314+
let msgs = bob.verification_machine.outgoing_messages();
2315+
eprintln!("{:?}", msgs);
2316+
assert!(msgs.len() == 1);
2317+
let event = msgs.first().map(|r| outgoing_request_to_event(bob.user_id(), r)).unwrap();
2318+
2319+
let alice_device =
2320+
bob.get_device(alice.user_id(), alice.device_id()).await.unwrap().unwrap();
2321+
2322+
assert!(!bob_sas.is_done());
2323+
assert!(!alice_device.verified());
2324+
// And Bob receives the Done message of alice.
2325+
bob.handle_verification_event(&event_done).await;
2326+
2327+
assert!(bob_sas.is_done());
2328+
assert!(alice_device.verified());
2329+
2330+
// ----------------------------------------------------------------------------
2331+
// On Alice's device:
2332+
2333+
assert!(!alice_sas.is_done());
2334+
assert!(!bob_device.verified());
2335+
// Alices receives the done message
2336+
eprintln!("{:?}", event);
2337+
alice.handle_verification_event(&event).await;
2338+
2339+
assert!(alice_sas.is_done());
2340+
assert!(bob_device.verified());
2341+
}
21732342
}

crates/matrix-sdk-crypto/src/verification/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,12 @@ pub(crate) mod test {
719719
let sender = sender.to_owned();
720720

721721
match content {
722+
AnyToDeviceEventContent::KeyVerificationRequest(c) => {
723+
AnyToDeviceEvent::KeyVerificationRequest(ToDeviceEvent { sender, content: c })
724+
}
725+
AnyToDeviceEventContent::KeyVerificationReady(c) => {
726+
AnyToDeviceEvent::KeyVerificationReady(ToDeviceEvent { sender, content: c })
727+
}
722728
AnyToDeviceEventContent::KeyVerificationKey(c) => {
723729
AnyToDeviceEvent::KeyVerificationKey(ToDeviceEvent { sender, content: c })
724730
}
@@ -731,6 +737,9 @@ pub(crate) mod test {
731737
AnyToDeviceEventContent::KeyVerificationMac(c) => {
732738
AnyToDeviceEvent::KeyVerificationMac(ToDeviceEvent { sender, content: c })
733739
}
740+
AnyToDeviceEventContent::KeyVerificationDone(c) => {
741+
AnyToDeviceEvent::KeyVerificationDone(ToDeviceEvent { sender, content: c })
742+
}
734743

735744
_ => unreachable!(),
736745
}

0 commit comments

Comments
 (0)