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
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
//-
// 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::borrow::Cow;

use chrono::prelude::*;

use super::super::defs::*;
use crate::account::model::Flag;
use crate::support::error::Error;
use crate::test_data::*;

#[test]
fn fetch_single_scalars() {
    let setup = set_up();
    let mut client = setup.connect("3501fess");
    quick_log_in(&mut client);
    examine_shared(&mut client);

    fetch_single!(client, c("FETCH * UID"), ref fr => {
        assert_eq!(21, fr.seqnum);
        has_msgatt_matching! {
            s::MsgAtt::Uid(22) in fr
        };
    });

    fetch_single!(client, c("FETCH 1 FLAGS"), ref fr => {
        assert_eq!(1, fr.seqnum);
        has_msgatt_matching! {
            s::MsgAtt::Flags(s::FlagsFetch::NotRecent(ref flags)) in fr => {
                assert_eq!(&[Flag::Answered], &flags[..]);
            }
        };
    });

    fetch_single!(client, c("UID FETCH 22 INTERNALDATE"), ref fr => {
        assert_eq!(21, fr.seqnum);
        has_msgatt_matching! {
            s::MsgAtt::InternalDate(id) in fr => {
                assert_eq!(22, id.day());
            }
        };

        // UID FETCH implicitly includes UID
        has_msgatt_matching! {
            s::MsgAtt::Uid(22) in fr
        };
    });

    fetch_single!(client, c("FETCH 2 RFC822.SIZE"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::Rfc822Size(1845473) in fr
        };
    });
}

#[test]
fn fetch_envelope() {
    let setup = set_up();
    let mut client = setup.connect("3501feev");
    quick_log_in(&mut client);
    examine_shared(&mut client);

    let expected_envelope = s::Envelope {
        date: Some(Cow::Borrowed("Fri, 21 Nov 1997 09:55:06 -0600")),
        subject: Some(Cow::Borrowed("This is the subject")),
        from: vec![s::Address::Real(s::RealAddress {
            display_name: None,
            routing: None,
            local_part: Cow::Borrowed("from"),
            domain: Cow::Borrowed("example.com"),
        })],
        sender: vec![s::Address::Real(s::RealAddress {
            display_name: None,
            routing: None,
            local_part: Cow::Borrowed("sender"),
            domain: Cow::Borrowed("example.com"),
        })],
        reply_to: vec![s::Address::Real(s::RealAddress {
            display_name: None,
            routing: None,
            local_part: Cow::Borrowed("reply-to"),
            domain: Cow::Borrowed("example.com"),
        })],
        to: vec![
            s::Address::GroupDelim(Some(Cow::Borrowed(
                "=?utf-8?b?QSBNw6RpbGluZyBMaXN0?=",
            ))),
            s::Address::Real(s::RealAddress {
                display_name: Some(Cow::Borrowed(
                    "=?utf-8?b?VMO2bSBPcndlbGw?=",
                )),
                routing: None,
                local_part: Cow::Borrowed("to"),
                domain: Cow::Borrowed("example.com"),
            }),
            s::Address::GroupDelim(None),
        ],
        cc: vec![s::Address::Real(s::RealAddress {
            display_name: None,
            routing: None,
            local_part: Cow::Borrowed("cc"),
            domain: Cow::Borrowed("example.com"),
        })],
        bcc: vec![s::Address::Real(s::RealAddress {
            display_name: None,
            routing: None,
            local_part: Cow::Borrowed("bcc"),
            domain: Cow::Borrowed("example.com"),
        })],
        in_reply_to: Some(Cow::Borrowed("<inreplyto@example.com>")),
        message_id: Some(Cow::Borrowed("<messageid@example.com>")),
    };

    fetch_single!(client, c("FETCH 1 ENVELOPE"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::Envelope(ref envelope) in fr => {
                assert_eq!(&expected_envelope, envelope);
            }
        };
    });

    let expected_envelope = s::Envelope {
        date: Some(Cow::Borrowed("Wed, 25 Apr 2001 14:59:00 -0700")),
        subject: Some(Cow::Borrowed("Fwd: failure delivery")),
        from: vec![s::Address::Real(s::RealAddress {
            display_name: None,
            routing: None,
            local_part: Cow::Borrowed("asama"),
            domain: Cow::Borrowed("yahoo.com"),
        })],
        // These two are implicitly copied from `from`
        sender: vec![s::Address::Real(s::RealAddress {
            display_name: None,
            routing: None,
            local_part: Cow::Borrowed("asama"),
            domain: Cow::Borrowed("yahoo.com"),
        })],
        reply_to: vec![s::Address::Real(s::RealAddress {
            display_name: None,
            routing: None,
            local_part: Cow::Borrowed("asama"),
            domain: Cow::Borrowed("yahoo.com"),
        })],
        to: vec![
            s::Address::Real(s::RealAddress {
                display_name: None,
                routing: None,
                local_part: Cow::Borrowed("dasovic"),
                domain: Cow::Borrowed("enron.com"),
            }),
            s::Address::Real(s::RealAddress {
                display_name: None,
                routing: None,
                local_part: Cow::Borrowed("dasovich"),
                domain: Cow::Borrowed("haas.berkeley.edu"),
            }),
        ],
        cc: vec![],
        bcc: vec![],
        in_reply_to: None,
        message_id: Some(Cow::Borrowed(
            "<2078948.1075843446892.JavaMail.evans@thyme>",
        )),
    };

    fetch_single!(client, c("FETCH 3 ENVELOPE"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::Envelope(ref envelope) in fr => {
                assert_eq!(&expected_envelope, envelope);
            }
        };
    });
}

#[test]
fn fetch_envelope_with_obsolete_routes() {
    let setup = set_up();
    let mut client = setup.connect("3501feor");
    quick_log_in(&mut client);
    quick_create(&mut client, "3501feor");
    quick_select(&mut client, "3501feor");

    client
        .start_append(
            "3501feor",
            s::AppendFragment::default(),
            WITH_OBSOLETE_ROUTING,
        )
        .unwrap();

    let mut buffer = Vec::new();
    let mut responses = client.finish_append(&mut buffer).unwrap();
    assert_tagged_ok_any(responses.pop().unwrap());

    fetch_single!(client, c("FETCH 1 ENVELOPE"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::Envelope(ref envelope) in fr => {
                assert_eq!(vec![
                    s::Address::Real(s::RealAddress {
                        display_name: None,
                        routing: Some(Cow::Borrowed("@route1.tld")),
                        local_part: Cow::Borrowed("foo"),
                        domain: Cow::Borrowed("bar.com"),
                    }),
                ], envelope.from);
                assert_eq!(vec![
                    s::Address::Real(s::RealAddress {
                        display_name: None,
                        routing: Some(Cow::Borrowed("@route2.tld,@route3")),
                        local_part: Cow::Borrowed("baz"),
                        domain: Cow::Borrowed("quux.com"),
                    }),
                ], envelope.to);
            }
        };
    });
}

#[test]
fn fetch_body_structure() {
    let setup = set_up();
    let mut client = setup.connect("3501febs");
    quick_log_in(&mut client);
    examine_shared(&mut client);

    fetch_single!(client, c("FETCH 1 BODY"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::ShortBodyStructure(ref bs) in fr => {
                check_christmass_tree_body_structure(bs, false);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODYSTRUCTURE"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::ExtendedBodyStructure(ref bs) in fr => {
                check_christmass_tree_body_structure(bs, true);
            }
        };
    });

    fetch_single!(client, c("FETCH 2 BODY"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::ShortBodyStructure(ref bs) in fr => {
                check_torture_test_body_structure(bs, false);
            }
        };
    });

    fetch_single!(client, c("FETCH 2 BODYSTRUCTURE"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::ExtendedBodyStructure(ref bs) in fr => {
                check_torture_test_body_structure(bs, true);
            }
        };
    });
}

fn check_christmass_tree_body_structure(bs: &s::Body<'_>, extended: bool) {
    let bs = match *bs {
        s::Body::SinglePart(ref bs) => bs,
        ref bs => panic!("Unexpected top-level body structure: {:#?}", bs),
    };

    let core = match bs.core {
        s::ClassifiedBodyType1Part::Text(ref text) => text,
        ref core => panic!("Unexpected core structure: {:#?}", core),
    };
    assert_eq!(
        &s::BodyTypeText {
            media_subtype: Cow::Borrowed("rich"),
            body_fields: s::BodyFields {
                content_type_parms: vec![
                    Cow::Borrowed("charset"),
                    Cow::Borrowed("utf-8"),
                    Cow::Borrowed("richness"),
                    Cow::Borrowed("low"),
                ],
                content_id: Some(Cow::Borrowed("<contentid@example.com>")),
                content_description: Some(Cow::Borrowed("A test message")),
                content_transfer_encoding: Cow::Borrowed("8bit"),
                size_octets: 85,
            },
            size_lines: 2,
        },
        core
    );

    if extended {
        assert_eq!(
            Some(s::BodyExt1Part {
                md5: Some(Cow::Borrowed("e874f86d668f69c35b59c901c8442719")),
                content_disposition: Some(s::ContentDisposition {
                    disposition: Cow::Borrowed("attachment"),
                    parms: vec![
                        Cow::Borrowed("name"),
                        Cow::Borrowed("foo.txt"),
                    ],
                }),
                content_language: Some(Cow::Borrowed("en")),
                content_location: Some(Cow::Borrowed("/foo/bar")),
            }),
            bs.ext
        );
    } else {
        assert!(bs.ext.is_none());
    }
}

fn check_torture_test_body_structure(bs: &s::Body<'_>, extended: bool) {
    // We don't check the whole body structure here. The more thorough test is
    // done by the tests in `bodystructure.rs`. The purpose of this test is to
    // ensure that the correct fields are assigned the correct values in the
    // varying body structure types.
    //
    // In other words, we look at one non-text single part, one message part,
    // and one multipart.
    let bs = match *bs {
        s::Body::Multipart(ref bs) => bs,
        ref bs => panic!("Unexpected top-level body structure: {:#?}", bs),
    };

    assert_eq!("MIXED", bs.media_subtype);

    if extended {
        assert_eq!(
            Some(s::BodyExtMPart {
                content_type_parms: vec![
                    Cow::Borrowed("boundary"),
                    Cow::Borrowed("owatagusiam"),
                ],
                content_disposition: Some(s::ContentDisposition {
                    disposition: Cow::Borrowed("inline"),
                    parms: vec![
                        Cow::Borrowed("name"),
                        Cow::Borrowed("torture"),
                    ],
                }),
                content_language: Some(Cow::Borrowed("en")),
                content_location: Some(Cow::Borrowed("/plugh")),
            }),
            bs.ext
        );
    } else {
        assert!(bs.ext.is_none());
    }

    // Part 2 is a message/rfc822 containing a multipart
    let p2 = match bs.bodies[1] {
        s::Body::SinglePart(ref p2) => p2,
        ref p2 => panic!("Unexpected structure for part 2: {:#?}", p2),
    };
    let p2_core = match p2.core {
        s::ClassifiedBodyType1Part::Message(ref p2c) => p2c,
        ref p2c => panic!("Unexpected core structure for part 2: {:#?}", p2c),
    };
    assert_eq!(
        s::BodyFields {
            content_type_parms: vec![],
            content_id: None,
            content_description: Some(Cow::Borrowed("Rich Text demo")),
            content_transfer_encoding: Cow::Borrowed("7bit"),
            size_octets: 4906,
        },
        p2_core.body_fields
    );
    assert_eq!(
        vec![s::Address::Real(s::RealAddress {
            display_name: Some(Cow::Borrowed("Nathaniel Borenstein")),
            routing: None,
            local_part: Cow::Borrowed("nsb"),
            domain: Cow::Borrowed("thumper.bellcore.com"),
        })],
        p2_core.envelope.from
    );
    assert_eq!(106, p2_core.size_lines);

    if extended {
        assert_eq!(
            Some(s::BodyExt1Part {
                md5: Some(Cow::Borrowed("01bf6a12f89ac63b1962d39b09385771")),
                content_disposition: None,
                content_language: None,
                content_location: None,
            }),
            p2.ext
        );
    } else {
        assert!(p2.ext.is_none());
    }

    let p2m = match *p2_core.body {
        s::Body::Multipart(ref p2m) => p2m,
        ref p2m => panic!("Unexpected structure for Part 2 inner: {:#?}", p2m),
    };

    // Part 2.3 is an application/andrew-inset
    let p23 = match p2m.bodies[2] {
        s::Body::SinglePart(ref p23) => p23,
        ref p23 => panic!("Unexpected structure for Part 2.3: {:#?}", p23),
    };
    let p23_core = match p23.core {
        s::ClassifiedBodyType1Part::Basic(ref p23c) => p23c,
        ref p23c => {
            panic!("Unexpected core structure for Part 2.3: {:#?}", p23c)
        }
    };
    assert_eq!("application", p23_core.media_type);
    assert_eq!("andrew-inset", p23_core.media_subtype);
    assert_eq!(
        s::BodyFields {
            content_type_parms: vec![],
            content_id: None,
            content_description: None,
            content_transfer_encoding: Cow::Borrowed("7bit"),
            size_octets: 917,
        },
        p23_core.body_fields
    );

    if extended {
        assert_eq!(
            Some(s::BodyExt1Part {
                md5: Some(Cow::Borrowed("9f7f13a2dfad1454eee9454146510303")),
                content_disposition: None,
                content_language: None,
                content_location: None,
            }),
            p23.ext
        );
    } else {
        assert!(p23.ext.is_none());
    }

    // Part 3 is a message/rfc822 containing a single part
    let p3 = match bs.bodies[2] {
        s::Body::SinglePart(ref p3) => p3,
        ref p3 => panic!("Unexpected structure for part 3: {:#?}", p3),
    };
    let p3_core = match p3.core {
        s::ClassifiedBodyType1Part::Message(ref p3c) => p3c,
        ref p3c => panic!("Unexpected core structure for part 3: {:#?}", p3c),
    };
    assert_eq!(
        s::BodyFields {
            content_type_parms: vec![],
            content_id: None,
            content_description: Some(Cow::Borrowed("Voice Mail demo")),
            content_transfer_encoding: Cow::Borrowed("7bit"),
            size_octets: 562270,
        },
        p3_core.body_fields
    );
    assert_eq!(
        vec![s::Address::Real(s::RealAddress {
            display_name: Some(Cow::Borrowed("Nathaniel Borenstein")),
            routing: None,
            local_part: Cow::Borrowed("nsb"),
            domain: Cow::Borrowed("thumper.bellcore.com"),
        })],
        p3_core.envelope.from
    );
    assert_eq!(7605, p3_core.size_lines);

    if extended {
        assert_eq!(
            Some(s::BodyExt1Part {
                md5: Some(Cow::Borrowed("58779ffa1437b598d5dc432e8095ae8f")),
                content_disposition: None,
                content_language: None,
                content_location: None,
            }),
            p3.ext
        );
    } else {
        assert!(p3.ext.is_none());
    }
}

#[test]
fn fetch_body_structure_unknown_cte() {
    let setup = set_up();
    let mut client = setup.connect("3501feuc");
    quick_log_in(&mut client);
    quick_create(&mut client, "3501feuc");

    client
        .start_append("3501feuc", s::AppendFragment::default(), UNKNOWN_CTE)
        .unwrap();
    let mut buffer = Vec::new();
    let mut responses = client.finish_append(&mut buffer).unwrap();
    assert_tagged_ok_any(responses.pop().unwrap());

    quick_select(&mut client, "3501feuc");
    fetch_single!(client, c("FETCH 1 BODYSTRUCTURE"), fr => {
        has_msgatt_matching! {
            s::MsgAtt::ExtendedBodyStructure(
                s::Body::SinglePart(s::BodyType1Part {
                    core: s::ClassifiedBodyType1Part::Text(ref txt),
                    ..
                })
            ) in fr => {
                assert_eq!(
                    "chunked",
                    txt.body_fields.content_transfer_encoding
                );
            }
        }
    });
}

#[test]
fn fetch_body_parts() {
    let setup = set_up();
    let mut client = setup.connect("3501febp");
    quick_log_in(&mut client);
    examine_shared(&mut client);

    fetch_single!(client, c("FETCH 1 RFC822"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Rfc822Full(lit) in fr => {
                assert_literal_like(
                    b"Remark:", b"even death may die.\r\n", 937, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 RFC822.HEADER"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Rfc822Header(lit) in fr => {
                assert_literal_like(
                    b"Remark:", b"Encoding: 8bit\r\n\r\n", 0, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 RFC822.TEXT"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Rfc822Text(lit) in fr => {
                assert_literal_like(
                    b"That is not dead which can eternal lie.\r\n",
                    "And with strange æons even death may die.\r\n".as_bytes(),
                    0, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODY[]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: None,
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_literal_like(
                    b"Remark:", b"even death may die.\r\n", 937, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODY[HEADER]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::TopLevel(
                    s::SectionText::Header(()))),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_literal_like(
                    b"Remark:", b"Encoding: 8bit\r\n\r\n", 0, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODY[HEADER.FIELDS (From To)]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::TopLevel(
                    s::SectionText::HeaderFields(s::SectionTextHeaderField {
                        negative: false,
                        headers
                    }))),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_eq!(vec![Cow::Borrowed("From"), Cow::Borrowed("To")],
                           headers);
                assert_literal_like(
                    b"From:", b"<to@example.com>;\r\n\r\n", 0, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODY[HEADER.FIELDS.NOT \
                             (Remark Content-Transfer-Encoding)]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::TopLevel(
                    s::SectionText::HeaderFields(s::SectionTextHeaderField {
                        negative: true,
                        headers
                    }))),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_eq!(vec![Cow::Borrowed("Remark"),
                                Cow::Borrowed("Content-Transfer-Encoding")],
                           headers);
                assert_literal_like(
                    b"From:", b"A test message\r\n\r\n", 0, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODY[TEXT]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::TopLevel(
                    s::SectionText::Text(()))),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_literal_like(
                    b"That is not dead which can eternal lie.\r\n",
                    "And with strange æons even death may die.\r\n".as_bytes(),
                    0, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODY[1]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::Sub(s::SubSectionSpec {
                    subscripts,
                    text: None,
                })),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_eq!(vec![1], subscripts);
                assert_literal_like(
                    b"That is not dead which can eternal lie.\r\n",
                    "And with strange æons even death may die.\r\n".as_bytes(),
                    0, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 2 BODY[1]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::Sub(s::SubSectionSpec {
                    subscripts,
                    text: None,
                })),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_eq!(vec![1], subscripts);
                assert_literal_like(
                    b"This is a demonstration",
                    b"new multi-part message standard.\r\n",
                    0,
                    false,
                    lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 2 BODY[1.MIME]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::Sub(s::SubSectionSpec {
                    subscripts,
                    text: Some(s::SectionText::Mime(())),
                })),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_eq!(vec![1], subscripts);
                assert_literal_like(
                    b"Content-Type: TEXT/PLAIN",
                    b"Content-Description: Explanation\r\n\r\n",
                    0,
                    false,
                    lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 2 BODY[2.MIME]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::Sub(s::SubSectionSpec {
                    subscripts,
                    text: Some(s::SectionText::Mime(())),
                })),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_eq!(vec![2], subscripts);
                assert_literal_like(
                    b"Content-Type: MESSAGE/RFC822",
                    b"Content-Description: Rich Text demo\r\n\r\n",
                    0,
                    false,
                    lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 2 BODY[2.HEADER]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::Sub(s::SubSectionSpec {
                    subscripts,
                    text: Some(s::SectionText::Header(())),
                })),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_eq!(vec![2], subscripts);
                assert_literal_like(
                    b"Received: from dimacs.rutgers",
                    b"Subject: Re: a MIME-Version misfeature\r\n\r\n",
                    0,
                    false,
                    lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 2 BODY[2.TEXT]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: Some(s::SectionSpec::Sub(s::SubSectionSpec {
                    subscripts,
                    text: Some(s::SectionText::Text(())),
                })),
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_eq!(vec![2], subscripts);
                assert_literal_like(
                    b"This message has been composed",
                    b"mu0M2YtJKaFk=--\r\n",
                    0,
                    false,
                    lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODY[]<1.5>"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: None,
                slice_origin: Some(1),
                data: lit,
            }) in fr => {
                assert_literal_like(b"emark", b"", 5, false, lit);
            }
        };
    });

    fetch_single!(client, c("FETCH 1 BODY[]<0.5000>"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: None,
                slice_origin: Some(0),
                data: lit,
            }) in fr => {
                assert_literal_like(
                    b"Remark:", b"even death may die.\r\n", 937, false, lit);
            }
        };
    });
}

#[test]
fn implicit_seen() {
    let setup = set_up();
    let mut client = setup.connect("3501feis");
    quick_log_in(&mut client);
    quick_create(&mut client, "3501feis");
    quick_append_enron(&mut client, "3501feis", 2);

    ok_command!(client, c("EXAMINE 3501feis"));
    // Nothing we do during EXAMINE should have any effect
    ok_command!(client, c("FETCH 1 RFC822"));
    ok_command!(client, c("UID FETCH 1 BODY[]"));

    fetch_single!(client, c("FETCH 1 FLAGS"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::Flags(s::FlagsFetch::Recent(ref flags)) in fr => {
                assert!(flags.is_empty());
            }
        };
    });

    ok_command!(client, c("SELECT 3501feis"));
    // RFC822.SIZE, RFC822.HEADER, and BODY.PEEK don't cause an implicit \Seen
    // status
    ok_command!(client, c("FETCH 1 RFC822.SIZE"));
    ok_command!(client, c("FETCH 1 RFC822.HEADER"));
    ok_command!(client, c("FETCH 1 BODY.PEEK[]"));

    fetch_single!(client, c("FETCH 1 FLAGS"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::Flags(s::FlagsFetch::Recent(ref flags)) in fr => {
                assert!(flags.is_empty());
            }
        };
    });

    // Fetching the IMAP4-style body without .PEEK sets \Seen
    ok_command!(client, c("FETCH 1 BODY[]"));
    fetch_single!(client, c("FETCH 1 FLAGS"), ref fr => {
        has_msgatt_matching! {
            s::MsgAtt::Flags(s::FlagsFetch::Recent(ref flags)) in fr => {
                assert_eq!(&vec![Flag::Seen], flags);
            }
        };
    });

    // If we fetch the body and FLAGS at the same time, FLAGS reflects the new
    // \Seen status.
    // We can't use fetch_single! here since we also get an echo from the
    // change notification about the flag.
    command!(mut responses = client, c("FETCH 2 (FLAGS BODY[HEADER])"));
    assert_eq!(3, responses.len());
    assert_tagged_ok(responses.pop().unwrap());
    // The actual fetch response is always before the echo
    match responses.into_iter().next().unwrap() {
        s::ResponseLine {
            tag: None,
            response: s::Response::Fetch(fr),
        } => {
            has_msgatt_matching! {
                s::MsgAtt::Flags(s::FlagsFetch::Recent(ref flags)) in fr => {
                    assert_eq!(&vec![Flag::Seen], flags);
                }
            };

            // Also ensure this is the fetch response we think it is
            has_msgatt_matching! { s::MsgAtt::Body(..) in fr };
        }
        r => panic!("Unexpected response: {:?}", r),
    }
}

#[test]
fn error_conditions() {
    let setup = set_up();
    let mut client = setup.connect("3501feec");
    quick_log_in(&mut client);
    quick_create(&mut client, "3501feec");
    quick_append_enron(&mut client, "3501feec", 2);
    quick_select(&mut client, "3501feec");

    let mut client2 = setup.connect("3501feec2");
    quick_log_in(&mut client2);
    quick_select(&mut client2, "3501feec");

    // Expunge UID 2 without letting the first client remove that from its
    // seqnum mapping.
    ok_command!(client2, c("XVANQUISH 2"));
    ok_command!(client2, c("XCRY PURGE"));

    // First attempt to fetch the expunged message - NO
    // We need to use BODY.PEEK[] or else it will try to store \Seen, which
    // causes it to become aware of the EXPUNGE command without needing to hit
    // disk.
    command!([response] = client, c("FETCH 2 BODY.PEEK[]"));
    unpack_cond_response! {
        (Some(_), s::RespCondType::No,
         Some(s::RespTextCode::ExpungeIssued(())), _) = response => ()
    };

    // Second attempt to fetch the expunged message - BYE
    client.write_raw(b"F1 FETCH 2 BODY.PEEK[]\r\n").unwrap();
    let mut buffer = Vec::new();
    let response = client.read_one_response(&mut buffer).unwrap();
    unpack_cond_response! {
        (None, s::RespCondType::Bye,
         Some(s::RespTextCode::ClientBug(())), _) = response => ()
    };

    let mut client = setup.connect("3501feec");
    quick_log_in(&mut client);
    quick_select(&mut client, "3501feec");

    // Also do some flag storing to make the first client aware of UID 3 even
    // though it is not in the UID map.
    quick_append_enron(&mut client2, "3501feec", 1);
    ok_command!(client2, c("UID STORE 3 +FLAGS.SILENT (\\Seen)"));
    ok_command!(client, c("STORE 1 +FLAGS.SILENT (\\Seen)"));

    command!([response] = client, c("UID FETCH 3 BODY[]"));
    assert_error_response(
        response,
        Some(s::RespTextCode::ClientBug(())),
        Error::UnaddressableMessage,
    );

    ok_command!(client, c("NOOP"));

    command!([response] = client, c("FETCH 100 BODY[]"));
    unpack_cond_response! {
        (Some(_), s::RespCondType::Bad, _, _) = response => ()
    }

    // UID FETCH for messages not in the current set are allowed
    command!(mut responses = client, c("UID FETCH 1:* UID"));
    assert_eq!(3, responses.len());
    assert_tagged_ok(responses.pop().unwrap());
}

#[test]
fn wrapped_headers_not_mangled() {
    let setup = set_up();
    let mut client = setup.connect("3501fehm");
    quick_log_in(&mut client);
    quick_create(&mut client, "3501fehm");
    quick_select(&mut client, "3501fehm");

    client
        .start_append(
            "3501fehm",
            s::AppendFragment {
                // This particular date (or something around it) is required to
                // get the bytes to line up in the right way to trigger the
                // bug.
                internal_date: Some(
                    FixedOffset::east(0).ymd(2003, 2, 26).and_hms(10, 58, 31),
                ),
                ..s::AppendFragment::default()
            },
            DOVECOT_PREFER_STANDALONE_DAEMONS,
        )
        .unwrap();

    let mut buffer = Vec::new();
    let mut responses = client.finish_append(&mut buffer).unwrap();
    assert_tagged_ok_any(responses.pop().unwrap());

    fetch_single!(client, c("FETCH 1 BODY.PEEK[]"), fr => {
        has_msgatt_matching! {
            move s::MsgAtt::Body(s::MsgAttBody {
                kind: s::FetchAttBodyKind::Body,
                section: None,
                slice_origin: None,
                data: lit,
            }) in fr => {
                assert_literal_like(
                    b"Received:", b"\r\n", 2954, false, lit);
            }
        };
    });
}