Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/imap_search.rs

19.4 KiB, 2 runs

created by r1870400018:74754, 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//! Steel's IMAP SEARCH against a real Maildir over a real socket.
2//!
3//! A client filter, a move and the send-and-file step all trust SEARCH to
4//! name exactly the messages that match. A criterion the server does not
5//! understand must be refused with a tagged BAD: answering it with the whole
6//! mailbox turns a narrow action into one on every message.
7
8use oxedyne_fe2o3_core::prelude::*;
9use oxedyne_fe2o3_mail::maildir::MaildirStore;
10use oxedyne_fe2o3_net::{
11 imap::server::ImapServer,
12 mail::{
13 store::{
14 FolderName,
15 MailStore,
16 MailUser,
17 MessageFlags,
18 MessageUid,
19 },
20 user::UserStore,
21 },
22};
23
24use std::{
25 fs,
26 path::PathBuf,
27 time::{
28 Duration,
29 UNIX_EPOCH,
30 },
31};
32
33use tokio::{
34 io::{
35 AsyncBufReadExt,
36 AsyncWriteExt,
37 BufReader,
38 },
39 net::{
40 TcpListener,
41 TcpStream,
42 },
43};
44
45
46const USER: &str = "alice@test.local";
47const PASS: &str = "correct-horse";
48
49#[derive(Clone, Debug)]
50struct OneUser;
51
52impl UserStore for OneUser {
53 fn authenticate(&self, address: &str, password: &str) -> Outcome<Option<MailUser>> {
54 if address.eq_ignore_ascii_case(USER) && password == PASS {
55 return self.lookup(address);
56 }
57 Ok(None)
58 }
59
60 fn lookup(&self, address: &str) -> Outcome<Option<MailUser>> {
61 if !address.eq_ignore_ascii_case(USER) { return Ok(None); }
62 Ok(Some(MailUser {
63 local: fmt!("alice"),
64 domain: fmt!("test.local"),
65 delivery_key: fmt!("test.local/alice"),
66 send_as: Vec::new(),
67 }))
68 }
69}
70
71/// One message to file, and the INTERNALDATE (days after the epoch) it is given.
72struct Msg {
73 from: &'static str,
74 to: &'static str,
75 cc: &'static str,
76 subj: &'static str,
77 mid: &'static str,
78 body: &'static str,
79 day: u64,
80 flags: MessageFlags,
81}
82
83impl Msg {
84 fn bytes(&self) -> Vec<u8> {
85 let mut s = fmt!("From: {}\r\nTo: {}\r\n", self.from, self.to);
86 if !self.cc.is_empty() { s.push_str(&fmt!("Cc: {}\r\n", self.cc)); }
87 s.push_str(&fmt!("Subject: {}\r\nMessage-ID: {}\r\n\r\n{}\r\n", self.subj, self.mid, self.body));
88 s.into_bytes()
89 }
90}
91
92fn corpus() -> Vec<Msg> {
93 let seen = MessageFlags { seen: true, ..MessageFlags::default() };
94 let ans = MessageFlags { answered: true, seen: true, ..MessageFlags::default() };
95 let draft = MessageFlags { draft: true, ..MessageFlags::default() };
96 vec![
97 // 2024-01-01 is day 19723 after the epoch.
98 Msg { from: "Bob <bob@example.org>", to: "alice@test.local", cc: "",
99 subj: "Quarterly Report", mid: "<a1@example.org>",
100 body: "The figures are attached.", day: 19723, flags: seen },
101 Msg { from: "carol@example.net", to: "alice@test.local", cc: "dave@example.com",
102 subj: "Lunch", mid: "<b2@example.net>",
103 body: "Shall we meet at noon? The REPORT can wait.", day: 19724, flags: MessageFlags::default() },
104 Msg { from: "Bob <bob@example.org>", to: "team@test.local", cc: "",
105 subj: "re: lunch", mid: "<c3@example.org>",
106 body: "Yes, noon suits me. xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
107 day: 19725, flags: ans },
108 Msg { from: "erin@example.com", to: "alice@test.local", cc: "bob@example.org",
109 subj: "Draft notes", mid: "<d4@example.com>",
110 body: "Nothing to see.", day: 19726, flags: draft },
111 ]
112}
113
114async fn read_until_tagged(r: &mut BufReader<tokio::net::tcp::OwnedReadHalf>, tag: &str)
115 -> Outcome<Vec<String>>
116{
117 let mut lines = Vec::new();
118 loop {
119 let mut line = String::new();
120 let n = res!(r.read_line(&mut line).await, IO, Network);
121 if n == 0 { return Err(err!("The server closed the connection."; IO, Network)); }
122 let done = line.starts_with(&fmt!("{} ", tag));
123 lines.push(line.trim_end().to_string());
124 if done { return Ok(lines); }
125 }
126}
127
128struct Conn {
129 r: BufReader<tokio::net::tcp::OwnedReadHalf>,
130 w: tokio::net::tcp::OwnedWriteHalf,
131 n: usize,
132}
133
134impl Conn {
135 async fn cmd(&mut self, text: &str) -> Outcome<Vec<String>> {
136 self.n += 1;
137 let tag = fmt!("t{}", self.n);
138 res!(self.w.write_all(fmt!("{} {}\r\n", tag, text).as_bytes()).await, IO, Network);
139 read_until_tagged(&mut self.r, &tag).await
140 }
141
142 /// The UIDs a `UID SEARCH` names, or the tagged status when it is not OK.
143 async fn uid_search(&mut self, criteria: &str) -> Outcome<Result<Vec<u32>, String>> {
144 let lines = res!(self.cmd(&fmt!("UID SEARCH {}", criteria)).await);
145 let last = lines.last().cloned().unwrap_or_default();
146 if !last.contains(" OK ") { return Ok(Err(last)); }
147 let mut out = Vec::new();
148 for l in &lines {
149 if let Some(rest) = l.strip_prefix("* SEARCH") {
150 for t in rest.split_whitespace() {
151 out.push(res!(t.parse::<u32>(), Decode));
152 }
153 }
154 }
155 out.sort();
156 Ok(Ok(out))
157 }
158}
159
160/// A Maildir holding `msgs` (bytes, flags, INTERNALDATE in days after the epoch), served over
161/// a socket, logged in with INBOX selected; with the UIDs given, in order, and the root.
162async fn serve(name: &str, msgs: Vec<(Vec<u8>, MessageFlags, u64)>) -> Outcome<(Conn, Vec<u32>, PathBuf)> {
163 let root = std::env::temp_dir().join(fmt!("fe2o3_imap_{}_{}", name, std::process::id()));
164 let _ = fs::remove_dir_all(&root);
165 res!(fs::create_dir_all(&root), IO, File);
166 let store = res!(MaildirStore::new(PathBuf::from(&root), "test.local"));
167 let user = match res!(OneUser.lookup(USER)) {
168 Some(u) => u,
169 None => return Err(err!("The test user did not resolve."; Test, Bug)),
170 };
171 res!(store.ensure_user(&user));
172 let inbox = FolderName::new("INBOX");
173 let mut uids: Vec<u32> = Vec::new();
174 for (bytes, flags, day) in msgs {
175 let t = UNIX_EPOCH + Duration::from_secs(day * 86_400 + 3_600);
176 let MessageUid(u) = res!(store.append(&user, &inbox, &bytes, flags, Some(t)));
177 uids.push(u);
178 }
179
180 let listener = res!(TcpListener::bind("127.0.0.1:0").await, IO, Network);
181 let addr = res!(listener.local_addr(), IO, Network);
182 let server = ImapServer {
183 store: store.clone(),
184 users: OneUser,
185 hostname: std::sync::Arc::new(fmt!("test.local")),
186 };
187 tokio::spawn(async move {
188 while let Ok((sock, peer)) = listener.accept().await {
189 let srv = server.clone();
190 tokio::spawn(async move { let _ = srv.run(sock, peer).await; });
191 }
192 });
193 let sock = res!(TcpStream::connect(addr).await, IO, Network);
194 let (r, w) = sock.into_split();
195 let mut c = Conn { r: BufReader::new(r), w, n: 0 };
196 let mut greeting = String::new();
197 res!(c.r.read_line(&mut greeting).await, IO, Network);
198 res!(c.cmd(&fmt!("LOGIN {} {}", USER, PASS)).await);
199 res!(c.cmd("SELECT INBOX").await);
200 Ok((c, uids, root))
201}
202
203#[tokio::test]
204async fn search_finds_exactly_what_matches_and_refuses_what_it_cannot() -> Outcome<()> {
205 let msgs = corpus().iter().map(|m| (m.bytes(), m.flags, m.day)).collect();
206 let (mut c, uids, root) = res!(serve("search", msgs).await);
207 // Every UID, in corpus order, so `pick(&[0, 2])` is messages 1 and 3.
208 let pick = |ix: &[usize]| -> Vec<u32> {
209 let mut v: Vec<u32> = ix.iter().map(|i| uids[*i]).collect();
210 v.sort();
211 v
212 };
213
214 let uid_range = fmt!("UID {}:{}", uids[1], uids[2]);
215 let cases: Vec<(&str, Vec<u32>)> = vec![
216 ("ALL", pick(&[0, 1, 2, 3])),
217 ("HEADER Message-ID \"<c3@example.org>\"", pick(&[2])),
218 ("HEADER message-id <b2@EXAMPLE.net>", pick(&[1])),
219 ("HEADER Message-ID \"<nowhere@example>\"", pick(&[])),
220 ("HEADER Cc \"\"", pick(&[1, 3])),
221 ("FROM bob", pick(&[0, 2])),
222 ("TO team@", pick(&[2])),
223 ("CC example.com", pick(&[1])),
224 ("BCC anyone", pick(&[])),
225 ("SUBJECT LUNCH", pick(&[1, 2])),
226 ("BODY report", pick(&[1])),
227 ("TEXT report", pick(&[0, 1])),
228 ("SINCE 3-Jan-2024", pick(&[2, 3])),
229 ("BEFORE 3-Jan-2024", pick(&[0, 1])),
230 ("ON \"2-Jan-2024\"", pick(&[1])),
231 ("LARGER 200", pick(&[2])),
232 ("SMALLER 200", pick(&[0, 1, 3])),
233 ("NOT FROM bob", pick(&[1, 3])),
234 ("OR FROM carol FROM erin", pick(&[1, 3])),
235 ("FROM bob SUBJECT lunch", pick(&[2])),
236 ("(FROM bob) (OR SEEN DRAFT)", pick(&[0, 2])),
237 ("SEEN", pick(&[0, 2])),
238 ("UNSEEN", pick(&[1, 3])),
239 ("ANSWERED", pick(&[2])),
240 ("UNANSWERED", pick(&[0, 1, 3])),
241 ("DRAFT", pick(&[3])),
242 ("UNDRAFT", pick(&[0, 1, 2])),
243 ("UNFLAGGED", pick(&[0, 1, 2, 3])),
244 ("UNDELETED", pick(&[0, 1, 2, 3])),
245 ("CHARSET UTF-8 SUBJECT report", pick(&[0])),
246 (uid_range.as_str(), pick(&[1, 2])),
247 ("2:3", pick(&[1, 2])),
248 ];
249 let mut failures = Vec::new();
250 for (q, want) in &cases {
251 match res!(c.uid_search(q).await) {
252 Ok(got) if &got == want => (),
253 other => failures.push(fmt!("{}: want {:?}, got {:?}", q, want, other)),
254 }
255 }
256 for q in ["FOO", "HEADER Message-ID", "SINCE notadate", "OR FROM bob", "LARGER x",
257 "KEYWORD", "(FROM bob", "CHARSET KOI8-R FROM bob"]
258 {
259 match res!(c.uid_search(q).await) {
260 Err(line) if line.contains(" BAD ") || line.contains(" NO ") => (),
261 other => failures.push(fmt!("{}: want BAD, got {:?}", q, other)),
262 }
263 }
264 // Every RFC 3501 key parses and is answered OK, alone and as the client sends it.
265 for q in ["ALL", "ANSWERED", "BCC x", "BEFORE 1-Jan-2030", "BODY x", "CC x", "DELETED",
266 "DRAFT", "FLAGGED", "FROM x", "HEADER X-Foo y", "KEYWORD $Junk", "LARGER 1", "NEW",
267 "NOT SEEN", "OLD", "ON 1-Jan-2024", "OR SEEN FLAGGED", "RECENT", "SEEN",
268 "SENTBEFORE 1-Jan-2030", "SENTON 1-Jan-2024", "SENTSINCE 1-Jan-2000", "SINCE 1-Jan-2000",
269 "SMALLER 10000", "SUBJECT x", "TEXT x", "TO x", "UID 1:*", "UNANSWERED", "UNDELETED",
270 "UNDRAFT", "UNFLAGGED", "UNKEYWORD $Junk", "UNSEEN", "1:*", "1,2:3", "(SEEN FLAGGED)",
271 "CHARSET US-ASCII ALL", "charset utf-8 not deleted", "uid 1:* undeleted"]
272 {
273 if let Err(line) = res!(c.uid_search(q).await) {
274 failures.push(fmt!("{}: a standard key was refused: {}", q, line));
275 }
276 }
277 // A synchronising literal, as Thunderbird sends a non-ASCII string.
278 res!(c.w.write_all(b"lit UID SEARCH SUBJECT {5}\r\n").await, IO, Network);
279 let mut cont = String::new();
280 res!(c.r.read_line(&mut cont).await, IO, Network);
281 res!(c.w.write_all(b"LUNCH\r\n").await, IO, Network);
282 let lines = res!(read_until_tagged(&mut c.r, "lit").await);
283 let want = fmt!("* SEARCH {} {}", uids[1], uids[2]);
284 if !cont.starts_with('+') || !lines.iter().any(|l| *l == want) {
285 failures.push(fmt!("SUBJECT literal: continuation {:?}, then {:?}", cont, lines));
286 }
287 let lines = res!(c.cmd("SEARCH FROM carol").await);
288 if !lines.iter().any(|l| l == "* SEARCH 2") {
289 failures.push(fmt!("SEARCH FROM carol by sequence: {:?}", lines));
290 }
291 assert!(failures.is_empty(), "SEARCH got these wrong:\n{}", failures.join("\n"));
292 let _ = fs::remove_dir_all(&root);
293 Ok(())
294}
295
296/// A non-ASCII subject or sender name travels as RFC 2047 encoded-words, and a search must
297/// match what the reader sees, not the encoding.
298#[tokio::test]
299async fn search_matches_header_values_with_their_encoded_words_decoded() -> Outcome<()> {
300 let plain = MessageFlags::default();
301 let msgs = vec![
302 // Subject "Grüße aus Köln", base64.
303 (b"From: Bob <bob@example.org>\r\nTo: alice@test.local\r\n\
304 Subject: =?UTF-8?B?R3LDvMOfZSBhdXMgS8O2bG4=?=\r\n\r\nHallo.\r\n".to_vec(), plain, 19723),
305 // From "Renée Dupont", Q-encoded.
306 (b"From: =?utf-8?Q?Ren=C3=A9e_Dupont?= <renee@example.fr>\r\nTo: alice@test.local\r\n\
307 Subject: Lunch\r\n\r\nMidi ?\r\n".to_vec(), plain, 19724),
308 (b"From: carol@example.net\r\nTo: alice@test.local\r\n\
309 Subject: Plain\r\n\r\nNothing encoded.\r\n".to_vec(), plain, 19725),
310 ];
311 let (mut c, uids, root) = res!(serve("search2047", msgs).await);
312 let mut failures = Vec::new();
313
314 // A non-ASCII needle arrives as a literal, as Thunderbird sends it.
315 for (needle, want) in [("grüße", uids[0]), ("KÖLN", uids[0])] {
316 let n = needle.as_bytes().len();
317 res!(c.w.write_all(fmt!("lit UID SEARCH CHARSET UTF-8 SUBJECT {{{}}}\r\n", n).as_bytes()).await,
318 IO, Network);
319 let mut cont = String::new();
320 res!(c.r.read_line(&mut cont).await, IO, Network);
321 res!(c.w.write_all(needle.as_bytes()).await, IO, Network);
322 res!(c.w.write_all(b"\r\n").await, IO, Network);
323 let lines = res!(read_until_tagged(&mut c.r, "lit").await);
324 if !lines.iter().any(|l| *l == fmt!("* SEARCH {}", want)) {
325 failures.push(fmt!("SUBJECT {}: continuation {:?}, then {:?}", needle, cont, lines));
326 }
327 }
328 let cases: Vec<(&str, Vec<u32>)> = vec![
329 ("FROM \"Dupont\"", vec![uids[1]]),
330 ("FROM \"e dupont\"", vec![uids[1]]), // `_` is a space in Q
331 ("HEADER From \"Renee Dupont\"", vec![]),
332 ("TEXT \"aus k\"", vec![uids[0]]),
333 ("SUBJECT \"R3LD\"", vec![]),
334 ("SUBJECT plain", vec![uids[2]]),
335 ];
336 for (q, want) in &cases {
337 match res!(c.uid_search(q).await) {
338 Ok(got) if &got == want => (),
339 other => failures.push(fmt!("{}: want {:?}, got {:?}", q, want, other)),
340 }
341 }
342 assert!(failures.is_empty(), "SEARCH missed decoded headers:\n{}", failures.join("\n"));
343 let _ = fs::remove_dir_all(&root);
344 Ok(())
345}
346
347#[tokio::test]
348async fn sent_keys_read_a_date_whose_weekday_has_no_comma() -> Outcome<()> {
349 let plain = MessageFlags::default();
350 let msgs = vec![
351 (b"From: bob@example.org\r\nTo: alice@test.local\r\nSubject: comma\r\n\
352 Date: Tue, 27 Oct 2026 10:00:00 +0000\r\n\r\nA.\r\n".to_vec(), plain, 20388),
353 // Not RFC 5322, but sent by some clients.
354 (b"From: bob@example.org\r\nTo: alice@test.local\r\nSubject: bare\r\n\
355 Date: Tue 27 Oct 2026 10:00:00 +0000\r\n\r\nB.\r\n".to_vec(), plain, 20388),
356 (b"From: bob@example.org\r\nTo: alice@test.local\r\nSubject: none\r\n\
357 Date: 27 Oct 2026 10:00:00 +0000\r\n\r\nC.\r\n".to_vec(), plain, 20388),
358 ];
359 let (mut c, uids, root) = res!(serve("sentweekday", msgs).await);
360 let mut failures = Vec::new();
361 for q in ["SENTON 27-Oct-2026", "SENTSINCE 27-Oct-2026", "SENTBEFORE 28-Oct-2026"] {
362 match res!(c.uid_search(q).await) {
363 Ok(got) if got == uids => (),
364 other => failures.push(fmt!("{}: want {:?}, got {:?}", q, uids, other)),
365 }
366 }
367 assert!(failures.is_empty(), "SENT* missed a Date:\n{}", failures.join("\n"));
368 let _ = fs::remove_dir_all(&root);
369 Ok(())
370}
371
372/// BODY and TEXT look at the text a reader sees, not the transfer encoding it travels in:
373/// a word in a base64 text/plain part, or in a quoted-printable text/html part of a
374/// multipart, in Latin-1 or UTF-8, is found; one only in an attachment's file name is not.
375#[tokio::test]
376async fn search_body_reads_decoded_text_parts() -> Outcome<()> {
377 let seen = MessageFlags::default();
378 // "Grüße aus Köln, zebrafink" in UTF-8, base64.
379 let b64 = "From: a@example.org\r\nSubject: one\r\nMIME-Version: 1.0\r\n\
380 Content-Type: text/plain; charset=utf-8\r\nContent-Transfer-Encoding: base64\r\n\r\n\
381 R3LDvMOfZSBhdXMg\r\nS8O2bG4sIHplYnJhZmluaw==\r\n".as_bytes().to_vec();
382 // A multipart/alternative whose HTML part, Latin-1 quoted-printable, says "Caf=E9 kolibri"
383 // with the word broken across a soft line break.
384 let qp = "From: b@example.org\r\nSubject: two\r\nMIME-Version: 1.0\r\n\
385 Content-Type: multipart/alternative; boundary=\"XX\"\r\n\r\npreamble\r\n\
386 --XX\r\nContent-Type: text/plain; charset=us-ascii\r\n\r\nsee the html\r\n\
387 --XX\r\nContent-Type: text/html; charset=iso-8859-1\r\nContent-Transfer-Encoding: quoted-printable\r\n\r\n\
388 <p>Caf=E9 koli=\r\nbri</p>\r\n--XX--\r\n".as_bytes().to_vec();
389 let plain = "From: c@example.org\r\nSubject: three\r\n\r\nnothing here but R3LD\r\n".as_bytes().to_vec();
390 // A sender name in a Latin-1 encoded-word, and an attachment whose name and base64 body
391 // are not the message's text.
392 let latin = "From: =?ISO-8859-1?Q?Andr=E9?= <d@example.org>\r\nSubject: four\r\n\r\nhi\r\n"
393 .as_bytes().to_vec();
394 let att = "From: e@example.org\r\nSubject: five\r\nMIME-Version: 1.0\r\n\
395 Content-Type: multipart/mixed; boundary=\"YY\"\r\n\r\n\
396 --YY\r\nContent-Type: text/plain; charset=windows-1252\r\n\
397 Content-Transfer-Encoding: quoted-printable\r\n\r\n=93quoted=94 text\r\n\
398 --YY\r\nContent-Type: application/pdf; name=\"wombat.pdf\"\r\n\
399 Content-Disposition: attachment; filename=\"wombat.pdf\"\r\n\
400 Content-Transfer-Encoding: base64\r\n\r\nd29tYmF0IHBkZg==\r\n--YY--\r\n".as_bytes().to_vec();
401 let msgs = vec![
402 (b64, seen, 19723), (qp, seen, 19724), (plain, seen, 19725),
403 (latin, seen, 19726), (att, seen, 19727),
404 ];
405 let (mut c, uids, root) = res!(serve("search_body", msgs).await);
406 let cases: Vec<(&str, Vec<u32>)> = vec![
407 ("BODY zebrafink", vec![uids[0]]),
408 ("CHARSET UTF-8 BODY \"köln\"", vec![uids[0]]),
409 ("BODY kolibri", vec![uids[1]]),
410 ("CHARSET UTF-8 BODY \"café\"", vec![uids[1]]),
411 ("TEXT kolibri", vec![uids[1]]),
412 ("BODY R3LD", vec![uids[2]]),
413 ("BODY preamble", vec![]),
414 ("CHARSET UTF-8 FROM \"andré\"", vec![uids[3]]),
415 ("CHARSET UTF-8 BODY \"\u{201c}quoted\u{201d}\"", vec![uids[4]]),
416 ("BODY wombat", vec![]),
417 ("BODY d29tYmF0", vec![]),
418 ];
419 let mut failures = Vec::new();
420 for (q, want) in &cases {
421 match res!(c.uid_search(q).await) {
422 Ok(got) if &got == want => (),
423 other => failures.push(fmt!("{}: want {:?}, got {:?}", q, want, other)),
424 }
425 }
426 let _ = fs::remove_dir_all(&root);
427 assert!(failures.is_empty(), "SEARCH BODY:\n{}", failures.join("\n"));
428 Ok(())
429}