Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/delivery_dedupe.rs

8.3 KiB, 1 run

created by r1870400018:74936, 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//! `MailStore::deliver` files a sender's retry once, and the `hematite-msgids` index behind it
2//! never blocks a delivery nor loses one (F2a part 5, QA F2a4 M1 to M4).
3use oxedyne_fe2o3_core::prelude::*;
4use oxedyne_fe2o3_mail::maildir::MaildirStore;
5use oxedyne_fe2o3_net::mail::store::{
6 FolderName,
7 MailStore,
8 MailUser,
9 MessageFlags,
10};
11
12use std::{
13 collections::BTreeMap,
14 fs,
15 path::{
16 Path,
17 PathBuf,
18 },
19 time::{
20 Duration,
21 Instant,
22 },
23};
24
25
26fn user() -> MailUser {
27 MailUser {
28 local: fmt!("alice"),
29 domain: fmt!("test.local"),
30 delivery_key: fmt!("test.local/alice"),
31 send_as: Vec::new(),
32 }
33}
34
35fn fresh(name: &str) -> Outcome<(PathBuf, MaildirStore)> {
36 let root = std::env::temp_dir().join(fmt!("fe2o3_dedupe_{}_{}", name, std::process::id()));
37 let _ = fs::remove_dir_all(&root);
38 res!(fs::create_dir_all(&root), IO, File);
39 let store = res!(MaildirStore::new(root.clone(), "test.local"));
40 res!(store.ensure_user(&user()));
41 Ok((root, store))
42}
43
44fn inbox_dir(root: &Path) -> PathBuf { root.join("test.local").join("alice") }
45
46fn flags() -> MessageFlags { MessageFlags { recent: true, ..MessageFlags::default() } }
47
48fn with_id(id: &str, tag: &str) -> Vec<u8> {
49 fmt!("From: bob@example.org\r\nTo: alice@test.local\r\nMessage-ID: {}\r\nSubject: {}\r\n\r\nid={}\r\n",
50 id, tag, tag).into_bytes()
51}
52
53/// `bytes` as a later hop delivers it: the fields relays and verifiers add, prepended.
54fn in_transit(bytes: &[u8], hop: &str) -> Vec<u8> {
55 let mut out = fmt!(
56 "Return-Path: <bob@example.org>\r\nDelivered-To: alice@test.local\r\n\
57 X-Original-To: alice@test.local\r\nReceived: from {} by mx; Mon, 26 Oct 2026 10:00:00 +0000\r\n\
58 Received-SPF: pass\r\nAuthentication-Results: mx; dkim=pass\r\nARC-Seal: i=1; cv=none; b={}\r\n\
59 DKIM-Signature: v=1; t={}; b=abc\r\n\tdef\r\n",
60 hop, hop, hop).into_bytes();
61 out.extend_from_slice(bytes);
62 out
63}
64
65/// UID → `id=` tag for every message in the INBOX.
66fn tags(store: &MaildirStore) -> Outcome<BTreeMap<u32, String>> {
67 let (u, f) = (user(), FolderName::new("INBOX"));
68 let mut out = BTreeMap::new();
69 for m in res!(store.list_messages(&u, &f, true)) {
70 if let Some(b) = res!(store.fetch_bytes(&u, &f, m.uid)) {
71 let s = String::from_utf8_lossy(&b).to_string();
72 let tag = match s.find("id=") { Some(i) => s[i + 3..].trim().to_string(), None => String::new() };
73 out.insert(m.uid.0, tag);
74 }
75 }
76 Ok(out)
77}
78
79fn is_root() -> bool {
80 match fs::read_to_string("/proc/self/status") {
81 Ok(s) => s.lines().any(|l| l.starts_with("Uid:") && l.split_whitespace().nth(2) == Some("0")),
82 Err(_) => false,
83 }
84}
85
86/// M1: a file the store cannot read is a Message-ID unknown, not a failed delivery.
87#[cfg(unix)]
88#[test]
89fn an_unreadable_file_does_not_block_delivery() -> Outcome<()> {
90 use std::os::unix::fs::PermissionsExt;
91 if is_root() { return Ok(()); } // root reads a 000 file
92 let (root, store) = res!(fresh("m1"));
93 let (u, ib) = (user(), FolderName::new("INBOX"));
94 let p = inbox_dir(&root).join("new").join("1.M1P1.other");
95 res!(fs::write(&p, with_id("<o@x>", "o")), IO, File);
96 res!(fs::set_permissions(&p, fs::Permissions::from_mode(0o000)), IO, File);
97 let r1 = store.deliver(&u, &ib, &with_id("<n@x>", "n"), flags(), None);
98 let r2 = store.deliver(&u, &ib, &with_id("<o@x>", "o"), flags(), None);
99 res!(fs::set_permissions(&p, fs::Permissions::from_mode(0o600)), IO, File);
100 assert!(r1.is_ok(), "deliver failed on an unreadable neighbour: {:?}", r1.err());
101 assert!(r2.is_ok(), "deliver failed on an unreadable neighbour: {:?}", r2.err());
102 let t = res!(tags(&store));
103 assert_eq!(t.values().filter(|v| *v == "o").count(), 2,
104 "an unreadable copy is unknown, so the delivery is filed: {:?}", t);
105 let _ = fs::remove_dir_all(&root);
106 Ok(())
107}
108
109/// M2: a row of the index naming the wrong Message-ID never drops a delivery.
110#[test]
111fn a_damaged_index_row_never_loses_a_message() -> Outcome<()> {
112 let (root, store) = res!(fresh("m2"));
113 let (u, ib) = (user(), FolderName::new("INBOX"));
114 res!(store.deliver(&u, &ib, &with_id("<a@x>", "a"), flags(), None));
115 res!(store.deliver(&u, &ib, &with_id("<b@x>", "b"), flags(), None));
116 let idx = inbox_dir(&root).join("hematite-msgids");
117 let text = res!(fs::read_to_string(&idx), IO, File);
118 assert!(text.contains("<a@x>"), "a is indexed: {:?}", text);
119 res!(fs::write(&idx, text.replace("<a@x>", "<z@x>")), IO, File);
120 res!(store.deliver(&u, &ib, &with_id("<z@x>", "z"), flags(), None));
121 let t = res!(tags(&store));
122 assert!(t.values().any(|v| v == "z"), "z was dropped on a damaged row: {:?}", t);
123 assert_eq!(t.len(), 3);
124 // The row is corrected, so a's retry is still filed once.
125 res!(store.deliver(&u, &ib, &with_id("<a@x>", "a"), flags(), None));
126 assert_eq!(res!(tags(&store)).len(), 3);
127 let _ = fs::remove_dir_all(&root);
128 Ok(())
129}
130
131/// M3: only an identical resend is dropped. The fields added in transit do not count; a change
132/// to any field the sender set, or to the body, does.
133#[test]
134fn only_an_identical_resend_is_dropped() -> Outcome<()> {
135 let (root, store) = res!(fresh("m3"));
136 let (u, ib) = (user(), FolderName::new("INBOX"));
137 let sent = with_id("<a@x>", "a");
138 let first = res!(store.deliver(&u, &ib, &in_transit(&sent, "hop1"), flags(), None));
139 let retry = res!(store.deliver(&u, &ib, &in_transit(&sent, "hop2"), flags(), None));
140 let bare = res!(store.deliver(&u, &ib, &sent, flags(), None));
141 assert_eq!((first, first), (retry, bare), "a resend differing only in transit fields is one");
142 assert_eq!(res!(tags(&store)).len(), 1);
143
144 let body = res!(store.deliver(&u, &ib, &with_id("<a@x>", "a-other-body"), flags(), None));
145 let mut subj = sent.clone();
146 let at = sent.windows(9).position(|w| w == b"Subject: ").unwrap_or(0) + 9;
147 subj.insert(at, b'X');
148 let field = res!(store.deliver(&u, &ib, &subj, flags(), None));
149 let resent = [b"Resent-From: carol@example.org\r\n".as_slice(), &sent].concat();
150 let redirect = res!(store.deliver(&u, &ib, &resent, flags(), None));
151 assert!(body != first && field != first && redirect != first && body != field && field != redirect);
152 assert_eq!(res!(tags(&store)).len(), 4, "a reused Message-ID with other content is filed");
153 let _ = fs::remove_dir_all(&root);
154 Ok(())
155}
156
157/// M4: a delivery is linear in the folder's size. Run with
158/// `cargo test -p oxedyne_fe2o3_mail --test delivery_dedupe -- --ignored --nocapture`.
159#[test]
160#[ignore]
161fn delivery_is_linear_in_the_folder() -> Outcome<()> {
162 let mut times: Vec<(usize, Duration, Duration)> = Vec::new();
163 for n in [5_000usize, 10_000, 20_000] {
164 let (root, store) = res!(fresh(&fmt!("m4_{}", n)));
165 let (u, ib) = (user(), FolderName::new("INBOX"));
166 let pad = fmt!("X-Pad: {}\r\n", "p".repeat(70));
167 let big = |i: usize| fmt!("From: b@x\r\nMessage-ID: <big{}@x>\r\n{}{}Subject: s\r\n\r\nid=big{}\r\n{}",
168 i, pad, pad, i, "body ".repeat(400)).into_bytes();
169 for i in 0..n {
170 res!(fs::write(inbox_dir(&root).join("cur").join(fmt!("{}.M{}P1.big:2,S", 1_000_000 + i, i)), big(i)), IO, File);
171 }
172 res!(store.list_messages(&u, &ib, false));
173 let t = Instant::now();
174 res!(store.deliver(&u, &ib, &with_id("<new0@x>", "new0"), flags(), None));
175 let build = t.elapsed();
176 let mut best = Duration::MAX;
177 for k in 1..4 {
178 let t = Instant::now();
179 res!(store.deliver(&u, &ib, &with_id(&fmt!("<new{}@x>", k), "new"), flags(), None));
180 best = best.min(t.elapsed());
181 }
182 let t = Instant::now();
183 res!(store.deliver(&u, &ib, &in_transit(&big(7), "hop"), flags(), None));
184 let hit = t.elapsed();
185 assert_eq!(res!(store.list_messages(&u, &ib, false)).len(), n + 4, "big7 is a retry");
186 println!("M4 n={} index build {:?}, deliver {:?}, retry hit {:?}", n, build, best, hit);
187 times.push((n, build, best));
188 let _ = fs::remove_dir_all(&root);
189 }
190 let ratio = times[2].2.as_secs_f64() / times[0].2.as_secs_f64();
191 println!("M4 deliver 20k / 5k = {:.1}", ratio);
192 assert!(ratio < 8.0, "x4 messages made deliver x{:.1}, more than linear", ratio);
193 Ok(())
194}