oxedyne/fe2o3/fe2o3_steel/tests/mail_delivery.rs
4.7 KiB, 1 run
created by r1870400018:74954, 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 | //! A message Steel cannot file is answered `451` so its sender retries, never `550` and never a |
| 2 | //! silent drop of one recipient beside others (RFC 5321 §4.2.5: a server that has not taken |
| 3 | //! responsibility for a message answers a temporary failure). |
| 4 | |
| 5 | use oxedyne_fe2o3_core::{ |
| 6 | file::ExclusiveLock, |
| 7 | prelude::*, |
| 8 | }; |
| 9 | use oxedyne_fe2o3_mail::{ |
| 10 | maildir::MaildirStore, |
| 11 | outbound::OutboundSpool, |
| 12 | passwd::PasswdFileUserStore, |
| 13 | }; |
| 14 | use oxedyne_fe2o3_net::{ |
| 15 | mail::{ |
| 16 | store::{ |
| 17 | FolderName, |
| 18 | MailStore, |
| 19 | }, |
| 20 | user::UserStore, |
| 21 | }, |
| 22 | smtp::handler::{ |
| 23 | HandlerOutcome, |
| 24 | SmtpHandler, |
| 25 | SmtpTransaction, |
| 26 | }, |
| 27 | }; |
| 28 | use oxedyne_fe2o3_steel::app::mail::AppMailHandler; |
| 29 | |
| 30 | use std::{ |
| 31 | fs, |
| 32 | path::PathBuf, |
| 33 | sync::Arc, |
| 34 | time::Duration, |
| 35 | }; |
| 36 | |
| 37 | |
| 38 | const USERS: &str = "{ \"users\": [ \ |
| 39 | { \"address\": \"alice@test.local\", \"delivery_dir\": \"test.local/alice\", \"argon2id\": \"x\" }, \ |
| 40 | { \"address\": \"bob@test.local\", \"delivery_dir\": \"test.local/bob\", \"argon2id\": \"x\" } ] }"; |
| 41 | |
| 42 | const MESSAGE: &str = "From: carol@a.example\r\nTo: alice@test.local, bob@test.local\r\n\ |
| 43 | Message-ID: <retry-1@a.example>\r\nSubject: Hi\r\n\r\nHello.\r\n"; |
| 44 | |
| 45 | fn scratch(tag: &str) -> Outcome<PathBuf> { |
| 46 | let dir = std::env::temp_dir().join(fmt!("fe2o3_steel_mail_{}_{}", tag, std::process::id())); |
| 47 | let _ = fs::remove_dir_all(&dir); |
| 48 | res!(fs::create_dir_all(dir.join("mail")), IO, File); |
| 49 | res!(fs::create_dir_all(dir.join("spool")), IO, File); |
| 50 | res!(fs::write(dir.join("users.jdat"), USERS), IO, File); |
| 51 | Ok(dir) |
| 52 | } |
| 53 | |
| 54 | fn handler(dir: &PathBuf) -> Outcome<AppMailHandler> { |
| 55 | let store = res!(MaildirStore::new(dir.join("mail"), "mx.test.local")) |
| 56 | .with_lock_wait(Duration::from_millis(300)); |
| 57 | let users = PasswdFileUserStore::new(dir.join("users.jdat")); |
| 58 | for addr in ["alice@test.local", "bob@test.local"] { |
| 59 | let u = res!(res!(users.lookup(addr)).ok_or_else(|| err!("No user {}.", addr; Missing))); |
| 60 | res!(store.ensure_user(&u)); |
| 61 | } |
| 62 | Ok(AppMailHandler { |
| 63 | store, |
| 64 | users, |
| 65 | spool: res!(OutboundSpool::new(dir.join("spool"))), |
| 66 | dkim: Vec::new(), |
| 67 | local_domains: Arc::new(vec![fmt!("test.local")]), |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | fn txn() -> SmtpTransaction { |
| 72 | SmtpTransaction { |
| 73 | mail_from: fmt!("carol@a.example"), |
| 74 | rcpt_to: vec![fmt!("alice@test.local"), fmt!("bob@test.local")], |
| 75 | helo_domain: fmt!("a.example"), |
| 76 | auth_user: None, |
| 77 | peer: ([192, 0, 2, 1], 25).into(), |
| 78 | tls: false, |
| 79 | raw_message: MESSAGE.as_bytes().to_vec(), |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | fn inbox_count(h: &AppMailHandler, addr: &str) -> Outcome<usize> { |
| 84 | let u = res!(res!(h.users.lookup(addr)).ok_or_else(|| err!("No user {}.", addr; Missing))); |
| 85 | Ok(res!(h.store.list_messages(&u, &FolderName::new("INBOX"), true)).len()) |
| 86 | } |
| 87 | |
| 88 | /// Bob's INBOX is locked past the wait: the whole message is answered 451 with 4.3.0, alice's copy |
| 89 | /// notwithstanding, and the retry once the lock is free lands one copy in each INBOX. |
| 90 | #[test] |
| 91 | fn a_failed_local_delivery_is_answered_451_and_the_retry_duplicates_nothing() -> Outcome<()> { |
| 92 | let dir = res!(scratch("451")); |
| 93 | let h = res!(handler(&dir)); |
| 94 | let lock_path = dir.join("mail").join("test.local/bob").join("hematite-folder.lock"); |
| 95 | let held = res!(ExclusiveLock::acquire(&lock_path)); |
| 96 | |
| 97 | match res!(h.deliver_inbound(txn())) { |
| 98 | HandlerOutcome::RejectTemporary(reason) => assert!(reason.starts_with("4.3.0"), |
| 99 | "the reason should carry the enhanced code 4.3.0: {:?}", reason), |
| 100 | other => return Err(err!("A failed delivery was answered {:?}, not 451.", other; Test)), |
| 101 | } |
| 102 | // The same for a sole recipient, which was answered 550 and bounced. |
| 103 | let mut sole = txn(); |
| 104 | sole.rcpt_to = vec![fmt!("bob@test.local")]; |
| 105 | match res!(h.deliver_inbound(sole)) { |
| 106 | HandlerOutcome::RejectTemporary(_) => {}, |
| 107 | other => return Err(err!("A sole failed recipient was answered {:?}, not 451.", other; Test)), |
| 108 | } |
| 109 | // Submission's local delivery is the same contract. |
| 110 | match res!(h.submit_outbound(txn())) { |
| 111 | HandlerOutcome::RejectTemporary(_) => {}, |
| 112 | other => return Err(err!("A failed local submission was answered {:?}, not 451.", other; Test)), |
| 113 | } |
| 114 | drop(held); |
| 115 | |
| 116 | match res!(h.deliver_inbound(txn())) { |
| 117 | HandlerOutcome::Accepted(_) => {}, |
| 118 | other => return Err(err!("The retry was answered {:?}.", other; Test)), |
| 119 | } |
| 120 | assert_eq!(res!(inbox_count(&h, "alice@test.local")), 1, "alice holds the message once"); |
| 121 | assert_eq!(res!(inbox_count(&h, "bob@test.local")), 1, "bob holds the message once"); |
| 122 | let _ = fs::remove_dir_all(&dir); |
| 123 | Ok(()) |
| 124 | } |