]> Sergey Matveev's repositories - public-inbox.git/blob - t/imap.t
imap: split out unit tests and benchmarks
[public-inbox.git] / t / imap.t
1 #!perl -w
2 # Copyright (C) 2020 all contributors <meta@public-inbox.org>
3 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # unit tests (no network) for IMAP, see t/imapd.t for end-to-end tests
5 use strict;
6 use Test::More;
7 use PublicInbox::IMAP;
8 use PublicInbox::IMAPD;
9
10 { # make sure we get '%' globbing right
11         my @n = map { { newsgroup => $_ } } (qw(x.y.z x.z.y));
12         my $self = { imapd => { grouplist => \@n } };
13         PublicInbox::IMAPD::refresh_inboxlist($self->{imapd});
14         my $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x', '%');
15         is(scalar($$res =~ tr/\n/\n/), 2, 'only one result');
16         like($$res, qr/ x\r\ntag OK/, 'saw expected');
17         $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x.', '%');
18         is(scalar($$res =~ tr/\n/\n/), 3, 'only one result');
19         is(scalar(my @x = ($$res =~ m/ x\.[zy]\r\n/g)), 2, 'match expected');
20
21         $res = PublicInbox::IMAP::cmd_list($self, 't', 'x.(?{die "RCE"})', '%');
22         like($$res, qr/\At OK /, 'refname does not match attempted RCE');
23         $res = PublicInbox::IMAP::cmd_list($self, 't', '', '(?{die "RCE"})%');
24         like($$res, qr/\At OK /, 'wildcard does not match attempted RCE');
25 }
26
27 {
28         my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
29         my $x = {};
30         my $r = $partial_prepare->($x, [], my $p = 'BODY.PEEK[9]');
31         ok($r, $p);
32         $r = $partial_prepare->($x, [], $p = 'BODY.PEEK[9]<5>');
33         ok($r, $p);
34         $r = $partial_prepare->($x, [], $p = 'BODY.PEEK[9]<5.1>');
35         ok($r, $p);
36         $r = $partial_prepare->($x, [], $p = 'BODY[1.1]');
37         ok($r, $p);
38         $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
39         ok($r, $p);
40         $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
41         ok($r, $p);
42         $r = $partial_prepare->($x, [], $p = 'BODY[HEDDER.FIELDS.NOT (TO)]');
43         ok(!$r, "rejected misspelling $p");
44         $r = $partial_prepare->($x, [], $p = 'BODY[1.1.HEADER.FIELDS (TO)]');
45         ok($r, $p);
46         my $partial_body = \&PublicInbox::IMAP::partial_body;
47         my $partial_hdr_get = \&PublicInbox::IMAP::partial_hdr_get;
48         my $partial_hdr_not = \&PublicInbox::IMAP::partial_hdr_not;
49         is_deeply($x, {
50                 'BODY.PEEK[9]' => [ $partial_body, 9, undef, undef, undef ],
51                 'BODY.PEEK[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
52                 'BODY.PEEK[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
53                 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
54                 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
55                                         undef, 'DATE FROM', undef, undef ],
56                 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
57                                                 undef, 'TO', undef, undef ],
58                 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
59                                                 '1.1', 'TO', undef, undef ],
60         }, 'structure matches expected');
61 }
62
63 done_testing;