test: add function to manually create encrypted messages

This commit is contained in:
link2xt
2026-05-09 19:19:32 +02:00
parent 6fb2f27831
commit 37abfa2c1c
3 changed files with 82 additions and 13 deletions

View File

@@ -42,12 +42,25 @@ impl EncryptHelper {
compress: bool,
seipd_version: SeipdVersion,
) -> Result<String> {
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<SignedPublicKey>,
raw_message: Vec<u8>,
compress: bool,
seipd_version: SeipdVersion,
) -> Result<String> {
let sign_key = load_self_secret_key(context).await?;
let ctext =
pgp::pk_encrypt(raw_message, keyring, sign_key, compress, seipd_version).await?;

View File

@@ -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: <hello@example.net>
To: Alice <alice@example.org>
From: Bob <bob@example.net>
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: <alice@example.org>
@@ -986,10 +991,8 @@ Content-Disposition: attachment; filename="location.kml"
</Document>
</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;

View File

@@ -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,56 @@ ORDER BY id"
}
}
pub async fn encrypt_raw_message(
context: &Context,
receivers: &[&TestContext],
payload: &[u8],
) -> Result<String> {
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 mut cleartext = format!("Autocrypt: {}", encryption_helper.get_aheader()).into_bytes();
cleartext.extend_from_slice(b"\r\n");
cleartext.extend_from_slice(payload);
let encrypted_payload = encryption_helper
.encrypt_raw(
context,
encryption_keyring,
cleartext,
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}
--{boundary}--\r
");
Ok(res)
}
impl Deref for TestContext {
type Target = Context;