]> Sergey Matveev's repositories - public-inbox.git/blob - t/imap.t
testcommon: allow OR-ing module dependencies
[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::TestCommon;
8 require_mods(qw(DBD::SQLite Email::Address::XS||Mail::Address));
9 require_ok 'PublicInbox::IMAP';
10 require_ok 'PublicInbox::IMAPD';
11 require_git 2.6;
12 use POSIX qw(strftime);
13
14 {
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');
20
21         is($parse_date->('2-10-1993'), undef, 'bad month');
22
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');
26 }
27
28 my ($tmpdir, $for_destroy) = tmpdir();
29 my $cfgfile = "$tmpdir/config";
30 {
31         open my $fh, '>', $cfgfile or BAIL_OUT $!;
32         print $fh <<EOF or BAIL_OUT $!;
33 [publicinbox "a"]
34         inboxdir = $tmpdir/a
35         newsgroup = x.y.z
36 [publicinbox "b"]
37         inboxdir = $tmpdir/b
38         newsgroup = x.z.y
39 [publicinbox "c"]
40         inboxdir = $tmpdir/c
41         newsgroup = IGNORE.THIS
42 EOF
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"]),
48                         "init $x");
49         }
50         my $imapd = PublicInbox::IMAPD->new;
51         my @w;
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');
63
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');
68 }
69
70 {
71         my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
72         my $x = {};
73         my $n = 0;
74         my $r = $partial_prepare->(\$n, $x, [], my $p = 'BODY[9]');
75         ok($r, $p);
76         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[9]<5>');
77         ok($r, $p);
78         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[9]<5.1>');
79         ok($r, $p);
80         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[1.1]');
81         ok($r, $p);
82         $r = $partial_prepare->(\$n, $x, [],
83                                 $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
84         ok($r, $p);
85         $r = $partial_prepare->(\$n, $x, [],
86                                 $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
87         ok($r, $p);
88         $r = $partial_prepare->(\$n, $x, [],
89                                 $p = 'BODY[HEDDER.FIELDS.NOT (TO)]');
90         ok(!$r, "rejected misspelling $p");
91         $r = $partial_prepare->(\$n, $x, [],
92                                 $p = 'BODY[1.1.HEADER.FIELDS (TO)]');
93         ok($r, $p);
94         my $partial_body = \&PublicInbox::IMAP::partial_body;
95         my $partial_hdr_get = \&PublicInbox::IMAP::partial_hdr_get;
96         my $partial_hdr_not = \&PublicInbox::IMAP::partial_hdr_not;
97         my $hdrs_regexp = \&PublicInbox::IMAP::hdrs_regexp;
98         is_deeply($x, {
99                 'BODY[9]' => [ $partial_body, 9, undef, undef, undef ],
100                 'BODY[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
101                 'BODY[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
102                 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
103                 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
104                                         undef, $hdrs_regexp->('DATE FROM'),
105                                         undef, undef ],
106                 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
107                                                 undef, $hdrs_regexp->('TO'),
108                                                 undef, undef ],
109                 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
110                                                 '1.1', $hdrs_regexp->('TO'),
111                                                 undef, undef ],
112         }, 'structure matches expected');
113 }
114
115 {
116         my $fetch_compile = \&PublicInbox::IMAP::fetch_compile;
117         my ($cb, $ops, $partial) = $fetch_compile->(['BODY[]']);
118         is($partial, undef, 'no partial fetch data');
119         is_deeply($ops, [
120                 undef, \&PublicInbox::IMAP::op_crlf_bref,
121                 'BODY[]', \&PublicInbox::IMAP::emit_rfc822
122         ], 'proper key and op compiled for BODY[]');
123
124         ($cb, $ops, $partial) = $fetch_compile->(['BODY', 'BODY[]']);
125         is_deeply($ops, [
126                 undef, \&PublicInbox::IMAP::op_crlf_bref,
127                 'BODY[]', \&PublicInbox::IMAP::emit_rfc822,
128                 undef, \&PublicInbox::IMAP::op_eml_new,
129                 'BODY', \&PublicInbox::IMAP::emit_body,
130         ], 'placed op_eml_new before emit_body');
131 }
132
133 # UID <=> MSN mapping
134
135 sub uo2m_str_new ($) {
136         no warnings 'uninitialized'; # uom2m_ary_new may have may have undef
137         pack('S*', @{$_[0]->uo2m_ary_new}); # 2 bytes per-MSN
138 }
139
140 {
141         my $ibx = bless { uid_range => [ 1, 2, 4 ] }, 'Uo2mTestInbox';
142         my $imap = bless { uid_base => 0, ibx => $ibx }, 'PublicInbox::IMAP';
143         my $uo2m = $imap->uo2m_ary_new;
144         is_deeply($uo2m, [ 1, 2, undef, 3 ], 'uo2m ary');
145         $uo2m = uo2m_str_new($imap);
146         is_deeply([ unpack('S*', $uo2m) ], [ 1, 2, 0, 3 ], 'uo2m str');
147
148         $ibx->{uid_range} = [ 1, 2, 4, 5, 6 ];
149         for ([ 1, 2, undef, 3 ], $uo2m) {
150                 $imap->{uo2m} = $_;
151                 is($imap->uid2msn(1), 1, 'uid2msn');
152                 is($imap->uid2msn(4), 3, 'uid2msn');
153                 is($imap->uo2m_last_uid, 4, 'uo2m_last_uid');
154                 $imap->uo2m_extend(6);
155                 is($imap->uid2msn(5), 4, 'uid2msn 5 => 4');
156                 is($imap->uid2msn(6), 5, 'uid2msn 6 => 5');
157                 is($imap->uo2m_last_uid, 6, 'uo2m_last_uid');
158
159                 my $msn2uid = $imap->msn2uid;
160                 my $range = '1,4:5';
161                 $imap->can('msn_to_uid_range')->($msn2uid, $range);
162                 is($range, '1,5:6', 'range converted');
163         }
164 }
165
166 done_testing;
167
168 package Uo2mTestInbox;
169 use strict;
170 require PublicInbox::DummyInbox;
171 our @ISA = qw(PublicInbox::DummyInbox);
172 sub over { shift }
173 sub uid_range {
174         my ($self, $beg, $end, undef) = @_;
175         [ grep { $_ >= $beg && $_ <= $end } @{$self->{uid_range}} ];
176 }