Skip to content

Commit 4b44365

Browse files
committed
test: HP-Outer headers are added to messages with standard Header Protection (#7130)
1 parent 1fba75d commit 4b44365

File tree

4 files changed

+43
-14
lines changed

4 files changed

+43
-14
lines changed

src/chat/chat_tests.rs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,12 +2632,6 @@ async fn test_can_send_group() -> Result<()> {
26322632
/// the recipients can't see the identity of their fellow recipients.
26332633
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
26342634
async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
2635-
fn contains(parsed: &MimeMessage, s: &str) -> bool {
2636-
assert_eq!(parsed.decrypting_failed, false);
2637-
let decoded_str = std::str::from_utf8(&parsed.decoded_data).unwrap();
2638-
decoded_str.contains(s)
2639-
}
2640-
26412635
let mut tcm = TestContextManager::new();
26422636
let alice = &tcm.alice().await;
26432637
let bob = &tcm.bob().await;
@@ -2669,8 +2663,8 @@ async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
26692663
);
26702664
let parsed = charlie.parse_msg(&auth_required).await;
26712665
assert!(parsed.get_header(HeaderDef::AutocryptGossip).is_some());
2672-
assert!(contains(&parsed, "[email protected]"));
2673-
assert_eq!(contains(&parsed, "[email protected]"), false);
2666+
assert!(parsed.decoded_data_contains("[email protected]"));
2667+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
26742668

26752669
let parsed_by_bob = bob.parse_msg(&auth_required).await;
26762670
assert!(parsed_by_bob.decrypting_failed);
@@ -2698,8 +2692,8 @@ async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
26982692
);
26992693
let parsed = charlie.parse_msg(&member_added).await;
27002694
assert!(parsed.get_header(HeaderDef::AutocryptGossip).is_some());
2701-
assert!(contains(&parsed, "[email protected]"));
2702-
assert_eq!(contains(&parsed, "[email protected]"), false);
2695+
assert!(parsed.decoded_data_contains("[email protected]"));
2696+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
27032697

27042698
let parsed_by_bob = bob.parse_msg(&member_added).await;
27052699
assert!(parsed_by_bob.decrypting_failed);
@@ -2713,8 +2707,8 @@ async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
27132707
let hi_msg = alice.send_text(alice_broadcast_id, "hi").await;
27142708
let parsed = charlie.parse_msg(&hi_msg).await;
27152709
assert_eq!(parsed.header_exists(HeaderDef::AutocryptGossip), false);
2716-
assert_eq!(contains(&parsed, "[email protected]"), false);
2717-
assert_eq!(contains(&parsed, "[email protected]"), false);
2710+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
2711+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
27182712

27192713
let parsed_by_bob = bob.parse_msg(&hi_msg).await;
27202714
assert_eq!(parsed_by_bob.decrypting_failed, false);
@@ -2730,8 +2724,8 @@ async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
27302724
27312725
);
27322726
let parsed = charlie.parse_msg(&member_removed).await;
2733-
assert!(contains(&parsed, "[email protected]"));
2734-
assert_eq!(contains(&parsed, "[email protected]"), false);
2727+
assert!(parsed.decoded_data_contains("[email protected]"));
2728+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
27352729

27362730
let parsed_by_bob = bob.parse_msg(&member_removed).await;
27372731
assert!(parsed_by_bob.decrypting_failed);

src/headerdef.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ pub enum HeaderDef {
138138
/// Advertised gossip topic for one webxdc.
139139
IrohGossipTopic,
140140

141+
/// See <https://www.rfc-editor.org/rfc/rfc9788.html#name-hp-outer-header-field>.
142+
HpOuter,
143+
141144
#[cfg(test)]
142145
TestHeader,
143146
}

src/mimefactory/mimefactory_tests.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -837,6 +837,30 @@ async fn test_protected_headers_directive() -> Result<()> {
837837
Ok(())
838838
}
839839

840+
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
841+
async fn test_hp_outer_headers() -> Result<()> {
842+
let mut tcm = TestContextManager::new();
843+
let t = &tcm.alice().await;
844+
let chat_id = t.get_self_chat().await.id;
845+
846+
for std_hp_composing in [false, true] {
847+
t.set_config_bool(Config::StdHeaderProtectionComposing, std_hp_composing)
848+
.await?;
849+
chat::send_text_msg(t, chat_id, "hi!".to_string()).await?;
850+
let sent_msg = t.pop_sent_msg().await;
851+
let msg = MimeMessage::from_bytes(t, sent_msg.payload.as_bytes(), None).await?;
852+
assert_eq!(msg.header_exists(HeaderDef::HpOuter), std_hp_composing);
853+
for hdr in ["Date", "From", "Message-ID"] {
854+
assert_eq!(
855+
msg.decoded_data_contains(&format!("HP-Outer: {hdr}:")),
856+
std_hp_composing,
857+
);
858+
}
859+
assert!(!msg.decoded_data_contains("HP-Outer: Content-Type"));
860+
}
861+
Ok(())
862+
}
863+
840864
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
841865
async fn test_dont_remove_self() -> Result<()> {
842866
let mut tcm = TestContextManager::new();

src/mimeparser.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,14 @@ impl MimeMessage {
10071007
self.headers.contains_key(hname) || self.headers_removed.contains(hname)
10081008
}
10091009

1010+
#[cfg(test)]
1011+
/// Returns whether the decrypted data contains the given `&str`.
1012+
pub(crate) fn decoded_data_contains(&self, s: &str) -> bool {
1013+
assert!(!self.decrypting_failed);
1014+
let decoded_str = str::from_utf8(&self.decoded_data).unwrap();
1015+
decoded_str.contains(s)
1016+
}
1017+
10101018
/// Returns `Chat-Group-ID` header value if it is a valid group ID.
10111019
pub fn get_chat_group_id(&self) -> Option<&str> {
10121020
self.get_header(HeaderDef::ChatGroupId)

0 commit comments

Comments
 (0)