lei/store: stop shard workers + cat-file on idle
[public-inbox.git] / t / imap.t
blob44b8ef2af11cb7898a9f5c69b0c491d9b358a813
1 #!perl -w
2 # Copyright (C) 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 use PublicInbox::IO qw(write_file);
14 my ($tmpdir, $for_destroy) = tmpdir();
15 my $cfgfile = "$tmpdir/config";
17         write_file '>', $cfgfile, <<EOF;
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.9
27 EOF
28         local $ENV{PI_CONFIG} = $cfgfile;
29         for my $x (qw(a b c)) {
30                 ok(run_script(['-init', '-Lbasic', '-V2', $x, "$tmpdir/$x",
31                                 "https://example.com/$x", "$x\@example.com"]),
32                         "init $x");
33         }
34         my $imapd = PublicInbox::IMAPD->new;
35         my @w;
36         local $SIG{__WARN__} = sub { push @w, @_ };
37         $imapd->refresh_groups;
38         my $self = { imapd => $imapd };
39         is(scalar(@w), 1, 'got a warning for slice-like name');
40         like($w[0], qr/ignore\.this\.9/i, 'warned about slice-like name');
41         my $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x', '%');
42         is(scalar($$res =~ tr/\n/\n/), 2, 'only one result');
43         like($$res, qr/ x\r\ntag OK/, 'saw expected');
44         $res = PublicInbox::IMAP::cmd_list($self, 'tag', 'x.', '%');
45         is(scalar($$res =~ tr/\n/\n/), 3, 'only one result');
46         is(scalar(my @x = ($$res =~ m/ x\.[zy]\r\n/g)), 2, 'match expected');
48         $res = PublicInbox::IMAP::cmd_list($self, 't', 'x.(?{die "RCE"})', '%');
49         like($$res, qr/\At OK /, 'refname does not match attempted RCE');
50         $res = PublicInbox::IMAP::cmd_list($self, 't', '', '(?{die "RCE"})%');
51         like($$res, qr/\At OK /, 'wildcard does not match attempted RCE');
55         my $partial_prepare = \&PublicInbox::IMAP::partial_prepare;
56         my $x = {};
57         my $n = 0;
58         my $r = $partial_prepare->(\$n, $x, [], my $p = 'BODY[9]');
59         ok($r, $p);
60         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[9]<5>');
61         ok($r, $p);
62         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[9]<5.1>');
63         ok($r, $p);
64         $r = $partial_prepare->(\$n, $x, [], $p = 'BODY[1.1]');
65         ok($r, $p);
66         $r = $partial_prepare->(\$n, $x, [],
67                                 $p = 'BODY[HEADER.FIELDS (DATE FROM)]');
68         ok($r, $p);
69         $r = $partial_prepare->(\$n, $x, [],
70                                 $p = 'BODY[HEADER.FIELDS.NOT (TO)]');
71         ok($r, $p);
72         $r = $partial_prepare->(\$n, $x, [],
73                                 $p = 'BODY[HEDDER.FIELDS.NOT (TO)]');
74         ok(!$r, "rejected misspelling $p");
75         $r = $partial_prepare->(\$n, $x, [],
76                                 $p = 'BODY[1.1.HEADER.FIELDS (TO)]');
77         ok($r, $p);
78         my $partial_body = \&PublicInbox::IMAP::partial_body;
79         my $partial_hdr_get = \&PublicInbox::IMAP::partial_hdr_get;
80         my $partial_hdr_not = \&PublicInbox::IMAP::partial_hdr_not;
81         my $hdrs_regexp = \&PublicInbox::IMAP::hdrs_regexp;
82         is_deeply($x, {
83                 'BODY[9]' => [ $partial_body, 9, undef, undef, undef ],
84                 'BODY[9]<5>' => [ $partial_body, 9, undef, 5, undef ],
85                 'BODY[9]<5.1>' => [ $partial_body, 9, undef, 5, 1 ],
86                 'BODY[1.1]' => [ $partial_body, '1.1', undef, undef, undef ],
87                 'BODY[HEADER.FIELDS (DATE FROM)]' => [ $partial_hdr_get,
88                                         undef, $hdrs_regexp->('DATE FROM'),
89                                         undef, undef ],
90                 'BODY[HEADER.FIELDS.NOT (TO)]' => [ $partial_hdr_not,
91                                                 undef, $hdrs_regexp->('TO'),
92                                                 undef, undef ],
93                 'BODY[1.1.HEADER.FIELDS (TO)]' => [ $partial_hdr_get,
94                                                 '1.1', $hdrs_regexp->('TO'),
95                                                 undef, undef ],
96         }, 'structure matches expected');
100         my $fetch_compile = \&PublicInbox::IMAP::fetch_compile;
101         my ($cb, $ops, $partial) = $fetch_compile->(['BODY[]']);
102         is($partial, undef, 'no partial fetch data');
103         is_deeply($ops, [
104                 undef, \&PublicInbox::IMAP::op_crlf_bref,
105                 'BODY[]', \&PublicInbox::IMAP::emit_rfc822
106         ], 'proper key and op compiled for BODY[]');
108         ($cb, $ops, $partial) = $fetch_compile->(['BODY', 'BODY[]']);
109         is_deeply($ops, [
110                 undef, \&PublicInbox::IMAP::op_crlf_bref,
111                 'BODY[]', \&PublicInbox::IMAP::emit_rfc822,
112                 undef, \&PublicInbox::IMAP::op_eml_new,
113                 'BODY', \&PublicInbox::IMAP::emit_body,
114         ], 'placed op_eml_new before emit_body');
117 # UID <=> MSN mapping
119 sub uo2m_str_new ($) {
120         no warnings 'uninitialized'; # uom2m_ary_new may have may have undef
121         pack('S*', @{$_[0]->uo2m_ary_new}); # 2 bytes per-MSN
125         my $ibx = bless { uid_range => [ 1, 2, 4 ] }, 'Uo2mTestInbox';
126         my $imap = bless { uid_base => 0, ibx => $ibx }, 'PublicInbox::IMAP';
127         my $uo2m = $imap->uo2m_ary_new;
128         is_deeply($uo2m, [ 1, 2, undef, 3 ], 'uo2m ary');
129         $uo2m = uo2m_str_new($imap);
130         is_deeply([ unpack('S*', $uo2m) ], [ 1, 2, 0, 3 ], 'uo2m str');
132         $ibx->{uid_range} = [ 1, 2, 4, 5, 6 ];
133         for ([ 1, 2, undef, 3 ], $uo2m) {
134                 $imap->{uo2m} = $_;
135                 is($imap->uid2msn(1), 1, 'uid2msn');
136                 is($imap->uid2msn(4), 3, 'uid2msn');
137                 is($imap->uo2m_last_uid, 4, 'uo2m_last_uid');
138                 $imap->uo2m_extend(6);
139                 is($imap->uid2msn(5), 4, 'uid2msn 5 => 4');
140                 is($imap->uid2msn(6), 5, 'uid2msn 6 => 5');
141                 is($imap->uo2m_last_uid, 6, 'uo2m_last_uid');
143                 my $msn2uid = $imap->msn2uid;
144                 my $range = '1,4:5';
145                 $imap->can('msn_to_uid_range')->($msn2uid, $range);
146                 is($range, '1,5:6', 'range converted');
147         }
150 done_testing;
152 package Uo2mTestInbox;
153 use strict;
154 require PublicInbox::DummyInbox;
155 our @ISA = qw(PublicInbox::DummyInbox);
156 sub over { shift }
157 sub uid_range {
158         my ($self, $beg, $end, undef) = @_;
159         [ grep { $_ >= $beg && $_ <= $end } @{$self->{uid_range}} ];