oxedyne/fe2o3/fe2o3_mail/tests/imap_move.rs
19.9 KiB, 3 runs
created by r1870400018:74938, 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 COPY, MOVE and UIDPLUS against a real Maildir over a real socket. |
| 2 | //! |
| 3 | //! A client files mail with `UID MOVE` where the server offers it, and with |
| 4 | //! `UID COPY`, `UID STORE +FLAGS (\Deleted)` and `UID EXPUNGE` where it offers |
| 5 | //! UIDPLUS, and learns the new UIDs from `COPYUID` and `APPENDUID`. Plain |
| 6 | //! EXPUNGE is no substitute for UID EXPUNGE: it removes every `\Deleted` |
| 7 | //! message, not just the ones being filed. |
| 8 | |
| 9 | use oxedyne_fe2o3_core::prelude::*; |
| 10 | use oxedyne_fe2o3_mail::maildir::MaildirStore; |
| 11 | use oxedyne_fe2o3_net::{ |
| 12 | imap::server::ImapServer, |
| 13 | mail::{ |
| 14 | store::{ |
| 15 | FolderName, |
| 16 | MailStore, |
| 17 | MailUser, |
| 18 | MessageFlags, |
| 19 | MessageUid, |
| 20 | }, |
| 21 | user::UserStore, |
| 22 | }, |
| 23 | }; |
| 24 | |
| 25 | use std::{ |
| 26 | collections::BTreeMap, |
| 27 | fs, |
| 28 | net::SocketAddr, |
| 29 | path::PathBuf, |
| 30 | time::Duration, |
| 31 | }; |
| 32 | |
| 33 | use tokio::{ |
| 34 | io::{ |
| 35 | AsyncBufReadExt, |
| 36 | AsyncWriteExt, |
| 37 | BufReader, |
| 38 | }, |
| 39 | net::{ |
| 40 | TcpListener, |
| 41 | TcpStream, |
| 42 | }, |
| 43 | }; |
| 44 | |
| 45 | |
| 46 | const USER: &str = "alice@test.local"; |
| 47 | const PASS: &str = "correct-horse"; |
| 48 | |
| 49 | #[derive(Clone, Debug)] |
| 50 | struct OneUser; |
| 51 | |
| 52 | impl 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(user())) |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | fn user() -> MailUser { |
| 67 | MailUser { |
| 68 | local: fmt!("alice"), |
| 69 | domain: fmt!("test.local"), |
| 70 | delivery_key: fmt!("test.local/alice"), |
| 71 | send_as: Vec::new(), |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | fn body(tag: &str) -> Vec<u8> { |
| 76 | fmt!("From: bob@example.org\r\nTo: alice@test.local\r\nSubject: {}\r\n\r\nid={}\r\n", |
| 77 | tag, tag).into_bytes() |
| 78 | } |
| 79 | |
| 80 | /// The `id=` tag a message carries, which names it independently of its UID. |
| 81 | fn tag_of(bytes: &[u8]) -> String { |
| 82 | let s = String::from_utf8_lossy(bytes); |
| 83 | match s.find("id=") { |
| 84 | Some(i) => s[i + 3..].trim().to_string(), |
| 85 | None => String::new(), |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /// UID → tag for every message in the folder now. |
| 90 | fn uid_map(store: &MaildirStore, f: &str) -> Outcome<BTreeMap<u32, String>> { |
| 91 | let (u, f) = (user(), FolderName::new(f)); |
| 92 | let msgs = res!(store.list_messages(&u, &f, true)); |
| 93 | let mut out = BTreeMap::new(); |
| 94 | for m in &msgs { |
| 95 | let bytes = res!(res!(store.fetch_bytes(&u, &f, m.uid)) |
| 96 | .ok_or_else(|| err!("UID {} listed but not fetched.", m.uid.0; Missing))); |
| 97 | out.insert(m.uid.0, tag_of(&bytes)); |
| 98 | } |
| 99 | Ok(out) |
| 100 | } |
| 101 | |
| 102 | struct Conn { |
| 103 | r: BufReader<tokio::net::tcp::OwnedReadHalf>, |
| 104 | w: tokio::net::tcp::OwnedWriteHalf, |
| 105 | n: usize, |
| 106 | } |
| 107 | |
| 108 | impl Conn { |
| 109 | async fn open(addr: SocketAddr, folder: &str) -> Outcome<Self> { |
| 110 | let sock = res!(TcpStream::connect(addr).await, IO, Network); |
| 111 | let (r, w) = sock.into_split(); |
| 112 | let mut c = Conn { r: BufReader::new(r), w, n: 0 }; |
| 113 | let mut greeting = String::new(); |
| 114 | res!(c.r.read_line(&mut greeting).await, IO, Network); |
| 115 | res!(c.cmd(&fmt!("LOGIN {} {}", USER, PASS)).await); |
| 116 | res!(c.cmd(&fmt!("SELECT {}", folder)).await); |
| 117 | Ok(c) |
| 118 | } |
| 119 | |
| 120 | async fn send(&mut self, bytes: &[u8]) -> Outcome<()> { |
| 121 | res!(self.w.write_all(bytes).await, IO, Network); |
| 122 | Ok(()) |
| 123 | } |
| 124 | |
| 125 | async fn cmd(&mut self, text: &str) -> Outcome<Vec<String>> { |
| 126 | self.n += 1; |
| 127 | let tag = fmt!("t{}", self.n); |
| 128 | res!(self.send(fmt!("{} {}\r\n", tag, text).as_bytes()).await); |
| 129 | self.until(&tag).await |
| 130 | } |
| 131 | |
| 132 | async fn until(&mut self, tag: &str) -> Outcome<Vec<String>> { |
| 133 | let mut lines = Vec::new(); |
| 134 | loop { |
| 135 | let mut line = String::new(); |
| 136 | let n = res!(self.r.read_line(&mut line).await, IO, Network); |
| 137 | if n == 0 { return Err(err!("The server closed the connection."; IO, Network)); } |
| 138 | let done = line.starts_with(&fmt!("{} ", tag)); |
| 139 | lines.push(line.trim_end().to_string()); |
| 140 | if done { return Ok(lines); } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | async fn status(&mut self, folder: &str, item: &str) -> Outcome<u32> { |
| 145 | let lines = res!(self.cmd(&fmt!("STATUS {} ({})", folder, item)).await); |
| 146 | for l in &lines { |
| 147 | if let Some(i) = l.find(&fmt!("{} ", item)) { |
| 148 | let rest = &l[i + item.len() + 1..]; |
| 149 | let digits: String = rest.chars().take_while(|c| c.is_ascii_digit()).collect(); |
| 150 | return Ok(res!(digits.parse::<u32>(), Decode)); |
| 151 | } |
| 152 | } |
| 153 | Err(err!("No {} in the STATUS answer {:?}.", item, lines; Test, Missing)) |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | /// The text of the response code `[NAME ...]` on any of the lines. |
| 158 | fn code(lines: &[String], name: &str) -> Option<String> { |
| 159 | let open = fmt!("[{} ", name); |
| 160 | for l in lines { |
| 161 | if let Some(i) = l.find(&open) { |
| 162 | let rest = &l[i + open.len()..]; |
| 163 | if let Some(j) = rest.find(']') { |
| 164 | return Some(rest[..j].to_string()); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | None |
| 169 | } |
| 170 | |
| 171 | fn last(lines: &[String]) -> String { |
| 172 | lines.last().cloned().unwrap_or_default() |
| 173 | } |
| 174 | |
| 175 | /// A uid-set expanded: `3:5,9` is 3, 4, 5, 9. |
| 176 | fn expand(set: &str) -> Vec<u32> { |
| 177 | let mut out = Vec::new(); |
| 178 | for piece in set.split(',') { |
| 179 | let (lo, hi) = match piece.split_once(':') { |
| 180 | Some((a, b)) => (a.parse::<u32>().unwrap_or(0), b.parse::<u32>().unwrap_or(0)), |
| 181 | None => { |
| 182 | let n = piece.parse::<u32>().unwrap_or(0); |
| 183 | (n, n) |
| 184 | }, |
| 185 | }; |
| 186 | for n in lo.min(hi)..=lo.max(hi) { out.push(n); } |
| 187 | } |
| 188 | out |
| 189 | } |
| 190 | |
| 191 | /// A Maildir with `n` messages tagged `<prefix>0..` in INBOX, served over a socket. |
| 192 | async fn serve(name: &str, prefix: &str, n: usize) -> Outcome<(SocketAddr, MaildirStore, Vec<u32>, PathBuf)> { |
| 193 | let root = std::env::temp_dir().join(fmt!("fe2o3_imap_move_{}_{}", name, std::process::id())); |
| 194 | let _ = fs::remove_dir_all(&root); |
| 195 | res!(fs::create_dir_all(&root), IO, File); |
| 196 | let store = res!(MaildirStore::new(PathBuf::from(&root), "test.local")); |
| 197 | let u = user(); |
| 198 | res!(store.ensure_user(&u)); |
| 199 | let inbox = FolderName::new("INBOX"); |
| 200 | let mut uids = Vec::new(); |
| 201 | for i in 0..n { |
| 202 | let MessageUid(uid) = res!(store.append(&u, &inbox, &body(&fmt!("{}{}", prefix, i)), |
| 203 | MessageFlags::default(), None)); |
| 204 | uids.push(uid); |
| 205 | } |
| 206 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, IO, Network); |
| 207 | let addr = res!(listener.local_addr(), IO, Network); |
| 208 | let server = ImapServer { |
| 209 | store: store.clone(), |
| 210 | users: OneUser, |
| 211 | hostname: std::sync::Arc::new(fmt!("test.local")), |
| 212 | }; |
| 213 | tokio::spawn(async move { |
| 214 | while let Ok((sock, peer)) = listener.accept().await { |
| 215 | let srv = server.clone(); |
| 216 | tokio::spawn(async move { let _ = srv.run(sock, peer).await; }); |
| 217 | } |
| 218 | }); |
| 219 | Ok((addr, store, uids, root)) |
| 220 | } |
| 221 | |
| 222 | #[tokio::test] |
| 223 | async fn capability_names_move_and_uidplus() -> Outcome<()> { |
| 224 | let (addr, _store, _uids, root) = res!(serve("cap", "c", 0).await); |
| 225 | let mut c = res!(Conn::open(addr, "INBOX").await); |
| 226 | let lines = res!(c.cmd("CAPABILITY").await); |
| 227 | let caps = lines.iter().find(|l| l.starts_with("* CAPABILITY")).cloned().unwrap_or_default(); |
| 228 | let words: Vec<&str> = caps.split_whitespace().collect(); |
| 229 | assert!(words.contains(&"MOVE") && words.contains(&"UIDPLUS"), |
| 230 | "CAPABILITY lacks MOVE or UIDPLUS: {}", caps); |
| 231 | let _ = fs::remove_dir_all(&root); |
| 232 | Ok(()) |
| 233 | } |
| 234 | |
| 235 | #[tokio::test] |
| 236 | async fn copy_files_a_copy_and_answers_copyuid() -> Outcome<()> { |
| 237 | let (addr, store, uids, root) = res!(serve("copy", "k", 3).await); |
| 238 | let mut c = res!(Conn::open(addr, "INBOX").await); |
| 239 | let validity = res!(c.status("Archive", "UIDVALIDITY").await); |
| 240 | let next = res!(c.status("Archive", "UIDNEXT").await); |
| 241 | |
| 242 | let lines = res!(c.cmd(&fmt!("UID COPY {},{} Archive", uids[0], uids[2])).await); |
| 243 | assert!(last(&lines).contains(" OK "), "UID COPY: {:?}", lines); |
| 244 | let cu = match code(&lines, "COPYUID") { |
| 245 | Some(cu) => cu, |
| 246 | None => return Err(err!("UID COPY gave no COPYUID: {:?}", lines; Test, Missing)), |
| 247 | }; |
| 248 | let parts: Vec<&str> = cu.split_whitespace().collect(); |
| 249 | assert_eq!(parts.len(), 3, "COPYUID {:?}", cu); |
| 250 | assert_eq!(parts[0], fmt!("{}", validity), "COPYUID's UIDVALIDITY is not the target's"); |
| 251 | assert_eq!(expand(parts[1]), vec![uids[0], uids[2]]); |
| 252 | let new = expand(parts[2]); |
| 253 | assert_eq!(new, vec![next, next + 1], "COPYUID's new UIDs"); |
| 254 | let archive = res!(uid_map(&store, "Archive")); |
| 255 | assert_eq!(archive.get(&next).map(|s| s.as_str()), Some("k0")); |
| 256 | assert_eq!(archive.get(&(next + 1)).map(|s| s.as_str()), Some("k2")); |
| 257 | assert_eq!(res!(uid_map(&store, "INBOX")).len(), 3, "COPY took messages from the source"); |
| 258 | |
| 259 | // By sequence number, as plain COPY names them. |
| 260 | let lines = res!(c.cmd("COPY 2 Archive").await); |
| 261 | assert!(last(&lines).contains(" OK ") && code(&lines, "COPYUID").is_some(), "COPY: {:?}", lines); |
| 262 | assert_eq!(res!(uid_map(&store, "Archive")).values().filter(|t| *t == "k1").count(), 1); |
| 263 | |
| 264 | let lines = res!(c.cmd("UID COPY 1:* Nowhere").await); |
| 265 | assert!(last(&lines).contains(" NO [TRYCREATE]"), "COPY to a missing folder: {:?}", lines); |
| 266 | let _ = fs::remove_dir_all(&root); |
| 267 | Ok(()) |
| 268 | } |
| 269 | |
| 270 | #[tokio::test] |
| 271 | async fn move_answers_copyuid_and_expunge() -> Outcome<()> { |
| 272 | let (addr, store, uids, root) = res!(serve("move", "m", 3).await); |
| 273 | let mut c = res!(Conn::open(addr, "INBOX").await); |
| 274 | let next = res!(c.status("Archive", "UIDNEXT").await); |
| 275 | |
| 276 | let lines = res!(c.cmd(&fmt!("UID MOVE {} Archive", uids[1])).await); |
| 277 | assert!(last(&lines).contains(" OK "), "UID MOVE: {:?}", lines); |
| 278 | let cu = code(&lines, "COPYUID").unwrap_or_default(); |
| 279 | assert_eq!(cu.split_whitespace().nth(2), Some(fmt!("{}", next).as_str()), "COPYUID {:?}", lines); |
| 280 | assert!(lines.iter().any(|l| l == "* 2 EXPUNGE"), "UID MOVE sent no EXPUNGE: {:?}", lines); |
| 281 | assert!(lines.iter().all(|l| !l.starts_with("* OK [COPYUID") || !l.contains("EXPUNGE"))); |
| 282 | let inbox = res!(uid_map(&store, "INBOX")); |
| 283 | let archive = res!(uid_map(&store, "Archive")); |
| 284 | assert_eq!(inbox.values().cloned().collect::<Vec<_>>(), vec![fmt!("m0"), fmt!("m2")]); |
| 285 | assert_eq!(archive.get(&next).map(|s| s.as_str()), Some("m1")); |
| 286 | |
| 287 | // By sequence number: message 2 is now m2. |
| 288 | let lines = res!(c.cmd("MOVE 2 Archive").await); |
| 289 | assert!(last(&lines).contains(" OK ") && lines.iter().any(|l| l == "* 2 EXPUNGE"), |
| 290 | "MOVE: {:?}", lines); |
| 291 | assert_eq!(res!(uid_map(&store, "INBOX")).len(), 1); |
| 292 | assert_eq!(res!(uid_map(&store, "Archive")).len(), 2); |
| 293 | |
| 294 | // EXAMINE is read-only: nothing may leave the folder. |
| 295 | res!(c.cmd("EXAMINE INBOX").await); |
| 296 | let lines = res!(c.cmd("UID MOVE 1:* Archive").await); |
| 297 | assert!(last(&lines).contains(" NO "), "MOVE out of a read-only selection: {:?}", lines); |
| 298 | // Nor may a flag change (RFC 3501 §6.3.2), by sequence or by UID. |
| 299 | for cmd in ["STORE 1 +FLAGS (\\Flagged)", "UID STORE 1:* +FLAGS.SILENT (\\Seen)"] { |
| 300 | let lines = res!(c.cmd(cmd).await); |
| 301 | assert!(last(&lines).contains(" NO "), "{} in a read-only selection: {:?}", cmd, lines); |
| 302 | } |
| 303 | let _ = fs::remove_dir_all(&root); |
| 304 | Ok(()) |
| 305 | } |
| 306 | |
| 307 | #[tokio::test] |
| 308 | async fn uid_expunge_removes_only_the_messages_it_names() -> Outcome<()> { |
| 309 | let (addr, store, uids, root) = res!(serve("uidexp", "e", 3).await); |
| 310 | let mut c = res!(Conn::open(addr, "INBOX").await); |
| 311 | res!(c.cmd(&fmt!("UID STORE {},{} +FLAGS.SILENT (\\Deleted)", uids[0], uids[2])).await); |
| 312 | let lines = res!(c.cmd(&fmt!("UID EXPUNGE {}", uids[2])).await); |
| 313 | assert!(last(&lines).contains(" OK "), "UID EXPUNGE: {:?}", lines); |
| 314 | assert!(lines.iter().any(|l| l == "* 3 EXPUNGE"), "UID EXPUNGE: {:?}", lines); |
| 315 | let inbox = res!(uid_map(&store, "INBOX")); |
| 316 | assert_eq!(inbox.keys().cloned().collect::<Vec<_>>(), vec![uids[0], uids[1]], |
| 317 | "UID EXPUNGE removed a message it did not name"); |
| 318 | let _ = fs::remove_dir_all(&root); |
| 319 | Ok(()) |
| 320 | } |
| 321 | |
| 322 | #[tokio::test] |
| 323 | async fn append_answers_appenduid() -> Outcome<()> { |
| 324 | let (addr, store, _uids, root) = res!(serve("appenduid", "a", 2).await); |
| 325 | let mut c = res!(Conn::open(addr, "INBOX").await); |
| 326 | let validity = res!(c.status("Archive", "UIDVALIDITY").await); |
| 327 | let next = res!(c.status("Archive", "UIDNEXT").await); |
| 328 | let msg = body("appended"); |
| 329 | c.n += 1; |
| 330 | let tag = fmt!("t{}", c.n); |
| 331 | res!(c.send(fmt!("{} APPEND Archive (\\Seen) {{{}+}}\r\n", tag, msg.len()).as_bytes()).await); |
| 332 | res!(c.send(&msg).await); |
| 333 | res!(c.send(b"\r\n").await); |
| 334 | let lines = res!(c.until(&tag).await); |
| 335 | assert!(last(&lines).contains(" OK "), "APPEND: {:?}", lines); |
| 336 | assert_eq!(code(&lines, "APPENDUID"), Some(fmt!("{} {}", validity, next)), "APPEND: {:?}", lines); |
| 337 | assert_eq!(res!(uid_map(&store, "Archive")).get(&next).map(|s| s.as_str()), Some("appended")); |
| 338 | let _ = fs::remove_dir_all(&root); |
| 339 | Ok(()) |
| 340 | } |
| 341 | |
| 342 | /// Two sessions move all they hold the other way at once, over and over: every |
| 343 | /// message is in exactly one folder at the end, and neither waits for ever on the |
| 344 | /// other's folder lock. |
| 345 | #[tokio::test(flavor = "multi_thread", worker_threads = 4)] |
| 346 | async fn opposite_moves_neither_deadlock_nor_lose_mail() -> Outcome<()> { |
| 347 | const N: usize = 30; |
| 348 | let (addr, store, _uids, root) = res!(serve("opposite", "o", N).await); |
| 349 | let u = user(); |
| 350 | let archive = FolderName::new("Archive"); |
| 351 | for i in 0..N { |
| 352 | res!(store.append(&u, &archive, &body(&fmt!("p{}", i)), MessageFlags::default(), None)); |
| 353 | } |
| 354 | let mut tasks = Vec::new(); |
| 355 | for (from, to) in [("INBOX", "Archive"), ("Archive", "INBOX")] { |
| 356 | tasks.push(tokio::spawn(async move { |
| 357 | let mut c = res!(Conn::open(addr, from).await); |
| 358 | for _ in 0..8 { |
| 359 | res!(c.cmd("NOOP").await); |
| 360 | let lines = res!(c.cmd(&fmt!("UID MOVE 1:* {}", to)).await); |
| 361 | if !last(&lines).contains(" OK ") { |
| 362 | return Err(err!("UID MOVE {} -> {}: {:?}", from, to, lines; Test, Unexpected)); |
| 363 | } |
| 364 | } |
| 365 | Ok::<(), Error<ErrTag>>(()) |
| 366 | })); |
| 367 | } |
| 368 | for t in tasks { |
| 369 | match tokio::time::timeout(Duration::from_secs(60), t).await { |
| 370 | Ok(Ok(outcome)) => res!(outcome), |
| 371 | Ok(Err(e)) => return Err(err!(e, "A mover panicked."; Test, Thread)), |
| 372 | Err(_) => return Err(err!("Opposite moves did not finish in 60 s."; Test, Timeout)), |
| 373 | } |
| 374 | } |
| 375 | let mut seen: BTreeMap<String, usize> = BTreeMap::new(); |
| 376 | for f in ["INBOX", "Archive"] { |
| 377 | for t in res!(uid_map(&store, f)).values() { |
| 378 | *seen.entry(t.clone()).or_insert(0) += 1; |
| 379 | } |
| 380 | } |
| 381 | assert_eq!(seen.len(), 2 * N, "messages lost: {:?}", seen); |
| 382 | assert!(seen.values().all(|n| *n == 1), "a message in both folders: {:?}", seen); |
| 383 | let _ = fs::remove_dir_all(&root); |
| 384 | Ok(()) |
| 385 | } |
| 386 | |
| 387 | /// The file under `dir` whose message carries the tag, at any depth. |
| 388 | fn file_of(dir: &std::path::Path, tag: &str) -> Option<PathBuf> { |
| 389 | for e in fs::read_dir(dir).ok()?.flatten() { |
| 390 | let p = e.path(); |
| 391 | if p.is_dir() { |
| 392 | if let Some(f) = file_of(&p, tag) { return Some(f); } |
| 393 | } else if fs::read(&p).map(|b| tag_of(&b) == tag).unwrap_or(false) { |
| 394 | return Some(p); |
| 395 | } |
| 396 | } |
| 397 | None |
| 398 | } |
| 399 | |
| 400 | #[tokio::test] |
| 401 | async fn a_stale_copy_or_move_sends_no_empty_copyuid() -> Outcome<()> { |
| 402 | // QA F2b-A defect 1: B moves a message A's view still holds; A's COPY and MOVE of it |
| 403 | // must not answer [COPYUID v ], and A learns in that response that it left. |
| 404 | let (addr, _store, uids, root) = res!(serve("stale", "s", 3).await); |
| 405 | let mut a = res!(Conn::open(addr, "INBOX").await); |
| 406 | let mut b = res!(Conn::open(addr, "INBOX").await); |
| 407 | let lines = res!(b.cmd(&fmt!("UID MOVE {} Archive", uids[1])).await); |
| 408 | assert!(last(&lines).contains(" OK "), "B's MOVE: {:?}", lines); |
| 409 | |
| 410 | let lines = res!(a.cmd(&fmt!("UID COPY {} Archive", uids[1])).await); |
| 411 | assert!(code(&lines, "COPYUID").is_none(), "a stale COPY answered COPYUID: {:?}", lines); |
| 412 | assert!(last(&lines).contains(" NO [EXPUNGEISSUED]"), "a stale COPY: {:?}", lines); |
| 413 | assert!(lines.iter().any(|l| l == "* 2 EXPUNGE"), "a stale COPY announced nothing: {:?}", lines); |
| 414 | |
| 415 | // Again, with the message gone from a fresh view and one more stale: B moves s2 too. |
| 416 | let lines = res!(b.cmd(&fmt!("UID MOVE {} Archive", uids[2])).await); |
| 417 | assert!(last(&lines).contains(" OK "), "B's second MOVE: {:?}", lines); |
| 418 | let lines = res!(a.cmd(&fmt!("UID MOVE {},{} Archive", uids[0], uids[2])).await); |
| 419 | let cu = code(&lines, "COPYUID").unwrap_or_default(); |
| 420 | let parts: Vec<&str> = cu.split_whitespace().collect(); |
| 421 | assert_eq!(parts.len(), 3, "a partly stale MOVE's COPYUID: {:?}", lines); |
| 422 | assert_eq!(expand(parts[1]), vec![uids[0]]); |
| 423 | assert!(last(&lines).contains(" OK "), "a partly stale MOVE: {:?}", lines); |
| 424 | assert!(lines.iter().filter(|l| l.ends_with(" EXPUNGE")).count() == 2, |
| 425 | "the moved and the vanished message both announced: {:?}", lines); |
| 426 | |
| 427 | // Announced, s2 has left A's view, so a UID MOVE of it now names no message at all. |
| 428 | let lines = res!(a.cmd(&fmt!("UID MOVE {} Archive", uids[2])).await); |
| 429 | assert!(code(&lines, "COPYUID").is_none() && last(&lines).contains(" OK "), |
| 430 | "a MOVE of a UID already announced gone: {:?}", lines); |
| 431 | let _ = fs::remove_dir_all(&root); |
| 432 | Ok(()) |
| 433 | } |
| 434 | |
| 435 | #[tokio::test] |
| 436 | async fn a_failed_copy_leaves_the_target_as_it_was() -> Outcome<()> { |
| 437 | // QA F2b-A defect 2, RFC 3501 §6.4.7: the second message cannot be read, so the COPY |
| 438 | // fails, and the copy of the first must not stay to be filed again on the retry. |
| 439 | let (addr, store, uids, root) = res!(serve("copyfail", "f", 3).await); |
| 440 | let path = res!(file_of(&root, "f1").ok_or_else(|| err!("No file for f1."; Test, Missing))); |
| 441 | res!(fs::set_permissions(&path, std::os::unix::fs::PermissionsExt::from_mode(0o000)), IO, File); |
| 442 | if fs::read(&path).is_ok() { |
| 443 | // Run as root: the file stays readable, and the failure cannot be made. |
| 444 | let _ = fs::remove_dir_all(&root); |
| 445 | return Ok(()); |
| 446 | } |
| 447 | let mut c = res!(Conn::open(addr, "INBOX").await); |
| 448 | let lines = res!(c.cmd(&fmt!("UID COPY {}:{} Archive", uids[0], uids[2])).await); |
| 449 | assert!(last(&lines).contains(" NO "), "a failing COPY: {:?}", lines); |
| 450 | assert!(res!(uid_map(&store, "Archive")).is_empty(), "a failed COPY left copies: {:?}", |
| 451 | res!(uid_map(&store, "Archive"))); |
| 452 | |
| 453 | res!(fs::set_permissions(&path, std::os::unix::fs::PermissionsExt::from_mode(0o600)), IO, File); |
| 454 | let lines = res!(c.cmd(&fmt!("UID COPY {}:{} Archive", uids[0], uids[2])).await); |
| 455 | assert!(last(&lines).contains(" OK "), "the retried COPY: {:?}", lines); |
| 456 | let tags: Vec<String> = res!(uid_map(&store, "Archive")).into_values().collect(); |
| 457 | assert_eq!(tags, vec![fmt!("f0"), fmt!("f1"), fmt!("f2")], "the retry filed one of each"); |
| 458 | let _ = fs::remove_dir_all(&root); |
| 459 | Ok(()) |
| 460 | } |
| 461 | |
| 462 | #[tokio::test] |
| 463 | async fn a_folder_read_keeps_a_tmp_file_with_an_old_mtime() -> Outcome<()> { |
| 464 | // The tmp/ sweep goes by creation time: an APPEND writing outside the lock sets its file's |
| 465 | // mtime to the INTERNALDATE, which may be years old, and must not lose it to a reader. |
| 466 | let (addr, _store, _uids, root) = res!(serve("tmpkeep", "t", 1).await); |
| 467 | let inbox = res!(file_of(&root, "t0").ok_or_else(|| err!("No file for t0."; Test, Missing))); |
| 468 | let tmp = res!(inbox.parent().and_then(|d| d.parent()).map(|d| d.join("tmp")) |
| 469 | .ok_or_else(|| err!("No folder above {:?}.", inbox; Test, Missing))); |
| 470 | let held = tmp.join("in-progress"); |
| 471 | res!(fs::write(&held, b"partial"), IO, File); |
| 472 | let f = res!(fs::File::options().write(true).open(&held), IO, File); |
| 473 | let old = std::time::SystemTime::now() - Duration::from_secs(3 * 365 * 86400); |
| 474 | res!(f.set_modified(old), IO, File); |
| 475 | let mut c = res!(Conn::open(addr, "INBOX").await); |
| 476 | res!(c.cmd("NOOP").await); |
| 477 | assert!(held.exists(), "a folder read swept a tmp/ file made a moment ago"); |
| 478 | let _ = fs::remove_dir_all(&root); |
| 479 | Ok(()) |
| 480 | } |