oxedyne/fe2o3/fe2o3_mail/tests/maildir_move.rs
8.9 KiB, 1 run
created by r1870400018:74942, 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 | //! The Maildir store's MOVE across a crash. |
| 2 | //! |
| 3 | //! MOVE is one rename(2) of the message file followed by both folders' uidlist |
| 4 | //! writes. A crash between the two must leave the message in exactly one folder, |
| 5 | //! and the folder it reached must number it above every UID it has issued, even |
| 6 | //! when the message once lived there: a reused UID would make a client that |
| 7 | //! remembers the old one act on the wrong message. |
| 8 | |
| 9 | use oxedyne_fe2o3_core::prelude::*; |
| 10 | use oxedyne_fe2o3_mail::maildir::MaildirStore; |
| 11 | use oxedyne_fe2o3_net::mail::store::{ |
| 12 | FolderName, |
| 13 | MailStore, |
| 14 | MailUser, |
| 15 | MessageFlags, |
| 16 | MessageUid, |
| 17 | }; |
| 18 | |
| 19 | use std::{ |
| 20 | collections::{ |
| 21 | BTreeMap, |
| 22 | BTreeSet, |
| 23 | }, |
| 24 | fs, |
| 25 | path::{ |
| 26 | Path, |
| 27 | PathBuf, |
| 28 | }, |
| 29 | thread, |
| 30 | time::{ |
| 31 | Duration, |
| 32 | Instant, |
| 33 | }, |
| 34 | }; |
| 35 | |
| 36 | |
| 37 | fn user() -> MailUser { |
| 38 | MailUser { |
| 39 | local: fmt!("alice"), |
| 40 | domain: fmt!("test.local"), |
| 41 | delivery_key: fmt!("test.local/alice"), |
| 42 | send_as: Vec::new(), |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | fn body(tag: &str) -> Vec<u8> { |
| 47 | fmt!("From: bob@example.org\r\nSubject: {}\r\n\r\nid={}\r\n", tag, tag).into_bytes() |
| 48 | } |
| 49 | |
| 50 | fn tag_of(bytes: &[u8]) -> String { |
| 51 | let s = String::from_utf8_lossy(bytes); |
| 52 | match s.find("id=") { |
| 53 | Some(i) => s[i + 3..].trim().to_string(), |
| 54 | None => String::new(), |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | fn uid_map(store: &MaildirStore, f: &FolderName) -> Outcome<BTreeMap<u32, String>> { |
| 59 | let u = user(); |
| 60 | let mut out = BTreeMap::new(); |
| 61 | for m in res!(store.list_messages(&u, f, true)) { |
| 62 | let bytes = res!(res!(store.fetch_bytes(&u, f, m.uid)) |
| 63 | .ok_or_else(|| err!("UID {} listed but not fetched.", m.uid.0; Missing))); |
| 64 | out.insert(m.uid.0, tag_of(&bytes)); |
| 65 | } |
| 66 | Ok(out) |
| 67 | } |
| 68 | |
| 69 | fn is_root() -> bool { |
| 70 | match fs::read_to_string("/proc/self/status") { |
| 71 | Ok(s) => s.lines().any(|l| l.starts_with("Uid:") |
| 72 | && l.split_whitespace().nth(2) == Some("0")), |
| 73 | Err(_) => false, |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #[cfg(unix)] |
| 78 | fn set_mode(p: &Path, mode: u32) -> Outcome<()> { |
| 79 | use std::os::unix::fs::PermissionsExt; |
| 80 | res!(fs::set_permissions(p, fs::Permissions::from_mode(mode)), IO, File); |
| 81 | Ok(()) |
| 82 | } |
| 83 | |
| 84 | /// Moves `uid` from `from` to `to` with a crash between the rename and the uidlist writes: |
| 85 | /// both folder directories, where the uidlists are rewritten, refuse the writes, while the |
| 86 | /// `cur/` directories the rename uses do not. |
| 87 | fn crashed_move(store: &MaildirStore, dirs: [&Path; 2], from: &FolderName, uid: u32, to: &FolderName) |
| 88 | -> Outcome<()> |
| 89 | { |
| 90 | for d in dirs { res!(set_mode(d, 0o555)); } |
| 91 | let outcome = store.move_to(&user(), from, &[MessageUid(uid)], to); |
| 92 | for d in dirs { res!(set_mode(d, 0o755)); } |
| 93 | assert!(outcome.is_err(), "the injected fault did not reach the move: {:?}", outcome); |
| 94 | Ok(()) |
| 95 | } |
| 96 | |
| 97 | #[cfg(unix)] |
| 98 | #[test] |
| 99 | fn a_crash_mid_move_leaves_one_copy_under_a_fresh_uid() -> Outcome<()> { |
| 100 | if is_root() { return Ok(()); } |
| 101 | let root = std::env::temp_dir().join(fmt!("fe2o3_maildir_move_{}", std::process::id())); |
| 102 | let _ = fs::remove_dir_all(&root); |
| 103 | res!(fs::create_dir_all(&root), IO, File); |
| 104 | let store = res!(MaildirStore::new(PathBuf::from(&root), "test.local")); |
| 105 | let u = user(); |
| 106 | res!(store.ensure_user(&u)); |
| 107 | let (inbox, archive) = (FolderName::new("INBOX"), FolderName::new("Archive")); |
| 108 | let inbox_dir = root.join("test.local").join("alice"); |
| 109 | let archive_dir = inbox_dir.join(".Archive"); |
| 110 | let mut uids = Vec::new(); |
| 111 | for i in 0..3 { |
| 112 | let MessageUid(uid) = res!(store.append(&u, &inbox, &body(&fmt!("i{}", i)), MessageFlags::default(), None)); |
| 113 | uids.push(uid); |
| 114 | } |
| 115 | res!(store.append(&u, &archive, &body("a0"), MessageFlags::default(), None)); |
| 116 | let inbox_next = res!(store.folder_status(&u, &inbox)).uid_next; |
| 117 | let archive_next = res!(store.folder_status(&u, &archive)).uid_next; |
| 118 | |
| 119 | // INBOX -> Archive, crashing before either uidlist is written. The INBOX uidlist is |
| 120 | // left naming the message, and is not read again before it comes back. |
| 121 | res!(crashed_move(&store, [&inbox_dir, &archive_dir], &inbox, uids[1], &archive)); |
| 122 | let after = res!(uid_map(&store, &archive)); |
| 123 | let moved: Vec<u32> = after.iter().filter(|(_, t)| *t == "i1").map(|(u, _)| *u).collect(); |
| 124 | assert_eq!(moved.len(), 1, "the message is not in Archive once: {:?}", after); |
| 125 | assert!(moved[0] >= archive_next, "Archive reused UID {} (UIDNEXT was {})", moved[0], archive_next); |
| 126 | |
| 127 | // And back, crashing again: INBOX's stale uidlist must not hand it its old UID. |
| 128 | res!(crashed_move(&store, [&archive_dir, &inbox_dir], &archive, moved[0], &inbox)); |
| 129 | let back = res!(uid_map(&store, &inbox)); |
| 130 | let returned: Vec<u32> = back.iter().filter(|(_, t)| *t == "i1").map(|(u, _)| *u).collect(); |
| 131 | assert_eq!(returned.len(), 1, "the message is not in INBOX once: {:?}", back); |
| 132 | assert!(returned[0] >= inbox_next, |
| 133 | "INBOX gave the returning message UID {}, not above every UID it had issued (UIDNEXT {})", |
| 134 | returned[0], inbox_next); |
| 135 | assert!(res!(uid_map(&store, &archive)).values().all(|t| t != "i1"), "left in Archive too"); |
| 136 | assert_eq!(back.len(), 3); |
| 137 | let _ = fs::remove_dir_all(&root); |
| 138 | Ok(()) |
| 139 | } |
| 140 | |
| 141 | /// MOVE-heavy stress: two threads move everything from INBOX to Archive, two move it back, |
| 142 | /// and one delivers, all at once. No (folder, UID) may name two messages, no UIDVALIDITY may |
| 143 | /// move, nothing may deadlock, and at the end every message is in exactly one folder. |
| 144 | #[test] |
| 145 | fn opposite_moves_and_deliveries_keep_every_message_once() -> Outcome<()> { |
| 146 | let root = std::env::temp_dir().join(fmt!("fe2o3_maildir_move_stress_{}", std::process::id())); |
| 147 | let _ = fs::remove_dir_all(&root); |
| 148 | res!(fs::create_dir_all(&root), IO, File); |
| 149 | let store = res!(MaildirStore::new(root.clone(), "argonaut")); |
| 150 | let u = user(); |
| 151 | let (inbox, arch) = (FolderName::new("INBOX"), FolderName::new("Archive")); |
| 152 | res!(store.ensure_user(&u)); |
| 153 | let mut tags: BTreeSet<String> = BTreeSet::new(); |
| 154 | for i in 0..20 { |
| 155 | let t = fmt!("seed{}", i); |
| 156 | res!(store.append(&u, &inbox, &body(&t), MessageFlags::default(), None)); |
| 157 | tags.insert(t); |
| 158 | } |
| 159 | let v0 = (res!(store.folder_status(&u, &inbox)).uid_validity, |
| 160 | res!(store.folder_status(&u, &arch)).uid_validity); |
| 161 | let start = Instant::now(); |
| 162 | let mut hs = Vec::new(); |
| 163 | for w in 0..5usize { |
| 164 | let r = root.clone(); |
| 165 | hs.push(thread::spawn(move || -> Outcome<(Vec<(u8, u32, String)>, Vec<String>)> { |
| 166 | let store = res!(MaildirStore::new(r, "argonaut")); |
| 167 | let u = user(); |
| 168 | let (inbox, arch) = (FolderName::new("INBOX"), FolderName::new("Archive")); |
| 169 | let mut seen = Vec::new(); |
| 170 | let mut sent = Vec::new(); |
| 171 | for i in 0..30 { |
| 172 | if w == 4 { |
| 173 | let t = fmt!("new{}", i); |
| 174 | res!(store.append(&u, &inbox, &body(&t), MessageFlags::default(), None)); |
| 175 | sent.push(t); |
| 176 | continue; |
| 177 | } |
| 178 | let (from, to, fi) = if w < 2 { (&inbox, &arch, 0u8) } else { (&arch, &inbox, 1u8) }; |
| 179 | let msgs = res!(store.list_messages(&u, from, false)); |
| 180 | for m in &msgs { |
| 181 | // None: moved by another worker since the listing. |
| 182 | if let Some(b) = res!(store.fetch_bytes(&u, from, m.uid)) { |
| 183 | seen.push((fi, m.uid.0, tag_of(&b))); |
| 184 | } |
| 185 | } |
| 186 | let uids: Vec<MessageUid> = msgs.iter().map(|m| m.uid).collect(); |
| 187 | if !uids.is_empty() { |
| 188 | res!(store.move_to(&u, from, &uids, to)); |
| 189 | } |
| 190 | } |
| 191 | Ok((seen, sent)) |
| 192 | })); |
| 193 | } |
| 194 | let mut by_key: BTreeMap<(u8, u32), String> = BTreeMap::new(); |
| 195 | let mut bad = Vec::new(); |
| 196 | for h in hs { |
| 197 | let (seen, sent) = match h.join() { |
| 198 | Ok(r) => res!(r), |
| 199 | Err(_) => return Err(err!("A worker panicked."; Test)), |
| 200 | }; |
| 201 | tags.extend(sent); |
| 202 | for (f, uid, t) in seen { |
| 203 | match by_key.get(&(f, uid)) { |
| 204 | Some(prev) if prev != &t => bad.push(fmt!("folder {} UID {} named {} and {}", f, uid, prev, t)), |
| 205 | Some(_) => (), |
| 206 | None => { by_key.insert((f, uid), t); }, |
| 207 | } |
| 208 | } |
| 209 | } |
| 210 | let took = start.elapsed(); |
| 211 | let a = res!(uid_map(&store, &inbox)); |
| 212 | let b = res!(uid_map(&store, &arch)); |
| 213 | let v1 = (res!(store.folder_status(&u, &inbox)).uid_validity, |
| 214 | res!(store.folder_status(&u, &arch)).uid_validity); |
| 215 | let mut held: Vec<String> = a.values().chain(b.values()).cloned().collect(); |
| 216 | held.sort(); |
| 217 | let want: Vec<String> = tags.into_iter().collect(); |
| 218 | let _ = fs::remove_dir_all(&root); |
| 219 | assert!(bad.is_empty(), "UIDs reused: {:?}", &bad[..bad.len().min(5)]); |
| 220 | assert_eq!(v0, v1, "UIDVALIDITY moved"); |
| 221 | assert_eq!(held, want, "every message in exactly one folder"); |
| 222 | assert!(took < Duration::from_secs(60), "took {:?}", took); |
| 223 | Ok(()) |
| 224 | } |