Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/imap_sessions.rs

18.6 KiB, 1 run

created by r1870400018:74940, 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//! Two IMAP sessions on one folder, over the real server and a real Maildir.
2//!
3//! Thunderbird and a phone on one account are two sessions. A flag one sets must not be
4//! undone by the other's stale copy, and a message the other expunges must not shift this
5//! session's sequence numbers until it has been told (RFC 3501 §7.4.1).
6//! From QA F2a's `qa_imap.rs` (D-20261026-01, M1 and M2).
7
8use oxedyne_fe2o3_core::{
9 file::ExclusiveLock,
10 prelude::*,
11};
12use oxedyne_fe2o3_mail::maildir::MaildirStore;
13use oxedyne_fe2o3_net::{
14 imap::server::ImapServer,
15 mail::{
16 store::{
17 FolderName,
18 MailStore,
19 MailUser,
20 MessageFlags,
21 },
22 user::UserStore,
23 },
24};
25
26use std::{
27 fs,
28 path::PathBuf,
29 sync::Arc,
30 time::Duration,
31};
32
33use tokio::{
34 io::{
35 AsyncBufReadExt,
36 AsyncWriteExt,
37 BufReader,
38 },
39 net::{
40 TcpListener,
41 TcpStream,
42 },
43};
44
45
46#[derive(Clone, Debug)]
47struct OneUser;
48
49impl UserStore for OneUser {
50 fn authenticate(&self, _a: &str, _p: &str) -> Outcome<Option<MailUser>> { self.lookup("") }
51 fn lookup(&self, _a: &str) -> Outcome<Option<MailUser>> {
52 Ok(Some(MailUser {
53 local: fmt!("alice"),
54 domain: fmt!("test.local"),
55 delivery_key: fmt!("test.local/alice"),
56 send_as: Vec::new(),
57 }))
58 }
59}
60
61/// One logged-in session with INBOX selected.
62struct Sess {
63 r: BufReader<TcpStream>,
64 n: u32,
65}
66
67impl Sess {
68 async fn open(port: u16) -> Outcome<Self> {
69 let t = res!(TcpStream::connect(("127.0.0.1", port)).await, IO, Network);
70 let mut s = Sess { r: BufReader::new(t), n: 0 };
71 let mut l = String::new();
72 res!(s.r.read_line(&mut l).await, IO, Network);
73 res!(s.cmd("LOGIN alice@test.local pw").await);
74 res!(s.cmd("SELECT INBOX").await);
75 Ok(s)
76 }
77
78 /// Every line of the reply, the tagged one last.
79 async fn cmd(&mut self, c: &str) -> Outcome<String> {
80 self.n += 1;
81 let tag = fmt!("t{}", self.n);
82 res!(self.r.get_mut().write_all(fmt!("{} {}\r\n", tag, c).as_bytes()).await, IO, Network);
83 let mut out = String::new();
84 loop {
85 let mut l = String::new();
86 if res!(self.r.read_line(&mut l).await, IO, Network) == 0 {
87 return Err(err!("The server closed the connection."; IO, Network));
88 }
89 out.push_str(&l);
90 if l.starts_with(&fmt!("{} ", tag)) { return Ok(out); }
91 }
92 }
93}
94
95/// A Maildir with `n` messages served on a local port.
96async fn setup(name: &str, n: usize) -> Outcome<(u16, PathBuf)> {
97 setup_waiting(name, n, Duration::from_secs(30)).await
98}
99
100/// As `setup`, with the store's lock wait set to `wait`.
101async fn setup_waiting(name: &str, n: usize, wait: Duration) -> Outcome<(u16, PathBuf)> {
102 let root = std::env::temp_dir().join(fmt!("fe2o3_imap_sessions_{}_{}", name, std::process::id()));
103 let _ = fs::remove_dir_all(&root);
104 res!(fs::create_dir_all(&root), IO, File);
105 let store = res!(MaildirStore::new(root.clone(), "test.local")).with_lock_wait(wait);
106 let u = match res!(OneUser.lookup("")) {
107 Some(u) => u,
108 None => return Err(err!("The test user did not resolve."; Test, Bug)),
109 };
110 res!(store.ensure_user(&u));
111 for i in 1..=n {
112 let bytes = fmt!("Subject: m{}\r\n\r\nid=m{}\r\n", i, i);
113 res!(store.append(&u, &FolderName::new("INBOX"), bytes.as_bytes(), MessageFlags::default(), None));
114 }
115 let lis = res!(TcpListener::bind("127.0.0.1:0").await, IO, Network);
116 let port = res!(lis.local_addr(), IO, Network).port();
117 let srv = ImapServer { store, users: OneUser, hostname: Arc::new(fmt!("test.local")) };
118 tokio::spawn(async move {
119 while let Ok((sock, peer)) = lis.accept().await {
120 let s = srv.clone();
121 tokio::spawn(async move { let _ = s.run(sock, peer).await; });
122 }
123 });
124 Ok((port, root))
125}
126
127/// The FLAGS a `UID FETCH n (FLAGS)` reply gives UID `uid`, if it names it.
128fn flags_of(reply: &str, uid: u32) -> Option<String> {
129 for l in reply.lines() {
130 if l.contains(&fmt!("UID {} ", uid)) || l.contains(&fmt!("UID {})", uid)) {
131 if let Some(i) = l.find("FLAGS (") {
132 let rest = &l[i + 7..];
133 return rest.find(')').map(|j| rest[..j].to_string());
134 }
135 }
136 }
137 None
138}
139
140/// Another session expunges UID 1; this one, not yet told, marks its message 2, which is UID 2.
141#[tokio::test(flavor = "multi_thread")]
142async fn sequence_numbers_hold_until_the_expunge_is_announced() -> Outcome<()> {
143 let (port, root) = res!(setup("seq", 3).await);
144 let mut a = res!(Sess::open(port).await);
145 let mut b = res!(Sess::open(port).await);
146 res!(b.cmd("UID STORE 1 +FLAGS (\\Deleted)").await);
147 res!(b.cmd("EXPUNGE").await);
148
149 let store = res!(a.cmd("STORE 2 +FLAGS (\\Deleted)").await);
150 assert!(!store.contains("EXPUNGE"), "an EXPUNGE was sent during STORE:\n{}", store);
151 assert!(store.contains("* 2 FETCH (FLAGS (\\Deleted))"), "STORE 2 answered:\n{}", store);
152
153 let fetch = res!(a.cmd("UID FETCH 1:* (UID FLAGS)").await);
154 assert!(fetch.starts_with("* 1 EXPUNGE\r\n"), "the expunge of UID 1 was not announced first:\n{}", fetch);
155 assert_eq!(flags_of(&fetch, 2).as_deref(), Some("\\Deleted"), "UID 2 after STORE 2:\n{}", fetch);
156 assert_eq!(flags_of(&fetch, 3).as_deref(), Some(""), "UID 3 after STORE 2:\n{}", fetch);
157 assert!(fetch.contains("* 1 FETCH (UID 2 "), "UID 2 is not message 1 after the EXPUNGE:\n{}", fetch);
158 let _ = fs::remove_dir_all(&root);
159 Ok(())
160}
161
162/// A non-UID SEARCH answers in the numbers the client holds, and NOOP then announces the
163/// expunge, highest first, and the delivery.
164#[tokio::test(flavor = "multi_thread")]
165async fn search_keeps_the_view_and_noop_reconciles_it() -> Outcome<()> {
166 let (port, root) = res!(setup("search", 4).await);
167 let mut a = res!(Sess::open(port).await);
168 let mut b = res!(Sess::open(port).await);
169 res!(b.cmd("UID STORE 1,3 +FLAGS (\\Deleted)").await);
170 res!(b.cmd("EXPUNGE").await);
171 res!(a.cmd("STORE 4 +FLAGS (\\Flagged)").await);
172
173 let search = res!(a.cmd("SEARCH FLAGGED").await);
174 assert!(search.contains("* SEARCH 4\r\n"), "SEARCH FLAGGED in the client's numbers:\n{}", search);
175 assert!(!search.contains("EXPUNGE"), "an EXPUNGE was sent during SEARCH:\n{}", search);
176
177 let noop = res!(a.cmd("NOOP").await);
178 let want = "* 3 EXPUNGE\r\n* 1 EXPUNGE\r\n";
179 assert!(noop.starts_with(want), "NOOP did not announce 3 then 1:\n{}", noop);
180 assert!(!noop.contains("EXISTS"), "NOOP sent EXISTS with nothing delivered:\n{}", noop);
181 let search = res!(a.cmd("SEARCH FLAGGED").await);
182 assert!(search.contains("* SEARCH 2\r\n"), "SEARCH FLAGGED after NOOP:\n{}", search);
183 let _ = fs::remove_dir_all(&root);
184 Ok(())
185}
186
187/// CHECK (RFC 3501 §6.4.1) is a checkpoint: it completes, and like NOOP it announces another
188/// session's expunge and a delivery.
189#[tokio::test(flavor = "multi_thread")]
190async fn check_completes_and_reconciles_the_view() -> Outcome<()> {
191 let (port, root) = res!(setup("check", 3).await);
192 let mut a = res!(Sess::open(port).await);
193 let mut b = res!(Sess::open(port).await);
194 res!(b.cmd("UID STORE 2 +FLAGS (\\Deleted)").await);
195 res!(b.cmd("EXPUNGE").await);
196 let store = res!(MaildirStore::new(root.clone(), "test.local"));
197 let u = res!(res!(OneUser.lookup("")).ok_or_else(|| err!("No test user."; Test, Bug)));
198 res!(store.append(&u, &FolderName::new("INBOX"), b"Subject: m4\r\n\r\nid=m4\r\n", MessageFlags::default(), None));
199
200 let check = res!(a.cmd("CHECK").await);
201 assert!(check.starts_with("* 2 EXPUNGE\r\n"), "CHECK did not announce the expunge:\n{}", check);
202 assert!(check.contains("* 3 EXISTS\r\n"), "CHECK did not announce the delivery:\n{}", check);
203 assert!(check.lines().last().map_or(false, |l| l.starts_with("t3 OK")), "CHECK answered:\n{}", check);
204 let _ = fs::remove_dir_all(&root);
205 Ok(())
206}
207
208/// An idling session is told of another's expunge, without being asked.
209#[tokio::test(flavor = "multi_thread")]
210async fn idle_announces_an_expunge() -> Outcome<()> {
211 let (port, root) = res!(setup("idle", 2).await);
212 let mut a = res!(Sess::open(port).await);
213 let mut b = res!(Sess::open(port).await);
214 res!(a.r.get_mut().write_all(b"i1 IDLE\r\n").await, IO, Network);
215 let mut l = String::new();
216 res!(a.r.read_line(&mut l).await, IO, Network);
217 assert!(l.starts_with("+"), "IDLE answered {:?}", l);
218 res!(b.cmd("UID STORE 1 +FLAGS (\\Deleted)").await);
219 res!(b.cmd("EXPUNGE").await);
220 l.clear();
221 let got = tokio::time::timeout(Duration::from_secs(15), a.r.read_line(&mut l)).await;
222 assert!(got.is_ok(), "no untagged response within 15 s of the expunge");
223 assert_eq!(l, "* 1 EXPUNGE\r\n");
224 res!(a.r.get_mut().write_all(b"DONE\r\n").await, IO, Network);
225 let _ = fs::remove_dir_all(&root);
226 Ok(())
227}
228
229/// A toggles \Seen on UID 1 while B sets \Flagged once per trial; \Flagged must survive.
230#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
231async fn a_concurrent_flag_is_never_lost() -> Outcome<()> {
232 let (port, root) = res!(setup("flags", 1).await);
233 let mut lost = 0;
234 let trials = 40;
235 for _ in 0..trials {
236 let mut a = res!(Sess::open(port).await);
237 let mut b = res!(Sess::open(port).await);
238 res!(b.cmd("UID STORE 1 -FLAGS (\\Flagged)").await);
239 let ta = tokio::spawn(async move {
240 for i in 0..30 {
241 let op = if i % 2 == 0 { "+" } else { "-" };
242 let _ = a.cmd(&fmt!("UID STORE 1 {}FLAGS.SILENT (\\Seen)", op)).await;
243 }
244 a
245 });
246 tokio::time::sleep(Duration::from_millis(3)).await;
247 res!(b.cmd("UID STORE 1 +FLAGS.SILENT (\\Flagged)").await);
248 if ta.await.is_err() { return Err(err!("Session A's task failed."; Test, Bug)); }
249 let f = res!(b.cmd("UID FETCH 1 (FLAGS)").await);
250 if !f.contains("\\Flagged") { lost += 1; }
251 }
252 assert_eq!(lost, 0, "\\Flagged was lost in {} of {} trials", lost, trials);
253 let _ = fs::remove_dir_all(&root);
254 Ok(())
255}
256
257/// "Mark folder read" on 3000 messages while mail arrives: the STORE takes the folder in
258/// chunks, so it finishes in seconds rather than re-reading the folder per message, and the
259/// delivery lands between chunks, well inside the store's lock wait (QA F2a part 3 B, H1).
260#[tokio::test(flavor = "multi_thread")]
261async fn a_bulk_store_lets_a_delivery_in() -> Outcome<()> {
262 let n = 3000;
263 let (port, root) = res!(setup("bulk", 0).await);
264 let new = root.join("test.local/alice/new");
265 for i in 0..n {
266 res!(fs::write(new.join(fmt!("1800000000.M{}P1Q{}.seed", i, i)), fmt!("Subject: m{}\r\n\r\nx\r\n", i)), IO, File);
267 }
268 let mut a = res!(Sess::open(port).await);
269 let r2 = root.clone();
270 let delivery = tokio::task::spawn_blocking(move || -> Outcome<Duration> {
271 std::thread::sleep(Duration::from_millis(300));
272 let store = res!(MaildirStore::new(r2, "test.local"));
273 let u = res!(res!(OneUser.lookup("")).ok_or_else(|| err!("No test user."; Test, Bug)));
274 let t = std::time::Instant::now();
275 res!(store.append(&u, &FolderName::new("INBOX"), b"Subject: late\r\n\r\nx\r\n", MessageFlags::default(), None));
276 Ok(t.elapsed())
277 });
278 let t = std::time::Instant::now();
279 let reply = res!(a.cmd(&fmt!("STORE 1:{} +FLAGS.SILENT (\\Seen)", n)).await);
280 let took = t.elapsed();
281 let waited = match delivery.await {
282 Ok(r) => res!(r),
283 Err(_) => return Err(err!("The delivery task failed."; Test, Bug)),
284 };
285 let _ = fs::remove_dir_all(&root);
286 assert!(reply.lines().last().map_or(false, |l| l.contains(" OK ")), "STORE answered:\n{}", reply);
287 println!("bulk STORE of {} took {:?}; the delivery took {:?}", n, took, waited);
288 assert!(took < Duration::from_secs(20), "the bulk STORE of {} took {:?}", n, took);
289 assert!(waited < Duration::from_secs(5), "the delivery waited {:?} behind the bulk STORE", waited);
290 Ok(())
291}
292
293/// A store error, the lock wait run out here, answers the command `NO [UNAVAILABLE]` and keeps
294/// the session (RFC 5530), where it closed the connection with no reply; a failed SELECT leaves
295/// no mailbox selected.
296#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
297async fn a_store_error_is_a_no_and_the_session_lives() -> Outcome<()> {
298 let (port, root) = res!(setup_waiting("unavailable", 2, Duration::from_millis(300)).await);
299 let lockp = root.join("test.local/alice/hematite-folder.lock");
300 for c in ["UID STORE 1 +FLAGS (\\Flagged)", "UID FETCH 1 (BODY[])", "FETCH 1 (BODY[])",
301 "NOOP", "STATUS INBOX (MESSAGES)", "SEARCH ALL", "UID SEARCH ALL", "EXPUNGE",
302 "APPEND INBOX {9}\r\nSubject:\r", "SELECT INBOX"]
303 {
304 let mut s = res!(Sess::open(port).await);
305 let held = res!(ExclusiveLock::acquire(&lockp));
306 let r = s.cmd(c).await;
307 drop(held);
308 let r = res!(r);
309 let last = r.lines().last().unwrap_or("");
310 assert!(last.starts_with(&fmt!("t{} NO [UNAVAILABLE] ", s.n)), "{:?} was answered {:?}", c, r);
311 let after = res!(s.cmd("NOOP").await);
312 assert!(after.lines().last().unwrap_or("").contains(" OK "), "{:?}: then NOOP {:?}", c, after);
313 if c == "SELECT INBOX" {
314 let f = res!(s.cmd("FETCH 1 (FLAGS)").await);
315 assert!(!f.contains(" OK "), "a failed SELECT selected the mailbox: {:?}", f);
316 }
317 }
318 let _ = fs::remove_dir_all(&root);
319 Ok(())
320}
321
322/// A bulk FETCH of bodies marks them \Seen a chunk at a time, under one hold of the folder per
323/// chunk, not one hold and one read of the whole folder per message, O(N^2), and a delivery
324/// gets in meanwhile.
325#[tokio::test(flavor = "multi_thread")]
326async fn a_bulk_fetch_marks_seen_by_chunk() -> Outcome<()> {
327 let n = 1500;
328 let (port, root) = res!(setup("bulkfetch", 0).await);
329 let new = root.join("test.local/alice/new");
330 for i in 0..n {
331 res!(fs::write(new.join(fmt!("1800000000.M{}P1Q{}.seed", i, i)), fmt!("Subject: m{}\r\n\r\nx\r\n", i)), IO, File);
332 }
333 let mut a = res!(Sess::open(port).await);
334 let r2 = root.clone();
335 let delivery = tokio::task::spawn_blocking(move || -> Outcome<Duration> {
336 std::thread::sleep(Duration::from_millis(300));
337 let store = res!(MaildirStore::new(r2, "test.local"));
338 let u = res!(res!(OneUser.lookup("")).ok_or_else(|| err!("No test user."; Test, Bug)));
339 let t = std::time::Instant::now();
340 res!(store.append(&u, &FolderName::new("INBOX"), b"Subject: late\r\n\r\nx\r\n", MessageFlags::default(), None));
341 Ok(t.elapsed())
342 });
343 let t = std::time::Instant::now();
344 let reply = res!(a.cmd(&fmt!("FETCH 1:{} (BODY[])", n)).await);
345 let took = t.elapsed();
346 let waited = match delivery.await {
347 Ok(r) => res!(r),
348 Err(_) => return Err(err!("The delivery task failed."; Test, Bug)),
349 };
350 let flags = res!(a.cmd(&fmt!("FETCH 1:{} (FLAGS)", n)).await);
351 let _ = fs::remove_dir_all(&root);
352 assert!(reply.lines().last().map_or(false, |l| l.contains(" OK ")), "FETCH answered:\n{}",
353 reply.lines().last().unwrap_or(""));
354 println!("bulk FETCH of {} took {:?}; the delivery took {:?}", n, took, waited);
355 assert!(took < Duration::from_secs(15), "the bulk FETCH of {} took {:?}", n, took);
356 assert!(waited < Duration::from_secs(5), "the delivery waited {:?} behind the bulk FETCH", waited);
357 assert_eq!(reply.matches("FLAGS (\\Seen)").count(), n, "every FETCH response carries the \\Seen it set");
358 let seen = flags.lines().filter(|l| l.contains("\\Seen")).count();
359 assert_eq!(seen, n, "every message fetched is \\Seen");
360 Ok(())
361}
362
363/// SUBSCRIBE and CREATE whose save fails answer NO, where they answered OK and LSUB then lacked
364/// the folder.
365#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
366async fn a_failed_subscribe_is_a_no() -> Outcome<()> {
367 let (port, root) = res!(setup_waiting("subscribe", 0, Duration::from_millis(300)).await);
368 let lockp = root.join("test.local/alice/hematite-subscriptions.lock");
369 let mut s = res!(Sess::open(port).await);
370 for c in ["SUBSCRIBE Keep", "CREATE Made"] {
371 let held = res!(ExclusiveLock::acquire(&lockp));
372 let r = s.cmd(c).await;
373 drop(held);
374 let r = res!(r);
375 assert!(r.lines().last().unwrap_or("").starts_with(&fmt!("t{} NO ", s.n)), "{:?} was answered {:?}", c, r);
376 }
377 let l = res!(s.cmd("SUBSCRIBE Keep").await);
378 assert!(l.lines().last().unwrap_or("").contains(" OK "), "the retry: {:?}", l);
379 let l = res!(s.cmd("LSUB \"\" \"*\"").await);
380 assert!(l.contains("Keep"), "LSUB: {:?}", l);
381 let _ = fs::remove_dir_all(&root);
382 Ok(())
383}
384
385/// \Recent is the session's: another session's STORE moves a new message to cur/, and this
386/// session's own STORE and FETCH still show it \Recent, as they did before F2a.
387#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
388async fn a_session_keeps_its_recent_after_a_store() -> Outcome<()> {
389 let (port, root) = res!(setup("recent", 1).await);
390 let mut a = res!(Sess::open(port).await);
391 let mut b = res!(Sess::open(port).await);
392 let ib = root.join("test.local/alice");
393 res!(fs::write(ib.join("tmp/1900000000.Mx.ext"), b"Subject: r\r\n\r\nid=r\r\n"), IO, File);
394 res!(fs::rename(ib.join("tmp/1900000000.Mx.ext"), ib.join("new/1900000000.Mx.ext")), IO, File);
395 res!(a.cmd("NOOP").await);
396 let f0 = res!(a.cmd("UID FETCH 2 (FLAGS)").await);
397 assert_eq!(flags_of(&f0, 2).as_deref(), Some("\\Recent"), "before: {:?}", f0);
398 res!(b.cmd("NOOP").await);
399 res!(b.cmd("UID STORE 2 +FLAGS (\\Answered)").await);
400 let f1 = res!(a.cmd("UID STORE 2 +FLAGS (\\Flagged)").await);
401 let f2 = res!(a.cmd("UID FETCH 2 (BODY[])").await);
402 let f3 = res!(a.cmd("UID FETCH 2 (FLAGS)").await);
403 let _ = fs::remove_dir_all(&root);
404 for (what, r) in [("STORE", &f1), ("FETCH BODY[]", &f2), ("FETCH", &f3)] {
405 // FETCH BODY[] shows its FLAGS after the literal, on a line of its own.
406 assert!(r.contains("\\Recent)"), "{} dropped \\Recent: {:?}", what, r);
407 }
408 Ok(())
409}
410
411/// System flags are case-insensitive (RFC 3501 §2.3.2): a client's `\seen` is `\Seen` in a
412/// STORE.
413#[tokio::test(flavor = "multi_thread")]
414async fn flag_names_match_in_any_case() -> Outcome<()> {
415 let (port, root) = res!(setup("flagcase", 1).await);
416 let mut a = res!(Sess::open(port).await);
417 res!(a.cmd("UID STORE 1 +FLAGS (\\seen \\FLAGGED)").await);
418 let f1 = res!(a.cmd("UID FETCH 1 (FLAGS)").await);
419 let _ = fs::remove_dir_all(&root);
420 assert_eq!(flags_of(&f1, 1).as_deref(), Some("\\Seen \\Flagged"), "STORE (\\seen \\FLAGGED): {:?}", f1);
421 Ok(())
422}