oxedyne/fe2o3/fe2o3_mail/tests/mail_query.rs
41.4 KiB, 210 runs
created by r1870400018:76334, 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 | //! `sync::query` against Steel over a real socket: a search over folders, page by page, through a |
| 2 | //! second actor, cuts, slowness and a garbled answer (the cases of `F/MAIL-qs/CASES.md`), a raw |
| 3 | //! read and a read by part that leave `\Seen` unset, and a read that fetches `BODYSTRUCTURE` first |
| 4 | //! and holds its byte cap by part. |
| 5 | |
| 6 | use oxedyne_fe2o3_core::prelude::*; |
| 7 | use oxedyne_fe2o3_mail::{ |
| 8 | maildir::MaildirStore, |
| 9 | search::{ |
| 10 | Day, |
| 11 | Limits as SLimits, |
| 12 | Query, |
| 13 | }, |
| 14 | sync::{ |
| 15 | query::{ |
| 16 | self, |
| 17 | Cursor, |
| 18 | Limits, |
| 19 | Page, |
| 20 | Range, |
| 21 | Role, |
| 22 | }, |
| 23 | AppendUid, |
| 24 | Caps, |
| 25 | CopyUid, |
| 26 | FetchChanged, |
| 27 | FetchWhat, |
| 28 | FetchedMessage, |
| 29 | MailSession, |
| 30 | MailboxInfo, |
| 31 | MailboxStatus, |
| 32 | Roles, |
| 33 | SelectWith, |
| 34 | Target, |
| 35 | UidSet, |
| 36 | }, |
| 37 | }; |
| 38 | use oxedyne_fe2o3_net::{ |
| 39 | imap::{ |
| 40 | client::{ |
| 41 | ImapClient, |
| 42 | ImapConfig, |
| 43 | Security, |
| 44 | }, |
| 45 | search::Segment, |
| 46 | server::ImapServer, |
| 47 | }, |
| 48 | mail::{ |
| 49 | store::{ |
| 50 | FlagUpdate, |
| 51 | FolderName, |
| 52 | MailStore, |
| 53 | MailUser, |
| 54 | MessageFlags, |
| 55 | MessageUid, |
| 56 | }, |
| 57 | user::UserStore, |
| 58 | }, |
| 59 | }; |
| 60 | |
| 61 | use std::{ |
| 62 | fs, |
| 63 | path::PathBuf, |
| 64 | time::Duration, |
| 65 | }; |
| 66 | |
| 67 | use tokio::net::TcpListener; |
| 68 | |
| 69 | |
| 70 | const USER: &str = "alice@test.local"; |
| 71 | const PASS: &str = "correct-horse"; // allowlist secret |
| 72 | |
| 73 | #[derive(Clone, Debug)] |
| 74 | struct OneUser; |
| 75 | |
| 76 | impl UserStore for OneUser { |
| 77 | fn authenticate(&self, address: &str, password: &str) -> Outcome<Option<MailUser>> { |
| 78 | if address.eq_ignore_ascii_case(USER) && password == PASS { |
| 79 | return self.lookup(address); |
| 80 | } |
| 81 | Ok(None) |
| 82 | } |
| 83 | |
| 84 | fn lookup(&self, address: &str) -> Outcome<Option<MailUser>> { |
| 85 | if !address.eq_ignore_ascii_case(USER) { return Ok(None); } |
| 86 | Ok(Some(MailUser { |
| 87 | local: fmt!("alice"), |
| 88 | domain: fmt!("test.local"), |
| 89 | delivery_key: fmt!("test.local/alice"), |
| 90 | send_as: Vec::new(), |
| 91 | })) |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | fn plain(from: &str, to: &str, subj: &str, body: &str) -> Vec<u8> { |
| 96 | fmt!("From: {}\r\nTo: {}\r\nSubject: {}\r\nMessage-ID: <{}@test>\r\n\r\n{}\r\n", |
| 97 | from, to, subj, subj.len(), body).into_bytes() |
| 98 | } |
| 99 | |
| 100 | /// A body in an alternative, then an attachment far larger than the read cap. |
| 101 | fn with_attachment() -> Vec<u8> { |
| 102 | let pdf = "QUJD".repeat(5000); // 20,000 octets of base64 |
| 103 | fmt!("From: Shop <shop@example.org>\r\n\ |
| 104 | To: alice@test.local\r\n\ |
| 105 | Subject: Invoice\r\n\ |
| 106 | MIME-Version: 1.0\r\n\ |
| 107 | Content-Type: multipart/mixed; boundary=\"mix\"\r\n\ |
| 108 | \r\n\ |
| 109 | --mix\r\n\ |
| 110 | Content-Type: multipart/alternative; boundary=\"alt\"\r\n\ |
| 111 | \r\n\ |
| 112 | --alt\r\n\ |
| 113 | Content-Type: text/plain; charset=utf-8\r\n\ |
| 114 | \r\n\ |
| 115 | Your invoice is attached. Total 42 dollars.\r\n\ |
| 116 | --alt\r\n\ |
| 117 | Content-Type: text/html; charset=utf-8\r\n\ |
| 118 | \r\n\ |
| 119 | <p>Your invoice is attached.</p>\r\n\ |
| 120 | --alt--\r\n\ |
| 121 | --mix\r\n\ |
| 122 | Content-Type: application/pdf; name=\"invoice.pdf\"\r\n\ |
| 123 | Content-Transfer-Encoding: base64\r\n\ |
| 124 | Content-Disposition: attachment; filename=\"invoice.pdf\"\r\n\ |
| 125 | \r\n\ |
| 126 | {}\r\n\ |
| 127 | --mix--\r\n", pdf).into_bytes() |
| 128 | } |
| 129 | |
| 130 | /// Steel with five messages in INBOX, logged in, with the roles listed. |
| 131 | async fn steel(name: &str) -> Outcome<(ImapClient, Roles, PathBuf)> { |
| 132 | let five = vec![ |
| 133 | plain("Umart Online <sales@umart.com.au>", USER, "Order 1001", "Thanks."), |
| 134 | plain("Bob <bob@example.org>", USER, "Your UMART receipt", "Attached."), |
| 135 | plain("Bob <bob@example.org>", "umart@example.org", "lunch", "Did umart call?"), |
| 136 | plain("Carol <carol@example.org>", USER, "unrelated", "Nothing here."), |
| 137 | with_attachment(), |
| 138 | ]; |
| 139 | let (mut c, root, _, _) = res!(serve(name, vec![("INBOX", five)]).await); |
| 140 | let lim = limits(1000); |
| 141 | let (roles, _) = res!(query::folders(&mut c, &lim).await); |
| 142 | Ok((c, roles, root)) |
| 143 | } |
| 144 | |
| 145 | /// Steel over a fresh store holding `boxes`, each folder filled oldest first, and a session on it. |
| 146 | async fn serve(name: &str, boxes: Vec<(&str, Vec<Vec<u8>>)>) |
| 147 | -> Outcome<(ImapClient, PathBuf, MaildirStore, MailUser)> |
| 148 | { |
| 149 | let root = std::env::temp_dir().join(fmt!("fe2o3_mail_query_{}_{}", name, std::process::id())); |
| 150 | let _ = fs::remove_dir_all(&root); |
| 151 | res!(fs::create_dir_all(&root), IO, File); |
| 152 | let store = res!(MaildirStore::new(PathBuf::from(&root), "test.local")); |
| 153 | let user = res!(res!(OneUser.lookup(USER)).ok_or_else(|| err!("No test user."; Test, Bug))); |
| 154 | res!(store.ensure_user(&user)); |
| 155 | for (b, ms) in boxes { |
| 156 | let f = FolderName::new(b); |
| 157 | store.create_folder(&user, &f).ok(); // the five roles are there already |
| 158 | for m in ms { |
| 159 | res!(store.append(&user, &f, &m, MessageFlags::default(), None)); |
| 160 | } |
| 161 | } |
| 162 | let listener = res!(TcpListener::bind("127.0.0.1:0").await, IO, Network); |
| 163 | let addr = res!(listener.local_addr(), IO, Network); |
| 164 | let server = ImapServer { |
| 165 | store: store.clone(), |
| 166 | users: OneUser, |
| 167 | hostname: std::sync::Arc::new(fmt!("test.local")), |
| 168 | }; |
| 169 | tokio::spawn(async move { |
| 170 | while let Ok((sock, peer)) = listener.accept().await { |
| 171 | let srv = server.clone(); |
| 172 | tokio::spawn(async move { |
| 173 | let _ = srv.run(sock, peer).await; |
| 174 | }); |
| 175 | } |
| 176 | }); |
| 177 | let cfg = ImapConfig::new(fmt!("127.0.0.1"), addr.port(), Security::Plain) |
| 178 | .with_timeout(Duration::from_secs(10)); |
| 179 | let mut c = res!(ImapClient::connect(&cfg).await); |
| 180 | res!(c.login(USER, PASS).await); |
| 181 | Ok((c, root, store, user)) |
| 182 | } |
| 183 | |
| 184 | fn limits(read: u32) -> Limits { |
| 185 | Limits { max_rows: 50, max_read_bytes: read, max_folders: 20 } |
| 186 | } |
| 187 | |
| 188 | async fn seen(c: &mut ImapClient, roles: &Roles) -> Outcome<Vec<u32>> { |
| 189 | let l = res!(query::list(c, roles, &Target::Inbox, &Range::default(), &limits(1000)).await); |
| 190 | Ok(l.rows.iter().filter(|h| h.seen()).map(|h| h.uid).collect()) |
| 191 | } |
| 192 | |
| 193 | /// SC8, `find`'s case on `search`: From or Subject, in any case, newest first. |
| 194 | #[tokio::test] |
| 195 | async fn search_matches_from_and_subject_in_any_case_and_nothing_else() -> Outcome<()> { |
| 196 | let (mut c, roles, root) = res!(steel("find").await); |
| 197 | let p = res!(query::search(&mut c, &Caps::default(), &roles, &res!(q("{from:umart subject:umart}")), |
| 198 | None, &slim(9), TODAY).await); |
| 199 | let subjects: Vec<&str> = p.hits.iter().map(|h| h.header.subject.as_str()).collect(); |
| 200 | assert_eq!(subjects, vec!["Your UMART receipt", "Order 1001"], |
| 201 | "From and Subject, newest first, and neither To nor the body"); |
| 202 | assert_eq!(p.coverage.total, 5); |
| 203 | assert!(p.cursor.is_none()); |
| 204 | let none = res!(query::search(&mut c, &Caps::default(), &roles, &res!(q("zebra")), None, &slim(9), TODAY).await); |
| 205 | assert!(none.hits.is_empty()); |
| 206 | let _ = fs::remove_dir_all(&root); |
| 207 | Ok(()) |
| 208 | } |
| 209 | |
| 210 | #[tokio::test] |
| 211 | async fn raw_leaves_seen_unset_and_holds_its_cap() -> Outcome<()> { |
| 212 | let (mut c, roles, root) = res!(steel("raw").await); |
| 213 | let whole = res!(res!(query::raw(&mut c, &roles, &Target::Inbox, 1, 1_000_000).await) |
| 214 | .ok_or_else(|| err!("UID 1 was not read."; Test, Missing))); |
| 215 | assert!(!whole.cut && whole.bytes.len() as u32 == whole.size, "the whole message: {:?}", whole); |
| 216 | assert!(String::from_utf8_lossy(&whole.bytes).contains("Order 1001")); |
| 217 | let part = res!(res!(query::raw(&mut c, &roles, &Target::Inbox, 5, 100).await) |
| 218 | .ok_or_else(|| err!("UID 5 was not read."; Test, Missing))); |
| 219 | assert!(part.cut && part.bytes.len() == 100, "cut at 100: {} of {}", part.bytes.len(), part.size); |
| 220 | assert!(res!(query::raw(&mut c, &roles, &Target::Inbox, 99, 100).await).is_none()); |
| 221 | assert_eq!(res!(seen(&mut c, &roles).await), Vec::<u32>::new(), "a raw read set \\Seen"); |
| 222 | let _ = fs::remove_dir_all(&root); |
| 223 | Ok(()) |
| 224 | } |
| 225 | |
| 226 | #[tokio::test] |
| 227 | async fn read_fetches_the_structure_first_and_holds_the_cap_by_part() -> Outcome<()> { |
| 228 | let (mut c, roles, root) = res!(steel("read").await); |
| 229 | // A cap far under the message's 20 KB, which a cut of the whole would spend on its head. |
| 230 | let r = res!(res!(query::read(&mut c, &roles, &Target::Inbox, 5, &limits(2000)).await) |
| 231 | .ok_or_else(|| err!("UID 5 was not read."; Test, Missing))); |
| 232 | assert!(r.header.size > 20_000); |
| 233 | assert_eq!(r.header.subject, "Invoice"); |
| 234 | assert_eq!(r.texts.len(), 1, "the plain form of the alternative only: {:?}", r.texts); |
| 235 | assert_eq!((r.texts[0].section.as_str(), r.texts[0].media.as_str()), ("1.1", "text/plain")); |
| 236 | assert!(r.texts[0].text.contains("Total 42 dollars") && !r.cut(), "{:?}", r.texts[0]); |
| 237 | assert_eq!(r.attachments.len(), 1); |
| 238 | let a = &r.attachments[0]; |
| 239 | assert_eq!((a.section.as_str(), a.media.as_str(), a.name.as_deref()), |
| 240 | ("2", "application/pdf", Some("invoice.pdf"))); |
| 241 | assert!(a.size >= 20_000, "the attachment's size, unread: {}", a.size); |
| 242 | |
| 243 | // A cap under the body cuts the body, and says so. |
| 244 | let r = res!(res!(query::read(&mut c, &roles, &Target::Inbox, 5, &limits(10)).await) |
| 245 | .ok_or_else(|| err!("UID 5 was not read."; Test, Missing))); |
| 246 | assert!(r.cut() && r.texts[0].text.len() <= 10, "{:?}", r.texts); |
| 247 | |
| 248 | // A message that is not multipart is its own part 1. |
| 249 | let r = res!(res!(query::read(&mut c, &roles, &Target::Inbox, 1, &limits(2000)).await) |
| 250 | .ok_or_else(|| err!("UID 1 was not read."; Test, Missing))); |
| 251 | assert_eq!(r.texts.len(), 1); |
| 252 | assert_eq!(r.texts[0].section, "1"); |
| 253 | assert!(r.texts[0].text.contains("Thanks."), "{:?}", r.texts); |
| 254 | assert_eq!(res!(seen(&mut c, &roles).await), Vec::<u32>::new(), "a read set \\Seen"); |
| 255 | let _ = fs::remove_dir_all(&root); |
| 256 | Ok(()) |
| 257 | } |
| 258 | |
| 259 | #[tokio::test] |
| 260 | async fn folders_counts_and_names_the_inbox_on_steel() -> Outcome<()> { |
| 261 | let (mut c, _, root) = res!(steel("folders").await); |
| 262 | let (_, fs) = res!(query::folders(&mut c, &limits(1000)).await); |
| 263 | let inbox = res!(fs.iter().find(|f| f.role == Some(Role::Inbox)) |
| 264 | .ok_or_else(|| err!("No inbox: {:?}", fs; Test, Missing))); |
| 265 | assert_eq!(inbox.count, Some(5)); |
| 266 | let _ = fs::remove_dir_all(&root); |
| 267 | Ok(()) |
| 268 | } |
| 269 | |
| 270 | /// Each read-only query opens its folder with `EXAMINE` (mail design I7): Steel refuses a flag |
| 271 | /// write in that session, which it does only after `EXAMINE`, and a `SELECT` after them all is |
| 272 | /// read-write again. |
| 273 | #[tokio::test] |
| 274 | async fn queries_examine_so_the_session_cannot_write_and_recent_is_kept() -> Outcome<()> { |
| 275 | let (mut c, roles, root) = res!(steel("examine").await); |
| 276 | let one = UidSet::from([1u32]); |
| 277 | let lim = limits(1000); |
| 278 | res!(query::list(&mut c, &roles, &Target::Inbox, &Range::default(), &lim).await); |
| 279 | assert!(c.uid_store_flags(&one, &["\\Flagged"], &[]).await.is_err(), |
| 280 | "a flag write was taken after list, so it opened with SELECT"); |
| 281 | res!(query::search(&mut c, &Caps::default(), &roles, &res!(q("umart")), None, &slim(9), TODAY).await); |
| 282 | assert!(c.uid_store_flags(&one, &["\\Flagged"], &[]).await.is_err(), |
| 283 | "a flag write was taken after search"); |
| 284 | res!(query::read(&mut c, &roles, &Target::Inbox, 1, &lim).await); |
| 285 | assert!(c.uid_store_flags(&one, &["\\Flagged"], &[]).await.is_err(), |
| 286 | "a flag write was taken after read"); |
| 287 | res!(query::raw(&mut c, &roles, &Target::Inbox, 1, 100).await); |
| 288 | assert!(c.uid_store_flags(&one, &["\\Flagged"], &[]).await.is_err(), |
| 289 | "a flag write was taken after raw"); |
| 290 | let st = res!(c.select("INBOX").await); |
| 291 | assert!(!st.read_only, "SELECT after the queries is read-write again"); |
| 292 | let _ = fs::remove_dir_all(&root); |
| 293 | Ok(()) |
| 294 | } |
| 295 | |
| 296 | |
| 297 | // ┌───────────────────────────────────────────────────────────────────────────┐ |
| 298 | // │ SEARCH (MAIL-qs): the cases of F/MAIL-qs/CASES.md │ |
| 299 | // └───────────────────────────────────────────────────────────────────────────┘ |
| 300 | |
| 301 | const TODAY: Day = Day { year: 2026, month: 11, day: 16 }; |
| 302 | |
| 303 | fn q(s: &str) -> Outcome<Query> { |
| 304 | Query::parse(s, &SLimits::DEFAULT) |
| 305 | } |
| 306 | |
| 307 | fn slim(rows: usize) -> SLimits { |
| 308 | SLimits { search_page_rows: rows, search_confirm_max_bytes: 1 << 20, search_scan_max_messages: 1000, |
| 309 | search_max_folders: 10, ..SLimits::DEFAULT } |
| 310 | } |
| 311 | |
| 312 | fn msg(i: u32, subj: &str, body: &str) -> Vec<u8> { |
| 313 | fmt!("From: a@test.local\r\nSubject: {}\r\nMessage-ID: <{}@t>\r\n\r\n{}\r\n", subj, i, body).into_bytes() |
| 314 | } |
| 315 | |
| 316 | /// `body` in a multipart of `ctype`'s two parts, each given as its header lines and its body. |
| 317 | fn multi(i: u32, ctype: &str, a: (&str, &str), b: (&str, &str)) -> Vec<u8> { |
| 318 | fmt!("From: a@test.local\r\nSubject: m\r\nMessage-ID: <{}@t>\r\nMIME-Version: 1.0\r\n\ |
| 319 | Content-Type: {}; boundary=\"b\"\r\n\r\n--b\r\n{}\r\n\r\n{}\r\n--b\r\n{}\r\n\r\n{}\r\n--b--\r\n", |
| 320 | i, ctype, a.0, a.1, b.0, b.1).into_bytes() |
| 321 | } |
| 322 | |
| 323 | /// A plain body and a PDF disposed as an attachment with `disp`'s parameters. |
| 324 | fn att(i: u32, body: &str, disp: &str) -> Vec<u8> { |
| 325 | multi(i, "multipart/mixed", ("Content-Type: text/plain", body), |
| 326 | (&fmt!("Content-Type: application/pdf\r\nContent-Disposition: attachment; {}", disp), "QUJD")) |
| 327 | } |
| 328 | |
| 329 | /// Another session's expunge, by the store. |
| 330 | fn gone(store: &MaildirStore, user: &MailUser, b: &str, uids: &[u32]) -> Outcome<()> { |
| 331 | let (f, u) = (FolderName::new(b), uids.iter().map(|x| MessageUid(*x)).collect::<Vec<_>>()); |
| 332 | res!(store.update_flags_many(user, &f, &u, FlagUpdate::Add(MessageFlags { deleted: true, ..Default::default() }))); |
| 333 | res!(store.expunge(user, &f, Some(&u))); |
| 334 | Ok(()) |
| 335 | } |
| 336 | |
| 337 | /// Steel's session as a search meets it, each call logged. `MAIL_FAULT` degrades every case: |
| 338 | /// `slow:<ms>` delays each call, `garble` widens each SEARCH's answer by every odd UID, `cut` |
| 339 | /// drops the third FETCH. |
| 340 | struct Tap { |
| 341 | c: ImapClient, |
| 342 | log: Vec<String>, |
| 343 | slow: u64, |
| 344 | garble: bool, // each SEARCH answered with the whole folder |
| 345 | wide: bool, // each SEARCH's answer widened by every odd UID |
| 346 | refuse: Option<u32>, // a FETCH holding this UID is rejected (BAD); 0: every FETCH |
| 347 | poison: Option<u32>, // this UID's text is refused (NO) |
| 348 | cut: Option<(&'static str, usize)>, // the nth call of this kind from now fails once, as a drop |
| 349 | gmail: bool, // X-GM-EXT-1, `\All` on "All Mail", X-GM-MSGID and labels |
| 350 | plain: bool, // no BODYSTRUCTURE, as from a server that garbles it |
| 351 | dates: Vec<(u32, String)>, // INTERNALDATE by UID |
| 352 | gone: Vec<&'static str>, // folders listed and since deleted, which EXAMINE refuses |
| 353 | bytes: usize, // of message text fetched |
| 354 | then: Option<Box<dyn FnOnce() -> Outcome<()>>>, // a second actor, between a SEARCH and its FETCH |
| 355 | } |
| 356 | |
| 357 | impl Tap { |
| 358 | async fn new(name: &str, boxes: Vec<(&str, Vec<Vec<u8>>)>, gmail: bool) |
| 359 | -> Outcome<(Self, Roles, PathBuf, MaildirStore, MailUser)> |
| 360 | { |
| 361 | let (c, root, store, user) = res!(serve(name, boxes).await); |
| 362 | let f = std::env::var("MAIL_FAULT").unwrap_or_default(); |
| 363 | let mut t = Self { c, log: Vec::new(), slow: f.strip_prefix("slow:").and_then(|n| n.parse().ok()).unwrap_or(0), |
| 364 | garble: false, wide: f == "garble", refuse: None, poison: None, cut: if f == "cut" { Some(("FETCH", 3)) } else { None }, |
| 365 | gmail, plain: false, dates: Vec::new(), gone: Vec::new(), bytes: 0, then: None }; |
| 366 | let roles = Roles::from_list(&res!(MailSession::list(&mut t).await)); |
| 367 | Ok((t, roles, root.clone(), store, user)) |
| 368 | } |
| 369 | |
| 370 | async fn call(&mut self, what: String) -> Outcome<()> { |
| 371 | tokio::time::sleep(Duration::from_millis(self.slow)).await; |
| 372 | if let Some((k, n)) = self.cut.filter(|(k, _)| what.starts_with(k)) { |
| 373 | self.cut = if n > 1 { Some((k, n - 1)) } else { None }; |
| 374 | if n == 1 { |
| 375 | self.log.push(fmt!("CUT {}", what)); |
| 376 | return Err(err!("The connection dropped at {}.", what; IO, Network, Interrupted)); |
| 377 | } |
| 378 | } |
| 379 | self.log.push(what); |
| 380 | Ok(()) |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | impl MailSession for Tap { |
| 385 | async fn capabilities(&mut self) -> Outcome<Vec<String>> { |
| 386 | let mut v = res!(MailSession::capabilities(&mut self.c).await); |
| 387 | if self.gmail { v.push(fmt!("X-GM-EXT-1")); } |
| 388 | Ok(v) |
| 389 | } |
| 390 | async fn list(&mut self) -> Outcome<Vec<MailboxInfo>> { |
| 391 | let (mut v, g) = (res!(MailSession::list(&mut self.c).await), self.gmail); |
| 392 | for m in v.iter_mut().filter(|m| g && m.name == "All Mail") { m.attrs.push(fmt!("\\All")); } |
| 393 | Ok(v) |
| 394 | } |
| 395 | async fn create(&mut self, mb: &str) -> Outcome<()> { |
| 396 | res!(self.call(fmt!("CREATE {}", mb)).await); |
| 397 | MailSession::create(&mut self.c, mb).await |
| 398 | } |
| 399 | async fn select(&mut self, mb: &str, w: &SelectWith) -> Outcome<MailboxStatus> { |
| 400 | res!(self.call(fmt!("SELECT {}", mb)).await); |
| 401 | MailSession::select(&mut self.c, mb, w).await |
| 402 | } |
| 403 | async fn examine(&mut self, mb: &str, w: &SelectWith) -> Outcome<MailboxStatus> { |
| 404 | res!(self.call(fmt!("EXAMINE {}", mb)).await); |
| 405 | if self.gone.contains(&mb) { |
| 406 | return Err(err!("IMAP server refused EXAMINE: [NONEXISTENT] {}", mb; IO, Network, NotFound)); |
| 407 | } |
| 408 | MailSession::examine(&mut self.c, mb, w).await |
| 409 | } |
| 410 | async fn uid_fetch(&mut self, u: &UidSet, w: FetchWhat) -> Outcome<Vec<FetchedMessage>> { |
| 411 | if let Some(f) = self.then.take() { res!(f()); } |
| 412 | res!(self.call(fmt!("FETCH {:?} {}", w, u)).await); |
| 413 | if self.refuse.map_or(false, |r| r == 0 || u.contains(r)) { |
| 414 | return Err(err!("IMAP server rejected UID FETCH as malformed: broken mail"; IO, Network, Wire)); |
| 415 | } |
| 416 | let mut v = res!(MailSession::uid_fetch(&mut self.c, u, w).await); |
| 417 | for m in v.iter_mut() { |
| 418 | self.bytes += m.body.len(); |
| 419 | if let Some((_, d)) = self.dates.iter().find(|(x, _)| *x == m.uid) { m.internal_date = d.clone(); } |
| 420 | if self.plain { m.structure = None; } |
| 421 | if self.gmail { |
| 422 | let id = String::from_utf8_lossy(&m.body).lines().find(|l| l.starts_with("Message-ID")) |
| 423 | .map(|l| l.chars().filter(char::is_ascii_digit).collect::<String>()); |
| 424 | m.gm_msgid = id.and_then(|d| d.parse().ok()); |
| 425 | m.gm_labels = vec![fmt!("\\Inbox")]; |
| 426 | } |
| 427 | } |
| 428 | Ok(v) |
| 429 | } |
| 430 | async fn uid_fetch_changed(&mut self, u: &UidSet, w: FetchWhat, ms: u64) -> Outcome<FetchChanged> { |
| 431 | res!(self.call(fmt!("FETCH CHANGED")).await); |
| 432 | MailSession::uid_fetch_changed(&mut self.c, u, w, ms).await |
| 433 | } |
| 434 | async fn uid_search(&mut self, c: &str) -> Outcome<Vec<u32>> { |
| 435 | res!(self.call(fmt!("SEARCH {}", c)).await); |
| 436 | MailSession::uid_search(&mut self.c, c).await |
| 437 | } |
| 438 | async fn uid_search_key(&mut self, a: &[Segment]) -> Outcome<Vec<u32>> { |
| 439 | res!(self.call(fmt!("SEARCH {:?}", a)).await); |
| 440 | let mut v = match self.garble { |
| 441 | true => res!(MailSession::uid_search(&mut self.c, "ALL").await), |
| 442 | false => res!(MailSession::uid_search_key(&mut self.c, a).await), |
| 443 | }; |
| 444 | if self.wide { |
| 445 | v.extend(res!(MailSession::uid_search(&mut self.c, "ALL").await).into_iter().filter(|u| u % 2 == 1)); |
| 446 | v.sort_unstable(); |
| 447 | v.dedup(); |
| 448 | } |
| 449 | Ok(v) |
| 450 | } |
| 451 | async fn uid_fetch_section(&mut self, uid: u32, s: &str, max: u32) -> Outcome<Option<FetchedMessage>> { |
| 452 | res!(self.call(fmt!("SECTION {} {}", uid, s)).await); |
| 453 | if self.poison == Some(uid) { |
| 454 | return Err(err!("IMAP server refused UID FETCH: [SERVERBUG] broken mail"; IO, Network, Invalid)); |
| 455 | } |
| 456 | let f = res!(MailSession::uid_fetch_section(&mut self.c, uid, s, max).await); |
| 457 | self.bytes += f.as_ref().map_or(0, |f| f.body.len()); |
| 458 | Ok(f) |
| 459 | } |
| 460 | fn read_bytes(&self) -> u64 { |
| 461 | self.c.read_bytes() |
| 462 | } |
| 463 | async fn count(&mut self, mb: &str) -> Outcome<u32> { |
| 464 | MailSession::count(&mut self.c, mb).await |
| 465 | } |
| 466 | async fn uid_store_flags(&mut self, u: &UidSet, a: &[&str], r: &[&str]) -> Outcome<()> { |
| 467 | res!(self.call(fmt!("STORE {}", u)).await); |
| 468 | MailSession::uid_store_flags(&mut self.c, u, a, r).await |
| 469 | } |
| 470 | async fn uid_copy(&mut self, u: &UidSet, to: &str) -> Outcome<Option<CopyUid>> { |
| 471 | res!(self.call(fmt!("COPY {}", u)).await); |
| 472 | MailSession::uid_copy(&mut self.c, u, to).await |
| 473 | } |
| 474 | async fn uid_move(&mut self, u: &UidSet, to: &str) -> Outcome<Option<CopyUid>> { |
| 475 | res!(self.call(fmt!("MOVE {}", u)).await); |
| 476 | MailSession::uid_move(&mut self.c, u, to).await |
| 477 | } |
| 478 | async fn uid_expunge(&mut self, u: &UidSet) -> Outcome<()> { |
| 479 | res!(self.call(fmt!("EXPUNGE {}", u)).await); |
| 480 | MailSession::uid_expunge(&mut self.c, u).await |
| 481 | } |
| 482 | async fn append(&mut self, mb: &str, f: &[&str], raw: &[u8]) -> Outcome<Option<AppendUid>> { |
| 483 | res!(self.call(fmt!("APPEND {}", mb)).await); |
| 484 | MailSession::append(&mut self.c, mb, f, raw).await |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | /// The pages of a run from `cur` to its end, a page that fails asked once more with the same |
| 489 | /// cursor (SB2), and then nothing of the run written (S3, SC6). |
| 490 | async fn run(t: &mut Tap, caps: &Caps, roles: &Roles, q: &Query, cur: Option<Cursor>, lim: &SLimits) |
| 491 | -> Outcome<Vec<Page>> |
| 492 | { |
| 493 | let (mut pages, mut cur) = (Vec::new(), cur); |
| 494 | for _ in 0..500 { |
| 495 | let p = match query::search(t, caps, roles, q, cur.as_ref(), lim, TODAY).await { |
| 496 | Ok(p) => p, |
| 497 | Err(_) => res!(query::search(t, caps, roles, q, cur.as_ref(), lim, TODAY).await), |
| 498 | }; |
| 499 | cur = p.cursor.clone(); |
| 500 | pages.push(p); |
| 501 | if cur.is_none() { break; } |
| 502 | } |
| 503 | let w = t.log.iter().find(|l| ["SELECT", "STORE", "COPY", "MOVE", "EXPUNGE", "APPEND", "CREATE"] |
| 504 | .iter().any(|v| l.starts_with(v))); |
| 505 | assert!(w.is_none() && cur.is_none(), "S3: {:?}; run ended: {}", w, cur.is_none()); |
| 506 | Ok(pages) |
| 507 | } |
| 508 | |
| 509 | fn uids(ps: &[Page]) -> Vec<u32> { |
| 510 | ps.iter().flat_map(|p| p.hits.iter().map(|h| h.uid)).collect() |
| 511 | } |
| 512 | |
| 513 | /// The UIDs asked for by each FETCH of `what` logged, a list a window (the calls after a SEARCH). |
| 514 | fn asked(log: &[String], what: &str) -> Vec<Vec<u32>> { |
| 515 | let mut out = vec![Vec::new()]; |
| 516 | for l in log { |
| 517 | match (l.starts_with("SEARCH"), l.strip_prefix(&fmt!("FETCH {} ", what))) { |
| 518 | (true, _) => out.push(Vec::new()), |
| 519 | (_, Some(u)) => if let (Ok(set), Some(w)) = (UidSet::parse(u), out.last_mut()) { w.extend(set.iter()); }, |
| 520 | _ => {} |
| 521 | } |
| 522 | } |
| 523 | out |
| 524 | } |
| 525 | |
| 526 | fn structures(log: &[String]) -> Vec<u32> { |
| 527 | asked(log, "BodyStructure").concat() |
| 528 | } |
| 529 | |
| 530 | /// Is any UID asked for twice by FETCHes of `what` in one window? |
| 531 | fn twice(log: &[String], what: &str) -> bool { |
| 532 | asked(log, what).into_iter().any(|mut w| { let n = w.len(); w.sort_unstable(); w.dedup(); w.len() < n }) |
| 533 | } |
| 534 | |
| 535 | /// SA1, SA3, SC4: `invoice` over 120 mails, every other a hit, 20 rows a page. Between pages |
| 536 | /// another session expunges three hits above the cursor and delivers five above the top, and |
| 537 | /// during the second page, between SEARCH and its first FETCH, expunges two candidates below it. |
| 538 | #[tokio::test] |
| 539 | async fn search_pages_hold_through_a_second_actor() -> Outcome<()> { |
| 540 | let ms = (1..=120).map(|i| msg(i, "m", if i % 2 == 0 { "your invoice" } else { "a receipt" })).collect(); |
| 541 | let (mut t, roles, root, store, user) = res!(Tap::new("qs_sa1", vec![("INBOX", ms)], false).await); |
| 542 | let (caps, q, lim) = (Caps::default(), res!(q("invoice")), slim(20)); |
| 543 | let p1 = res!(run(&mut t, &caps, &roles, &q, None, &SLimits { search_page_rows: 1000, ..lim.clone() }).await); |
| 544 | assert_eq!(uids(&p1), (2..=120).rev().step_by(2).collect::<Vec<u32>>(), "SC4: one unbounded pass"); |
| 545 | let cut = t.cut.take(); // the degraded cut falls in the run below, not on the pages asserted whole |
| 546 | let p1 = res!(query::search(&mut t, &caps, &roles, &q, None, &lim, TODAY).await); |
| 547 | assert_eq!(p1.hits.iter().map(|h| h.uid).collect::<Vec<_>>(), (82..=120).rev().step_by(2).collect::<Vec<u32>>()); |
| 548 | res!(gone(&store, &user, "INBOX", &[120, 118, 116])); |
| 549 | for i in 121..=125 { |
| 550 | res!(store.append(&user, &FolderName::new("INBOX"), &msg(i, "m", "late invoice"), MessageFlags::default(), None)); |
| 551 | } |
| 552 | let (s2, u2) = (store.clone(), user.clone()); |
| 553 | t.then = Some(Box::new(move || gone(&s2, &u2, "INBOX", &[80, 78]))); |
| 554 | t.cut = cut; |
| 555 | let rest = res!(run(&mut t, &caps, &roles, &q, p1.cursor.clone(), &lim).await); |
| 556 | assert!(rest.iter().all(|p| p.hits.windows(2).all(|w| w[0].uid > w[1].uid)), "newest first"); |
| 557 | assert_eq!(uids(&rest), (2..=76).rev().step_by(2).collect::<Vec<u32>>(), "no hole, no repeat, nothing above the top"); |
| 558 | assert_eq!(p1.coverage.checked + rest.iter().map(|p| p.coverage.checked).sum::<usize>(), |
| 559 | 118, "every message but the two gone"); |
| 560 | assert!(rest.iter().all(|p| p.coverage.top_uid == 120), "the run's top: {:?}", rest[0].coverage); |
| 561 | let _ = fs::remove_dir_all(&root); |
| 562 | Ok(()) |
| 563 | } |
| 564 | |
| 565 | /// SA5, SA6, SA2, SC9: each page examines its own folder; a cursor that does not fit is refused; |
| 566 | /// a folder renumbered under its name restarts from its newest; a folder gone since LIST is named. |
| 567 | #[tokio::test] |
| 568 | async fn search_cursor_examines_its_folder_and_restarts_or_refuses() -> Outcome<()> { |
| 569 | let ms = |k: u32| (1..=30).map(|i| msg(k + i, "m", "invoice")).collect::<Vec<_>>(); |
| 570 | let (mut t, roles, root, ..) = res!(Tap::new("qs_sa2", |
| 571 | vec![("INBOX", ms(0)), ("Archive", ms(100)), ("Empty", vec![]), ("Gone", vec![])], false).await); |
| 572 | let (caps, lim) = (Caps::default(), slim(10)); |
| 573 | let (qa, qb) = (res!(q("invoice in:INBOX")), res!(q("invoice in:Archive"))); |
| 574 | let cut = t.cut.take(); // as in SA1 |
| 575 | let a1 = res!(query::search(&mut t, &caps, &roles, &qa, None, &lim, TODAY).await); |
| 576 | let b1 = res!(query::search(&mut t, &caps, &roles, &qb, None, &lim, TODAY).await); |
| 577 | let n = t.log.len(); |
| 578 | let a2 = res!(query::search(&mut t, &caps, &roles, &qa, a1.cursor.as_ref(), &lim, TODAY).await); |
| 579 | assert_eq!(t.log.get(n).map(String::as_str), Some("EXAMINE INBOX"), "SA5: {:?}", &t.log[n..]); |
| 580 | assert_eq!(uids(&[a2]), (11..=20).rev().collect::<Vec<u32>>()); |
| 581 | let bc = res!(b1.cursor.ok_or_else(|| err!("No cursor."; Test, Missing))); |
| 582 | assert!(query::search(&mut t, &caps, &roles, &qa, Some(&bc), &lim, TODAY).await.is_err(), "SA6: another query's"); |
| 583 | assert!(Cursor::parse("xyz").is_err(), "SA6: no cursor"); |
| 584 | let edited = res!(Cursor::parse(&fmt!("{}9", bc))); |
| 585 | assert!(query::search(&mut t, &caps, &roles, &qb, Some(&edited), &lim, TODAY).await.is_err(), "SA6: edited"); |
| 586 | let me = root.join("test.local/alice"); |
| 587 | res!(fs::remove_file(me.join(".Archive/dovecot-uidlist")), IO, File); |
| 588 | let b2 = res!(query::search(&mut t, &caps, &roles, &qb, Some(&bc), &lim, TODAY).await); |
| 589 | assert_eq!(b2.coverage.restarted, vec![fmt!("Archive")], "SA2"); |
| 590 | assert_eq!(b2.hits.first().map(|h| h.uid), Some(b2.coverage.top_uid), "SA2: from its newest"); |
| 591 | (t.gone, t.cut) = (vec!["Gone"], cut); // Steel answers EXAMINE of a folder gone from disk as an empty one |
| 592 | let all = res!(run(&mut t, &caps, &roles, &res!(q("invoice in:all")), None, &slim(100)).await); |
| 593 | assert!(all[0].coverage.skipped.iter().any(|(f, _)| f == "Gone"), "SC9: {:?}", all[0].coverage); |
| 594 | assert_eq!(uids(&all).len(), 60); |
| 595 | let _ = fs::remove_dir_all(&root); |
| 596 | Ok(()) |
| 597 | } |
| 598 | |
| 599 | /// SB1, SB2: a drop at the seventh text fetch of page 2 keeps what was confirmed and resumes to |
| 600 | /// the uncut run's answer; a drop at the SEARCH is an error, and the same cursor gives the page. |
| 601 | #[tokio::test] |
| 602 | async fn search_cut_mid_page_keeps_what_was_confirmed() -> Outcome<()> { |
| 603 | let ms = (1..=60).map(|i| msg(i, "m", if i % 2 == 0 { "invoice" } else { "receipt" })).collect(); |
| 604 | let (mut t, roles, root, ..) = res!(Tap::new("qs_sb1", vec![("INBOX", ms)], false).await); |
| 605 | let (caps, q, lim) = (Caps::default(), res!(q("invoice")), slim(10)); |
| 606 | t.cut = None; |
| 607 | let whole = uids(&res!(run(&mut t, &caps, &roles, &q, None, &lim).await)); |
| 608 | let p1 = res!(query::search(&mut t, &caps, &roles, &q, None, &lim, TODAY).await); |
| 609 | t.cut = Some(("SECTION", 7)); |
| 610 | let p2 = res!(query::search(&mut t, &caps, &roles, &q, p1.cursor.as_ref(), &lim, TODAY).await); |
| 611 | // Six confirmed and, where narrowed, six decided by their structures; widened, six confirmed. |
| 612 | assert!(p2.coverage.stopped.is_some() && p2.coverage.checked == if t.wide { 6 } else { 12 }, "SB1: {:?}", p2.coverage); |
| 613 | let mut got = uids(&[p1.clone(), p2.clone()]); |
| 614 | got.extend(uids(&res!(run(&mut t, &caps, &roles, &q, p2.cursor.clone(), &lim).await))); |
| 615 | assert_eq!(got, whole, "SB1: the union of the cut run"); |
| 616 | t.cut = Some(("SEARCH", 1)); |
| 617 | assert!(query::search(&mut t, &caps, &roles, &q, p1.cursor.as_ref(), &lim, TODAY).await.is_err(), "SB2"); |
| 618 | let again = res!(query::search(&mut t, &caps, &roles, &q, p1.cursor.as_ref(), &lim, TODAY).await); |
| 619 | assert_eq!(uids(&[again]), whole[10..20].to_vec(), "SB2: the same page"); |
| 620 | let _ = fs::remove_dir_all(&root); |
| 621 | Ok(()) |
| 622 | } |
| 623 | |
| 624 | /// SC1, SC5, SC7, SA4, SB5, SB6, SB8: what one folder answers, whole words, a text past the |
| 625 | /// byte bound, INTERNALDATE's own day, flags set mid-page, an unreadable structure, a garbled |
| 626 | /// SEARCH, and text no server key can carry. |
| 627 | #[tokio::test] |
| 628 | async fn search_corpus_answers_by_the_matcher() -> Outcome<()> { |
| 629 | let big = |at_end: bool| fmt!("{}{}{}", if at_end { "z ".repeat(2_450_000) } else { "x".repeat(100) }, |
| 630 | " needle ", if at_end { String::new() } else { "y ".repeat(2_500_000) }); |
| 631 | let ms = vec![msg(1, "m", "cat in the hat"), msg(2, "m", "concat of parts"), msg(3, "Grüße aus Berlin", "hallo"), |
| 632 | with_attachment(), msg(5, "m", &big(false)), msg(6, "m", &big(true)), msg(7, "m", "dated"), |
| 633 | multi(8, "multipart/alternative", ("Content-Type: text/plain", " "), ("Content-Type: text/html", "<p>quokka</p>")), |
| 634 | att(9, "cv", "filename*=utf-8''r%C3%A9sum%C3%A9.pdf")]; |
| 635 | let (mut t, roles, root, store, user) = res!(Tap::new("qs_sc1", vec![("INBOX", ms)], false).await); |
| 636 | let (caps, lim) = (Caps::default(), slim(10)); |
| 637 | let ask = |s: &str| q(s); |
| 638 | let cat = res!(run(&mut t, &caps, &roles, &res!(ask("cat match:word")), None, &lim).await); |
| 639 | assert_eq!(uids(&cat), vec![1], "SC1"); |
| 640 | t.garble = true; |
| 641 | let wide = res!(run(&mut t, &caps, &roles, &res!(ask("cat match:word")), None, &lim).await); |
| 642 | assert_eq!(uids(&wide), vec![1], "SB6: the matcher decides"); |
| 643 | assert!(wide.iter().map(|p| p.coverage.checked + p.coverage.undecided).sum::<usize>() >= 7, "SB6"); |
| 644 | t.garble = false; |
| 645 | let needle = res!(run(&mut t, &caps, &roles, &res!(ask("needle")), None, &lim).await); |
| 646 | assert_eq!(uids(&needle), vec![5], "SC5: a found leaf stands"); |
| 647 | assert_eq!(needle.iter().map(|p| p.coverage.undecided).sum::<usize>(), 1, "SC5: the cut miss undecided"); |
| 648 | t.dates = vec![(7, fmt!("30-Jan-2026 23:30:00 -0700"))]; |
| 649 | let after = res!(run(&mut t, &caps, &roles, &res!(ask("after:2026/01/31")), None, &lim).await); |
| 650 | assert!(!uids(&after).contains(&7) && uids(&after).len() == 8, "SC7: {:?}", uids(&after)); |
| 651 | let (s2, u2) = (store.clone(), user.clone()); |
| 652 | t.then = Some(Box::new(move || { |
| 653 | res!(s2.update_flags_many(&u2, &FolderName::new("INBOX"), &[MessageUid(1), MessageUid(2)], |
| 654 | FlagUpdate::Add(MessageFlags { seen: true, ..Default::default() }))); |
| 655 | Ok(()) |
| 656 | })); |
| 657 | let unread = res!(run(&mut t, &caps, &roles, &res!(ask("is:unread")), None, &lim).await); |
| 658 | assert_eq!(uids(&unread), vec![9, 8, 7, 6, 5, 4, 3], "SA4: the flags as fetched"); |
| 659 | let g = res!(run(&mut t, &caps, &roles, &res!(ask("Grüße")), None, &lim).await); |
| 660 | assert_eq!(uids(&g), vec![3], "SB8"); |
| 661 | let html = res!(run(&mut t, &caps, &roles, &res!(ask("quokka")), None, &lim).await); |
| 662 | assert_eq!(uids(&html), vec![8], "C1: a blank plain form gives way to the HTML, as the reader reads it"); |
| 663 | let cv = res!(run(&mut t, &caps, &roles, &res!(ask("filename:résumé")), None, &lim).await); |
| 664 | assert_eq!(uids(&cv), vec![9], "C2: an RFC 2231 file name"); |
| 665 | t.plain = true; |
| 666 | let named = res!(run(&mut t, &caps, &roles, &res!(ask("filename:invoice")), None, &lim).await); |
| 667 | assert!(named[0].hits.is_empty() && named[0].coverage.undecided > 0, "SB5: {:?}", named[0].coverage); |
| 668 | let _ = fs::remove_dir_all(&root); |
| 669 | Ok(()) |
| 670 | } |
| 671 | |
| 672 | /// SC3, SC2: all folders, a message in two once; Gmail's All Mail searched alone, Junk unticked, |
| 673 | /// its labels named, and a part-word a bounded scan. |
| 674 | #[tokio::test] |
| 675 | async fn search_all_folders_once_per_message_gmail_and_not() -> Outcome<()> { |
| 676 | let hit = msg(1, "m", "invoice due"); |
| 677 | let (mut t, roles, root, ..) = res!(Tap::new("qs_sc3", |
| 678 | vec![("INBOX", vec![hit.clone()]), ("Archive", vec![hit.clone()])], false).await); |
| 679 | let all = res!(q("invoice in:all")); |
| 680 | let ps = res!(run(&mut t, &Caps::default(), &roles, &all, None, &slim(10)).await); |
| 681 | assert_eq!(ps.iter().flat_map(|p| &p.hits).map(|h| h.folders.clone()).collect::<Vec<_>>(), |
| 682 | vec![vec![fmt!("INBOX"), fmt!("Archive")]], "SC3"); |
| 683 | let few = res!(run(&mut t, &Caps::default(), &roles, &all, None, &SLimits { search_max_folders: 2, ..slim(10) }).await); |
| 684 | assert!(!few[0].coverage.skipped.is_empty(), "SC3: the folders over the bound named"); |
| 685 | let _ = fs::remove_dir_all(&root); |
| 686 | |
| 687 | let (mut t, roles, root, ..) = res!(Tap::new("qs_sc2", vec![("INBOX", vec![hit.clone()]), |
| 688 | ("All Mail", vec![hit.clone(), msg(2, "m", "invoice two")]), ("Junk", vec![msg(3, "m", "invoice spam")])], true).await); |
| 689 | let gm = Caps { gmail: true, ..Default::default() }; |
| 690 | t.cut = None; |
| 691 | let ps = res!(run(&mut t, &gm, &roles, &all, None, &slim(10)).await); |
| 692 | assert_eq!(t.log.iter().filter(|l| l.starts_with("EXAMINE")).collect::<Vec<_>>(), vec!["EXAMINE All Mail"], "SC2"); |
| 693 | assert_eq!(uids(&ps), vec![2, 1]); |
| 694 | assert!(ps[0].hits.iter().all(|h| h.folders == ["All Mail", "\\Inbox"]), "SC2: labels"); |
| 695 | let scan = res!(run(&mut t, &gm, &roles, &res!(q("voic in:all")), None, |
| 696 | &SLimits { search_scan_max_messages: 1, ..slim(10) }).await); |
| 697 | assert!(scan.iter().all(|p| !p.coverage.complete && p.coverage.checked <= 1), "SC2: a bounded scan"); |
| 698 | assert_eq!(uids(&scan), vec![2, 1]); |
| 699 | let _ = fs::remove_dir_all(&root); |
| 700 | Ok(()) |
| 701 | } |
| 702 | |
| 703 | /// The read's narrowing: a rare word over 600 mails fetches bodies only for its candidates, the |
| 704 | /// SEARCH's answer and the mails whose attachment names hold it, against today's scan (the Tap |
| 705 | /// garbled: every UID a candidate); `-zanzibar` keeps every other mail, the one named so excluded; |
| 706 | /// and a negation of an attachment fact runs the structure pass under the run's own fault. |
| 707 | #[tokio::test] |
| 708 | async fn search_rare_word_reads_bodies_of_candidates_only() -> Outcome<()> { |
| 709 | let mut ms: Vec<Vec<u8>> = (1..=600).map(|i| msg(i, "m", "nothing to see")).collect(); |
| 710 | for i in [100, 300, 500] { ms[i - 1] = msg(i as u32, "m", "off to zanzibar"); } |
| 711 | ms[399] = att(400, "see attached", "filename=\"zanzibar.pdf\""); |
| 712 | ms[199] = att(200, "an invoice", "filename=\"a.pdf\""); |
| 713 | let (mut t, roles, root, ..) = res!(Tap::new("qs_rare", vec![("INBOX", ms)], false).await); |
| 714 | let fault = (t.cut.take(), t.slow, std::mem::take(&mut t.wide)); |
| 715 | t.slow = 0; // a measurement, run undegraded |
| 716 | let (caps, rare, lim) = (Caps::default(), res!(q("zanzibar")), slim(1000)); |
| 717 | let now = res!(run(&mut t, &caps, &roles, &rare, None, &lim).await); |
| 718 | let (calls, bytes) = (std::mem::take(&mut t.log), std::mem::take(&mut t.bytes)); |
| 719 | t.garble = true; |
| 720 | let before = res!(run(&mut t, &caps, &roles, &rare, None, &lim).await); |
| 721 | println!("rare word: {} calls, {} bytes; as a scan: {} calls, {} bytes", calls.len(), bytes, t.log.len(), t.bytes); |
| 722 | assert_eq!((uids(&now), uids(&before)), (vec![500, 400, 300, 100], vec![500, 400, 300, 100])); |
| 723 | assert!(calls.iter().filter(|l| l.starts_with("SECTION")).count() <= 4 && calls.len() * 20 < t.log.len(), "{:?}", calls); |
| 724 | (t.garble, t.cut, t.slow, t.wide) = (false, fault.0, fault.1, fault.2); |
| 725 | let not = res!(run(&mut t, &caps, &roles, &res!(q("-zanzibar")), None, &lim).await); |
| 726 | assert!(uids(¬).len() == 596 && !uids(¬).contains(&400), "{}", uids(¬).len()); |
| 727 | for (s, want) in [("zanzibar -has:attachment", vec![500, 300, 100]), ("has:attachment -zanzibar", vec![200])] { |
| 728 | t.log.clear(); |
| 729 | let ps = res!(run(&mut t, &caps, &roles, &res!(q(s)), None, &lim).await); |
| 730 | assert!(uids(&ps) == want && !structures(&t.log).is_empty(), "T2 {}: {:?}, {} calls", s, uids(&ps), t.log.len()); |
| 731 | } |
| 732 | let _ = fs::remove_dir_all(&root); |
| 733 | Ok(()) |
| 734 | } |
| 735 | |
| 736 | |
| 737 | /// F1: a message whose FETCH the server rejects (BAD) and one whose text it refuses (NO) are each |
| 738 | /// undecided, and the run goes on to every other hit. |
| 739 | #[tokio::test] |
| 740 | async fn search_refused_message_is_undecided_and_the_run_goes_on() -> Outcome<()> { |
| 741 | let ms = (1..=60).map(|i| msg(i, "m", "zanzibar")).collect(); |
| 742 | let (mut t, roles, root, ..) = res!(Tap::new("qs_f1", vec![("INBOX", ms)], false).await); |
| 743 | (t.refuse, t.poison) = (Some(45), Some(30)); |
| 744 | let ps = res!(run(&mut t, &Caps::default(), &roles, &res!(q("body:zanzibar")), None, &slim(10)).await); |
| 745 | assert_eq!(uids(&ps), (1..=60).rev().filter(|u| *u != 30 && *u != 45).collect::<Vec<u32>>()); |
| 746 | assert_eq!(ps.iter().map(|p| p.coverage.undecided).sum::<usize>(), 2); |
| 747 | let _ = fs::remove_dir_all(&root); |
| 748 | Ok(()) |
| 749 | } |
| 750 | |
| 751 | /// F3: a selective key with a word: structures are fetched only for the key's answer that the |
| 752 | /// narrowed key left, not for the whole window. |
| 753 | #[tokio::test] |
| 754 | async fn search_narrowing_keeps_to_the_keys_answer() -> Outcome<()> { |
| 755 | let shop = |i: u32, body: &str| fmt!("From: shop@amazon.example\r\nSubject: order\r\nMessage-ID: <{}@t>\r\n\r\n{}\r\n", |
| 756 | i, body).into_bytes(); |
| 757 | let mut ms: Vec<Vec<u8>> = (1..=600).map(|i| msg(i, "m", "nothing to see")).collect(); |
| 758 | for i in (10..=600).step_by(20) { ms[i - 1] = shop(i as u32, "your order"); } |
| 759 | ms[89] = shop(90, "zanzibar"); |
| 760 | let (mut t, roles, root, ..) = res!(Tap::new("qs_f3", vec![("INBOX", ms)], false).await); |
| 761 | let ps = res!(run(&mut t, &Caps::default(), &roles, &res!(q("from:amazon zanzibar")), None, &slim(1000)).await); |
| 762 | let st = structures(&t.log); |
| 763 | assert!(uids(&ps) == vec![90] && st.len() <= 30, "{:?}: {} structures", uids(&ps), st.len()); |
| 764 | let _ = fs::remove_dir_all(&root); |
| 765 | Ok(()) |
| 766 | } |
| 767 | |
| 768 | /// F2: a byte bound below one window's structures. Each fetch is charged with what it read, the |
| 769 | /// window is taken in chunks, each page's first candidate is read within the bound, and the run |
| 770 | /// finds every hit with nothing fetched twice in a window. |
| 771 | #[tokio::test] |
| 772 | async fn search_byte_bound_holds_the_structure_pass() -> Outcome<()> { |
| 773 | let mut ms: Vec<Vec<u8>> = (1..=200).map(|i| att(i, "nothing", "filename=\"a-rather-long-file-name.pdf\"")).collect(); |
| 774 | for i in [50, 120, 180] { ms[i - 1] = att(i as u32, "zanzibar", "filename=\"x.pdf\""); } |
| 775 | let (mut t, roles, root, ..) = res!(Tap::new("qs_f2", vec![("INBOX", ms)], false).await); |
| 776 | let lim = SLimits { search_confirm_max_bytes: 4000, ..slim(20) }; |
| 777 | let ps = res!(run(&mut t, &Caps::default(), &roles, &res!(q("zanzibar")), None, &lim).await); |
| 778 | assert_eq!(uids(&ps), vec![180, 120, 50]); |
| 779 | assert!(ps.len() > 1 && !twice(&t.log, "BodyStructure") && !twice(&t.log, "Structure") |
| 780 | && ps.iter().all(|p| p.coverage.undecided == 0), "{} pages", ps.len()); |
| 781 | let _ = fs::remove_dir_all(&root); |
| 782 | Ok(()) |
| 783 | } |
| 784 | |
| 785 | /// R3-2: a server that refuses every FETCH ends the page early in its own words, narrowed or not. |
| 786 | #[tokio::test] |
| 787 | async fn search_server_refusing_every_fetch_stops_the_page() -> Outcome<()> { |
| 788 | let ms = (1..=300).map(|i| msg(i, "m", "zanzibar")).collect(); |
| 789 | let (mut t, roles, root, ..) = res!(Tap::new("qs_r32", vec![("INBOX", ms)], false).await); |
| 790 | (t.cut, t.refuse) = (None, Some(0)); |
| 791 | for s in ["body:zanzibar", "zanzibar"] { |
| 792 | t.log.clear(); |
| 793 | let p = res!(query::search(&mut t, &Caps::default(), &roles, &res!(q(s)), None, &slim(20), TODAY).await); |
| 794 | let fetches = t.log.iter().filter(|l| l.starts_with("FETCH")).count(); |
| 795 | assert!(p.coverage.stopped.as_deref().map_or(false, |w| w.contains("broken mail")) |
| 796 | && p.coverage.undecided <= 20 && p.cursor.is_some() && fetches <= 2 * 21, "{}: {:?}, {} FETCHes", s, p.coverage, fetches); |
| 797 | } |
| 798 | let _ = fs::remove_dir_all(&root); |
| 799 | Ok(()) |
| 800 | } |
| 801 | |
| 802 | /// R3-1: mails that are hits by body and by attachment name in turn: each UID is fetched once, |
| 803 | /// and the bytes read are near those of a run whose every hit the narrowed key names. |
| 804 | #[tokio::test] |
| 805 | async fn search_alternate_hits_fetch_each_once() -> Outcome<()> { |
| 806 | let by_body = |i: u32| att(i, "zanzibar", "filename=\"a.pdf\""); |
| 807 | let mixed = (1..=100).map(|i| if i % 2 == 0 { by_body(i) } else { att(i, "nothing", "filename=\"zanzibar.pdf\"") }).collect(); |
| 808 | let mut bytes = Vec::new(); |
| 809 | for (name, ms) in [("qs_r31a", (1..=100).map(by_body).collect::<Vec<_>>()), ("qs_r31m", mixed)] { |
| 810 | let (mut t, roles, root, ..) = res!(Tap::new(name, vec![("INBOX", ms)], false).await); |
| 811 | t.cut = None; // a measurement: a page asked again would fetch again |
| 812 | let from = t.c.read_bytes(); |
| 813 | let ps = res!(run(&mut t, &Caps::default(), &roles, &res!(q("zanzibar")), None, &slim(20)).await); |
| 814 | assert_eq!(uids(&ps).len(), 100); |
| 815 | assert_eq!(asked(&t.log, "Structure").concat().len(), 100, "{}: candidates asked", name); |
| 816 | bytes.push(t.c.read_bytes() - from); |
| 817 | let _ = fs::remove_dir_all(&root); |
| 818 | } |
| 819 | println!("all by body {} bytes; alternate {} bytes", bytes[0], bytes[1]); |
| 820 | assert!(bytes[1] < bytes[0] * 3 / 2, "{:?}", bytes); |
| 821 | Ok(()) |
| 822 | } |