test: use encrypted messages in more tests

This change is a preparation for ignoring
unencrypted messages by default.

New test_utils::encrypt_raw_message and
test_utils::receive_encrypted_imf are
used to encrypt the messages before
"receiving" them with receive_imf.
This commit is contained in:
link2xt
2026-05-09 19:19:32 +02:00
committed by l
parent ac3c5c55e4
commit 02f5dba620
24 changed files with 1086 additions and 984 deletions

View File

@@ -169,49 +169,54 @@ pub(crate) async fn intercept_get_updates(
#[cfg(test)]
mod tests {
use crate::chat::{ChatId, create_group};
use crate::chat::create_group;
use crate::chatlist::Chatlist;
use crate::contact::Contact;
use crate::message::Message;
use crate::test_utils::TestContext;
use crate::test_utils::TestContextManager;
use crate::webxdc::StatusUpdateSerial;
use crate::{EventType, location};
use anyhow::Result;
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_maps_integration() -> Result<()> {
let t = TestContext::new_alice().await;
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;
let bytes = include_bytes!("../../test-data/webxdc/mapstest-integration-set.xdc");
let file = t.get_blobdir().join("maps.xdc");
let file = alice.get_blobdir().join("maps.xdc");
tokio::fs::write(&file, bytes).await.unwrap();
t.set_webxdc_integration(file.to_str().unwrap()).await?;
alice.set_webxdc_integration(file.to_str().unwrap()).await?;
let chatlist = Chatlist::try_load(&t, 0, None, None).await?;
let summary = chatlist.get_summary(&t, 0, None).await?;
let chatlist = Chatlist::try_load(alice, 0, None, None).await?;
let summary = chatlist.get_summary(alice, 0, None).await?;
assert_eq!(summary.text, "No messages.");
// Integrate Webxdc into a chat with Bob;
// sending updates is intercepted by integrations and results in setting a POI in core
let bob_id = Contact::create(&t, "", "bob@example.net").await?;
let bob_chat_id = ChatId::create_for_contact(&t, bob_id).await?;
let integration_id = t.init_webxdc_integration(Some(bob_chat_id)).await?.unwrap();
let bob_chat_id = alice.create_chat_id(bob).await;
let integration_id = alice
.init_webxdc_integration(Some(bob_chat_id))
.await?
.unwrap();
assert!(!integration_id.is_special());
let integration = Message::load_from_db(&t, integration_id).await?;
let info = integration.get_webxdc_info(&t).await?;
let integration = Message::load_from_db(alice, integration_id).await?;
let info = integration.get_webxdc_info(alice).await?;
assert_eq!(info.name, "Maps Test 2");
assert_eq!(info.internet_access, true);
t.send_webxdc_status_update(
integration_id,
r#"{"payload": {"action": "pos", "lat": 11.0, "lng": 12.0, "label": "poi #1"}}"#,
)
.await?;
t.evtracker
alice
.send_webxdc_status_update(
integration_id,
r#"{"payload": {"action": "pos", "lat": 11.0, "lng": 12.0, "label": "poi #1"}}"#,
)
.await?;
alice
.evtracker
.get_matching(|evt| matches!(evt, EventType::WebxdcStatusUpdate { .. }))
.await;
let updates = t
let updates = alice
.get_webxdc_status_updates(integration_id, StatusUpdateSerial(0))
.await?;
assert!(updates.contains(r#""lat":11"#));
@@ -220,28 +225,32 @@ mod tests {
assert!(updates.contains(r#""contactId":"#)); // checking for sth. that is not in the sent update make sure integration is called
assert!(updates.contains(r#""name":"Me""#));
assert!(updates.contains(r##""color":"#"##));
let locations = location::get_range(&t, Some(bob_chat_id), None, 0, 0).await?;
let locations = location::get_range(alice, Some(bob_chat_id), None, 0, 0).await?;
assert_eq!(locations.len(), 1);
let location = locations.last().unwrap();
assert_eq!(location.latitude, 11.0);
assert_eq!(location.longitude, 12.0);
assert_eq!(location.independent, 1);
let msg = t.get_last_msg().await;
let msg = alice.get_last_msg().await;
assert_eq!(msg.text, "poi #1");
assert_eq!(msg.chat_id, bob_chat_id);
// Integrate Webxdc into another group
let group_id = create_group(&t, "foo").await?;
let integration_id = t.init_webxdc_integration(Some(group_id)).await?.unwrap();
let group_id = create_group(alice, "foo").await?;
let integration_id = alice
.init_webxdc_integration(Some(group_id))
.await?
.unwrap();
let locations = location::get_range(&t, Some(group_id), None, 0, 0).await?;
let locations = location::get_range(alice, Some(group_id), None, 0, 0).await?;
assert_eq!(locations.len(), 0);
t.send_webxdc_status_update(
integration_id,
r#"{"payload": {"action": "pos", "lat": 22.0, "lng": 23.0, "label": "poi #2"}}"#,
)
.await?;
let updates = t
alice
.send_webxdc_status_update(
integration_id,
r#"{"payload": {"action": "pos", "lat": 22.0, "lng": 23.0, "label": "poi #2"}}"#,
)
.await?;
let updates = alice
.get_webxdc_status_updates(integration_id, StatusUpdateSerial(0))
.await?;
assert!(!updates.contains(r#""lat":11"#));
@@ -249,27 +258,27 @@ mod tests {
assert!(updates.contains(r#""lat":22"#));
assert!(updates.contains(r#""lng":23"#));
assert!(updates.contains(r#""label":"poi #2""#));
let locations = location::get_range(&t, Some(group_id), None, 0, 0).await?;
let locations = location::get_range(alice, Some(group_id), None, 0, 0).await?;
assert_eq!(locations.len(), 1);
let location = locations.last().unwrap();
assert_eq!(location.latitude, 22.0);
assert_eq!(location.longitude, 23.0);
assert_eq!(location.independent, 1);
let msg = t.get_last_msg().await;
let msg = alice.get_last_msg().await;
assert_eq!(msg.text, "poi #2");
assert_eq!(msg.chat_id, group_id);
// In global map, both POI are visible
let integration_id = t.init_webxdc_integration(None).await?.unwrap();
let integration_id = alice.init_webxdc_integration(None).await?.unwrap();
let updates = t
let updates = alice
.get_webxdc_status_updates(integration_id, StatusUpdateSerial(0))
.await?;
assert!(updates.contains(r#""lat":11"#));
assert!(updates.contains(r#""label":"poi #1""#));
assert!(updates.contains(r#""lat":22"#));
assert!(updates.contains(r#""label":"poi #2""#));
let locations = location::get_range(&t, None, None, 0, 0).await?;
let locations = location::get_range(alice, None, None, 0, 0).await?;
assert_eq!(locations.len(), 2);
Ok(())

View File

@@ -11,8 +11,8 @@ use crate::chat::{
use crate::chatlist::Chatlist;
use crate::config::Config;
use crate::ephemeral;
use crate::receive_imf::receive_imf;
use crate::securejoin::get_securejoin_qr;
use crate::test_utils;
use crate::test_utils::{E2EE_INFO_MSGS, TestContext, TestContextManager};
use crate::tools::{self, SystemTime};
use crate::{message, sql};
@@ -257,24 +257,26 @@ async fn test_resend_webxdc_instance_and_info() -> Result<()> {
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_receive_webxdc_instance() -> Result<()> {
let t = TestContext::new_alice().await;
receive_imf(
&t,
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
let bob = &tcm.bob().await;
test_utils::receive_encrypted_imf(
alice,
bob,
include_bytes!("../../test-data/message/webxdc_good_extension.eml"),
false,
)
.await?;
let instance = t.get_last_msg().await;
let instance = alice.get_last_msg().await;
assert_eq!(instance.viewtype, Viewtype::Webxdc);
assert_eq!(instance.get_filename().unwrap(), "minimal.xdc");
receive_imf(
&t,
test_utils::receive_encrypted_imf(
alice,
bob,
include_bytes!("../../test-data/message/webxdc_bad_extension.eml"),
false,
)
.await?;
let instance = t.get_last_msg().await;
let instance = alice.get_last_msg().await;
assert_eq!(instance.viewtype, Viewtype::File); // we require the correct extension, only a mime type is not sufficient
assert_eq!(instance.get_filename().unwrap(), "index.html");
@@ -682,13 +684,14 @@ async fn expect_status_update_event(t: &TestContext, instance_id: MsgId) -> Resu
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_send_webxdc_status_update() -> Result<()> {
let alice = TestContext::new_alice().await;
let mut tcm = TestContextManager::new();
let alice = &tcm.alice().await;
alice.set_config_bool(Config::BccSelf, true).await?;
let bob = TestContext::new_bob().await;
let bob = &tcm.bob().await;
// Alice sends an webxdc instance and a status update
let alice_chat = alice.create_email_chat(&bob).await;
let alice_instance = send_webxdc_instance(&alice, alice_chat.id).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);
assert!(!sent1.payload().contains("report-type=status-update"));
@@ -697,7 +700,7 @@ async fn test_send_webxdc_status_update() -> Result<()> {
.send_webxdc_status_update(alice_instance.id, r#"{"payload" : {"foo":"bar"}}"#)
.await?;
alice.flush_status_updates().await?;
expect_status_update_event(&alice, alice_instance.id).await?;
expect_status_update_event(alice, alice_instance.id).await?;
let sent2 = &alice.pop_sent_msg().await;
let alice_update = sent2.load_from_db().await;
assert!(alice_update.hidden);
@@ -706,10 +709,10 @@ async fn test_send_webxdc_status_update() -> Result<()> {
assert_eq!(alice_update.text, BODY_DESCR.to_string());
assert_eq!(alice_update.chat_id, alice_instance.chat_id);
assert_eq!(
alice_update.parent(&alice).await?.unwrap().id,
alice_update.parent(alice).await?.unwrap().id,
alice_instance.id
);
assert_eq!(alice_chat.id.get_msg_cnt(&alice).await?, 1);
assert_eq!(alice_chat.id.get_msg_cnt(alice).await?, 1);
assert!(sent2.payload().contains("report-type=status-update"));
assert!(sent2.payload().contains(BODY_DESCR));
assert_eq!(
@@ -735,12 +738,12 @@ async fn test_send_webxdc_status_update() -> Result<()> {
let bob_chat_id = bob_instance.chat_id;
assert_eq!(bob_instance.rfc724_mid, alice_instance.rfc724_mid);
assert_eq!(bob_instance.viewtype, Viewtype::Webxdc);
assert_eq!(bob_chat_id.get_msg_cnt(&bob).await?, 1);
assert_eq!(bob_chat_id.get_msg_cnt(bob).await?, 1);
let bob_received_update = bob.recv_msg_opt(sent2).await;
assert!(bob_received_update.is_none());
expect_status_update_event(&bob, bob_instance.id).await?;
assert_eq!(bob_chat_id.get_msg_cnt(&bob).await?, 1);
expect_status_update_event(bob, bob_instance.id).await?;
assert_eq!(bob_chat_id.get_msg_cnt(bob).await?, 1);
assert_eq!(
bob.get_webxdc_status_updates(bob_instance.id, StatusUpdateSerial(0))
@@ -749,19 +752,19 @@ async fn test_send_webxdc_status_update() -> Result<()> {
);
// Alice has a second device and also receives messages there
let alice2 = TestContext::new_alice().await;
let alice2 = &tcm.alice().await;
alice2.recv_msg(sent1).await;
alice2.recv_msg_trash(sent2).await;
let alice2_instance = alice2.get_last_msg().await;
let alice2_chat_id = alice2_instance.chat_id;
assert_eq!(alice2_instance.viewtype, Viewtype::Webxdc);
assert_eq!(alice2_chat_id.get_msg_cnt(&alice2).await?, 1);
assert_eq!(alice2_chat_id.get_msg_cnt(alice2).await?, 1);
// To support the second device, Alice has enabled bcc_self and will receive their own messages;
// these messages, however, should be ignored
alice.recv_msg_opt(sent1).await;
alice.recv_msg_opt(sent2).await;
assert_eq!(alice_chat.id.get_msg_cnt(&alice).await?, 1);
assert_eq!(alice_chat.id.get_msg_cnt(alice).await?, 1);
assert_eq!(
alice
.get_webxdc_status_updates(alice_instance.id, StatusUpdateSerial(0))
@@ -983,7 +986,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_email_chat(&bob).await.id;
let alice_chat_id = alice.create_chat(&bob).await.id;
// prepare webxdc instance,
// status updates are not sent for drafts, therefore send_webxdc_status_update() returns Ok(None)
@@ -1030,8 +1033,6 @@ async fn test_draft_and_send_webxdc_status_update() -> Result<()> {
let bob_instance = bob.recv_msg(&sent1).await;
assert_eq!(bob_instance.viewtype, Viewtype::Webxdc);
assert_eq!(bob_instance.get_filename().unwrap(), "minimal.xdc");
assert!(sent1.payload().contains("Content-Type: application/json"));
assert!(sent1.payload().contains("status-update.json"));
assert_eq!(
bob.get_webxdc_status_updates(bob_instance.id, StatusUpdateSerial(0))
.await?,
@@ -1557,16 +1558,18 @@ async fn test_webxdc_info_msg_no_cleanup_on_interrupted_series() -> Result<()> {
// even if they use the deprecated option `request_internet_access` in manifest.toml
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_webxdc_no_internet_access() -> Result<()> {
let t = TestContext::new_alice().await;
let mut tcm = TestContextManager::new();
let t = &tcm.alice().await;
let bob = &tcm.bob().await;
let self_id = t.get_self_chat().await.id;
let single_id = t.create_chat_with_contact("bob", "bob@e.com").await.id;
let group_id = create_group(&t, "chat").await?;
let broadcast_id = create_broadcast(&t, "Channel".to_string()).await?;
let single_id = t.create_chat_id(bob).await;
let group_id = create_group(t, "chat").await?;
let broadcast_id = create_broadcast(t, "Channel".to_string()).await?;
for chat_id in [self_id, single_id, group_id, broadcast_id] {
for internet_xdc in [true, false] {
let mut instance = create_webxdc_instance(
&t,
t,
"foo.xdc",
if internet_xdc {
include_bytes!("../../test-data/webxdc/request-internet-access.xdc")
@@ -1574,14 +1577,14 @@ async fn test_webxdc_no_internet_access() -> Result<()> {
include_bytes!("../../test-data/webxdc/minimal.xdc")
},
)?;
let instance_id = send_msg(&t, chat_id, &mut instance).await?;
let instance_id = send_msg(t, chat_id, &mut instance).await?;
t.send_webxdc_status_update(
instance_id,
r#"{"summary":"real summary", "payload": 42}"#,
)
.await?;
let instance = Message::load_from_db(&t, instance_id).await?;
let info = instance.get_webxdc_info(&t).await?;
let instance = Message::load_from_db(t, instance_id).await?;
let info = instance.get_webxdc_info(t).await?;
assert_eq!(info.internet_access, false);
}
}