Skip to content

Commit be769cd

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

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
@@ -2631,12 +2631,6 @@ async fn test_can_send_group() -> Result<()> {
26312631
/// the recipients can't see the identity of their fellow recipients.
26322632
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
26332633
async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
2634-
fn contains(parsed: &MimeMessage, s: &str) -> bool {
2635-
assert_eq!(parsed.decrypting_failed, false);
2636-
let decoded_str = std::str::from_utf8(&parsed.decoded_data).unwrap();
2637-
decoded_str.contains(s)
2638-
}
2639-
26402634
let mut tcm = TestContextManager::new();
26412635
let alice = &tcm.alice().await;
26422636
let bob = &tcm.bob().await;
@@ -2668,8 +2662,8 @@ async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
26682662
);
26692663
let parsed = charlie.parse_msg(&auth_required).await;
26702664
assert!(parsed.get_header(HeaderDef::AutocryptGossip).is_some());
2671-
assert!(contains(&parsed, "[email protected]"));
2672-
assert_eq!(contains(&parsed, "[email protected]"), false);
2665+
assert!(parsed.decoded_data_contains("[email protected]"));
2666+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
26732667

26742668
let parsed_by_bob = bob.parse_msg(&auth_required).await;
26752669
assert!(parsed_by_bob.decrypting_failed);
@@ -2697,8 +2691,8 @@ async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
26972691
);
26982692
let parsed = charlie.parse_msg(&member_added).await;
26992693
assert!(parsed.get_header(HeaderDef::AutocryptGossip).is_some());
2700-
assert!(contains(&parsed, "[email protected]"));
2701-
assert_eq!(contains(&parsed, "[email protected]"), false);
2694+
assert!(parsed.decoded_data_contains("[email protected]"));
2695+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
27022696

27032697
let parsed_by_bob = bob.parse_msg(&member_added).await;
27042698
assert!(parsed_by_bob.decrypting_failed);
@@ -2712,8 +2706,8 @@ async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
27122706
let hi_msg = alice.send_text(alice_broadcast_id, "hi").await;
27132707
let parsed = charlie.parse_msg(&hi_msg).await;
27142708
assert_eq!(parsed.header_exists(HeaderDef::AutocryptGossip), false);
2715-
assert_eq!(contains(&parsed, "[email protected]"), false);
2716-
assert_eq!(contains(&parsed, "[email protected]"), false);
2709+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
2710+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
27172711

27182712
let parsed_by_bob = bob.parse_msg(&hi_msg).await;
27192713
assert_eq!(parsed_by_bob.decrypting_failed, false);
@@ -2729,8 +2723,8 @@ async fn test_broadcast_members_cant_see_each_other() -> Result<()> {
27292723
27302724
);
27312725
let parsed = charlie.parse_msg(&member_removed).await;
2732-
assert!(contains(&parsed, "[email protected]"));
2733-
assert_eq!(contains(&parsed, "[email protected]"), false);
2726+
assert!(parsed.decoded_data_contains("[email protected]"));
2727+
assert_eq!(parsed.decoded_data_contains("[email protected]"), false);
27342728

27352729
let parsed_by_bob = bob.parse_msg(&member_removed).await;
27362730
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
@@ -1008,6 +1008,14 @@ impl MimeMessage {
10081008
self.headers.contains_key(hname) || self.headers_removed.contains(hname)
10091009
}
10101010

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

0 commit comments

Comments
 (0)