Oregami
Repositories/oxedyne/fe2o3

oxedyne/fe2o3/fe2o3_mail/tests/mailqx_hostile.rs

10.7 KiB, 13 runs

created by r2968587332:77968, 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//! MAILq-x examiner's hostile cases against `search` (round 3). Each prints what it saw.
2
3use oxedyne_fe2o3_core::prelude::*;
4use oxedyne_fe2o3_mail::search::{
5 Day,
6 Limits,
7 Mail,
8 Matcher,
9 Mode,
10 Query,
11 Term,
12};
13
14use std::time::Instant;
15
16const TODAY: Day = Day { year: 2026, month: 3, day: 4 };
17const LIM: Limits = Limits { search_regex_max_len: 200, search_max_depth: 16, search_max_terms: 500,
18 snippet_chars: 40, snippets_per_part: 2, ..Limits::DEFAULT };
19
20fn mail(raw: &[u8]) -> Mail<'_> {
21 Mail { raw, seen: false, flagged: false, size: raw.len() as u64, day: Day::parse("2026/03/04").ok() }
22}
23
24fn text_msg(subject: &str, body: &str) -> Vec<u8> {
25 format!("From: Ada <ada@x.example>\r\nTo: Bob <bob@x.example>\r\n\
26 Subject: {}\r\nContent-Type: text/plain; charset=utf-8\r\n\r\n{}\r\n", subject, body).into_bytes()
27}
28
29fn ans(q: &Query, raw: &[u8]) -> String {
30 match Matcher::new(q, &LIM, TODAY) {
31 Err(e) => format!("new-err {}", e),
32 Ok(m) => match m.matches(&mail(raw)) {
33 Ok(Some(_)) => "hit".into(),
34 Ok(None) => "miss".into(),
35 Err(_) => "unknown".into(),
36 },
37 }
38}
39
40fn ans_s(s: &str, raw: &[u8]) -> String {
41 match Query::parse(s, &LIM) {
42 Err(e) => format!("parse-err {}", e),
43 Ok(q) => ans(&q, raw),
44 }
45}
46
47// A leaf that already found its text in one part is true, whatever a later part could not finish.
48#[test]
49fn qx_found_then_unknown() {
50 let pat = "(?:a+b+c+d+e+f+g+h+){4}x";
51 let subj = "abcdefghabcdefghabcdefghabcdefghx";
52 let mut big = "lorem ipsum ".repeat(5 * 1024 * 1024 / 12);
53 big.push_str("end");
54 let raw = text_msg(subj, &big);
55 let q = Query { terms: Term::Text { scope: None, mode: Mode::Regex, text: pat.into() }, ..Query::default() };
56 let small = text_msg(subj, "short");
57 eprintln!("QX found-then-unknown: small body {} | 5 MB body {}", ans(&q, &small), ans(&q, &raw));
58 assert_eq!(ans(&q, &raw), "hit", "a match in Subject is answered unknown because Body could not finish");
59}
60
61// snippet_chars bounds a snippet.
62#[test]
63fn qx_snippet_bound() -> Outcome<()> {
64 let body = "word ".repeat(20_000);
65 let raw = text_msg("x", &body);
66 let q = res!(Query::parse(r"/[\s\S]+/", &LIM));
67 let h = res!(res!(res!(Matcher::new(&q, &LIM, TODAY)).matches(&mail(&raw))).ok_or_else(|| err!("no hit"; Missing)));
68 let longest = h.parts.iter().flat_map(|p| p.snippets.iter()).map(|s| s.text.chars().count()).max().unwrap_or(0);
69 eprintln!("QX snippet: snippet_chars {} longest snippet {} chars", LIM.snippet_chars, longest);
70 assert!(longest <= 4 * LIM.snippet_chars, "a snippet of {} chars against snippet_chars {}", longest, LIM.snippet_chars);
71 Ok(())
72}
73
74// Marks only from terms that hold where the hit is decided.
75#[test]
76fn qx_marks_from_failed_branch() -> Outcome<()> {
77 let raw = text_msg("x", "alpha one beta two");
78 let q = res!(Query::parse("(alpha zeta) OR beta", &LIM));
79 let h = res!(res!(res!(Matcher::new(&q, &LIM, TODAY)).matches(&mail(&raw))).ok_or_else(|| err!("no hit"; Missing)));
80 let marked: Vec<String> = h.parts.iter().flat_map(|p| p.snippets.iter())
81 .flat_map(|s| s.spans.iter().map(|x| s.text[x.start..x.end].to_string()).collect::<Vec<_>>()).collect();
82 eprintln!("QX marks: {:?}", marked);
83 assert!(!marked.iter().any(|m| m == "alpha"), "alpha is marked though its branch failed: {:?}", marked);
84 Ok(())
85}
86
87// 100,000 leaves and a 1 MB query: time and answer.
88#[test]
89fn qx_many_leaves() {
90 let raw = text_msg("x", "w99999 here");
91 let s: String = (0..100_000).map(|i| format!("w{} OR ", i)).collect::<String>() + "zz";
92 let t = Instant::now();
93 let q = Query::parse(&s, &LIM);
94 let tp = t.elapsed();
95 let t = Instant::now();
96 let a = match &q { Ok(q) => ans(q, &raw), Err(e) => format!("parse-err {}", e) };
97 eprintln!("QX 100k-OR: {} bytes, parse {:?}, compile+match {:?} -> {}", s.len(), tp, t.elapsed(), a);
98 let s: String = (0..100_000).map(|i| format!("w{} ", i)).collect();
99 let t = Instant::now();
100 let a = ans_s(&s, &raw);
101 eprintln!("QX 100k-AND: {} bytes, all {:?} -> {}", s.len(), t.elapsed(), a);
102 let s = "x".repeat(1 << 20);
103 let t = Instant::now();
104 eprintln!("QX 1MB-word: {:?} -> {}", t.elapsed(), ans_s(&s, &raw));
105 let s = "(a) ".repeat(1 << 18);
106 let t = Instant::now();
107 eprintln!("QX 1MB-groups: -> {} in {:?}", ans_s(&s, &raw), t.elapsed());
108}
109
110// Work per message: leaves times text, with no bound on the leaves; and no short cut once decided.
111#[test]
112fn qx_leaves_times_text() {
113 let body = "lorem ipsum ".repeat(1024 * 1024 / 12);
114 let raw = text_msg("x", &body);
115 for n in [1usize, 10, 100, 400] {
116 let s: String = (0..n).map(|i| format!("zq{} OR ", i)).collect::<String>() + "lorem";
117 let t = Instant::now();
118 let a = ans_s(&s, &raw);
119 eprintln!("QX {} leaves OR'd over a 1 MB body: {} in {:?}", n + 1, a, t.elapsed());
120 }
121 // Decided by the first child: `-lorem` under All is false at once; the rest still run.
122 let s: String = "-lorem ".to_string() + &(0..100).map(|i| format!("zq{} ", i)).collect::<String>();
123 let t = Instant::now();
124 eprintln!("QX All decided by its first child, 100 more: {} in {:?}", ans_s(&s, &raw), t.elapsed());
125}
126
127// Empty AND and OR: their meaning must survive the round trip through the typed text (the JDAT form).
128#[test]
129fn qx_empty_groups_round_trip() {
130 let m = text_msg("hello", "alpha");
131 for s in ["()", "{}", "-{}", "-()", "alpha {}", "alpha OR {}", "{} OR alpha"] {
132 let q = Query::parse(s, &LIM).ok();
133 let back = q.as_ref().map(|q| q.to_string());
134 let q2 = back.as_ref().and_then(|b| Query::parse(b, &LIM).ok());
135 let a1 = ans_s(s, &m);
136 let a2 = back.as_ref().map(|b| ans_s(b, &m)).unwrap_or_default();
137 eprintln!("QX empty {:>12?} -> {:?} | {} -> {} | same tree {}", s, back, a1, a2, q == q2);
138 }
139 let q = match Query::parse("{}", &LIM) { Ok(q) => q, Err(e) => panic!("{}", e) };
140 let back = q.to_string();
141 assert_eq!(ans_s("{}", &m), ans_s(&back, &m), "'{{}}' printed as {:?} changes its answer", back);
142}
143
144// NOT of NOT, unknown leaves, empty values, a scope over a filter.
145#[test]
146fn qx_odd_leaves() {
147 let m = text_msg("hello", "a cat here");
148 for s in ["--cat", "---cat", "- cat", "foo:bar", "is:nonsense", "from:\"\"", "\"\"", "-\"\"", "subject:",
149 "from:is:read", "from:(is:read)", "from:-is:read", "to:larger:1", "FROM:ada", "From:Ada", "after:+2026/+3/+4",
150 "after:0/1/1", "after:65535/12/31", "after:65536/1/1", "after:2026/03/04T00", "larger:0", "smaller:0",
151 "larger:18446744073709551615", "larger:18446744073709551616", "larger:16E", "OR", "a OR", "OR a", "( OR )",
152 "older_than:7d", "newer_than:2d", "label:work", "size:1000000", "deliveredto:me@x", "list:info@x",
153 "category:primary", "rfc822msgid:abc", "AROUND", "a AROUND 3 b", "+cat", "\" \""] {
154 let q = Query::parse(s, &LIM);
155 eprintln!("QX odd {:>28?} -> {} | printed {:?}", s, ans_s(s, &m),
156 q.as_ref().map(|q| q.to_string()).unwrap_or_else(|e| format!("ERR {}", e)));
157 }
158 // `from:` given a filter: the parser's own message says 'from:' takes text, not another operator.
159 assert!(Query::parse("from:is:read", &LIM).is_err(), "from:is:read accepted as {:?}",
160 Query::parse("from:is:read", &LIM).map(|q| q.to_string()));
161}
162
163// A regex term built in code (a sheet, a daimon) and sent as its JDAT form, the typed text.
164#[test]
165fn qx_regex_and_text_round_trip() {
166 let m = text_msg("x", "path a/b and a\\b");
167 for (mode, t) in [(Mode::Regex, r"a\/b"), (Mode::Regex, r"a\\"), (Mode::Word, "a\\"), (Mode::Phrase, "\" x"),
168 (Mode::Word, "x)"), (Mode::Word, "\u{0}"), (Mode::Word, "a\r\nb"), (Mode::Phrase, "a\r\nb"), (Mode::Word, "in:x"),
169 (Mode::Word, "-"), (Mode::Word, "/"), (Mode::Regex, "")] {
170 let q = Query { terms: Term::Text { scope: None, mode, text: t.into() }, ..Query::default() };
171 let s = q.to_string();
172 let back = Query::parse(&s, &LIM);
173 let same = back.as_ref().map(|b| *b == q).unwrap_or(false);
174 eprintln!("QX rt {:?} {:?} -> {:?} -> same {} | {} vs {}", mode, t, s, same, ans(&q, &m),
175 back.as_ref().map(|b| ans(b, &m)).unwrap_or_else(|e| format!("ERR {}", e)));
176 }
177 let q = Query { terms: Term::Text { scope: None, mode: Mode::Regex, text: r"a\/b".into() }, ..Query::default() };
178 assert_eq!(ans(&q, &m), Query::parse(&q.to_string(), &LIM).map(|b| ans(&b, &m)).unwrap_or_else(|e| format!("ERR {}", e)));
179}
180
181// NUL, raw 8-bit and junk bytes in a message and a query: answers, no panic.
182#[test]
183fn qx_nul_and_junk() {
184 let mut raw = b"From: A\0da <a@x>\r\nSubject: s\0ub \xff\xfe junk\r\nContent-Type: text/plain\r\n\r\nbo\0dy \xc3\x28 x\r\n".to_vec();
185 for s in ["ada", "\u{0}", "sub", "body", "bo\u{0}dy", "\u{fffd}"] {
186 eprintln!("QX nul {:?} -> {}", s, ans_s(s, &raw));
187 }
188 raw.truncate(7);
189 eprintln!("QX cut-short {}", ans_s("a", &raw));
190 eprintln!("QX empty-raw {}", ans_s("a", b""));
191 eprintln!("QX no-blank-line {}", ans_s("x", b"Subject: x"));
192}
193
194// Case and Unicode folding (Match case off), and whole word in non-Latin script.
195#[test]
196fn qx_case_folding() {
197 let cases = [("école", "ÉCOLE"), ("ÉCOLE", "école"), ("σοφια", "ΣΟΦΙΑ"), ("ΣΟΦΙΑΣ", "σοφιας"), ("straße", "STRASSE"),
198 ("STRASSE", "straße"), ("istanbul", "İSTANBUL"), ("Dž", "dž"), ("Ⅻ", "ⅻ"), ("file", "FILE"), ("e\u{301}cole", "école")];
199 for (needle, hay) in cases {
200 let q = Query { terms: Term::Text { scope: None, mode: Mode::Word, text: needle.into() }, ..Query::default() };
201 let ww = Query { whole_word: true, ..q.clone() };
202 eprintln!("QX fold {:?} in {:?}: {} | whole word {}", needle, hay, ans(&q, &text_msg("x", hay)),
203 ans(&ww, &text_msg("x", &format!("le {} ici", hay))));
204 }
205 let q = Query { terms: Term::Text { scope: None, mode: Mode::Word, text: "école".into() }, ..Query::default() };
206 assert_eq!(ans(&q, &text_msg("x", "ÉCOLE")), "hit");
207}
208
209// Dates at the bounds; the day is the caller's.
210#[test]
211fn qx_dates() {
212 for s in ["2024/02/29", "2023/02/29", "1900/02/29", "2000/02/29", "2026/13/01", "2026/00/10", "2026/1/1", "2026-01-01",
213 "2026/01/01/", "/2026/01/01", " 2026/01/01", "9999/12/31", "2026/04/31"] {
214 eprintln!("QX day {:?} -> {:?}", s, Day::parse(s).map(|d| d.to_string()).map_err(|e| e.to_string()));
215 }
216 let m = text_msg("x", "y");
217 for s in ["after:2026/03/04", "before:2026/03/04", "after:2026/03/05", "before:2026/03/05", "-after:2026/03/05",
218 "after:2026/03/04 before:2026/03/04"] {
219 eprintln!("QX date {:?} on 2026/03/04 -> {}", s, ans_s(s, &m));
220 }
221 assert_eq!(ans_s("after:2026/03/04", &m), "hit");
222 assert_eq!(ans_s("before:2026/03/04", &m), "miss");
223}