From f7139331e70a606201fd0b32c944a7c11d9f4278 Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Wed, 21 Apr 2021 18:03:12 +0200 Subject: [PATCH] test that the correct headers are moved --- src/mimefactory.rs | 59 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 58 insertions(+), 1 deletion(-) diff --git a/src/mimefactory.rs b/src/mimefactory.rs index 846233d72..33492cc94 100644 --- a/src/mimefactory.rs +++ b/src/mimefactory.rs @@ -1345,14 +1345,16 @@ fn maybe_encode_words(words: &str) -> String { #[cfg(test)] mod tests { use super::*; - use crate::chat::ChatId; + use async_std::prelude::*; + use crate::chat::ChatId; use crate::contact::Origin; use crate::dc_receive_imf::dc_receive_imf; use crate::mimeparser::MimeMessage; use crate::test_utils::TestContext; use crate::{chatlist::Chatlist, test_utils::get_chat_msg}; + use async_std::fs::File; use pretty_assertions::assert_eq; #[test] @@ -1903,4 +1905,59 @@ mod tests { assert!(!headers.lines().any(|l| l.trim().is_empty())); } + + #[async_std::test] + async fn test_selfavatar_unencrypted() -> anyhow::Result<()> { + // create chat with bob, set selfavatar + let t = TestContext::new_alice().await; + let chat = t.create_chat_with_contact("bob", "bob@example.org").await; + + let file = t.dir.path().join("avatar.png"); + let bytes = include_bytes!("../test-data/image/avatar64x64.png"); + File::create(&file).await?.write_all(bytes).await?; + t.set_config(Config::Selfavatar, Some(file.to_str().unwrap())) + .await?; + + // send message to bob: that should get multipart/mixed because of the avatar moved to inner header; + // make sure, `Subject:` stays in the outer header (imf header) + let mut msg = Message::new(Viewtype::Text); + msg.set_text(Some("this is the text!".to_string())); + + let payload = t.send_msg(chat.id, &mut msg).await.payload(); + let mut payload = payload.splitn(3, "\r\n\r\n"); + let outer = payload.next().unwrap(); + let inner = payload.next().unwrap(); + let body = payload.next().unwrap(); + + assert_eq!(outer.match_indices("multipart/mixed").count(), 1); + assert_eq!(outer.match_indices("Subject:").count(), 1); + assert_eq!(outer.match_indices("Autocrypt:").count(), 1); + assert_eq!(outer.match_indices("Chat-User-Avatar:").count(), 0); + + assert_eq!(inner.match_indices("text/plain").count(), 1); + assert_eq!(inner.match_indices("Chat-User-Avatar:").count(), 1); + assert_eq!(inner.match_indices("Subject:").count(), 0); + + assert_eq!(body.match_indices("this is the text!").count(), 1); + + // if another message is sent, that one must not contain the avatar + // and no artificial multipart/mixed nesting + let payload = t.send_msg(chat.id, &mut msg).await.payload(); + let mut payload = payload.splitn(2, "\r\n\r\n"); + let outer = payload.next().unwrap(); + let body = payload.next().unwrap(); + + assert_eq!(outer.match_indices("text/plain").count(), 1); + assert_eq!(outer.match_indices("Subject:").count(), 1); + assert_eq!(outer.match_indices("Autocrypt:").count(), 1); + assert_eq!(outer.match_indices("multipart/mixed").count(), 0); + assert_eq!(outer.match_indices("Chat-User-Avatar:").count(), 0); + + assert_eq!(body.match_indices("this is the text!").count(), 1); + assert_eq!(body.match_indices("text/plain").count(), 0); + assert_eq!(body.match_indices("Chat-User-Avatar:").count(), 0); + assert_eq!(body.match_indices("Subject:").count(), 0); + + Ok(()) + } }