From b07e20b955ad822906d150b7deedb6fb5c7aa126 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sun, 19 Sep 2021 03:36:20 +0000 Subject: [PATCH] ephemeral: add failing rollback protection test --- src/ephemeral.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) diff --git a/src/ephemeral.rs b/src/ephemeral.rs index 618d48112..354e1acb0 100644 --- a/src/ephemeral.rs +++ b/src/ephemeral.rs @@ -512,6 +512,7 @@ mod tests { use super::*; use crate::config::Config; + use crate::dc_receive_imf::dc_receive_imf; use crate::download::DownloadState; use crate::test_utils::TestContext; use crate::{ @@ -838,4 +839,95 @@ mod tests { Ok(()) } + + // Regression test for a bug in the timer rollback protection. + #[async_std::test] + async fn test_ephemeral_timer_references() -> Result<()> { + let alice = TestContext::new_alice().await; + + // Message with Message-ID and no timer is received. + dc_receive_imf( + &alice, + b"From: Bob \n\ + To: Alice \n\ + Chat-Version: 1.0\n\ + Subject: Subject\n\ + Message-ID: \n\ + Date: Sun, 22 Mar 2020 00:10:00 +0000\n\ + \n\ + hello\n", + "INBOX", + 1, + false, + ) + .await?; + + let msg = alice.get_last_msg().await; + let chat_id = msg.chat_id; + assert_eq!(chat_id.get_ephemeral_timer(&alice).await?, Timer::Disabled); + + // Message with Message-ID is received. + dc_receive_imf( + &alice, + b"From: Bob \n\ + To: Alice \n\ + Chat-Version: 1.0\n\ + Subject: Subject\n\ + Message-ID: \n\ + Date: Sun, 22 Mar 2020 00:11:00 +0000\n\ + Ephemeral-Timer: 60\n\ + \n\ + second message\n", + "INBOX", + 2, + false, + ) + .await?; + assert_eq!( + chat_id.get_ephemeral_timer(&alice).await?, + Timer::Enabled { duration: 60 } + ); + let msg = alice.get_last_msg().await; + + // Message is deleted from the database when its timer expires. + msg.id.delete_from_db(&alice).await?; + + // Message with Message-ID , referencing and + // , is received. The message is not in the + // database anymore, so the timer should be applied unconditionally without rollback + // protection. + // + // Previously Delta Chat fallen back to using in this case and + // compared received timer value to the timer value of the . Because + // their timer values are the same ("disabled"), Delta Chat assumed that the timer was not + // changed explicitly and the change should be ignored. + // + // The message also contains a quote of the first message to test that only References: + // header and not In-Reply-To: is consulted by the rollback protection. + dc_receive_imf( + &alice, + b"From: Bob \n\ + To: Alice \n\ + Chat-Version: 1.0\n\ + Subject: Subject\n\ + Message-ID: \n\ + Date: Sun, 22 Mar 2020 00:12:00 +0000\n\ + References: \n\ + In-Reply-To: \n\ + \n\ + > hello\n", + "INBOX", + 3, + false, + ) + .await?; + + let msg = alice.get_last_msg().await; + assert_eq!( + msg.chat_id.get_ephemeral_timer(&alice).await?, + Timer::Disabled + ); + + Ok(()) + } }