Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/draft_fold.rs

3.3 KiB, 1 run

created by r427506860:79512, which is this file's identity for as long as the history lasts, whatever it is later renamed to

download · who wrote it · its history

1//! A reply's thread headers are folded at whitespace, so that a long thread's References keeps
2//! every line within RFC 5322's 998 octets (§2.1.1) and reads back whole.
3
4use oxedyne_fe2o3_core::prelude::*;
5use oxedyne_fe2o3_mail::message::{
6 DraftMessage,
7 MessageView,
8};
9
10
11fn ids(n: usize) -> Vec<String> {
12 // Gmail-sized ids, about 70 octets each.
13 (0..n).map(|i| fmt!("<CAKx{:04}jQ7cVbN3pLr8mWzE5tYuH2gF9sDqA1oZ6iX0vB4nM-Tw@mail.gmail.com>", i)).collect()
14}
15
16fn draft(irt: &str, refs: &str) -> DraftMessage {
17 DraftMessage {
18 from: fmt!("me@x.example"),
19 subject: fmt!("Re: s"),
20 body: fmt!("Yes.\n"),
21 in_reply_to: irt.to_string(),
22 references: refs.to_string(),
23 date: fmt!("Mon, 1 Sep 2026 12:00:00 +0000"),
24 message_id: fmt!("<r@x.example>"),
25 ..Default::default()
26 }
27}
28
29/// The header block's lines, CRLF-split.
30fn head_lines(raw: &[u8]) -> Outcome<Vec<String>> {
31 let s = res!(std::str::from_utf8(raw).map_err(|e| err!(e, "The built message is not UTF-8."; Invalid, Input)));
32 let head = res!(s.split("\r\n\r\n").next().ok_or_else(|| err!("No header block."; Missing)));
33 Ok(head.split("\r\n").map(|l| l.to_string()).collect())
34}
35
36#[test]
37fn forty_references_fold_and_read_back() -> Outcome<()> {
38 let all = ids(40);
39 let d = draft(&all[39], &all.join(" "));
40 let raw = res!(d.build());
41 let lines = res!(head_lines(&raw));
42 // A line passes 78 octets only where one id alone takes it there, and never 998.
43 for l in &lines {
44 if l.len() > 998 || (l.len() > 78 && l.matches('<').count() > 1) {
45 return Err(err!("A header line of {} octets: {}", l.len(), l; Invalid, Output));
46 }
47 }
48 let n = lines.iter().position(|l| l.starts_with("References:"));
49 let n = res!(n.ok_or_else(|| err!("No References line."; Missing)));
50 if !lines[n + 1].starts_with(' ') {
51 return Err(err!("References was not folded onto a continuation line."; Invalid, Output));
52 }
53 let v = MessageView::parse(&raw);
54 let got: Vec<&str> = v.references.split_whitespace().collect();
55 if got != all.iter().map(|s| s.as_str()).collect::<Vec<&str>>() {
56 return Err(err!("References read back as {:?}", v.references; Invalid, Output));
57 }
58 if v.in_reply_to.trim() != all[39] {
59 return Err(err!("In-Reply-To read back as {:?}", v.in_reply_to; Invalid, Output));
60 }
61 Ok(())
62}
63
64#[test]
65fn short_references_stay_on_one_line() -> Outcome<()> {
66 let raw = res!(draft("<m3@x.example>", "<r1@x.example> <r2@x.example> <m3@x.example>").build());
67 let lines = res!(head_lines(&raw));
68 if !lines.iter().any(|l| l == "References: <r1@x.example> <r2@x.example> <m3@x.example>") {
69 return Err(err!("A short References was changed: {:?}", lines; Invalid, Output));
70 }
71 if !lines.iter().any(|l| l == "In-Reply-To: <m3@x.example>") {
72 return Err(err!("In-Reply-To was changed: {:?}", lines; Invalid, Output));
73 }
74 Ok(())
75}
76
77#[test]
78fn an_id_past_a_whole_line_is_refused() -> Outcome<()> {
79 let long = fmt!("<{}@x.example>", "a".repeat(1000));
80 match draft(&long, "").build() {
81 Ok(_) => Err(err!("A Message-ID of over 998 octets was written into a header."; Invalid, Output)),
82 Err(_) => Ok(()),
83 }
84}