Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/smtp_runtime.rs

8.5 KiB, 1 run

created by r1870400018:74758, 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//! An SMTP delivery waiting on a Maildir folder lock must not hold the async runtime.
2//!
3//! Steel's handler files an inbound message with `MailStore::append`, which waits on the
4//! folder's flock. Called on a runtime worker, that wait takes the worker with it, and on a
5//! single-worker runtime no other session, and no plain timer, moves until the lock goes.
6
7use oxedyne_fe2o3_core::{
8 file::ExclusiveLock,
9 prelude::*,
10};
11use oxedyne_fe2o3_mail::maildir::MaildirStore;
12use oxedyne_fe2o3_net::{
13 mail::{
14 store::{
15 FolderName,
16 MailStore,
17 MailUser,
18 MessageFlags,
19 },
20 user::UserStore,
21 },
22 smtp::{
23 handler::{
24 HandlerOutcome,
25 SmtpHandler,
26 SmtpTransaction,
27 },
28 server::{
29 SmtpMode,
30 SmtpServer,
31 },
32 },
33};
34
35use std::{
36 fs,
37 path::PathBuf,
38 sync::{
39 mpsc,
40 Arc,
41 },
42 time::Duration,
43};
44
45use tokio::{
46 io::{
47 AsyncBufReadExt,
48 AsyncWriteExt,
49 BufReader,
50 },
51 net::{
52 tcp::{
53 OwnedReadHalf,
54 OwnedWriteHalf,
55 },
56 TcpListener,
57 TcpStream,
58 },
59};
60
61
62const RCPT: &str = "alice@test.local";
63
64fn alice() -> MailUser {
65 MailUser {
66 local: fmt!("alice"),
67 domain: fmt!("test.local"),
68 delivery_key: fmt!("test.local/alice"),
69 send_as: Vec::new(),
70 }
71}
72
73#[derive(Clone, Debug)]
74struct OneUser;
75
76impl UserStore for OneUser {
77 fn authenticate(&self, _address: &str, _password: &str) -> Outcome<Option<MailUser>> {
78 Ok(None)
79 }
80
81 fn lookup(&self, address: &str) -> Outcome<Option<MailUser>> {
82 Ok(if address.eq_ignore_ascii_case(RCPT) { Some(alice()) } else { None })
83 }
84}
85
86/// Files every inbound message in alice's INBOX, as Steel's handler does.
87#[derive(Clone)]
88struct Deliver(MaildirStore);
89
90impl SmtpHandler for Deliver {
91 fn deliver_inbound(&self, txn: SmtpTransaction) -> Outcome<HandlerOutcome> {
92 let flags = MessageFlags { recent: true, ..MessageFlags::default() };
93 res!(self.0.append(&alice(), &FolderName::new("INBOX"), &txn.raw_message, flags, None));
94 Ok(HandlerOutcome::Accepted(fmt!("T1")))
95 }
96
97 fn submit_outbound(&self, _txn: SmtpTransaction) -> Outcome<HandlerOutcome> {
98 Ok(HandlerOutcome::RejectPermanent(fmt!("not a submission server")))
99 }
100
101 fn rcpt_acceptable(&self, address: &str) -> bool {
102 address.eq_ignore_ascii_case(RCPT)
103 }
104}
105
106/// Reads one reply, multi-line included, and returns its last line.
107async fn reply(r: &mut BufReader<OwnedReadHalf>) -> Outcome<String> {
108 loop {
109 let mut line = String::new();
110 let n = res!(r.read_line(&mut line).await, IO, Network);
111 if n == 0 {
112 return Err(err!("The server closed mid-reply."; IO, Network));
113 }
114 if line.as_bytes().get(3) != Some(&b'-') {
115 return Ok(line.trim_end().to_string());
116 }
117 }
118}
119
120async fn say(r: &mut BufReader<OwnedReadHalf>, w: &mut OwnedWriteHalf, text: &str) -> Outcome<String> {
121 res!(w.write_all(text.as_bytes()).await, IO, Network);
122 reply(r).await
123}
124
125async fn connect(addr: std::net::SocketAddr) -> Outcome<(BufReader<OwnedReadHalf>, OwnedWriteHalf)> {
126 let sock = res!(TcpStream::connect(addr).await, IO, Network);
127 let (r, w) = sock.into_split();
128 let mut r = BufReader::new(r);
129 res!(reply(&mut r).await);
130 Ok((r, w))
131}
132
133#[derive(Debug)]
134enum Event {
135 Ticked, // a plain timer task ran
136 OtherSession(String), // the second session's reply to EHLO
137 Delivered(String), // the first session's reply to the end of DATA
138 Failed(String),
139}
140
141/// The waiting delivery, the second session and the timer all share one runtime thread.
142fn serve_on_one_thread(store: MaildirStore, tx: mpsc::Sender<Event>) -> Outcome<()> {
143 let rt = res!(tokio::runtime::Builder::new_current_thread().enable_all().build(), IO);
144 rt.block_on(async move {
145 let listener = res!(TcpListener::bind("127.0.0.1:0").await, IO, Network);
146 let addr = res!(listener.local_addr(), IO, Network);
147 let server = SmtpServer {
148 handler: Deliver(store),
149 users: OneUser,
150 tls_acceptor: None,
151 hostname: Arc::new(fmt!("mx.test.local")),
152 mode: SmtpMode::Receive,
153 };
154 tokio::spawn(async move {
155 while let Ok((sock, peer)) = listener.accept().await {
156 let srv = server.clone();
157 tokio::spawn(async move { let _ = srv.run(sock, peer).await; });
158 }
159 });
160
161 // Session A delivers into the locked INBOX, and waits there.
162 let (mut ra, mut wa) = res!(connect(addr).await);
163 for line in ["EHLO a.example\r\n", "MAIL FROM:<bob@a.example>\r\n",
164 "RCPT TO:<alice@test.local>\r\n", "DATA\r\n"]
165 {
166 res!(say(&mut ra, &mut wa, line).await);
167 }
168 let tx_a = tx.clone();
169 let a = tokio::spawn(async move {
170 let msg = "From: bob@a.example\r\nTo: alice@test.local\r\nSubject: Hi\r\n\r\nHello.\r\n.\r\n";
171 let ev = match say(&mut ra, &mut wa, msg).await {
172 Ok(line) => Event::Delivered(line),
173 Err(e) => Event::Failed(fmt!("session A: {}", e)),
174 };
175 let _ = tx_a.send(ev);
176 });
177 // Give A's delivery time to reach the store and block.
178 tokio::time::sleep(Duration::from_millis(300)).await;
179
180 let tx_t = tx.clone();
181 tokio::spawn(async move {
182 tokio::time::sleep(Duration::from_millis(10)).await;
183 let _ = tx_t.send(Event::Ticked);
184 });
185 let ev = match connect(addr).await {
186 Ok((mut rb, mut wb)) => match say(&mut rb, &mut wb, "EHLO b.example\r\n").await {
187 Ok(line) => Event::OtherSession(line),
188 Err(e) => Event::Failed(fmt!("session B EHLO: {}", e)),
189 },
190 Err(e) => Event::Failed(fmt!("session B connect: {}", e)),
191 };
192 let _ = tx.send(ev);
193 let _ = a.await;
194 Ok(())
195 })
196}
197
198#[test]
199fn a_delivery_waiting_on_a_folder_lock_holds_no_runtime_worker() -> Outcome<()> {
200 let root = std::env::temp_dir().join(fmt!("fe2o3_smtp_runtime_{}", std::process::id()));
201 let _ = fs::remove_dir_all(&root);
202 res!(fs::create_dir_all(&root), IO, File);
203 let store = res!(MaildirStore::new(PathBuf::from(&root), "test.local"));
204 res!(store.ensure_user(&alice()));
205
206 // Another holder of INBOX's lock: an IMAP session or another delivery, as the store sees it.
207 let lock_path = root.join("test.local").join("alice").join("hematite-folder.lock");
208 let lock = res!(ExclusiveLock::acquire(&lock_path));
209
210 let (tx, rx) = mpsc::channel::<Event>();
211 let worker = std::thread::spawn(move || serve_on_one_thread(store, tx));
212
213 // While the lock is held, the other session and the timer must both get through.
214 let mut ticked = false;
215 let mut other: Option<String> = None;
216 let mut failures: Vec<String> = Vec::new();
217 while !(ticked && other.is_some()) {
218 match rx.recv_timeout(Duration::from_secs(5)) {
219 Ok(Event::Ticked) => ticked = true,
220 Ok(Event::OtherSession(l)) => other = Some(l),
221 Ok(ev) => { failures.push(fmt!("while locked: {:?}", ev)); break; }
222 Err(_) => {
223 failures.push(fmt!("while INBOX was locked, nothing else ran \
224 (timer ticked: {}, other session: {:?})", ticked, other));
225 break;
226 }
227 }
228 }
229 if let Some(line) = &other {
230 if !line.starts_with("250") {
231 failures.push(fmt!("EHLO while INBOX was locked: {}", line));
232 }
233 }
234
235 // Once the lock goes, the waiting delivery is filed and accepted.
236 drop(lock);
237 let mut delivered = false;
238 while !delivered {
239 match rx.recv_timeout(Duration::from_secs(10)) {
240 Ok(Event::Delivered(l)) => {
241 if !l.starts_with("250") { failures.push(fmt!("end of DATA: {}", l)); }
242 delivered = true;
243 }
244 Ok(Event::Failed(e)) => { failures.push(e); break; }
245 Ok(_) => (),
246 Err(_) => { failures.push(fmt!("the delivery never finished after the lock went")); break; }
247 }
248 }
249 let _ = worker.join();
250 let _ = fs::remove_dir_all(&root);
251 assert!(failures.is_empty(), "{}", failures.join("\n"));
252 Ok(())
253}