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;
9 use PublicInbox::TestCommon;
10 require_mods(qw(DBD::SQLite));
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $cfgfile = "$tmpdir/config";
16 open my $fh, '>', $cfgfile or BAIL_OUT $!;
17 print $fh <<EOF or BAIL_OUT $!;
26 newsgroup = IGNORE.THIS
28 close $fh or BAIL_OUT $!;
29 local $ENV{PI_CONFIG} = $cfgfile;
30 for my $x (qw(a b c)) {
31 ok(run_script(['-init', '-Lbasic', '-V2', $x, "$tmpdir/$x",
32 "https://example.com/$x", "$x\@example.com"]),
35 my $imapd = PublicInbox::IMAPD->new;
37 local $SIG{__WARN__} = sub { push @w, @_ };
38 $imapd->refresh_groups;
39 my $self = { imapd => $imapd };
40 is(scalar(@w), 1, 'got a warning for upper-case');
41 like($w[0], qr/IGNORE\.THIS/, 'warned about upper-case');
42 my $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x', '%');
43 is(scalar($$res =~ tr/\n/\n/), 2, 'only one result');
44 like($$res, qr/ x\r\ntag OK/, 'saw expected');
45 $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x.', '%');
46 is(scalar($$res =~ tr/\n/\n/), 3, 'only one result');
47 is(scalar(my @x = ($$res =~ m/ x\.[zy]\r\n/g)), 2, 'match expected');
49 $res = PublicInbox::IMAP::cmd_list($self, 't', 'x.(?{die "RCE"})', '%');
50 like($$res, qr/\At OK /, 'refname does not match attempted RCE');
51 $res = PublicInbox::IMAP::cmd_list($self, 't', '', '(?{die "RCE"})%');
52 like($$res, qr/\At OK /, 'wildcard does not match attempted RCE');
56 my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
58 my $r = $partial_prepare->($x, [], my $p = 'BODY[9]');
60 $r = $partial_prepare->($x, [], $p = 'BODY[9]<5>');
62 $r = $partial_prepare->($x, [], $p = 'BODY[9]<5.1>');
64 $r = $partial_prepare->($x, [], $p = 'BODY[1.1]');
66 $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
68 $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
70 $r = $partial_prepare->($x, [], $p = 'BODY[HEDDER.FIELDS.NOT (TO)]');
71 ok(!$r, "rejected misspelling $p");
72 $r = $partial_prepare->($x, [], $p = 'BODY[1.1.HEADER.FIELDS (TO)]');
74 my $partial_body = \&PublicInbox::IMAP::partial_body;
75 my $partial_hdr_get = \&PublicInbox::IMAP::partial_hdr_get;
76 my $partial_hdr_not = \&PublicInbox::IMAP::partial_hdr_not;
77 my $hdrs_regexp = \&PublicInbox::IMAP::hdrs_regexp;
79 'BODY[9]' => [ $partial_body, 9, undef, undef, undef ],
80 'BODY[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
81 'BODY[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
82 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
83 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
84 undef, $hdrs_regexp->('DATE FROM'),
86 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
87 undef, $hdrs_regexp->('TO'),
89 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
90 '1.1', $hdrs_regexp->('TO'),
92 }, 'structure matches expected');