Skip to content

Commit 2639b5c

Browse files
committed
test: Add a test on protection message sort timestamp (#5088)
Even if `vc-request-with-auth` is received with a delay, the protection message must have the sort timestamp equal to the Sent timestamp of `vc-request-with-auth`, otherwise subsequent chat messages would also have greater sort timestamps and while it doesn't affect the chat itself (because Sent timestamps are shown to a user), it affects the chat position in the chatlist because chats there are sorted by sort timestamps of the last messages, so the user sees chats sorted out of order. That's what happened in #5088 where a user restores the backup made before setting up a verified chat with their contact and fetches new messages, including `vc-request-with-auth` and also messages from other chats, after that.
1 parent 3975866 commit 2639b5c

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/securejoin.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,20 @@ mod tests {
765765
use crate::stock_str::chat_protection_enabled;
766766
use crate::test_utils::get_chat_msg;
767767
use crate::test_utils::{TestContext, TestContextManager};
768-
use crate::tools::EmailAddress;
768+
use crate::tools::{EmailAddress, SystemTime};
769+
use std::time::Duration;
769770

770771
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
771772
async fn test_setup_contact() {
773+
test_setup_contact_ex(false).await
774+
}
775+
776+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
777+
async fn test_setup_contact_protection_timestamp() {
778+
test_setup_contact_ex(true).await
779+
}
780+
781+
async fn test_setup_contact_ex(check_protection_timestamp: bool) {
772782
let mut tcm = TestContextManager::new();
773783
let alice = tcm.alice().await;
774784
let bob = tcm.bob().await;
@@ -857,6 +867,10 @@ mod tests {
857867
// Check Bob sent the right message.
858868
let sent = bob.pop_sent_msg().await;
859869
let msg = alice.parse_msg(&sent).await;
870+
let vc_request_with_auth_ts_sent = msg
871+
.get_header(HeaderDef::Date)
872+
.and_then(|value| mailparse::dateparse(value).ok())
873+
.unwrap();
860874
assert!(msg.was_encrypted());
861875
assert_eq!(
862876
msg.get_header(HeaderDef::SecureJoin).unwrap(),
@@ -880,6 +894,10 @@ mod tests {
880894
.unwrap();
881895
assert_eq!(contact_bob.is_verified(&alice.ctx).await.unwrap(), false);
882896

897+
if check_protection_timestamp {
898+
SystemTime::shift(Duration::from_secs(3600));
899+
}
900+
883901
// Step 5+6: Alice receives vc-request-with-auth, sends vc-contact-confirm
884902
alice.recv_msg(&sent).await;
885903
assert_eq!(contact_bob.is_verified(&alice.ctx).await.unwrap(), true);
@@ -905,6 +923,9 @@ mod tests {
905923
assert!(msg.is_info());
906924
let expected_text = chat_protection_enabled(&alice).await;
907925
assert_eq!(msg.get_text(), expected_text);
926+
if check_protection_timestamp {
927+
assert_eq!(msg.timestamp_sort, vc_request_with_auth_ts_sent);
928+
}
908929
}
909930

910931
// Check Alice sent the right message to Bob.

src/tests/verified_chats.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,8 @@ async fn test_create_protected_grp_multidev() -> Result<()> {
793793
);
794794

795795
let sent = alice.send_text(group_id, "Hey").await;
796+
// This time shift is necessary to reproduce the bug when the original message is sorted over
797+
// the "protection enabled" message so that these messages have different timestamps.
796798
SystemTime::shift(std::time::Duration::from_secs(3600));
797799
let msg = alice1.recv_msg(&sent).await;
798800
let group1 = Chat::load_from_db(alice1, msg.chat_id).await?;

0 commit comments

Comments
 (0)