]> Sergey Matveev's repositories - public-inbox.git/blob - t/imap.t
aa262a196329f49d79808c7b4c951a6fb5ecc81f
[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 @w;
12         local $SIG{__WARN__} = sub { push @w, @_ };
13         my @n = map { { newsgroup => $_ } } (qw(x.y.z x.z.y IGNORE.THIS));
14         my $self = { imapd => { grouplist => \@n } };
15         PublicInbox::IMAPD::refresh_inboxlist($self->{imapd});
16         is(scalar(@w), 1, 'got a warning for upper-case');
17         like($w[0], qr/IGNORE\.THIS/, 'warned about upper-case');
18
19         my $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x', '%');
20         is(scalar($$res =~ tr/\n/\n/), 2, 'only one result');
21         like($$res, qr/ x\r\ntag OK/, 'saw expected');
22         $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x.', '%');
23         is(scalar($$res =~ tr/\n/\n/), 3, 'only one result');
24         is(scalar(my @x = ($$res =~ m/ x\.[zy]\r\n/g)), 2, 'match expected');
25
26         $res = PublicInbox::IMAP::cmd_list($self, 't', 'x.(?{die "RCE"})', '%');
27         like($$res, qr/\At OK /, 'refname does not match attempted RCE');
28         $res = PublicInbox::IMAP::cmd_list($self, 't', '', '(?{die "RCE"})%');
29         like($$res, qr/\At OK /, 'wildcard does not match attempted RCE');
30 }
31
32 {
33         my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
34         my $x = {};
35         my $r = $partial_prepare->($x, [], my $p = 'BODY[9]');
36         ok($r, $p);
37         $r = $partial_prepare->($x, [], $p = 'BODY[9]<5>');
38         ok($r, $p);
39         $r = $partial_prepare->($x, [], $p = 'BODY[9]<5.1>');
40         ok($r, $p);
41         $r = $partial_prepare->($x, [], $p = 'BODY[1.1]');
42         ok($r, $p);
43         $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
44         ok($r, $p);
45         $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
46         ok($r, $p);
47         $r = $partial_prepare->($x, [], $p = 'BODY[HEDDER.FIELDS.NOT (TO)]');
48         ok(!$r, "rejected misspelling $p");
49         $r = $partial_prepare->($x, [], $p = 'BODY[1.1.HEADER.FIELDS (TO)]');
50         ok($r, $p);
51         my $partial_body = \&PublicInbox::IMAP::partial_body;
52         my $partial_hdr_get = \&PublicInbox::IMAP::partial_hdr_get;
53         my $partial_hdr_not = \&PublicInbox::IMAP::partial_hdr_not;
54         my $hdrs_regexp = \&PublicInbox::IMAP::hdrs_regexp;
55         is_deeply($x, {
56                 'BODY[9]' => [ $partial_body, 9, undef, undef, undef ],
57                 'BODY[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
58                 'BODY[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
59                 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
60                 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
61                                         undef, $hdrs_regexp->('DATE FROM'),
62                                         undef, undef ],
63                 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
64                                                 undef, $hdrs_regexp->('TO'),
65                                                 undef, undef ],
66                 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
67                                                 '1.1', $hdrs_regexp->('TO'),
68                                                 undef, undef ],
69         }, 'structure matches expected');
70 }
71
72 done_testing;