10 "github.com/mjl-/mox/imapclient"
13var searchMsg = strings.ReplaceAll(`Date: Mon, 1 Jan 2022 10:00:00 +0100 (CEST)
14From: mjl <mjl@mox.example>
16To: mox <mox@mox.example>
19Reply-To: <noreply@mox.example>
20Message-Id: <123@mox.example>
22Content-Type: multipart/alternative; boundary=x
25Content-Type: text/plain; charset=utf-8
30Content-Type: text/html; charset=utf-8
37func (tc *testconn) xsearch(nums ...uint32) {
40 tc.xuntagged(imapclient.UntaggedSearch(nums))
43func (tc *testconn) xsearchmodseq(modseq int64, nums ...uint32) {
50 tc.xuntagged(imapclient.UntaggedSearchModSeq{Nums: nums, ModSeq: modseq})
53func (tc *testconn) xesearch(exp imapclient.UntaggedEsearch) {
56 exp.Correlator = tc.client.LastTag
60func TestSearch(t *testing.T) {
63 tc.client.Login("mjl@mox.example", password0)
64 tc.client.Select("inbox")
66 // Add 5 and delete first 4 messages. So UIDs start at 5.
67 received := time.Date(2020, time.January, 1, 10, 0, 0, 0, time.UTC)
68 for i := 0; i < 5; i++ {
69 tc.client.Append("inbox", nil, &received, []byte(exampleMsg))
71 tc.client.StoreFlagsSet("1:4", true, `\Deleted`)
74 received = time.Date(2022, time.January, 1, 9, 0, 0, 0, time.UTC)
75 tc.client.Append("inbox", nil, &received, []byte(searchMsg))
77 received = time.Date(2022, time.January, 1, 9, 0, 0, 0, time.UTC)
78 mostFlags := []string{
92 tc.client.Append("inbox", mostFlags, &received, []byte(searchMsg))
94 // We now have sequence numbers 1,2,3 and UIDs 5,6,7.
96 tc.transactf("ok", "search all")
99 tc.transactf("ok", "uid search all")
102 tc.transactf("ok", "search answered")
105 tc.transactf("ok", `search bcc "bcc@mox.example"`)
108 tc.transactf("ok", "search before 1-Jan-2038")
110 tc.transactf("ok", "search before 1-Jan-2020")
111 tc.xsearch() // Before is about received, not date header of message.
113 tc.transactf("ok", `search body "Joe"`)
115 tc.transactf("ok", `search body "Joe" body "bogus"`)
117 tc.transactf("ok", `search body "Joe" text "Blurdybloop"`)
119 tc.transactf("ok", `search body "Joe" not text "mox"`)
121 tc.transactf("ok", `search body "Joe" not not body "Joe"`)
123 tc.transactf("ok", `search body "this is plain text"`)
125 tc.transactf("ok", `search body "this is html"`)
128 tc.transactf("ok", `search cc "xcc@mox.example"`)
131 tc.transactf("ok", `search deleted`)
134 tc.transactf("ok", `search flagged`)
137 tc.transactf("ok", `search from "foobar@Blurdybloop.example"`)
140 tc.transactf("ok", `search keyword $Forwarded`)
143 tc.transactf("ok", `search keyword Custom1`)
146 tc.transactf("ok", `search keyword custom2`)
149 tc.transactf("ok", `search new`)
150 tc.xsearch() // New requires a message to be recent. We pretend all messages are not recent.
152 tc.transactf("ok", `search old`)
155 tc.transactf("ok", `search on 1-Jan-2022`)
158 tc.transactf("ok", `search recent`)
161 tc.transactf("ok", `search seen`)
164 tc.transactf("ok", `search since 1-Jan-2020`)
167 tc.transactf("ok", `search subject "afternoon"`)
170 tc.transactf("ok", `search text "Joe"`)
173 tc.transactf("ok", `search to "mooch@owatagu.siam.edu.example"`)
176 tc.transactf("ok", `search unanswered`)
179 tc.transactf("ok", `search undeleted`)
182 tc.transactf("ok", `search unflagged`)
185 tc.transactf("ok", `search unkeyword $Junk`)
188 tc.transactf("ok", `search unkeyword custom1`)
191 tc.transactf("ok", `search unseen`)
194 tc.transactf("ok", `search draft`)
197 tc.transactf("ok", `search header "subject" "afternoon"`)
200 tc.transactf("ok", `search larger 1`)
203 tc.transactf("ok", `search not text "mox"`)
206 tc.transactf("ok", `search or seen unseen`)
209 tc.transactf("ok", `search or unseen seen`)
212 tc.transactf("ok", `search sentbefore 8-Feb-1994`)
215 tc.transactf("ok", `search senton 7-Feb-1994`)
218 tc.transactf("ok", `search sentsince 6-Feb-1994`)
221 tc.transactf("ok", `search smaller 9999999`)
224 tc.transactf("ok", `search uid 1`)
227 tc.transactf("ok", `search uid 5`)
230 tc.transactf("ok", `search or larger 1000000 smaller 1`)
233 tc.transactf("ok", `search undraft`)
236 tc.transactf("no", `search charset unknown text "mox"`)
237 tc.transactf("ok", `search charset us-ascii text "mox"`)
239 tc.transactf("ok", `search charset utf-8 text "mox"`)
242 esearchall := func(ss string) imapclient.UntaggedEsearch {
243 return imapclient.UntaggedEsearch{All: esearchall0(ss)}
246 uint32ptr := func(v uint32) *uint32 {
250 // Do new-style ESEARCH requests with RETURN. We should get an ESEARCH response.
251 tc.transactf("ok", "search return () all")
252 tc.xesearch(esearchall("1:3")) // Without any options, "ALL" is implicit.
254 tc.transactf("ok", "search return (min max count all) all")
255 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, Max: 3, Count: uint32ptr(3), All: esearchall0("1:3")})
257 tc.transactf("ok", "UID search return (min max count all) all")
258 tc.xesearch(imapclient.UntaggedEsearch{UID: true, Min: 5, Max: 7, Count: uint32ptr(3), All: esearchall0("5:7")})
260 tc.transactf("ok", "search return (min) all")
261 tc.xesearch(imapclient.UntaggedEsearch{Min: 1})
263 tc.transactf("ok", "search return (min) 3")
264 tc.xesearch(imapclient.UntaggedEsearch{Min: 3})
266 tc.transactf("ok", "search return (min) NOT all")
267 tc.xesearch(imapclient.UntaggedEsearch{}) // Min not present if no match.
269 tc.transactf("ok", "search return (max) all")
270 tc.xesearch(imapclient.UntaggedEsearch{Max: 3})
272 tc.transactf("ok", "search return (max) 1")
273 tc.xesearch(imapclient.UntaggedEsearch{Max: 1})
275 tc.transactf("ok", "search return (max) not all")
276 tc.xesearch(imapclient.UntaggedEsearch{}) // Max not present if no match.
278 tc.transactf("ok", "search return (min max) all")
279 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, Max: 3})
281 tc.transactf("ok", "search return (min max) 1")
282 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, Max: 1})
284 tc.transactf("ok", "search return (min max) not all")
285 tc.xesearch(imapclient.UntaggedEsearch{})
287 tc.transactf("ok", "search return (all) not all")
288 tc.xesearch(imapclient.UntaggedEsearch{}) // All not present if no match.
290 tc.transactf("ok", "search return (min max all) not all")
291 tc.xesearch(imapclient.UntaggedEsearch{})
293 tc.transactf("ok", "search return (min max all count) not all")
294 tc.xesearch(imapclient.UntaggedEsearch{Count: uint32ptr(0)})
296 tc.transactf("ok", "search return (min max count all) 1,3")
297 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, Max: 3, Count: uint32ptr(2), All: esearchall0("1,3")})
299 tc.transactf("ok", "search return (min max count all) UID 5,7")
300 tc.xesearch(imapclient.UntaggedEsearch{Min: 1, Max: 3, Count: uint32ptr(2), All: esearchall0("1,3")})
302 tc.transactf("ok", "uid search return (min max count all) 1,3")
303 tc.xesearch(imapclient.UntaggedEsearch{UID: true, Min: 5, Max: 7, Count: uint32ptr(2), All: esearchall0("5,7")})
305 tc.transactf("ok", "uid search return (min max count all) UID 5,7")
306 tc.xesearch(imapclient.UntaggedEsearch{UID: true, Min: 5, Max: 7, Count: uint32ptr(2), All: esearchall0("5,7")})
308 tc.transactf("no", `search return () charset unknown text "mox"`)
309 tc.transactf("ok", `search return () charset us-ascii text "mox"`)
310 tc.xesearch(esearchall("2:3"))
311 tc.transactf("ok", `search return () charset utf-8 text "mox"`)
312 tc.xesearch(esearchall("2:3"))
314 tc.transactf("bad", `search return (unknown) all`)
316 tc.transactf("ok", "search return (save) 2")
318 tc.transactf("ok", "fetch $ (uid)")
319 tc.xuntagged(imapclient.UntaggedFetch{Seq: 2, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(6)}})
321 tc.transactf("ok", "search return (all) $")
322 tc.xesearch(esearchall("2"))
324 tc.transactf("ok", "search return (save) $")
327 tc.transactf("ok", "search return (save all) all")
328 tc.xesearch(esearchall("1:3"))
330 tc.transactf("ok", "search return (all save) all")
331 tc.xesearch(esearchall("1:3"))
333 tc.transactf("ok", "search return (min save) all")
334 tc.xesearch(imapclient.UntaggedEsearch{Min: 1})
335 tc.transactf("ok", "fetch $ (uid)")
336 tc.xuntagged(imapclient.UntaggedFetch{Seq: 1, Attrs: []imapclient.FetchAttr{imapclient.FetchUID(5)}})
338 // Do a seemingly old-style search command with IMAP4rev2 enabled. We'll still get ESEARCH responses.
339 tc.client.Enable("IMAP4rev2")
340 tc.transactf("ok", `search undraft`)
341 tc.xesearch(esearchall("1:2"))
343 // Long commands should be rejected, not allocating too much memory.
344 lit := make([]byte, 100*1024+1)
348 writeTextLit := func(n int, expok bool) {
349 _, err := fmt.Fprintf(tc.client, " TEXT ")
350 tcheck(t, err, "write text")
352 _, err = fmt.Fprintf(tc.client, "{%d}\r\n", n)
353 tcheck(t, err, "write literal size")
354 line, err := tc.client.Readline()
355 tcheck(t, err, "read line")
356 if expok && !strings.HasPrefix(line, "+") {
357 tcheck(t, fmt.Errorf("no continuation after writing size: %s", line), "sending literal")
358 } else if !expok && !strings.HasPrefix(line, "x0 BAD [TOOBIG]") {
359 tcheck(t, fmt.Errorf("got line %s", line), "expected TOOBIG error")
364 _, err = tc.client.Write(lit[:n])
365 tcheck(t, err, "write literal data")
368 // More than 100k for a literal.
369 _, err := fmt.Fprintf(tc.client, "x0 uid search")
370 tcheck(t, err, "write start of uit search")
371 writeTextLit(100*1024+1, false)
373 // More than 1mb total for literals.
374 _, err = fmt.Fprintf(tc.client, "x0 uid search")
375 tcheck(t, err, "write start of uit search")
376 for i := 0; i < 10; i++ {
377 writeTextLit(100*1024, true)
379 writeTextLit(1, false)
381 // More than 1000 literals.
382 _, err = fmt.Fprintf(tc.client, "x0 uid search")
383 tcheck(t, err, "write start of uit search")
384 for i := 0; i < 1000; i++ {
385 writeTextLit(1, true)
387 writeTextLit(1, false)
391// esearchall makes an UntaggedEsearch response with All set, for comparisons.
392func esearchall0(ss string) imapclient.NumSet {
393 seqset := imapclient.NumSet{}
394 for _, rs := range strings.Split(ss, ",") {
395 t := strings.Split(rs, ":")
402 v, err := strconv.ParseUint(t[0], 10, 32)
410 v, err := strconv.ParseUint(t[1], 10, 32)
418 seqset.Ranges = append(seqset.Ranges, imapclient.NumRange{First: first, Last: last})