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
8 use PublicInbox::IMAPD;
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');
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');
28 my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
30 my $r = $partial_prepare->($x, [], my $p = 'BODY[9]');
32 $r = $partial_prepare->($x, [], $p = 'BODY[9]<5>');
34 $r = $partial_prepare->($x, [], $p = 'BODY[9]<5.1>');
36 $r = $partial_prepare->($x, [], $p = 'BODY[1.1]');
38 $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
40 $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
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)]');
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 my $hdrs_regexp = \&PublicInbox::IMAP::hdrs_regexp;
51 'BODY[9]' => [ $partial_body, 9, undef, undef, undef ],
52 'BODY[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
53 'BODY[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
54 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
55 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
56 undef, $hdrs_regexp->('DATE FROM'),
58 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
59 undef, $hdrs_regexp->('TO'),
61 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
62 '1.1', $hdrs_regexp->('TO'),
64 }, 'structure matches expected');