]> Sergey Matveev's repositories - public-inbox.git/blob - t/imap.t
www: drop --subject from "git send-email" instructions
[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 v5.10.1;
7 use PublicInbox::TestCommon;
8 require_git 2.6;
9 require_mods(qw(-imapd));
10 require_ok 'PublicInbox::IMAP';
11 require_ok 'PublicInbox::IMAPD';
12
13 my ($tmpdir, $for_destroy) = tmpdir();
14 my $cfgfile = "$tmpdir/config";
15 {
16         open my $fh, '>', $cfgfile or BAIL_OUT $!;
17         print $fh <<EOF or BAIL_OUT $!;
18 [publicinbox "a"]
19         inboxdir = $tmpdir/a
20         newsgroup = x.y.z
21 [publicinbox "b"]
22         inboxdir = $tmpdir/b
23         newsgroup = x.z.y
24 [publicinbox "c"]
25         inboxdir = $tmpdir/c
26         newsgroup = IGNORE.THIS
27 EOF
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"]),
33                         "init $x");
34         }
35         my $imapd = PublicInbox::IMAPD->new;
36         my @w;
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');
48
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');
53 }
54
55 {
56         my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
57         my $x = {};
58         my $n = 0;
59         my $r = $partial_prepare->(\$n, $x, [], my $p = 'BODY[9]');
60         ok($r, $p);
61         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[9]<5>');
62         ok($r, $p);
63         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[9]<5.1>');
64         ok($r, $p);
65         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[1.1]');
66         ok($r, $p);
67         $r = $partial_prepare->(\$n, $x, [],
68                                 $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
69         ok($r, $p);
70         $r = $partial_prepare->(\$n, $x, [],
71                                 $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
72         ok($r, $p);
73         $r = $partial_prepare->(\$n, $x, [],
74                                 $p = 'BODY[HEDDER.FIELDS.NOT (TO)]');
75         ok(!$r, "rejected misspelling $p");
76         $r = $partial_prepare->(\$n, $x, [],
77                                 $p = 'BODY[1.1.HEADER.FIELDS (TO)]');
78         ok($r, $p);
79         my $partial_body = \&PublicInbox::IMAP::partial_body;
80         my $partial_hdr_get = \&PublicInbox::IMAP::partial_hdr_get;
81         my $partial_hdr_not = \&PublicInbox::IMAP::partial_hdr_not;
82         my $hdrs_regexp = \&PublicInbox::IMAP::hdrs_regexp;
83         is_deeply($x, {
84                 'BODY[9]' => [ $partial_body, 9, undef, undef, undef ],
85                 'BODY[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
86                 'BODY[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
87                 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
88                 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
89                                         undef, $hdrs_regexp->('DATE FROM'),
90                                         undef, undef ],
91                 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
92                                                 undef, $hdrs_regexp->('TO'),
93                                                 undef, undef ],
94                 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
95                                                 '1.1', $hdrs_regexp->('TO'),
96                                                 undef, undef ],
97         }, 'structure matches expected');
98 }
99
100 {
101         my $fetch_compile = \&PublicInbox::IMAP::fetch_compile;
102         my ($cb, $ops, $partial) = $fetch_compile->(['BODY[]']);
103         is($partial, undef, 'no partial fetch data');
104         is_deeply($ops, [
105                 undef, \&PublicInbox::IMAP::op_crlf_bref,
106                 'BODY[]', \&PublicInbox::IMAP::emit_rfc822
107         ], 'proper key and op compiled for BODY[]');
108
109         ($cb, $ops, $partial) = $fetch_compile->(['BODY', 'BODY[]']);
110         is_deeply($ops, [
111                 undef, \&PublicInbox::IMAP::op_crlf_bref,
112                 'BODY[]', \&PublicInbox::IMAP::emit_rfc822,
113                 undef, \&PublicInbox::IMAP::op_eml_new,
114                 'BODY', \&PublicInbox::IMAP::emit_body,
115         ], 'placed op_eml_new before emit_body');
116 }
117
118 # UID <=> MSN mapping
119
120 sub uo2m_str_new ($) {
121         no warnings 'uninitialized'; # uom2m_ary_new may have may have undef
122         pack('S*', @{$_[0]->uo2m_ary_new}); # 2 bytes per-MSN
123 }
124
125 {
126         my $ibx = bless { uid_range => [ 1, 2, 4 ] }, 'Uo2mTestInbox';
127         my $imap = bless { uid_base => 0, ibx => $ibx }, 'PublicInbox::IMAP';
128         my $uo2m = $imap->uo2m_ary_new;
129         is_deeply($uo2m, [ 1, 2, undef, 3 ], 'uo2m ary');
130         $uo2m = uo2m_str_new($imap);
131         is_deeply([ unpack('S*', $uo2m) ], [ 1, 2, 0, 3 ], 'uo2m str');
132
133         $ibx->{uid_range} = [ 1, 2, 4, 5, 6 ];
134         for ([ 1, 2, undef, 3 ], $uo2m) {
135                 $imap->{uo2m} = $_;
136                 is($imap->uid2msn(1), 1, 'uid2msn');
137                 is($imap->uid2msn(4), 3, 'uid2msn');
138                 is($imap->uo2m_last_uid, 4, 'uo2m_last_uid');
139                 $imap->uo2m_extend(6);
140                 is($imap->uid2msn(5), 4, 'uid2msn 5 => 4');
141                 is($imap->uid2msn(6), 5, 'uid2msn 6 => 5');
142                 is($imap->uo2m_last_uid, 6, 'uo2m_last_uid');
143
144                 my $msn2uid = $imap->msn2uid;
145                 my $range = '1,4:5';
146                 $imap->can('msn_to_uid_range')->($msn2uid, $range);
147                 is($range, '1,5:6', 'range converted');
148         }
149 }
150
151 done_testing;
152
153 package Uo2mTestInbox;
154 use strict;
155 require PublicInbox::DummyInbox;
156 our @ISA = qw(PublicInbox::DummyInbox);
157 sub over { shift }
158 sub uid_range {
159         my ($self, $beg, $end, undef) = @_;
160         [ grep { $_ >= $beg && $_ <= $end } @{$self->{uid_range}} ];
161 }