test: use encrypted messages in more tests

This change is a preparation for ignoring
unencrypted messages by default.

New test_utils::encrypt_raw_message and
test_utils::receive_encrypted_imf are
used to encrypt the messages before
"receiving" them with receive_imf.
This commit is contained in:
link2xt
2026-05-09 19:19:32 +02:00
parent ac3c5c55e4
commit e5cba4c73b
24 changed files with 1086 additions and 984 deletions

View File

@@ -9,6 +9,7 @@ use crate::download::DownloadState;
use crate::location;
use crate::message::markseen_msgs;
use crate::receive_imf::receive_imf;
use crate::test_utils;
use crate::test_utils::{TestContext, TestContextManager};
use crate::timesmearing::MAX_SECONDS_TO_LEND_FROM_FUTURE;
use crate::{
@@ -310,20 +311,22 @@ async fn test_ephemeral_timer_rollback() -> Result<()> {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_ephemeral_delete_msgs() -> Result<()> {
let t = TestContext::new_alice().await;
let mut tcm = TestContextManager::new();
let t = &tcm.alice().await;
let bob = &tcm.bob().await;
let self_chat = t.get_self_chat().await;
assert_eq!(next_expiration_timestamp(&t).await, None);
assert_eq!(next_expiration_timestamp(t).await, None);
t.send_text(self_chat.id, "Saved message, which we delete manually")
.await;
let msg = t.get_last_msg_in(self_chat.id).await;
msg.id.trash(&t, false).await?;
check_msg_is_deleted(&t, &self_chat, msg.id).await;
msg.id.trash(t, false).await?;
check_msg_is_deleted(t, &self_chat, msg.id).await;
self_chat
.id
.set_ephemeral_timer(&t, Timer::Enabled { duration: 3600 })
.set_ephemeral_timer(t, Timer::Enabled { duration: 3600 })
.await
.unwrap();
@@ -331,7 +334,7 @@ async fn test_ephemeral_delete_msgs() -> Result<()> {
let now = time();
let msg = t.send_text(self_chat.id, "Message text").await;
check_msg_will_be_deleted(&t, msg.sender_msg_id, &self_chat, now + 3599, time() + 3601)
check_msg_will_be_deleted(t, msg.sender_msg_id, &self_chat, now + 3599, time() + 3601)
.await
.unwrap();
@@ -343,17 +346,17 @@ async fn test_ephemeral_delete_msgs() -> Result<()> {
let now = time();
let msg = t.send_text(self_chat.id, "Message text").await;
check_msg_will_be_deleted(&t, msg.sender_msg_id, &self_chat, now + 3559, time() + 3601)
check_msg_will_be_deleted(t, msg.sender_msg_id, &self_chat, now + 3559, time() + 3601)
.await
.unwrap();
// Send a message to Bob which will be deleted after 1800s because of DeleteDeviceAfter.
let bob_chat = t.create_chat_with_contact("", "bob@example.net").await;
let bob_chat = t.create_chat(bob).await;
let now = time();
let msg = t.send_text(bob_chat.id, "Message text").await;
check_msg_will_be_deleted(
&t,
t,
msg.sender_msg_id,
&bob_chat,
now + 1799,
@@ -368,13 +371,13 @@ async fn test_ephemeral_delete_msgs() -> Result<()> {
// This tests that the message is deleted at min(ephemeral deletion time, DeleteDeviceAfter deletion time).
bob_chat
.id
.set_ephemeral_timer(&t, Timer::Enabled { duration: 60 })
.set_ephemeral_timer(t, Timer::Enabled { duration: 60 })
.await?;
let now = time();
let msg = t.send_text(bob_chat.id, "Message text").await;
check_msg_will_be_deleted(&t, msg.sender_msg_id, &bob_chat, now + 59, time() + 61)
check_msg_will_be_deleted(t, msg.sender_msg_id, &bob_chat, now + 59, time() + 61)
.await
.unwrap();
@@ -557,50 +560,54 @@ async fn test_delete_expired_imap_messages() -> Result<()> {
// Regression test for a bug in the timer rollback protection.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_ephemeral_timer_references() -> Result<()> {
let alice = TestContext::new_alice().await;
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;
// Message with Message-ID <first@example.com> and no timer is received.
receive_imf(
&alice,
b"From: Bob <bob@example.com>\n\
To: Alice <alice@example.org>\n\
Chat-Version: 1.0\n\
Subject: Subject\n\
Message-ID: <first@example.com>\n\
Date: Sun, 22 Mar 2020 00:10:00 +0000\n\
\n\
hello\n",
false,
let encrypted_msg = test_utils::encrypt_raw_message(
bob,
&[alice],
b"From: Bob <bob@example.net>\r\n\
To: Alice <alice@example.org>\r\n\
Chat-Version: 1.0\r\n\
Subject: Subject\r\n\
Message-ID: <first@example.com>\r\n\
Date: Sun, 22 Mar 2020 00:10:00 +0000\r\n\
\r\n\
hello\r\n",
)
.await?;
receive_imf(alice, encrypted_msg.as_bytes(), 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);
assert_eq!(chat_id.get_ephemeral_timer(alice).await?, Timer::Disabled);
// Message with Message-ID <second@example.com> is received.
receive_imf(
&alice,
b"From: Bob <bob@example.com>\n\
To: Alice <alice@example.org>\n\
Chat-Version: 1.0\n\
Subject: Subject\n\
Message-ID: <second@example.com>\n\
Date: Sun, 22 Mar 2020 00:11:00 +0000\n\
Ephemeral-Timer: 60\n\
\n\
second message\n",
false,
let encrypted_msg = test_utils::encrypt_raw_message(
bob,
&[alice],
b"From: Bob <bob@example.net>\r\n\
To: Alice <alice@example.org>\r\n\
Chat-Version: 1.0\r\n\
Subject: Subject\r\n\
Message-ID: <second@example.com>\r\n\
Date: Sun, 22 Mar 2020 00:11:00 +0000\r\n\
Ephemeral-Timer: 60\r\n\
\r\n\
second message\r\n",
)
.await?;
receive_imf(alice, encrypted_msg.as_bytes(), false).await?;
assert_eq!(
chat_id.get_ephemeral_timer(&alice).await?,
chat_id.get_ephemeral_timer(alice).await?,
Timer::Enabled { duration: 60 }
);
let msg = alice.get_last_msg().await;
// Message is deleted when its timer expires.
msg.id.trash(&alice, false).await?;
msg.id.trash(alice, false).await?;
// Message with Message-ID <third@example.com>, referencing <first@example.com> and
// <second@example.com>, is received. The message <second@example.come> is not in the
@@ -614,25 +621,26 @@ async fn test_ephemeral_timer_references() -> Result<()> {
//
// 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.
receive_imf(
&alice,
b"From: Bob <bob@example.com>\n\
To: Alice <alice@example.org>\n\
Chat-Version: 1.0\n\
Subject: Subject\n\
Message-ID: <third@example.com>\n\
Date: Sun, 22 Mar 2020 00:12:00 +0000\n\
References: <first@example.com> <second@example.com>\n\
In-Reply-To: <first@example.com>\n\
\n\
> hello\n",
false,
let encrypted_msg = test_utils::encrypt_raw_message(
bob,
&[alice],
b"From: Bob <bob@example.net>\r\n\
To: Alice <alice@example.org>\r\n\
Chat-Version: 1.0\r\n\
Subject: Subject\r\n\
Message-ID: <third@example.com>\r\n\
Date: Sun, 22 Mar 2020 00:12:00 +0000\r\n\
References: <first@example.com> <second@example.com>\r\n\
In-Reply-To: <first@example.com>\r\n\
\r\n\
> hello\r\n",
)
.await?;
receive_imf(alice, encrypted_msg.as_bytes(), false).await?;
let msg = alice.get_last_msg().await;
assert_eq!(
msg.chat_id.get_ephemeral_timer(&alice).await?,
msg.chat_id.get_ephemeral_timer(alice).await?,
Timer::Disabled
);
@@ -644,24 +652,20 @@ async fn test_ephemeral_timer_references() -> Result<()> {
// successful reconnection.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_ephemeral_msg_offline() -> Result<()> {
let alice = TestContext::new_alice().await;
let chat = alice
.create_chat_with_contact("Bob", "bob@example.org")
.await;
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;
let chat = alice.create_chat(bob).await;
let duration = 60;
chat.id
.set_ephemeral_timer(&alice, Timer::Enabled { duration })
.set_ephemeral_timer(alice, Timer::Enabled { duration })
.await?;
let mut msg = Message::new_text("hi".to_string());
assert!(
chat::send_msg_sync(&alice, chat.id, &mut msg)
.await
.is_err()
);
assert!(chat::send_msg_sync(alice, chat.id, &mut msg).await.is_err());
let stmt = "SELECT COUNT(*) FROM smtp WHERE msg_id=?";
assert!(alice.sql.exists(stmt, (msg.id,)).await?);
let now = time();
check_msg_will_be_deleted(&alice, msg.id, &chat, now, now + i64::from(duration) + 1).await?;
check_msg_will_be_deleted(alice, msg.id, &chat, now, now + i64::from(duration) + 1).await?;
assert!(alice.sql.exists(stmt, (msg.id,)).await?);
Ok(())