From 0d99d8cc2370cbf9bec82b8b53ea31ea8bb53881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Damir=20Jeli=C4=87?= Date: Thu, 24 Dec 2020 15:22:51 +0100 Subject: [PATCH] crypto: Test verification request starting up to SAS. --- .../src/verification/requests.rs | 123 +++++++++++++++--- 1 file changed, 107 insertions(+), 16 deletions(-) diff --git a/matrix_sdk_crypto/src/verification/requests.rs b/matrix_sdk_crypto/src/verification/requests.rs index 6a96d9e6..51098de0 100644 --- a/matrix_sdk_crypto/src/verification/requests.rs +++ b/matrix_sdk_crypto/src/verification/requests.rs @@ -34,7 +34,7 @@ use crate::{ ReadOnlyDevice, Sas, UserIdentities, }; -use super::sas::OutgoingContent; +use super::sas::{OutgoingContent, StartContent}; const SUPPORTED_METHODS: &[VerificationMethod] = &[VerificationMethod::MSasV1]; @@ -173,6 +173,24 @@ impl VerificationRequest { _ => todo!(), } } + + pub(crate) fn start( + &self, + device: ReadOnlyDevice, + user_identity: Option, + ) -> Option<(Sas, StartContent)> { + match &*self.inner.lock().unwrap() { + InnerRequest::Ready(s) => Some(s.clone().start_sas( + self.room_id(), + self.store.clone(), + self.account.clone(), + self.private_cross_signing_identity.clone(), + device, + user_identity, + )), + _ => None, + } + } } #[derive(Debug)] @@ -394,20 +412,22 @@ impl RequestState { fn start_sas( self, - _store: Arc>, - _account: ReadOnlyAccount, - _private_identity: PrivateCrossSigningIdentity, - _other_device: ReadOnlyDevice, - _other_identity: Option, - ) -> (Sas, OutgoingContent) { - todo!() - // Sas::start_in_room( - // account, - // private_identity, - // other_device, - // store, - // other_identity, - // ) + room_id: &RoomId, + store: Arc>, + account: ReadOnlyAccount, + private_identity: PrivateCrossSigningIdentity, + other_device: ReadOnlyDevice, + other_identity: Option, + ) -> (Sas, StartContent) { + Sas::start_in_room( + self.state.flow_id, + room_id.clone(), + account, + private_identity, + other_device, + store, + other_identity, + ) } } @@ -423,10 +443,11 @@ struct Passive { #[cfg(test)] mod test { - use std::convert::TryFrom; + use std::{convert::TryFrom, time::SystemTime}; use matrix_sdk_common::{ api::r0::message::send_message_event::Response as RoomMessageResponse, + events::{SyncMessageEvent, Unsigned}, identifiers::{event_id, room_id, DeviceIdBox, UserId}, }; use matrix_sdk_test::async_test; @@ -434,6 +455,8 @@ mod test { use crate::{ olm::{PrivateCrossSigningIdentity, ReadOnlyAccount}, store::{CryptoStore, MemoryStore}, + verification::sas::StartContent, + ReadOnlyDevice, }; use super::VerificationRequest; @@ -497,4 +520,72 @@ mod test { assert!(bob_request.is_ready()); assert!(alice_request.is_ready()); } + + #[async_test] + async fn test_requesting_until_sas() { + let event_id = event_id!("$1234localhost"); + let room_id = room_id!("!test:localhost"); + + let alice = ReadOnlyAccount::new(&alice_id(), &alice_device_id()); + let alice_device = ReadOnlyDevice::from_account(&alice).await; + + let alice_store: Box = Box::new(MemoryStore::new()); + let alice_identity = PrivateCrossSigningIdentity::empty(alice_id()); + + let bob = ReadOnlyAccount::new(&bob_id(), &bob_device_id()); + let bob_device = ReadOnlyDevice::from_account(&bob).await; + let bob_store: Box = Box::new(MemoryStore::new()); + let bob_identity = PrivateCrossSigningIdentity::empty(alice_id()); + + let bob_request = VerificationRequest::new( + bob, + bob_identity, + bob_store.into(), + room_id.clone().into(), + &alice_id(), + ); + + let content = bob_request.request().unwrap(); + + let alice_request = VerificationRequest::from_request_event( + alice, + alice_identity, + alice_store.into(), + &room_id, + &bob_id(), + &event_id, + &content, + ); + + let content = alice_request.accept().unwrap(); + + let response = RoomMessageResponse::new(event_id.clone()); + bob_request.mark_as_sent(&response); + + bob_request.receive_ready(&alice_id(), &content).unwrap(); + + assert!(bob_request.is_ready()); + assert!(alice_request.is_ready()); + + let (bob_sas, start_content) = bob_request.start(alice_device, None).unwrap(); + + let event = if let StartContent::Room(_, c) = start_content { + SyncMessageEvent { + content: c, + event_id: event_id.clone(), + sender: bob_id(), + origin_server_ts: SystemTime::now(), + unsigned: Unsigned::default(), + } + } else { + panic!("Invalid start event content type"); + }; + + let alice_sas = alice_request + .into_started_sas(&event, bob_device, None) + .unwrap(); + + assert!(!bob_sas.is_canceled()); + assert!(!alice_sas.is_canceled()); + } }