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