mirror of
https://github.com/chatmail/core.git
synced 2026-05-11 10:56:29 +03:00
test: add function to manually create encrypted messages
This commit is contained in:
17
src/e2ee.rs
17
src/e2ee.rs
@@ -42,12 +42,25 @@ impl EncryptHelper {
|
|||||||
compress: bool,
|
compress: bool,
|
||||||
seipd_version: SeipdVersion,
|
seipd_version: SeipdVersion,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
let sign_key = load_self_secret_key(context).await?;
|
|
||||||
|
|
||||||
let mut raw_message = Vec::new();
|
let mut raw_message = Vec::new();
|
||||||
let cursor = Cursor::new(&mut raw_message);
|
let cursor = Cursor::new(&mut raw_message);
|
||||||
mail_to_encrypt.clone().write_part(cursor).ok();
|
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 =
|
let ctext =
|
||||||
pgp::pk_encrypt(raw_message, keyring, sign_key, compress, seipd_version).await?;
|
pgp::pk_encrypt(raw_message, keyring, sign_key, compress, seipd_version).await?;
|
||||||
|
|
||||||
|
|||||||
@@ -871,6 +871,7 @@ mod tests {
|
|||||||
use crate::config::Config;
|
use crate::config::Config;
|
||||||
use crate::message::MessageState;
|
use crate::message::MessageState;
|
||||||
use crate::receive_imf::receive_imf;
|
use crate::receive_imf::receive_imf;
|
||||||
|
use crate::test_utils;
|
||||||
use crate::test_utils::{ExpectedEvents, TestContext, TestContextManager};
|
use crate::test_utils::{ExpectedEvents, TestContext, TestContextManager};
|
||||||
use crate::tools::SystemTime;
|
use crate::tools::SystemTime;
|
||||||
|
|
||||||
@@ -939,12 +940,15 @@ mod tests {
|
|||||||
/// Tests that location.kml is hidden.
|
/// Tests that location.kml is hidden.
|
||||||
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
|
||||||
async fn receive_location_kml() -> Result<()> {
|
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(
|
let encrypted_message = test_utils::encrypt_raw_message(
|
||||||
&alice,
|
bob,
|
||||||
|
&[alice],
|
||||||
br#"Subject: Hello
|
br#"Subject: Hello
|
||||||
Message-ID: hello@example.net
|
Message-ID: <hello@example.net>
|
||||||
To: Alice <alice@example.org>
|
To: Alice <alice@example.org>
|
||||||
From: Bob <bob@example.net>
|
From: Bob <bob@example.net>
|
||||||
Date: Mon, 20 Dec 2021 00:00:00 +0000
|
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
|
Content-Type: text/plain; charset=utf-8; format=flowed; delsp=no
|
||||||
|
|
||||||
Text message."#,
|
Text message."#,
|
||||||
false,
|
|
||||||
)
|
)
|
||||||
.await?;
|
.await?;
|
||||||
|
receive_imf(alice, encrypted_message.as_bytes(), false).await?;
|
||||||
let received_msg = alice.get_last_msg().await;
|
let received_msg = alice.get_last_msg().await;
|
||||||
assert_eq!(received_msg.text, "Text message.");
|
assert_eq!(received_msg.text, "Text message.");
|
||||||
|
|
||||||
receive_imf(
|
let encrypted_message = test_utils::encrypt_raw_message(
|
||||||
&alice,
|
bob,
|
||||||
|
&[alice],
|
||||||
br#"Subject: locations
|
br#"Subject: locations
|
||||||
MIME-Version: 1.0
|
MIME-Version: 1.0
|
||||||
To: <alice@example.org>
|
To: <alice@example.org>
|
||||||
@@ -986,10 +991,8 @@ Content-Disposition: attachment; filename="location.kml"
|
|||||||
</Document>
|
</Document>
|
||||||
</kml>
|
</kml>
|
||||||
|
|
||||||
--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF--"#,
|
--U8BOG8qNXfB0GgLiQ3PKUjlvdIuLRF--"#).await?;
|
||||||
false,
|
receive_imf(alice, encrypted_message.as_bytes(), false).await?;
|
||||||
)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// Received location message is not visible, last message stays the same.
|
// Received location message is not visible, last message stays the same.
|
||||||
let received_msg2 = alice.get_last_msg().await;
|
let received_msg2 = alice.get_last_msg().await;
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ use pretty_assertions::assert_eq;
|
|||||||
use tempfile::{TempDir, tempdir};
|
use tempfile::{TempDir, tempdir};
|
||||||
use tokio::runtime::Handle;
|
use tokio::runtime::Handle;
|
||||||
use tokio::{fs, task};
|
use tokio::{fs, task};
|
||||||
|
use uuid::Uuid;
|
||||||
|
|
||||||
use crate::chat::{
|
use crate::chat::{
|
||||||
self, Chat, ChatId, ChatIdBlocked, MessageListOptions, add_to_chat_contacts_table, create_group,
|
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,
|
Contact, ContactId, Modifier, Origin, import_vcard, make_vcard, mark_contact_id_as_verified,
|
||||||
};
|
};
|
||||||
use crate::context::Context;
|
use crate::context::Context;
|
||||||
|
use crate::e2ee::EncryptHelper;
|
||||||
use crate::events::{Event, EventEmitter, EventType, Events};
|
use crate::events::{Event, EventEmitter, EventType, Events};
|
||||||
use crate::key::{self, DcKey, self_fingerprint};
|
use crate::key::{self, DcKey, self_fingerprint};
|
||||||
use crate::log::warn;
|
use crate::log::warn;
|
||||||
use crate::login_param::EnteredLoginParam;
|
use crate::login_param::EnteredLoginParam;
|
||||||
use crate::message::{Message, MessageState, MsgId, update_msg_state};
|
use crate::message::{Message, MessageState, MsgId, update_msg_state};
|
||||||
use crate::mimeparser::{MimeMessage, SystemMessage};
|
use crate::mimeparser::{MimeMessage, SystemMessage};
|
||||||
|
use crate::pgp::SeipdVersion;
|
||||||
use crate::receive_imf::receive_imf;
|
use crate::receive_imf::receive_imf;
|
||||||
use crate::securejoin::{get_securejoin_qr, join_securejoin};
|
use crate::securejoin::{get_securejoin_qr, join_securejoin};
|
||||||
use crate::smtp::msg_has_pending_smtp_job;
|
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<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 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 {
|
impl Deref for TestContext {
|
||||||
type Target = Context;
|
type Target = Context;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user