oxedyne/fe2o3/fe2o3_mail/tests/mail_addr.rs
10.5 KiB, 6 runs
created by r3907241492:79272, 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 | //! MAIL-addr: an address field is read once, from the raw header, into mailboxes, and a reply's |
| 2 | //! headers and envelope are both written from those mailboxes. The header shapes are those the |
| 3 | //! archive held that the reply path refused or misread, the names and domains made up. |
| 4 | |
| 5 | use oxedyne_fe2o3_core::prelude::*; |
| 6 | use oxedyne_fe2o3_mail::message::{ |
| 7 | headers, |
| 8 | listed, |
| 9 | mailboxes, |
| 10 | shown, |
| 11 | DraftMessage, |
| 12 | Mailbox, |
| 13 | MessageView, |
| 14 | ParsedMessage, |
| 15 | }; |
| 16 | use oxedyne_fe2o3_net::email::header::{ |
| 17 | addresses, |
| 18 | mailbox_list, |
| 19 | }; |
| 20 | |
| 21 | use std::io::Write; |
| 22 | |
| 23 | |
| 24 | fn mb(name: &str, addr: &str) -> Mailbox { |
| 25 | Mailbox { name: name.to_string(), addr: addr.to_string() } |
| 26 | } |
| 27 | |
| 28 | fn msg(head: &str) -> Vec<u8> { |
| 29 | fmt!("{}\r\nSubject: s\r\nMessage-ID: <m@x.example>\r\n\r\nhi\r\n", head).into_bytes() |
| 30 | } |
| 31 | |
| 32 | fn reply(to: Vec<Mailbox>, cc: Vec<Mailbox>) -> DraftMessage { |
| 33 | DraftMessage { |
| 34 | from: fmt!("me@x.example"), |
| 35 | to, |
| 36 | cc, |
| 37 | subject: fmt!("Re: s"), |
| 38 | body: fmt!("Yes.\n"), |
| 39 | date: fmt!("Mon, 1 Sep 2026 12:00:00 +0000"), |
| 40 | message_id: fmt!("<r@x.example>"), |
| 41 | ..Default::default() |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /// The addresses the built `To` and `Cc` name, read back by the strict reader, which must be |
| 46 | /// the envelope's; and the mailboxes a reader of the built message sees. |
| 47 | fn agree(d: &DraftMessage) -> (Vec<String>, Vec<Mailbox>) { |
| 48 | let raw = d.build().expect("the reply builds"); |
| 49 | let mut named = Vec::new(); |
| 50 | for (n, v) in headers(&raw) { |
| 51 | if n == "to" || n == "cc" { |
| 52 | named.extend(addresses(&v).expect("the header reads back")); |
| 53 | } |
| 54 | } |
| 55 | assert_eq!(named, d.recipients().expect("an envelope"), "header and envelope differ: {}", |
| 56 | String::from_utf8_lossy(&raw)); |
| 57 | let v = MessageView::parse(&raw); |
| 58 | (named, v.to.into_iter().chain(v.cc).collect()) |
| 59 | } |
| 60 | |
| 61 | /// A sender's encoded Reply-To name that decodes to an address and a comma stays a name. |
| 62 | #[test] |
| 63 | fn a_reply_to_name_adds_no_recipient() { |
| 64 | let raw = msg("From: =?utf-8?Q?evil=40y.example=2C?= <ada@x.example>\r\n\ |
| 65 | Reply-To: =?utf-8?Q?evil2=40y.example=2C?= <ada@x.example>"); |
| 66 | let v = MessageView::parse(&raw); |
| 67 | assert_eq!(v.reply_to, vec![mb("evil2@y.example,", "ada@x.example")]); |
| 68 | assert_eq!(v.from, vec![mb("evil@y.example,", "ada@x.example")]); |
| 69 | let (named, seen) = agree(&reply(v.reply_to.clone(), Vec::new())); |
| 70 | assert_eq!(named, vec!["ada@x.example"]); |
| 71 | assert_eq!(seen, v.reply_to); |
| 72 | // The text a person edits, or an agent reads, reads back as the same mailbox. |
| 73 | assert_eq!(mailboxes(&shown(&v.reply_to)).expect("reads"), v.reply_to); |
| 74 | assert_eq!(mailboxes(&ParsedMessage::parse(&raw).from).expect("reads"), v.from); |
| 75 | // A list row's text, from the raw field alone. |
| 76 | let row = shown(&listed("=?utf-8?Q?evil=40y.example=2C?= <ada@x.example>")); |
| 77 | assert_eq!(row, "\"evil@y.example,\" <ada@x.example>"); |
| 78 | assert_eq!(addresses(&row).expect("reads"), vec!["ada@x.example"]); |
| 79 | } |
| 80 | |
| 81 | #[test] |
| 82 | fn a_name_with_a_comma_round_trips() { |
| 83 | let v = MessageView::parse(&msg("From: \"Hall, Van\" <van@x.example>\r\nTo: me@x.example")); |
| 84 | assert_eq!(v.from, vec![mb("Hall, Van", "van@x.example")]); |
| 85 | assert_eq!(shown(&v.from), "\"Hall, Van\" <van@x.example>"); |
| 86 | assert_eq!(mailboxes(&shown(&v.from)).expect("reads"), v.from); |
| 87 | let (named, seen) = agree(&reply(v.from.clone(), Vec::new())); |
| 88 | assert_eq!(named, vec!["van@x.example"]); |
| 89 | assert_eq!(seen, v.from); |
| 90 | } |
| 91 | |
| 92 | /// Each kind of real header the reply path refused (BC9-send READ, F7-a), read as RFC 5322 |
| 93 | /// reads it: a group gives its members or none, a comment is dropped, a quoted local part is |
| 94 | /// kept, an encoded name holding a comma is one name, and only what is no mailbox is left out. |
| 95 | #[test] |
| 96 | fn each_kind_the_archive_refused_is_read() { |
| 97 | for (what, field, want) in [ |
| 98 | ("an empty group", "undisclosed-recipients:;", vec![]), |
| 99 | ("an empty group, named", "AllStudents:;", vec![]), |
| 100 | ("an empty group, spaced", "Undisclosed recipients:;", vec![]), |
| 101 | ("a group with members", "Team: a@x.example, B <b@y.example>;", |
| 102 | vec![mb("", "a@x.example"), mb("B", "b@y.example")]), |
| 103 | ("a comment after >", "N. P. <n.p@x.example> ((Contractor))", vec![mb("N. P.", "n.p@x.example")]), |
| 104 | ("a quoted local part", "\".alias\"@y.example", vec![mb("", "\".alias\"@y.example")]), |
| 105 | ("quoted, with a comment", "\"Country: au\"@mail.example (Mail Hub)", |
| 106 | vec![mb("", "\"Country: au\"@mail.example")]), |
| 107 | ("an encoded name, a comma", "=?utf-8?Q?A=20Grammar=20School=2C=20Kew?= <office@school.example>", |
| 108 | vec![mb("A Grammar School, Kew", "office@school.example")]), |
| 109 | ("a bare name, then one", "pat,alias@y.example", vec![mb("", "alias@y.example")]), |
| 110 | ("a group in brackets", "<Undisclosed-Recipient:;>", vec![]), |
| 111 | ] { |
| 112 | let v = MessageView::parse(&msg(&fmt!("From: f@x.example\r\nTo: {}", field))); |
| 113 | assert_eq!(v.to, want, "{}", what); |
| 114 | assert_eq!(mailboxes(&shown(&v.to)).expect("reads"), v.to, "{}", what); |
| 115 | if want.is_empty() { |
| 116 | continue; |
| 117 | } |
| 118 | let (named, seen) = agree(&reply(vec![mb("F", "f@x.example")], v.to.clone())); |
| 119 | let addrs: Vec<String> = want.iter().map(|m| m.addr.clone()).collect(); |
| 120 | assert_eq!(named[1..], addrs[..], "{}", what); |
| 121 | assert_eq!(seen[1..], want[..], "{}", what); |
| 122 | } |
| 123 | // What is left out is said, member by member. |
| 124 | let l = mailbox_list("pat,alias@y.example"); |
| 125 | assert!(l.len() == 2 && l[0].is_err() && l[1].is_ok(), "{:?}", l); |
| 126 | } |
| 127 | |
| 128 | /// A name is quoted whenever it is not words of atext, so the header names the envelope's |
| 129 | /// mailbox whatever the name holds: a trailing backslash once made it the mailbox `"x\\"`. |
| 130 | #[test] |
| 131 | fn a_name_ending_in_a_backslash_names_its_own_mailbox() { |
| 132 | for name in ["x\\", "a\\\"b", "\"x\" <evil@y.example>,", "=?utf-8?Q?evil=40y.example?=", "a b", |
| 133 | "Ada\r\nBcc: e@y.example", "Zoë, Ada"] |
| 134 | { |
| 135 | let (named, seen) = agree(&reply(vec![mb(name, "ada@x.example")], Vec::new())); |
| 136 | assert_eq!(named, vec!["ada@x.example"], "{:?}", name); |
| 137 | let want = name.replace(['\r', '\n'], " "); |
| 138 | assert_eq!(seen, vec![mb(&want, "ada@x.example")], "{:?}", name); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /// Every address `build` writes is the envelope's, and what cannot be a mailbox is refused. |
| 143 | #[test] |
| 144 | fn the_headers_and_the_envelope_name_the_same_mailboxes() { |
| 145 | let all = vec![mb("Hall, Van", "van@x.example"), mb("", "\".me\"@x.example"), |
| 146 | mb("Ö", "jörg@bücher.example"), mb("x\\", "a+tag@[192.0.2.1]")]; |
| 147 | let (named, seen) = agree(&reply(all[..2].to_vec(), all[2..].to_vec())); |
| 148 | assert_eq!(named, all.iter().map(|m| m.addr.clone()).collect::<Vec<String>>()); |
| 149 | assert_eq!(seen, all); |
| 150 | let quoted_from = DraftMessage { from: fmt!("\".me\"@x.example"), ..reply(all.clone(), Vec::new()) }; |
| 151 | assert!(quoted_from.build().is_ok()); |
| 152 | for addr in ["ada@x.example.", "a@b@c.example", "<ada@x.example>", "ada@x.example (c)", " ", "", |
| 153 | "a@x.example, b@y.example", "\"open@x.example"] |
| 154 | { |
| 155 | let d = reply(vec![mb("A", addr)], Vec::new()); |
| 156 | assert!(d.build().is_err() && d.recipients().is_err(), "{:?} was taken", addr); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /// The archive count, run on header blocks sampled from his mail (`MAIL_HEADS`, length-prefixed |
| 161 | /// blocks as `sample_heads.py` writes them); local only, and skipped without it. Writes each |
| 162 | /// member that is no mailbox, and each mailbox a reply would refuse, to `MAIL_HEADS_OUT`. |
| 163 | #[test] |
| 164 | fn archive_count() { |
| 165 | let (path, out) = match (std::env::var("MAIL_HEADS"), std::env::var("MAIL_HEADS_OUT")) { |
| 166 | (Ok(p), Ok(o)) => (p, o), |
| 167 | _ => return, |
| 168 | }; |
| 169 | let raw = std::fs::read(&path).expect("the sample"); |
| 170 | let mut f = std::fs::File::create(&out).expect("the count"); |
| 171 | let (mut msgs, mut fields, mut boxes, mut none, mut refused) = (0usize, 0usize, 0usize, 0usize, 0usize); |
| 172 | let mut i = 0; |
| 173 | while i + 4 <= raw.len() { |
| 174 | let n = u32::from_le_bytes([raw[i], raw[i + 1], raw[i + 2], raw[i + 3]]) as usize; |
| 175 | let mut blk = raw[i + 4..i + 4 + n].to_vec(); |
| 176 | blk.extend_from_slice(b"\r\nx\r\n"); |
| 177 | i += 4 + n; |
| 178 | msgs += 1; |
| 179 | for (name, v) in headers(&blk) { |
| 180 | let name = name.to_lowercase(); |
| 181 | if !matches!(name.as_str(), "from" | "to" | "cc" | "reply-to") { |
| 182 | continue; |
| 183 | } |
| 184 | fields += 1; |
| 185 | for m in mailbox_list(&v) { |
| 186 | match m { |
| 187 | Ok(m) => { |
| 188 | boxes += 1; |
| 189 | if let Err(e) = reply(vec![m.clone()], Vec::new()).build() { |
| 190 | refused += 1; |
| 191 | let _ = writeln!(f, "R\t{}\t{:?}\t{}", name, m, e.plain()); |
| 192 | } |
| 193 | }, |
| 194 | Err(e) => { |
| 195 | none += 1; |
| 196 | let _ = writeln!(f, "N\t{}\t{:?}\t{}", name, v, e.plain()); |
| 197 | }, |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | let _ = writeln!(f, "#\tmsgs={}\tfields={}\tmailboxes={}\tnot_mailboxes={}\treply_refused={}", |
| 203 | msgs, fields, boxes, none, refused); |
| 204 | } |
| 205 | |
| 206 | fn reply_to(f: &str) -> Vec<Mailbox> { |
| 207 | MessageView::parse(&msg(&fmt!("From: f@x.example\r\nReply-To: {}", f))).reply_to |
| 208 | } |
| 209 | |
| 210 | fn refused(addr: &str) { |
| 211 | let d = reply(vec![mb("A", addr)], Vec::new()); |
| 212 | assert!(d.build().is_err() && d.recipients().is_err(), "{:?} was taken", addr); |
| 213 | } |
| 214 | |
| 215 | /// READ A1: a bad member is refused whole, never read again from a comma inside its brackets. |
| 216 | #[test] |
| 217 | fn a_bad_member_is_refused_whole() { |
| 218 | assert_eq!(reply_to("A <ada@x.example,evil@y.example,>"), vec![]); |
| 219 | assert_eq!(reply_to("B <b@x.example>, A <ada@x.example,evil@y.example,>, c@z.example"), |
| 220 | vec![mb("B", "b@x.example"), mb("", "c@z.example")]); |
| 221 | } |
| 222 | |
| 223 | /// READ A2: an address holding `=?` is none, since a reader may take it for an encoded-word. |
| 224 | #[test] |
| 225 | fn an_address_holding_an_encoded_word_opener_is_none() { |
| 226 | assert_eq!(reply_to("=?utf-8?Q?evil@y.example,?= <ada@x.example>"), vec![mb("?=", "ada@x.example")]); |
| 227 | refused("=?utf-8?Q?evil@y.example"); |
| 228 | refused("a=?b@x.example"); |
| 229 | } |
| 230 | |
| 231 | /// READ A3: a domain literal holds only what RFC 5321 lets reach `RCPT TO`. |
| 232 | #[test] |
| 233 | fn a_domain_literal_carries_no_smtp_parameters() { |
| 234 | refused("ada@[x> NOTIFY=SUCCESS ORCPT=rfc822;evil@y.example <]"); |
| 235 | refused("ada@[192.0.2.1 x]"); |
| 236 | for addr in ["a@[192.0.2.1]", "a@[IPv6:2001:db8::1]"] { |
| 237 | assert_eq!(agree(&reply(vec![mb("", addr)], Vec::new())).0, vec![addr], "{:?}", addr); |
| 238 | } |
| 239 | } |