]> Sergey Matveev's repositories - public-inbox.git/blob - xt/imapd-validate.t
b7b66d0583f49869ca94785e072505f02fb9050b
[public-inbox.git] / xt / imapd-validate.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 # Expensive test to validate compression and TLS.
5 use strict;
6 use Test::More;
7 use Symbol qw(gensym);
8 use PublicInbox::DS qw(now);
9 use POSIX qw(_exit);
10 use PublicInbox::TestCommon;
11 my $inbox_dir = $ENV{GIANT_INBOX_DIR};
12 plan skip_all => "GIANT_INBOX_DIR not defined for $0" unless $inbox_dir;
13 # how many emails to read into memory at once per-process
14 my $BATCH = $ENV{TEST_BATCH} // 100;
15 my $REPEAT = $ENV{TEST_REPEAT} // 1;
16
17 require_mods(qw(Mail::IMAPClient));
18 my $imap_client = 'Mail::IMAPClient';
19 my $can_compress = $imap_client->can('compress');
20 if ($can_compress) { # hope this gets fixed upstream, soon
21         require PublicInbox::IMAPClient;
22         $imap_client = 'PublicInbox::IMAPClient';
23 }
24
25 my $test_tls = $ENV{TEST_SKIP_TLS} ? 0 : eval { require IO::Socket::SSL };
26 my ($cert, $key) = qw(certs/server-cert.pem certs/server-key.pem);
27 if ($test_tls && !-r $key || !-r $cert) {
28         plan skip_all =>
29                 "certs/ missing for $0, run $^X ./certs/create-certs.perl";
30 }
31 my ($tmpdir, $for_destroy) = tmpdir();
32 my %OPT = qw(User u Password p);
33 my (%STARTTLS_OPT, %IMAPS_OPT, $td, $newsgroup, $mailbox, $make_local_server);
34 if (($ENV{IMAP_TEST_URL} // '') =~ m!\Aimap://([^/]+)/(.+)\z!) {
35         ($OPT{Server}, $mailbox) = ($1, $2);
36         $OPT{Server} =~ s/:([0-9]+)\z// and $OPT{Port} = $1 + 0;
37         %STARTTLS_OPT = %OPT;
38         %IMAPS_OPT = (%OPT, Port => 993) if $OPT{Port} == 143;
39 } else {
40         require_mods(qw(DBD::SQLite));
41         $make_local_server->();
42         $mailbox = "$newsgroup.1-50000";
43 }
44
45 my %opts = (imap => \%OPT, 'imap+compress' => { %OPT, Compress => 1 });
46 my $uid_max = do {
47         my $mic = $imap_client->new(%OPT) or BAIL_OUT "new $!";
48         $mic->examine($mailbox) or BAIL_OUT "examine: $!";
49         my $next = $mic->uidnext($mailbox) or BAIL_OUT "uidnext: $!";
50         $next - 1;
51 };
52
53 if (scalar keys %STARTTLS_OPT) {
54         $opts{starttls} = \%STARTTLS_OPT;
55         $opts{'starttls+compress'} = { %STARTTLS_OPT, Compress => 1 };
56 }
57 if (scalar keys %IMAPS_OPT) {
58         $opts{imaps} = \%IMAPS_OPT;
59         $opts{'imaps+compress'} = { %IMAPS_OPT, Compress => 1 };
60 }
61
62 my $do_get_all = sub {
63         my ($desc, $opt) = @_;
64         local $SIG{__DIE__} = sub { print STDERR $desc, ': ', @_; _exit(1) };
65         my $t0 = now();
66         my $dig = Digest::SHA->new(1);
67         my $mic = $imap_client->new(%$opt);
68         $mic->examine($mailbox) or die "examine: $!";
69         my $uid_base = 1;
70         my $bytes = 0;
71         my $nr = 0;
72         until ($uid_base > $uid_max) {
73                 my $end = $uid_base + $BATCH;
74                 my $ret = $mic->fetch_hash("$uid_base:$end", 'BODY[]') or last;
75                 for my $uid ($uid_base..$end) {
76                         $dig->add($uid);
77                         my $h = delete $ret->{$uid} or next;
78                         my $body = delete $h->{'BODY[]'} or
79                                                 die "no BODY[] for UID=$uid";
80                         $dig->add($body);
81                         $bytes += length($body);
82                         ++$nr;
83                 }
84                 $uid_base = $end + 1;
85         }
86         $mic->logout or die "logout failed: $!";
87         my $elapsed = sprintf('%0.3f', now() - $t0);
88         my $res = $dig->hexdigest;
89         print STDERR "# $desc $res (${elapsed}s) $bytes bytes, NR=$nr\n";
90         $res;
91 };
92
93 my (%pids, %res);
94 for (1..$REPEAT) {
95         while (my ($desc, $opt) = each %opts) {
96                 pipe(my ($r, $w)) or die;
97                 my $pid = fork;
98                 if ($pid == 0) {
99                         close $r or die;
100                         my $res = $do_get_all->($desc, $opt);
101                         print $w $res or die;
102                         close $w or die;
103                         _exit(0);
104                 }
105                 close $w or die;
106                 $pids{$pid} = [ $desc, $r ];
107         }
108 }
109
110 while (scalar keys %pids) {
111         my $pid = waitpid(-1, 0) or next;
112         my $child = delete $pids{$pid} or next;
113         my ($desc, $rpipe) = @$child;
114         is($?, 0, "$desc done");
115         my $sum = do { local $/; <$rpipe> };
116         push @{$res{$sum}}, $desc;
117 }
118 is(scalar keys %res, 1, 'all got the same result');
119 $td->kill;
120 $td->join;
121 is($?, 0, 'no error on -imapd exit');
122 done_testing;
123
124 BEGIN {
125
126 $make_local_server = sub {
127         require PublicInbox::Inbox;
128         $newsgroup = 'inbox.test';
129         my $ibx = { inboxdir => $inbox_dir, newsgroup => $newsgroup };
130         $ibx = PublicInbox::Inbox->new($ibx);
131         my $pi_config = "$tmpdir/config";
132         {
133                 open my $fh, '>', $pi_config or die "open($pi_config): $!";
134                 print $fh <<"" or die "print $pi_config: $!";
135 [publicinbox "test"]
136         newsgroup = $newsgroup
137         inboxdir = $inbox_dir
138         address = test\@example.com
139
140                 close $fh or die "close($pi_config): $!";
141         }
142         my ($out, $err) = ("$tmpdir/out", "$tmpdir/err");
143         for ($out, $err) {
144                 open my $fh, '>', $_ or die "truncate: $!";
145         }
146         my $imap = tcp_server();
147         my $rdr = { 3 => $imap };
148         $OPT{Server} = $imap->sockhost;
149         $OPT{Port} = $imap->sockport;
150
151         # not using multiple workers, here, since we want to increase
152         # the chance of tripping concurrency bugs within PublicInbox/IMAP*.pm
153         my $cmd = [ '-imapd', "--stdout=$out", "--stderr=$err", '-W0' ];
154         push @$cmd, '-limap://'.$imap->sockhost.':'.$imap->sockport;
155         if ($test_tls) {
156                 my $imaps = tcp_server();
157                 $rdr->{4} = $imaps;
158                 push @$cmd, '-limaps://'.$imaps->sockhost.':'.$imaps->sockport;
159                 push @$cmd, "--cert=$cert", "--key=$key";
160                 my $tls_opt = [
161                         SSL_hostname => 'server.local',
162                         SSL_verifycn_name => 'server.local',
163                         SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(),
164                         SSL_ca_file => 'certs/test-ca.pem',
165                 ];
166                 %STARTTLS_OPT = (%OPT, Starttls => $tls_opt);
167                 %IMAPS_OPT = (%OPT, Ssl => $tls_opt,
168                         Server => $imaps->sockhost,
169                         Port => $imaps->sockport
170                 );
171         }
172         print STDERR "# CMD ". join(' ', @$cmd). "\n";
173         my $env = { PI_CONFIG => $pi_config };
174         $td = start_script($cmd, $env, $rdr);
175 };
176 } # BEGIN