diff --git a/src/chat/chat_tests.rs b/src/chat/chat_tests.rs index ee5c62e969..277242158e 100644 --- a/src/chat/chat_tests.rs +++ b/src/chat/chat_tests.rs @@ -1883,10 +1883,6 @@ async fn test_sticker( msg.set_file_and_deduplicate(&alice, &file, Some(filename), None)?; let sent_msg = alice.send_msg(alice_chat.id, &mut msg).await; - let mime = sent_msg.payload(); - if res_viewtype == Viewtype::Sticker { - assert_eq!(mime.match_indices("Chat-Content: sticker").count(), 1); - } let msg = bob.recv_msg(&sent_msg).await; assert_eq!(msg.chat_id, bob_chat.id); diff --git a/src/contact/contact_tests.rs b/src/contact/contact_tests.rs index 3691d5752a..51534e9f6f 100644 --- a/src/contact/contact_tests.rs +++ b/src/contact/contact_tests.rs @@ -1207,16 +1207,16 @@ async fn test_reset_encryption() -> Result<()> { let alice = &tcm.alice().await; let bob = &tcm.bob().await; - let msg = tcm.send_recv_accept(alice, bob, "Hello!").await; - assert_eq!(msg.get_showpadlock(), false); - - let msg = tcm.send_recv(bob, alice, "Hi!").await; + let msg = tcm.send_recv_accept(bob, alice, "Hi!").await; assert_eq!(msg.get_showpadlock(), true); + + let alice_bob_chat_id = msg.chat_id; let alice_bob_contact_id = msg.from_id; alice_bob_contact_id.reset_encryption(alice).await?; - let msg = tcm.send_recv(alice, bob, "Unencrypted").await; + let sent = alice.send_text(alice_bob_chat_id, "Unencrypted").await; + let msg = bob.recv_msg(&sent).await; assert_eq!(msg.get_showpadlock(), false); Ok(()) @@ -1235,6 +1235,7 @@ async fn test_reset_verified_encryption() -> Result<()> { let alice_bob_chat_id = msg.chat_id; let alice_bob_contact_id = msg.from_id; + alice_bob_contact_id.reset_encryption(alice).await?; // Check that the contact is still verified after resetting encryption. @@ -1250,7 +1251,8 @@ async fn test_reset_verified_encryption() -> Result<()> { "bob@example.net sent a message from another device." ); - let msg = tcm.send_recv(alice, bob, "Unencrypted").await; + let sent = alice.send_text(alice_bob_chat_id, "Unencrypted").await; + let msg = bob.recv_msg(&sent).await; assert_eq!(msg.get_showpadlock(), false); Ok(()) diff --git a/src/e2ee.rs b/src/e2ee.rs index 0e7af8324a..4297c74b6b 100644 --- a/src/e2ee.rs +++ b/src/e2ee.rs @@ -228,8 +228,8 @@ Sent with my Delta Chat Messenger: https://delta.chat"; let alice = tcm.alice().await; let bob = tcm.bob().await; - let chat_alice = alice.create_chat(&bob).await.id; - let chat_bob = bob.create_chat(&alice).await.id; + let chat_alice = alice.create_email_chat(&bob).await.id; + let chat_bob = bob.create_email_chat(&alice).await.id; // Alice sends unencrypted message to Bob let mut msg = Message::new(Viewtype::Text); diff --git a/src/mimeparser/mimeparser_tests.rs b/src/mimeparser/mimeparser_tests.rs index 6d80463608..024e213e96 100644 --- a/src/mimeparser/mimeparser_tests.rs +++ b/src/mimeparser/mimeparser_tests.rs @@ -1811,26 +1811,39 @@ async fn test_take_last_header() { ); } -#[tokio::test(flavor = "multi_thread", worker_threads = 2)] -async fn test_protect_autocrypt() -> Result<()> { +async fn test_protect_autocrypt(enabled: bool) -> Result<()> { let mut tcm = TestContextManager::new(); let alice = &tcm.alice().await; let bob = &tcm.bob().await; + let chat = alice.create_chat(bob).await; alice - .set_config_bool(Config::ProtectAutocrypt, true) + .set_config_bool(Config::ProtectAutocrypt, enabled) .await?; - bob.set_config_bool(Config::ProtectAutocrypt, true).await?; - - let msg = tcm.send_recv_accept(alice, bob, "Hello!").await; - assert_eq!(msg.get_showpadlock(), false); - - let msg = tcm.send_recv(bob, alice, "Hi!").await; + let sent = alice.send_text(chat.id, "Hello!").await; + assert_eq!(sent.payload().contains("Autocrypt: "), !enabled); + let msg = bob.recv_msg(&sent).await; assert_eq!(msg.get_showpadlock(), true); Ok(()) } +/// Tests that if `protect_autocrypt` is enabled, +/// `Autocrypt` header does not appear in the outer headers +/// of encrypted messages. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_protect_autocrypt_enabled() -> Result<()> { + test_protect_autocrypt(true).await +} + +/// Tests that if `protect_autocrypt` is disabled, +/// `Autocrypt` header appears in the outer headers +/// of encrypted messages. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn test_protect_autocrypt_false() -> Result<()> { + test_protect_autocrypt(false).await +} + /// Tests that CRLF before MIME boundary /// is not treated as the part body. /// diff --git a/src/receive_imf/receive_imf_tests.rs b/src/receive_imf/receive_imf_tests.rs index 17338f9c14..4df492c679 100644 --- a/src/receive_imf/receive_imf_tests.rs +++ b/src/receive_imf/receive_imf_tests.rs @@ -1,3 +1,4 @@ +use rand::Rng; use std::time::Duration; use tokio::fs; @@ -1894,7 +1895,6 @@ async fn test_save_mime_headers_off() -> anyhow::Result<()> { let msg = bob.recv_msg(&alice.pop_sent_msg().await).await; assert_eq!(msg.get_text(), "hi!"); - assert!(!msg.get_showpadlock()); let mime = message::get_mime_headers(&bob, msg.id).await?; assert!(mime.is_empty()); Ok(()) @@ -4642,7 +4642,14 @@ async fn test_download_later() -> Result<()> { let bob = tcm.bob().await; let bob_chat = bob.create_chat(&alice).await; - let text = String::from_utf8(vec![b'a'; MIN_DOWNLOAD_LIMIT as usize])?; + + // Generate a random string so OpenPGP does not compress it. + let text: String = rand::thread_rng() + .sample_iter(&rand::distributions::Alphanumeric) + .take(MIN_DOWNLOAD_LIMIT as usize) + .map(char::from) + .collect(); + let sent_msg = bob.send_text(bob_chat.id, &text).await; let msg = alice.recv_msg(&sent_msg).await; assert_eq!(msg.download_state, DownloadState::Available); diff --git a/src/securejoin/securejoin_tests.rs b/src/securejoin/securejoin_tests.rs index 3d7b690ab5..afc7fa1188 100644 --- a/src/securejoin/securejoin_tests.rs +++ b/src/securejoin/securejoin_tests.rs @@ -816,8 +816,13 @@ async fn test_shared_bobs_key() -> Result<()> { let bob3_addr = "bob3@example.net"; bob3.configure_addr(bob3_addr).await; imex(bob3, ImexMode::ImportSelfKeys, export_dir.path(), None).await?; - tcm.send_recv(bob3, alice, "hi Alice!").await; - let msg = tcm.send_recv(alice, bob3, "hi Bob3!").await; + let chat = bob3.create_email_chat(alice).await; + let sent = bob3.send_text(chat.id, "hi Alice!").await; + let msg = alice.recv_msg(&sent).await; + assert!(!msg.get_showpadlock()); + let chat = alice.create_email_chat(bob3).await; + let sent = alice.send_text(chat.id, "hi Bob3!").await; + let msg = bob3.recv_msg(&sent).await; assert!(msg.get_showpadlock()); let mut bob_ids = HashSet::new(); diff --git a/src/test_utils.rs b/src/test_utils.rs index 2439031413..de41beb0b7 100644 --- a/src/test_utils.rs +++ b/src/test_utils.rs @@ -29,7 +29,7 @@ use crate::config::Config; use crate::constants::DC_CHAT_ID_TRASH; use crate::constants::DC_GCL_NO_SPECIALS; use crate::constants::{Blocked, Chattype}; -use crate::contact::{Contact, ContactId, Modifier, Origin}; +use crate::contact::{import_vcard, make_vcard, Contact, ContactId, Modifier, Origin}; use crate::context::Context; use crate::e2ee::EncryptHelper; use crate::events::{Event, EventEmitter, EventType, Events}; @@ -715,11 +715,26 @@ impl TestContext { /// Creates or returns an existing 1:1 [`Chat`] with another account. /// - /// This first creates a contact using the configured details on the other account, then - /// creates a 1:1 chat with this contact. + /// This first creates a contact by exporting a vCard from the `other` + /// and importing it into `self`, + /// then creates a 1:1 chat with this contact. pub async fn create_chat(&self, other: &TestContext) -> Chat { + let vcard = make_vcard(other, &[ContactId::SELF]).await.unwrap(); + let contact_ids = import_vcard(self, &vcard).await.unwrap(); + assert_eq!(contact_ids.len(), 1); + let contact_id = contact_ids.first().unwrap(); + let chat_id = ChatId::create_for_contact(self, *contact_id).await.unwrap(); + Chat::load_from_db(self, chat_id).await.unwrap() + } + + /// Creates or returns an existing 1:1 [`Chat`] with another account + /// by email address. + /// + /// This function can be used to create unencrypted chats. + pub async fn create_email_chat(&self, other: &TestContext) -> Chat { let contact = self.add_or_lookup_contact(other).await; let chat_id = ChatId::create_for_contact(self, contact.id).await.unwrap(); + Chat::load_from_db(self, chat_id).await.unwrap() } diff --git a/src/webxdc/webxdc_tests.rs b/src/webxdc/webxdc_tests.rs index 9586082d94..300e620b8f 100644 --- a/src/webxdc/webxdc_tests.rs +++ b/src/webxdc/webxdc_tests.rs @@ -726,7 +726,7 @@ async fn test_send_webxdc_status_update() -> Result<()> { let bob = TestContext::new_bob().await; // Alice sends an webxdc instance and a status update - let alice_chat = alice.create_chat(&bob).await; + let alice_chat = alice.create_email_chat(&bob).await; let alice_instance = send_webxdc_instance(&alice, alice_chat.id).await?; let sent1 = &alice.pop_sent_msg().await; assert_eq!(alice_instance.viewtype, Viewtype::Webxdc); @@ -1022,7 +1022,7 @@ async fn test_pop_status_update() -> Result<()> { async fn test_draft_and_send_webxdc_status_update() -> Result<()> { let alice = TestContext::new_alice().await; let bob = TestContext::new_bob().await; - let alice_chat_id = alice.create_chat(&bob).await.id; + let alice_chat_id = alice.create_email_chat(&bob).await.id; // prepare webxdc instance, // status updates are not sent for drafts, therefore send_webxdc_status_update() returns Ok(None)