crymap 1.0.1

A simple, secure IMAP server with encrypted data at rest
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
//-
// Copyright (c) 2020, Jason Lingle
//
// This file is part of Crymap.
//
// Crymap is free software: you can  redistribute it and/or modify it under the
// terms of  the GNU General Public  License as published by  the Free Software
// Foundation, either version  3 of the License, or (at  your option) any later
// version.
//
// Crymap is distributed  in the hope that  it will be useful,  but WITHOUT ANY
// WARRANTY; without  even the implied  warranty of MERCHANTABILITY  or FITNESS
// FOR  A PARTICULAR  PURPOSE.  See the  GNU General  Public  License for  more
// details.
//
// You should have received a copy of the GNU General Public License along with
// Crymap. If not, see <http://www.gnu.org/licenses/>.

use std::sync::Arc;

use log::warn;
use regex::{self, Regex};

use super::defs::*;
use super::search_backend::{self, Op};
use crate::account::model::*;
use crate::mime::fetch::search::{OptionalSearchParts, SearchFetcher};
use crate::mime::grovel;
use crate::support::error::Error;
use crate::support::threading;

impl StatefulMailbox {
    /// The `SEARCH` command.
    pub fn seqnum_search(
        &mut self,
        request: &SearchRequest,
    ) -> Result<SearchResponse<Seqnum>, Error> {
        let result = self.search(request)?;

        Ok(SearchResponse {
            max_modseq: result.max_modseq,
            first_modseq: result.first_modseq,
            last_modseq: result.last_modseq,
            hit_uids: result.hit_uids,
            hits: result
                .hits
                .into_iter()
                .map(|uid| {
                    self.state
                        .uid_to_seqnum(uid)
                        // We only find things which are returned by
                        // `.uids()`, which itself is restricted to the
                        // addressable set, so the case of being unable to map
                        // back to a sequence number should never come up.
                        .expect("Search found unaddressable UID?")
                })
                .collect(),
        })
    }

    /// The `UID SEARCH` command.
    pub fn search(
        &mut self,
        request: &SearchRequest,
    ) -> Result<SearchResponse<Uid>, Error> {
        static SG: threading::ScatterGather = threading::ScatterGather {
            batch_size: 4,
            escalate: std::time::Duration::from_millis(10),
            buffer_size: 32,
        };

        let mut ops = Vec::new();
        self.compile_and(&mut ops, &request.queries);
        let want = search_backend::want(&ops);

        let ops = Arc::new(ops);
        let mut hits = Vec::new();
        SG.run(
            self.state.uids(),
            |uid| (uid, self.search_one(uid, &ops, want)),
            |(uid, hit)| {
                if hit {
                    hits.push(uid);
                }
            },
        );
        // RFC 3501 doesn't require the results to be in any particular order,
        // but this step is very cheap and there could be clients depending on
        // it. We also need the output sorted for ESEARCH.
        hits.sort_unstable();

        Ok(SearchResponse {
            first_modseq: hits
                .first()
                .and_then(|&uid| self.state.message_status(uid))
                .map(|ms| ms.last_modified()),
            last_modseq: hits
                .last()
                .and_then(|&uid| self.state.message_status(uid))
                .map(|ms| ms.last_modified()),
            max_modseq: hits
                .iter()
                .copied()
                .filter_map(|uid| self.state.message_status(uid))
                .map(|m| m.last_modified())
                .max(),
            hit_uids: hits.clone(),
            hits,
        })
    }

    fn search_one(
        &self,
        uid: Uid,
        ops: &Arc<Vec<Op>>,
        want: OptionalSearchParts,
    ) -> bool {
        let ops = Arc::clone(ops);

        let result = self.access_message(uid).and_then(|accessor| {
            grovel::grovel(
                &accessor,
                SearchFetcher::new(want, move |sd| {
                    search_backend::eval(&ops, sd)
                }),
            )
        });

        match result {
            Ok(r) => r,
            // If the message is gone meanwhile, just ignore it
            Err(Error::ExpungedMessage) | Err(Error::NxMessage) => false,
            Err(e) => {
                warn!(
                    "{} Error evaluating UID {} for search: {}",
                    self.s.log_prefix,
                    uid.0.get(),
                    e
                );
                false
            }
        }
    }

    fn compile_and(&self, dst: &mut Vec<Op>, queries: &[SearchQuery]) {
        if queries.is_empty() {
            dst.push(Op::True);
            return;
        }

        let mut first = true;
        for q in queries {
            self.compile_one(dst, q);
            if !first {
                dst.push(Op::And);
            }
            first = false;
        }
    }

    fn compile_one(&self, dst: &mut Vec<Op>, query: &SearchQuery) {
        match *query {
            SearchQuery::SequenceSet(ref seqnums) => {
                let uids =
                    self.state.seqnum_range_to_uid(seqnums, false).unwrap();
                dst.push(Op::UidIn(uids));
            }

            SearchQuery::All => dst.push(Op::True),

            SearchQuery::Answered => dst.push(Op::Flag(Flag::Answered)),

            SearchQuery::Bcc(ref pat) => {
                dst.push(Op::Bcc(to_regex(pat)));
            }

            SearchQuery::Before(date) => {
                dst.push(Op::InternalDateCompare(date, true, false, false));
            }

            SearchQuery::Body(ref pat) => {
                dst.push(Op::Content(Arc::new(to_regex(pat))));
            }

            SearchQuery::Cc(ref pat) => {
                dst.push(Op::Cc(to_regex(pat)));
            }

            SearchQuery::Deleted => dst.push(Op::Flag(Flag::Deleted)),

            SearchQuery::Draft => dst.push(Op::Flag(Flag::Draft)),

            SearchQuery::Flagged => dst.push(Op::Flag(Flag::Flagged)),

            SearchQuery::From(ref pat) => {
                dst.push(Op::From(to_regex(pat)));
            }

            SearchQuery::Header(ref name, ref value) => {
                let mut name = name.to_owned();
                name.make_ascii_lowercase();
                dst.push(Op::Header(name, to_regex(value)));
            }

            SearchQuery::Keyword(ref name) => {
                dst.push(Op::Flag(Flag::Keyword(name.to_owned())));
            }

            SearchQuery::Larger(thresh) => {
                dst.push(Op::SizeCompare(thresh, false, false, true));
            }

            SearchQuery::New => {
                dst.push(Op::Recent);
                dst.push(Op::Flag(Flag::Seen));
                dst.push(Op::Not);
                dst.push(Op::And);
            }

            SearchQuery::Not(ref sub) => {
                self.compile_one(dst, sub);
                dst.push(Op::Not);
            }

            SearchQuery::Old => {
                dst.push(Op::Recent);
                dst.push(Op::Not);
            }

            SearchQuery::On(date) => {
                dst.push(Op::InternalDateCompare(date, false, true, false));
            }

            SearchQuery::Or(ref a, ref b) => {
                self.compile_one(dst, a);
                self.compile_one(dst, b);
                dst.push(Op::Or);
            }

            SearchQuery::Recent => dst.push(Op::Recent),

            SearchQuery::Seen => dst.push(Op::Flag(Flag::Seen)),

            SearchQuery::SentBefore(date) => {
                dst.push(Op::DateCompare(date, true, false, false));
            }

            SearchQuery::SentOn(date) => {
                dst.push(Op::DateCompare(date, false, true, false));
            }

            SearchQuery::SentSince(date) => {
                // RFC 3501 specifies >=, not >
                dst.push(Op::DateCompare(date, false, true, true));
            }

            SearchQuery::Since(date) => {
                // RFC 3501 specifies >=, not >
                dst.push(Op::InternalDateCompare(date, false, true, true));
            }

            SearchQuery::Smaller(thresh) => {
                dst.push(Op::SizeCompare(thresh, true, false, false));
            }

            SearchQuery::Subject(ref pat) => {
                dst.push(Op::Subject(to_regex(pat)));
            }

            SearchQuery::Text(ref pat) => {
                let regex = Arc::new(to_regex(pat));
                dst.push(Op::AnyHeader(Arc::clone(&regex)));
                dst.push(Op::Content(regex));
                dst.push(Op::Or);
            }

            SearchQuery::To(ref pat) => {
                dst.push(Op::To(to_regex(pat)));
            }

            SearchQuery::UidSet(ref uids) => {
                dst.push(Op::UidIn(uids.to_owned()))
            }

            SearchQuery::Unanswered => {
                dst.push(Op::Flag(Flag::Answered));
                dst.push(Op::Not);
            }

            SearchQuery::Undeleted => {
                dst.push(Op::Flag(Flag::Deleted));
                dst.push(Op::Not);
            }

            SearchQuery::Undraft => {
                dst.push(Op::Flag(Flag::Draft));
                dst.push(Op::Not);
            }

            SearchQuery::Unflagged => {
                dst.push(Op::Flag(Flag::Flagged));
                dst.push(Op::Not);
            }

            SearchQuery::Unkeyword(ref name) => {
                dst.push(Op::Flag(Flag::Keyword(name.to_owned())));
                dst.push(Op::Not);
            }

            SearchQuery::Unseen => {
                dst.push(Op::Flag(Flag::Seen));
                dst.push(Op::Not);
            }

            SearchQuery::And(ref queries) => self.compile_and(dst, queries),

            SearchQuery::Modseq(ms) => {
                dst.push(Op::Modseq(ms));
            }

            SearchQuery::EmailId(ref email_id) => {
                dst.push(Op::EmailId(email_id.clone()));
            }

            SearchQuery::ThreadId(_) => {
                // We don't support THREADID, so this never matches
                dst.push(Op::True);
                dst.push(Op::Not);
            }
        }
    }
}

/// We use the regex library for substring matching both for its excellent
/// performance and to take advantage of its Unicode-aware case insensitivity.
///
/// The case-insensitivity this implements is not the `i;unicode-casemap`
/// comparison that Crispin designed in RFC 5051 (formally required by the
/// I18NLEVEL=1 extension, RFC 5255), but is preferable since it is actually a
/// comparison sanctioned by the Unicode Consortium and isn't 13+ years out of
/// date.
fn to_regex(pat: &str) -> Regex {
    let mut regex_str = String::new();
    for (ix, chunk) in
        pat.split_whitespace().filter(|s| !s.is_empty()).enumerate()
    {
        if 0 != ix {
            regex_str.push_str("[ \r\n\t]+");
        }
        regex_str.push_str(&regex::escape(chunk));
    }

    regex::RegexBuilder::new(&regex_str)
        .case_insensitive(true)
        .build()
        .expect("Created bad regex")
}

#[cfg(test)]
mod test {
    use chrono::prelude::*;

    use super::super::test_prelude::*;
    use super::*;
    use crate::test_data::*;

    #[test]
    fn creates_proper_regexen() {
        let regex = to_regex("foo \\bar");
        assert!(regex.is_match("foo \\bar"));
        assert!(regex.is_match("FOO \\BAR"));
        assert!(regex.is_match("foo  \\bar"));
        assert!(regex.is_match("foo\t\\bar"));
        assert!(regex.is_match("foo\r\n\\bar"));
        assert!(regex.is_match("xfoo  \\barx"));
        assert!(!regex.is_match("foo\\bar"));
    }

    // Since setup is a bit slow and at the same time uninteresting, all the
    // searching tests are in this one test function.
    //
    // Note that `imap::integration_tests::rfc3501::search` has a test parallel
    // to this one. If you update this test, also update that one.
    #[test]
    fn test_search_queries() {
        use crate::account::model::SearchQuery::*;

        let setup = set_up();
        let (mut mb1, _) = setup.stateless.clone().select().unwrap();
        let (mut mb2, _) = setup.stateless.clone().select().unwrap();

        let mut uids = [CHRISTMAS_TREE, TORTURE_TEST]
            .iter()
            .map(|data| simple_append_data(&setup.stateless, data))
            .collect::<Vec<_>>();
        // Claim \Recent for the first two messages in the second session
        mb2.poll().unwrap();

        uids.extend(
            ENRON_SMALL_MULTIPARTS
                .iter()
                .map(|data| simple_append_data(&setup.stateless, data)),
        );
        // Claim the ENRON messages as \Recent in the test session
        mb1.poll().unwrap();

        // Create a gap in the sequence number / UID mapping
        mb1.vanquish(&SeqRange::just(uids[5])).unwrap();

        // Set each flag on a distinct message
        mb1.store(&StoreRequest {
            ids: &SeqRange::just(uids[0]),
            flags: &[Flag::Answered],
            remove_listed: false,
            remove_unlisted: false,
            loud: false,
            unchanged_since: None,
        })
        .unwrap();
        mb1.store(&StoreRequest {
            ids: &SeqRange::just(uids[1]),
            flags: &[Flag::Deleted],
            remove_listed: false,
            remove_unlisted: false,
            loud: false,
            unchanged_since: None,
        })
        .unwrap();
        mb1.store(&StoreRequest {
            ids: &SeqRange::just(uids[2]),
            flags: &[Flag::Draft],
            remove_listed: false,
            remove_unlisted: false,
            loud: false,
            unchanged_since: None,
        })
        .unwrap();
        mb1.store(&StoreRequest {
            ids: &SeqRange::just(uids[3]),
            flags: &[Flag::Flagged],
            remove_listed: false,
            remove_unlisted: false,
            loud: false,
            unchanged_since: None,
        })
        .unwrap();
        mb1.store(&StoreRequest {
            ids: &SeqRange::just(uids[4]),
            flags: &[Flag::Seen],
            remove_listed: false,
            remove_unlisted: false,
            loud: false,
            unchanged_since: None,
        })
        .unwrap();
        mb1.store(&StoreRequest {
            ids: &SeqRange::just(uids[6]),
            flags: &[Flag::Keyword("$Important".to_owned())],
            remove_listed: false,
            remove_unlisted: false,
            loud: false,
            unchanged_since: None,
        })
        .unwrap();
        mb1.poll().unwrap();

        macro_rules! uids {
            ($($ix:expr),*) => {
                vec![$(uids[$ix],)*] as Vec<Uid>
            }
        }

        macro_rules! uids_compl {
            ($($ix:expr),*) => {
                uids.iter()
                    .copied()
                    .enumerate()
                    .filter(|&(ix, _)| $($ix != ix)&&*)
                    .map(|(_, uid)| uid)
                    .collect::<Vec<_>>()
            }
        }

        macro_rules! search {
            ($($query:expr),*) => {
                mb1.search(&SearchRequest {
                    queries: vec![$($query),*],
                }).unwrap().hits
            }
        }

        let today = Utc::today().naive_utc();

        assert_eq!(
            uids![4, 6, 7],
            search!(SequenceSet(SeqRange::range(Seqnum::u(5), Seqnum::u(7))))
        );

        assert_eq!(uids_compl![5], search!(All));

        assert_eq!(uids![0], search!(Answered));

        assert_eq!(uids![0], search!(Bcc("bcc@example.com".to_owned())));
        assert_eq!(uids![], search!(Bcc("foo@bar.com".to_owned())));

        assert_eq!(uids![], search!(Before(NaiveDate::from_ymd(2000, 1, 1))));
        assert_eq!(uids![], search!(Before(today)));
        assert_eq!(
            uids_compl![5],
            search!(Before(NaiveDate::from_ymd(3000, 1, 1)))
        );

        assert_eq!(uids![2, 3], search!(Body("prezo".to_owned())));

        assert_eq!(uids![0], search!(Cc("cc@example.com".to_owned())));
        assert_eq!(uids![], search!(Cc("bcc@example.com".to_owned())));

        assert_eq!(uids![1], search!(Deleted));

        assert_eq!(uids![2], search!(Draft));

        assert_eq!(uids![3], search!(Flagged));

        assert_eq!(uids![4, 6, 7], search!(From("tom.acton".to_owned())));

        assert_eq!(
            uids![0],
            search!(Header("Xyzzy".to_owned(), "nothing".to_owned()))
        );
        assert_eq!(
            uids![2, 3],
            search!(Header("x-origin".to_owned(), "dasovich-j".to_owned()))
        );

        assert_eq!(uids![6], search!(Keyword("$Important".to_owned())));
        assert_eq!(uids![6], search!(Keyword("$important".to_owned())));
        assert_eq!(uids![], search!(Keyword("plugh".to_owned())));

        // There's a message with size 4002, so we're also testing here that
        // equality is excluded
        assert_eq!(uids![1, 12, 13], search!(Larger(4002)));

        // 0 and 1 are not \Recent, 4 is \Seen, 5 was expunged
        assert_eq!(uids_compl![0, 1, 4, 5], search!(New));

        assert_eq!(
            uids_compl![2, 3, 5],
            search!(Not(Box::new(Body("prezo".to_owned()))))
        );

        assert_eq!(uids![0, 1], search!(Old));

        assert_eq!(uids_compl![5], search!(On(today)));
        assert_eq!(uids![], search!(On(NaiveDate::from_ymd(2000, 1, 1))));
        assert_eq!(uids![], search!(On(NaiveDate::from_ymd(3000, 1, 1))));

        assert_eq!(
            uids![2, 3, 6],
            search!(Or(
                Box::new(Keyword("$Important".to_owned())),
                Box::new(Body("prezo".to_owned()))
            ))
        );

        assert_eq!(uids_compl![0, 1, 5], search!(Recent));

        assert_eq!(uids![4], search!(Seen));

        assert_eq!(
            uids![0, 1],
            search!(SentBefore(NaiveDate::from_ymd(2001, 4, 2)))
        );
        assert_eq!(
            uids![4, 6, 7],
            search!(SentOn(NaiveDate::from_ymd(2001, 4, 2)))
        );
        assert_eq!(
            uids_compl![0, 1, 5],
            search!(SentSince(NaiveDate::from_ymd(2001, 4, 2)))
        );

        assert_eq!(uids_compl![5], search!(Since(today)));
        assert_eq!(
            uids_compl![5],
            search!(Since(NaiveDate::from_ymd(1970, 1, 1)))
        );
        assert_eq!(uids![], search!(Since(NaiveDate::from_ymd(3000, 1, 1))));

        // There's a message with size 1245, so this also tests exclusion of
        // equality
        assert_eq!(uids![0, 6], search!(Smaller(1245)));

        assert_eq!(uids![4, 6, 7], search!(Subject("entex".to_owned())));

        assert_eq!(uids![4, 6, 7], search!(Text("entex".to_owned())));
        assert_eq!(uids![2, 3], search!(Text("prezo".to_owned())));

        assert_eq!(uids![4, 6, 7], search!(To("daren".to_owned())));

        assert_eq!(
            uids![3, 4, 6, 7],
            search!(UidSet(SeqRange::range(uids[3], uids[7])))
        );

        assert_eq!(uids_compl![0, 5], search!(Unanswered));

        assert_eq!(uids_compl![1, 5], search!(Undeleted));

        assert_eq!(uids_compl![2, 5], search!(Undraft));

        assert_eq!(uids_compl![3, 5], search!(Unflagged));

        assert_eq!(
            uids_compl![5, 6],
            search!(Unkeyword("$Important".to_owned()))
        );
        assert_eq!(
            uids_compl![5, 6],
            search!(Unkeyword("$important".to_owned()))
        );
        assert_eq!(uids_compl![5], search!(Unkeyword("plugh".to_owned())));

        assert_eq!(uids_compl![4, 5], search!(Unseen));

        assert_eq!(
            uids![2, 3],
            search!(
                UidSet(SeqRange::range(uids[0], uids[3])),
                Text("enron".to_owned())
            )
        );
        assert_eq!(
            uids![2, 3],
            search!(And(vec![
                UidSet(SeqRange::range(uids[0], uids[3])),
                Text("enron".to_owned())
            ]))
        );
    }

    #[test]
    fn search_encoded_bodies() {
        let setup = set_up();
        let (mut mb1, _) = setup.stateless.clone().select().unwrap();
        let uid1 = simple_append_data(
            &mb1.stateless(),
            b"Content-Type: text/plain; charset=\"SHIFT-JIS\"\n\
              Content-Transfer-Encoding: base64\n\
              \n\
              iOqPj4LJiOqU1IuWgrOC6oLIgqKCsYLGgvCCtYLmgqQ=\n",
        );
        let uid2 = simple_append_data(
            &mb1.stateless(),
            b"Content-Type: text/plain; charset=UTF-7\n\
              Content-Transfer-Encoding: quoted-printable\n\
              \n\
              That is not dead which can eternal lie.\n\
              And with strange +AOY-=\n\
              ons even death may die.\n",
        );
        mb1.poll().unwrap();

        let result = mb1
            .search(&SearchRequest {
                queries: vec![SearchQuery::Text("許されない".to_owned())],
            })
            .unwrap();
        assert_eq!(vec![uid1], result.hits);

        let result = mb1
            .search(&SearchRequest {
                queries: vec![SearchQuery::Text("ÆONS".to_owned())],
            })
            .unwrap();
        assert_eq!(vec![uid2], result.hits);
    }

    #[test]
    fn test_seqnum_search() {
        let setup = set_up();
        let (mut mb1, _) = setup.stateless.clone().select().unwrap();

        for _ in 0..3 {
            simple_append(&mb1.stateless());
        }

        mb1.poll().unwrap();
        mb1.vanquish(&SeqRange::just(Uid::MIN)).unwrap();
        mb1.poll().unwrap();

        let result = mb1
            .seqnum_search(&SearchRequest {
                queries: vec![SearchQuery::All],
            })
            .unwrap();

        assert_eq!(vec![Seqnum::u(1), Seqnum::u(2)], result.hits);
    }

    #[test]
    fn expunged_message_ignored() {
        let setup = set_up();
        let (mut mb1, _) = setup.stateless.clone().select().unwrap();
        let (mut mb2, _) = setup.stateless.clone().select().unwrap();

        let uids = ENRON_SMALL_MULTIPARTS
            .iter()
            .map(|data| simple_append_data(&setup.stateless, data))
            .collect::<Vec<_>>();
        mb1.poll().unwrap();
        mb2.poll().unwrap();

        mb2.vanquish(&SeqRange::range(uids[1], uids[uids.len() - 1]))
            .unwrap();
        // Poll cycle and purge is needed to actually expunge the files
        mb2.poll().unwrap();
        mb2.purge_all();

        let result = mb1
            .search(&SearchRequest {
                // Use Text search to force loading of the message (and subsequent
                // failure since most of them are gone).
                queries: vec![SearchQuery::Text("@".to_owned())],
            })
            .unwrap();

        assert_eq!(vec![uids[0]], result.hits);
    }

    #[test]
    fn correct_modseqs_returned() {
        let setup = set_up();
        let (mut mb, _) = setup.stateless.select().unwrap();

        let uids = ENRON_SMALL_MULTIPARTS[..5]
            .iter()
            .map(|data| simple_append_data(mb.stateless(), data))
            .collect::<Vec<_>>();
        mb.poll().unwrap();

        // Give UID 1 the highest modseq
        mb.store(&StoreRequest {
            ids: &SeqRange::just(uids[1]),
            flags: &[Flag::Answered],
            remove_listed: false,
            remove_unlisted: false,
            loud: false,
            unchanged_since: None,
        })
        .unwrap();

        let result = mb
            .search(&SearchRequest {
                queries: vec![SearchQuery::All],
            })
            .unwrap();
        assert_eq!(
            Some(Modseq::new(uids[0], Cid::GENESIS)),
            result.first_modseq
        );
        assert_eq!(
            Some(Modseq::new(uids[4], Cid::GENESIS)),
            result.last_modseq
        );
        assert_eq!(Some(Modseq::new(uids[4], Cid(1))), result.max_modseq);

        let result = mb
            .search(&SearchRequest {
                queries: vec![SearchQuery::Text("gaap".to_owned())],
            })
            .unwrap();
        assert_eq!(None, result.first_modseq);
        assert_eq!(None, result.last_modseq);
        assert_eq!(None, result.max_modseq);
    }
}