From 84bc3f8baeaa16e7ffe92c38ecaed0af559ec4d5 Mon Sep 17 00:00:00 2001 From: link2xt Date: Sat, 9 May 2026 19:19:32 +0200 Subject: [PATCH] test: add function to manually create encrypted messages --- src/e2ee.rs | 17 ++++++++++++++-- src/location.rs | 25 +++++++++++++----------- src/test_utils.rs | 50 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 13 deletions(-) diff --git a/src/e2ee.rs b/src/e2ee.rs index 9a1f639bf..0ca08e1be 100644 --- a/src/e2ee.rs +++ b/src/e2ee.rs @@ -42,12 +42,25 @@ impl EncryptHelper { compress: bool, seipd_version: SeipdVersion, ) -> Result { - let sign_key = load_self_secret_key(context).await?; - let mut raw_message = Vec::new(); let cursor = Cursor::new(&mut raw_message); mail_to_encrypt.clone().write_part(cursor).ok(); + let ctext = self + .encrypt_raw(context, keyring, raw_message, compress, seipd_version) + .await?; + Ok(ctext) + } + + pub async fn encrypt_raw( + self, + context: &Context, + keyring: Vec, + raw_message: Vec, + compress: bool, + seipd_version: SeipdVersion, + ) -> Result { + let sign_key = load_self_secret_key(context).await?; let ctext = pgp::pk_encrypt(raw_message, keyring, sign_key, compress, seipd_version).await?; diff --git a/src/location.rs b/src/location.rs index ac0ff2a47..51d67d65e 100644 --- a/src/location.rs +++ b/src/location.rs @@ -871,6 +871,7 @@ mod tests { use crate::config::Config; use crate::message::MessageState; use crate::receive_imf::receive_imf; + use crate::test_utils; use crate::test_utils::{ExpectedEvents, TestContext, TestContextManager}; use crate::tools::SystemTime; @@ -939,12 +940,15 @@ mod tests { /// Tests that location.kml is hidden. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn receive_location_kml() -> Result<()> { - let alice = TestContext::new_alice().await; + let mut tcm = TestContextManager::new(); + let alice = &tcm.alice().await; + let bob = &tcm.bob().await; - receive_imf( - &alice, + let encrypted_message = test_utils::encrypt_raw_message( + bob, + &[alice], br#"Subject: Hello -Message-ID: hello@example.net +Message-ID: To: Alice From: Bob Date: Mon, 20 Dec 2021 00:00:00 +0000 @@ -952,14 +956,15 @@ Chat-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no Text message."#, - false, ) .await?; + receive_imf(alice, encrypted_message.as_bytes(), false).await?; let received_msg = alice.get_last_msg().await; assert_eq!(received_msg.text, "Text message."); - receive_imf( - &alice, + let encrypted_message = test_utils::encrypt_raw_message( + bob, + &[alice], br#"Subject: locations MIME-Version: 1.0 To: @@ -986,10 +991,8 @@ Content-Disposition: attachment; filename="location.kml" ---U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF--"#, - false, - ) - .await?; +--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF--"#).await?; + receive_imf(alice, encrypted_message.as_bytes(), false).await?; // Received location message is not visible, last message stays the same. let received_msg2 = alice.get_last_msg().await; diff --git a/src/test_utils.rs b/src/test_utils.rs index 94bdce166..2f0d920f7 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -20,6 +20,7 @@ use pretty_assertions::assert_eq; use tempfile::{TempDir, tempdir}; use tokio::runtime::Handle; use tokio::{fs, task}; +use uuid::Uuid; use crate::chat::{ self, Chat, ChatId, ChatIdBlocked, MessageListOptions, add_to_chat_contacts_table, create_group, @@ -32,12 +33,14 @@ use crate::contact::{ Contact, ContactId, Modifier, Origin, import_vcard, make_vcard, mark_contact_id_as_verified, }; use crate::context::Context; +use crate::e2ee::EncryptHelper; use crate::events::{Event, EventEmitter, EventType, Events}; use crate::key::{self, DcKey, self_fingerprint}; use crate::log::warn; use crate::login_param::EnteredLoginParam; use crate::message::{Message, MessageState, MsgId, update_msg_state}; use crate::mimeparser::{MimeMessage, SystemMessage}; +use crate::pgp::SeipdVersion; use crate::receive_imf::receive_imf; use crate::securejoin::{get_securejoin_qr, join_securejoin}; use crate::smtp::msg_has_pending_smtp_job; @@ -1214,6 +1217,53 @@ ORDER BY id" } } +pub async fn encrypt_raw_message( + context: &Context, + receivers: &[&TestContext], + payload: &[u8], +) -> Result { + let encryption_helper = EncryptHelper::new(context).await?; + let mut encryption_keyring = vec![encryption_helper.public_key.clone()]; + + for receiver in receivers { + encryption_keyring.push(key::load_self_public_key(receiver).await?); + } + + let from = context.get_primary_self_addr().await?; + let compress = false; + let encrypted_payload = encryption_helper + .encrypt_raw( + context, + encryption_keyring, + payload.to_vec(), + compress, + SeipdVersion::V2, + ) + .await?; + let boundary = Uuid::new_v4(); + + let res = format!( +"Content-Type: multipart/encrypted; protocol=\"application/pgp-encrypted\"; boundary=\"{boundary}\"\r +MIME-Version: 1.0\r +From: {from}\r +Subject: [...]\r +\r +\r +--{boundary} +Content-Type: application/pgp-encrypted\r +\r +Version: 1\r +\r +--{boundary}\r +Content-Type: application/octet-stream\r +\r +{encrypted_payload} +\r +--{boundary}-- +"); + Ok(res) +} + impl Deref for TestContext { type Target = Context;