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));
12 use POSIX qw(strftime);
15 my $parse_date = \&PublicInbox::IMAP::parse_date;
16 is(strftime('%Y-%m-%d', gmtime($parse_date->('02-Oct-1993'))),
17 '1993-10-02', 'parse_date works');
18 is(strftime('%Y-%m-%d', gmtime($parse_date->('2-Oct-1993'))),
19 '1993-10-02', 'parse_date works w/o leading zero');
21 is($parse_date->('2-10-1993'), undef, 'bad month');
23 # from what I can tell, RFC 3501 says nothing about date-month
24 # case-insensitivity, so be case-sensitive for now
25 is($parse_date->('02-oct-1993'), undef, 'case-sensitive month');
28 my ($tmpdir, $for_destroy) = tmpdir();
29 my $cfgfile = "$tmpdir/config";
31 open my $fh, '>', $cfgfile or BAIL_OUT $!;
32 print $fh <<EOF or BAIL_OUT $!;
41 newsgroup = IGNORE.THIS
43 close $fh or BAIL_OUT $!;
44 local $ENV{PI_CONFIG} = $cfgfile;
45 for my $x (qw(a b c)) {
46 ok(run_script(['-init', '-Lbasic', '-V2', $x, "$tmpdir/$x",
47 "https://example.com/$x", "$x\@example.com"]),
50 my $imapd = PublicInbox::IMAPD->new;
52 local $SIG{__WARN__} = sub { push @w, @_ };
53 $imapd->refresh_groups;
54 my $self = { imapd => $imapd };
55 is(scalar(@w), 1, 'got a warning for upper-case');
56 like($w[0], qr/IGNORE\.THIS/, 'warned about upper-case');
57 my $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x', '%');
58 is(scalar($$res =~ tr/\n/\n/), 2, 'only one result');
59 like($$res, qr/ x\r\ntag OK/, 'saw expected');
60 $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x.', '%');
61 is(scalar($$res =~ tr/\n/\n/), 3, 'only one result');
62 is(scalar(my @x = ($$res =~ m/ x\.[zy]\r\n/g)), 2, 'match expected');
64 $res = PublicInbox::IMAP::cmd_list($self, 't', 'x.(?{die "RCE"})', '%');
65 like($$res, qr/\At OK /, 'refname does not match attempted RCE');
66 $res = PublicInbox::IMAP::cmd_list($self, 't', '', '(?{die "RCE"})%');
67 like($$res, qr/\At OK /, 'wildcard does not match attempted RCE');
71 my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
73 my $r = $partial_prepare->($x, [], my $p = 'BODY[9]');
75 $r = $partial_prepare->($x, [], $p = 'BODY[9]<5>');
77 $r = $partial_prepare->($x, [], $p = 'BODY[9]<5.1>');
79 $r = $partial_prepare->($x, [], $p = 'BODY[1.1]');
81 $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
83 $r = $partial_prepare->($x, [], $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
85 $r = $partial_prepare->($x, [], $p = 'BODY[HEDDER.FIELDS.NOT (TO)]');
86 ok(!$r, "rejected misspelling $p");
87 $r = $partial_prepare->($x, [], $p = 'BODY[1.1.HEADER.FIELDS (TO)]');
89 my $partial_body = \&PublicInbox::IMAP::partial_body;
90 my $partial_hdr_get = \&PublicInbox::IMAP::partial_hdr_get;
91 my $partial_hdr_not = \&PublicInbox::IMAP::partial_hdr_not;
92 my $hdrs_regexp = \&PublicInbox::IMAP::hdrs_regexp;
94 'BODY[9]' => [ $partial_body, 9, undef, undef, undef ],
95 'BODY[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
96 'BODY[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
97 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
98 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
99 undef, $hdrs_regexp->('DATE FROM'),
101 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
102 undef, $hdrs_regexp->('TO'),
104 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
105 '1.1', $hdrs_regexp->('TO'),
107 }, 'structure matches expected');