]> Sergey Matveev's repositories - public-inbox.git/blob - t/imap.t
update copyrights for 2021
[public-inbox.git] / t / imap.t
1 #!perl -w
2 # Copyright (C) 2020-2021 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_git 2.6;
9 require_mods(qw(DBD::SQLite Email::Address::XS||Mail::Address
10         Parse::RecDescent));
11 require_ok 'PublicInbox::IMAP';
12 require_ok 'PublicInbox::IMAPD';
13
14 my ($tmpdir, $for_destroy) = tmpdir();
15 my $cfgfile = "$tmpdir/config";
16 {
17         open my $fh, '>', $cfgfile or BAIL_OUT $!;
18         print $fh <<EOF or BAIL_OUT $!;
19 [publicinbox "a"]
20         inboxdir = $tmpdir/a
21         newsgroup = x.y.z
22 [publicinbox "b"]
23         inboxdir = $tmpdir/b
24         newsgroup = x.z.y
25 [publicinbox "c"]
26         inboxdir = $tmpdir/c
27         newsgroup = IGNORE.THIS
28 EOF
29         close $fh or BAIL_OUT $!;
30         local $ENV{PI_CONFIG} = $cfgfile;
31         for my $x (qw(a b c)) {
32                 ok(run_script(['-init', '-Lbasic', '-V2', $x, "$tmpdir/$x",
33                                 "https://example.com/$x", "$x\@example.com"]),
34                         "init $x");
35         }
36         my $imapd = PublicInbox::IMAPD->new;
37         my @w;
38         local $SIG{__WARN__} = sub { push @w, @_ };
39         $imapd->refresh_groups;
40         my $self = { imapd => $imapd };
41         is(scalar(@w), 1, 'got a warning for upper-case');
42         like($w[0], qr/IGNORE\.THIS/, 'warned about upper-case');
43         my $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x', '%');
44         is(scalar($$res =~ tr/\n/\n/), 2, 'only one result');
45         like($$res, qr/ x\r\ntag OK/, 'saw expected');
46         $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x.', '%');
47         is(scalar($$res =~ tr/\n/\n/), 3, 'only one result');
48         is(scalar(my @x = ($$res =~ m/ x\.[zy]\r\n/g)), 2, 'match expected');
49
50         $res = PublicInbox::IMAP::cmd_list($self, 't', 'x.(?{die "RCE"})', '%');
51         like($$res, qr/\At OK /, 'refname does not match attempted RCE');
52         $res = PublicInbox::IMAP::cmd_list($self, 't', '', '(?{die "RCE"})%');
53         like($$res, qr/\At OK /, 'wildcard does not match attempted RCE');
54 }
55
56 {
57         my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
58         my $x = {};
59         my $n = 0;
60         my $r = $partial_prepare->(\$n, $x, [], my $p = 'BODY[9]');
61         ok($r, $p);
62         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[9]<5>');
63         ok($r, $p);
64         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[9]<5.1>');
65         ok($r, $p);
66         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[1.1]');
67         ok($r, $p);
68         $r = $partial_prepare->(\$n, $x, [],
69                                 $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
70         ok($r, $p);
71         $r = $partial_prepare->(\$n, $x, [],
72                                 $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
73         ok($r, $p);
74         $r = $partial_prepare->(\$n, $x, [],
75                                 $p = 'BODY[HEDDER.FIELDS.NOT (TO)]');
76         ok(!$r, "rejected misspelling $p");
77         $r = $partial_prepare->(\$n, $x, [],
78                                 $p = 'BODY[1.1.HEADER.FIELDS (TO)]');
79         ok($r, $p);
80         my $partial_body = \&PublicInbox::IMAP::partial_body;
81         my $partial_hdr_get = \&PublicInbox::IMAP::partial_hdr_get;
82         my $partial_hdr_not = \&PublicInbox::IMAP::partial_hdr_not;
83         my $hdrs_regexp = \&PublicInbox::IMAP::hdrs_regexp;
84         is_deeply($x, {
85                 'BODY[9]' => [ $partial_body, 9, undef, undef, undef ],
86                 'BODY[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
87                 'BODY[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
88                 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
89                 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
90                                         undef, $hdrs_regexp->('DATE FROM'),
91                                         undef, undef ],
92                 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
93                                                 undef, $hdrs_regexp->('TO'),
94                                                 undef, undef ],
95                 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
96                                                 '1.1', $hdrs_regexp->('TO'),
97                                                 undef, undef ],
98         }, 'structure matches expected');
99 }
100
101 {
102         my $fetch_compile = \&PublicInbox::IMAP::fetch_compile;
103         my ($cb, $ops, $partial) = $fetch_compile->(['BODY[]']);
104         is($partial, undef, 'no partial fetch data');
105         is_deeply($ops, [
106                 undef, \&PublicInbox::IMAP::op_crlf_bref,
107                 'BODY[]', \&PublicInbox::IMAP::emit_rfc822
108         ], 'proper key and op compiled for BODY[]');
109
110         ($cb, $ops, $partial) = $fetch_compile->(['BODY', 'BODY[]']);
111         is_deeply($ops, [
112                 undef, \&PublicInbox::IMAP::op_crlf_bref,
113                 'BODY[]', \&PublicInbox::IMAP::emit_rfc822,
114                 undef, \&PublicInbox::IMAP::op_eml_new,
115                 'BODY', \&PublicInbox::IMAP::emit_body,
116         ], 'placed op_eml_new before emit_body');
117 }
118
119 # UID <=> MSN mapping
120
121 sub uo2m_str_new ($) {
122         no warnings 'uninitialized'; # uom2m_ary_new may have may have undef
123         pack('S*', @{$_[0]->uo2m_ary_new}); # 2 bytes per-MSN
124 }
125
126 {
127         my $ibx = bless { uid_range => [ 1, 2, 4 ] }, 'Uo2mTestInbox';
128         my $imap = bless { uid_base => 0, ibx => $ibx }, 'PublicInbox::IMAP';
129         my $uo2m = $imap->uo2m_ary_new;
130         is_deeply($uo2m, [ 1, 2, undef, 3 ], 'uo2m ary');
131         $uo2m = uo2m_str_new($imap);
132         is_deeply([ unpack('S*', $uo2m) ], [ 1, 2, 0, 3 ], 'uo2m str');
133
134         $ibx->{uid_range} = [ 1, 2, 4, 5, 6 ];
135         for ([ 1, 2, undef, 3 ], $uo2m) {
136                 $imap->{uo2m} = $_;
137                 is($imap->uid2msn(1), 1, 'uid2msn');
138                 is($imap->uid2msn(4), 3, 'uid2msn');
139                 is($imap->uo2m_last_uid, 4, 'uo2m_last_uid');
140                 $imap->uo2m_extend(6);
141                 is($imap->uid2msn(5), 4, 'uid2msn 5 => 4');
142                 is($imap->uid2msn(6), 5, 'uid2msn 6 => 5');
143                 is($imap->uo2m_last_uid, 6, 'uo2m_last_uid');
144
145                 my $msn2uid = $imap->msn2uid;
146                 my $range = '1,4:5';
147                 $imap->can('msn_to_uid_range')->($msn2uid, $range);
148                 is($range, '1,5:6', 'range converted');
149         }
150 }
151
152 done_testing;
153
154 package Uo2mTestInbox;
155 use strict;
156 require PublicInbox::DummyInbox;
157 our @ISA = qw(PublicInbox::DummyInbox);
158 sub over { shift }
159 sub uid_range {
160         my ($self, $beg, $end, undef) = @_;
161         [ grep { $_ >= $beg && $_ <= $end } @{$self->{uid_range}} ];
162 }