Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 32 additions & 26 deletions src/calls/calls_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::config::Config;
use crate::constants::DC_CHAT_ID_TRASH;
use crate::message::MessageState;
use crate::receive_imf::receive_imf;
use crate::test_utils;
use crate::test_utils::{TestContext, TestContextManager};

struct CallSetup {
Expand Down Expand Up @@ -634,20 +635,23 @@ async fn test_forward_call() -> Result<()> {
async fn test_end_text_call() -> Result<()> {
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;

let received1 = receive_imf(
alice,
b"From: bob@example.net\n\
To: alice@example.org\n\
Message-ID: <first@example.net>\n\
Date: Sun, 22 Mar 2020 22:37:57 +0000\n\
Chat-Version: 1.0\n\
\n\
Hello\n",
false,
let encrypted_message = test_utils::encrypt_raw_message(
bob,
&[alice],
b"From: bob@example.net\r\n\
To: alice@example.org\r\n\
Message-ID: <first@example.net>\r\n\
Date: Sun, 22 Mar 2020 22:37:57 +0000\r\n\
Chat-Version: 1.0\r\n\
\r\n\
Hello\r\n",
)
.await?
.unwrap();
.await?;
let received1 = receive_imf(alice, encrypted_message.as_bytes(), false)
.await?
.unwrap();
assert_eq!(received1.msg_ids.len(), 1);
let msg = Message::load_from_db(alice, received1.msg_ids[0])
.await
Expand All @@ -656,21 +660,23 @@ async fn test_end_text_call() -> Result<()> {

// Receiving "Call ended" message that refers
// to the text message does not result in an error.
let received2 = receive_imf(
alice,
b"From: bob@example.net\n\
To: alice@example.org\n\
Message-ID: <second@example.net>\n\
Date: Sun, 22 Mar 2020 23:37:57 +0000\n\
In-Reply-To: <first@example.net>\n\
Chat-Version: 1.0\n\
Chat-Content: call-ended\n\
\n\
Call ended\n",
false,
let encrypted_message2 = test_utils::encrypt_raw_message(
bob,
&[alice],
b"From: bob@example.net\r\n\
To: alice@example.org\r\n\
Message-ID: <second@example.net>\r\n\
Date: Sun, 22 Mar 2020 23:37:57 +0000\r\n\
In-Reply-To: <first@example.net>\r\n\
Chat-Version: 1.0\r\n\
Chat-Content: call-ended\r\n\
\r\n\
Call ended\r\n",
)
.await?
.unwrap();
.await?;
let received2 = receive_imf(alice, encrypted_message2.as_bytes(), false)
.await?
.unwrap();
assert_eq!(received2.msg_ids.len(), 1);
assert_eq!(received2.chat_id, DC_CHAT_ID_TRASH);

Expand Down
99 changes: 52 additions & 47 deletions src/chat/chat_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1049,6 +1049,7 @@ async fn chatlist_len(ctx: &Context, listflags: usize) -> usize {
async fn test_archive() {
// create two chats
let t = TestContext::new_alice().await;

let mut msg = Message::new_text("foo".to_string());
let msg_id = add_device_msg(&t, None, Some(&mut msg)).await.unwrap();
let chat_id1 = message::Message::load_from_db(&t, msg_id)
Expand Down Expand Up @@ -1381,6 +1382,9 @@ async fn test_markfresh_chat() -> Result<()> {
async fn test_archive_fresh_msgs() -> Result<()> {
let t = TestContext::new_alice().await;

// FIXME: use encrypted messages
t.set_config(Config::ProcessUnencrypted, Some("1")).await?;

async fn msg_from(t: &TestContext, name: &str, num: u32) -> Result<()> {
receive_imf(
t,
Expand Down Expand Up @@ -1873,52 +1877,46 @@ async fn test_lookup_self_by_contact_id() {

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_marknoticed_chat() -> Result<()> {
let t = TestContext::new_alice().await;
let chat = t.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;

receive_imf(
&t,
b"From: bob@example.org\n\
To: alice@example.org\n\
Message-ID: <1@example.org>\n\
Chat-Version: 1.0\n\
Date: Fri, 23 Apr 2021 10:00:57 +0000\n\
\n\
hello\n",
false,
)
.await?;
let bob_chat_id = bob.create_chat_id(alice).await;
let sent = bob.send_text(bob_chat_id, "hello").await;
alice.recv_msg(&sent).await;

let chats = Chatlist::try_load(&t, 0, None, None).await?;
let chats = Chatlist::try_load(alice, 0, None, None).await?;
assert_eq!(chats.len(), 1);
assert_eq!(chats.get_chat_id(0)?, chat.id);
assert_eq!(chat.id.get_fresh_msg_cnt(&t).await?, 1);
assert_eq!(t.get_fresh_msgs().await?.len(), 1);
assert_eq!(chat.id.get_fresh_msg_cnt(alice).await?, 1);
assert_eq!(alice.get_fresh_msgs().await?.len(), 1);

let msgs = get_chat_msgs(&t, chat.id).await?;
assert_eq!(msgs.len(), 1);
let msg_id = match msgs.first().unwrap() {
let msgs = get_chat_msgs(alice, chat.id).await?;
assert_eq!(msgs.len(), 2);
let msg_id = match msgs.last().unwrap() {
ChatItem::Message { msg_id } => *msg_id,
_ => MsgId::new_unset(),
};
let msg = message::Message::load_from_db(&t, msg_id).await?;
let msg = message::Message::load_from_db(alice, msg_id).await?;
assert_eq!(msg.state, MessageState::InFresh);

marknoticed_chat(&t, chat.id).await?;
marknoticed_chat(alice, chat.id).await?;

let chats = Chatlist::try_load(&t, 0, None, None).await?;
let chats = Chatlist::try_load(alice, 0, None, None).await?;
assert_eq!(chats.len(), 1);
let msg = message::Message::load_from_db(&t, msg_id).await?;
let msg = message::Message::load_from_db(alice, msg_id).await?;
assert_eq!(msg.state, MessageState::InNoticed);
assert_eq!(chat.id.get_fresh_msg_cnt(&t).await?, 0);
assert_eq!(t.get_fresh_msgs().await?.len(), 0);
assert_eq!(chat.id.get_fresh_msg_cnt(alice).await?, 0);
assert_eq!(alice.get_fresh_msgs().await?.len(), 0);

Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_contact_request_fresh_messages() -> Result<()> {
let t = TestContext::new_alice().await;
t.set_config(Config::ProcessUnencrypted, Some("1")).await?;

let chats = Chatlist::try_load(&t, 0, None, None).await?;
assert_eq!(chats.len(), 0);
Expand Down Expand Up @@ -1970,47 +1968,53 @@ async fn test_contact_request_fresh_messages() -> Result<()> {

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_contact_request_archive() -> Result<()> {
let t = TestContext::new_alice().await;
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;

receive_imf(
&t,
b"From: bob@example.org\n\
To: alice@example.org\n\
Message-ID: <2@example.org>\n\
Chat-Version: 1.0\n\
Date: Sun, 22 Mar 2021 19:37:57 +0000\n\
\n\
hello\n",
false,
)
.await?;
let bob_chat_id = bob.create_chat_id(alice).await;
let bob_sent_text = bob.send_text(bob_chat_id, "hello").await;
alice.recv_msg(&bob_sent_text).await;

let chats = Chatlist::try_load(&t, 0, None, None).await?;
let chats = Chatlist::try_load(alice, 0, None, None).await?;
assert_eq!(chats.len(), 1);
let chat_id = chats.get_chat_id(0)?;
assert!(Chat::load_from_db(&t, chat_id).await?.is_contact_request());
assert_eq!(get_archived_cnt(&t).await?, 0);
assert!(
Chat::load_from_db(alice, chat_id)
.await?
.is_contact_request()
);
assert_eq!(get_archived_cnt(alice).await?, 0);

// archive request without accepting or blocking
chat_id.set_visibility(&t, ChatVisibility::Archived).await?;
chat_id
.set_visibility(alice, ChatVisibility::Archived)
.await?;

let chats = Chatlist::try_load(&t, 0, None, None).await?;
let chats = Chatlist::try_load(alice, 0, None, None).await?;
assert_eq!(chats.len(), 1);
let chat_id = chats.get_chat_id(0)?;
assert!(chat_id.is_archived_link());
assert_eq!(get_archived_cnt(&t).await?, 1);
assert_eq!(get_archived_cnt(alice).await?, 1);

let chats = Chatlist::try_load(&t, DC_GCL_ARCHIVED_ONLY, None, None).await?;
let chats = Chatlist::try_load(alice, DC_GCL_ARCHIVED_ONLY, None, None).await?;
assert_eq!(chats.len(), 1);
let chat_id = chats.get_chat_id(0)?;
assert!(Chat::load_from_db(&t, chat_id).await?.is_contact_request());
assert!(
Chat::load_from_db(alice, chat_id)
.await?
.is_contact_request()
);

Ok(())
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_classic_email_chat() -> Result<()> {
let alice = TestContext::new_alice().await;
alice
.set_config(Config::ProcessUnencrypted, Some("1"))
.await?;

// Alice receives a classic (non-chat) message from Bob.
receive_imf(
Expand Down Expand Up @@ -6070,6 +6074,7 @@ async fn test_no_key_contacts_in_adhoc_chats() -> Result<()> {
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;
let charlie = &tcm.charlie().await;
alice.set_config_bool(Config::ProcessUnencrypted, true).await?;

let chat_id = receive_imf(
alice,
Expand Down
3 changes: 3 additions & 0 deletions src/chatlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ mod tests {
add_contact_to_chat, create_broadcast, create_group, get_chat_contacts,
remove_contact_from_chat, send_text_msg, set_chat_name,
};
use crate::config::Config;
use crate::receive_imf::receive_imf;
use crate::securejoin::get_securejoin_qr;
use crate::stock_str::StockMessage;
Expand Down Expand Up @@ -665,6 +666,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_search_single_chat() -> anyhow::Result<()> {
let t = TestContext::new_alice().await;
t.set_config(Config::ProcessUnencrypted, Some("1")).await?;

// receive a one-to-one-message
receive_imf(
Expand Down Expand Up @@ -725,6 +727,7 @@ mod tests {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_search_single_chat_without_authname() -> anyhow::Result<()> {
let t = TestContext::new_alice().await;
t.set_config(Config::ProcessUnencrypted, Some("1")).await?;

// receive a one-to-one-message without authname set
receive_imf(
Expand Down
5 changes: 5 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,11 @@ pub enum Config {
/// Experimental option denoting that the current profile is shared between multiple team members.
/// For now, the only effect of this option is that seen flags are not synchronized.
TeamProfile,

/// Process unencrypted messages.
///
/// Unencrypted messages are fetched and processed only if this setting is explicitly enabled.
ProcessUnencrypted,
}

impl Config {
Expand Down
8 changes: 8 additions & 0 deletions src/contact/contact_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -927,9 +927,17 @@ async fn test_synchronize_status() -> Result<()> {
// Alice has two devices.
let alice1 = &tcm.alice().await;
let alice2 = &tcm.alice().await;
alice1
.set_config_bool(Config::ProcessUnencrypted, true)
.await?;
alice2
.set_config_bool(Config::ProcessUnencrypted, true)
.await?;

// Bob has one device.
let bob = &tcm.bob().await;
bob.set_config_bool(Config::ProcessUnencrypted, true)
.await?;

let default_status = alice1.get_config(Config::Selfstatus).await?;

Expand Down
6 changes: 6 additions & 0 deletions src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,12 @@ impl Context {
"team_profile",
self.get_config_bool(Config::TeamProfile).await?.to_string(),
);
res.insert(
"process_unencrypted",
self.get_config_bool(Config::ProcessUnencrypted)
.await?
.to_string(),
);

let elapsed = time_elapsed(&self.creation_time);
res.insert("uptime", duration_to_str(elapsed));
Expand Down
5 changes: 5 additions & 0 deletions src/decrypt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ pub(crate) fn validate_detached_signature<'a, 'b>(
#[cfg(test)]
mod tests {
use super::*;
use crate::config::Config;
use crate::receive_imf::receive_imf;
use crate::test_utils::TestContext;

Expand Down Expand Up @@ -402,6 +403,8 @@ mod tests {
assert!(get_attachment_mime(&mail).is_some());

let bob = TestContext::new_bob().await;
bob.set_config(Config::ProcessUnencrypted, Some("1"))
.await?;
receive_imf(&bob, attachment_mime, false).await?;
let msg = bob.get_last_msg().await;
// Subject should be prepended because the attachment doesn't have "Chat-Version".
Expand All @@ -416,6 +419,8 @@ mod tests {
// Desktop via MS Exchange (actually made with TB though).
let mixed_up_mime = include_bytes!("../test-data/message/mixed-up-long.eml");
let bob = TestContext::new_bob().await;
bob.set_config(Config::ProcessUnencrypted, Some("1"))
.await?;
receive_imf(&bob, mixed_up_mime, false).await?;
let msg = bob.get_last_msg().await;
assert!(!msg.get_text().is_empty());
Expand Down
19 changes: 17 additions & 2 deletions src/e2ee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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?;

Expand Down Expand Up @@ -147,6 +160,8 @@ Sent with my Delta Chat Messenger: https://delta.chat";
let mut tcm = TestContextManager::new();
let bob = &tcm.bob().await;
bob.set_config_bool(Config::IsChatmail, true).await?;
bob.set_config_bool(Config::ProcessUnencrypted, true)
.await?;
let bob_chat_id = receive_imf(
bob,
b"From: alice@example.org\n\
Expand Down
Loading
Loading