Skip to content

Commit

Permalink
test: transfer vCards in TestContext.create_chat()
Browse files Browse the repository at this point in the history
SecureJoin and importing a vCard are the primary
ways we want to support for creating contacts.
Typing in an email address and relying on Autocrypt
results in sending the first message unencrypted
and we want to clearly separate unencrypted and encrypted
chats in the future.

To make the tests more stable, we set up test contacts
with vCards as this always immediately
results in creating a single encrypted chat
and this is not going to change.
  • Loading branch information
link2xt committed Mar 5, 2025
1 parent 075889e commit 3efa105
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 30 deletions.
4 changes: 0 additions & 4 deletions src/chat/chat_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
14 changes: 8 additions & 6 deletions src/contact/contact_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
Expand All @@ -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.
Expand All @@ -1250,7 +1251,8 @@ async fn test_reset_verified_encryption() -> Result<()> {
"[email protected] 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(())
Expand Down
4 changes: 2 additions & 2 deletions src/e2ee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
31 changes: 22 additions & 9 deletions src/mimeparser/mimeparser_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down
11 changes: 9 additions & 2 deletions src/receive_imf/receive_imf_tests.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use rand::Rng;
use std::time::Duration;

use tokio::fs;
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions src/securejoin/securejoin_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,13 @@ async fn test_shared_bobs_key() -> Result<()> {
let bob3_addr = "[email protected]";
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();
Expand Down
21 changes: 18 additions & 3 deletions src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()
}

Expand Down
4 changes: 2 additions & 2 deletions src/webxdc/webxdc_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 3efa105

Please sign in to comment.