]> Sergey Matveev's repositories - public-inbox.git/blob - xt/imapd-validate.t
f96ec8791b925eab85e9f5dbd29272f82a051796
[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, $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 }
43
44 my %opts = (imap => \%OPT, 'imap+compress' => { %OPT, Compress => 1 });
45 my $uid_max = do {
46         my $mic = $imap_client->new(%OPT) or BAIL_OUT "new $!";
47         $mic->examine($mailbox) or BAIL_OUT "examine: $!";
48         my $next = $mic->uidnext($mailbox) or BAIL_OUT "uidnext: $!";
49         $next - 1;
50 };
51
52 if (scalar keys %STARTTLS_OPT) {
53         $opts{starttls} = \%STARTTLS_OPT;
54         $opts{'starttls+compress'} = { %STARTTLS_OPT, Compress => 1 };
55 }
56 if (scalar keys %IMAPS_OPT) {
57         $opts{imaps} = \%IMAPS_OPT;
58         $opts{'imaps+compress'} = { %IMAPS_OPT, Compress => 1 };
59 }
60
61 my $do_get_all = sub {
62         my ($desc, $opt) = @_;
63         local $SIG{__DIE__} = sub { print STDERR $desc, ': ', @_; _exit(1) };
64         my $t0 = now();
65         my $dig = Digest::SHA->new(1);
66         my $mic = $imap_client->new(%$opt);
67         $mic->examine($mailbox) or die "examine: $!";
68         my $uid_base = 1;
69         my $bytes = 0;
70         my $nr = 0;
71         until ($uid_base > $uid_max) {
72                 my $end = $uid_base + $BATCH;
73                 my $ret = $mic->fetch_hash("$uid_base:$end", 'BODY[]') or last;
74                 for my $uid ($uid_base..$end) {
75                         $dig->add($uid);
76                         my $h = delete $ret->{$uid} or next;
77                         my $body = delete $h->{'BODY[]'} or
78                                                 die "no BODY[] for UID=$uid";
79                         $dig->add($body);
80                         $bytes += length($body);
81                         ++$nr;
82                 }
83                 $uid_base = $end + 1;
84         }
85         $mic->logout or die "logout failed: $!";
86         my $elapsed = sprintf('%0.3f', now() - $t0);
87         my $res = $dig->hexdigest;
88         print STDERR "# $desc $res (${elapsed}s) $bytes bytes, NR=$nr\n";
89         $res;
90 };
91
92 my (%pids, %res);
93 for (1..$REPEAT) {
94         while (my ($desc, $opt) = each %opts) {
95                 pipe(my ($r, $w)) or die;
96                 my $pid = fork;
97                 if ($pid == 0) {
98                         close $r or die;
99                         my $res = $do_get_all->($desc, $opt);
100                         print $w $res or die;
101                         close $w or die;
102                         _exit(0);
103                 }
104                 close $w or die;
105                 $pids{$pid} = [ $desc, $r ];
106         }
107 }
108
109 while (scalar keys %pids) {
110         my $pid = waitpid(-1, 0) or next;
111         my $child = delete $pids{$pid} or next;
112         my ($desc, $rpipe) = @$child;
113         is($?, 0, "$desc done");
114         my $sum = do { local $/; <$rpipe> };
115         push @{$res{$sum}}, $desc;
116 }
117 is(scalar keys %res, 1, 'all got the same result');
118 $td->kill;
119 $td->join;
120 is($?, 0, 'no error on -imapd exit');
121 done_testing;
122
123 BEGIN {
124
125 $make_local_server = sub {
126         require PublicInbox::Inbox;
127         $mailbox = 'inbox.test';
128         my $ibx = { inboxdir => $inbox_dir, newsgroup => $mailbox };
129         $ibx = PublicInbox::Inbox->new($ibx);
130         my $pi_config = "$tmpdir/config";
131         {
132                 open my $fh, '>', $pi_config or die "open($pi_config): $!";
133                 print $fh <<"" or die "print $pi_config: $!";
134 [publicinbox "test"]
135         newsgroup = $mailbox
136         inboxdir = $inbox_dir
137         address = test\@example.com
138
139                 close $fh or die "close($pi_config): $!";
140         }
141         my ($out, $err) = ("$tmpdir/out", "$tmpdir/err");
142         for ($out, $err) {
143                 open my $fh, '>', $_ or die "truncate: $!";
144         }
145         my $imap = tcp_server();
146         my $rdr = { 3 => $imap };
147         $OPT{Server} = $imap->sockhost;
148         $OPT{Port} = $imap->sockport;
149
150         # not using multiple workers, here, since we want to increase
151         # the chance of tripping concurrency bugs within PublicInbox/IMAP*.pm
152         my $cmd = [ '-imapd', "--stdout=$out", "--stderr=$err", '-W0' ];
153         push @$cmd, '-limap://'.$imap->sockhost.':'.$imap->sockport;
154         if ($test_tls) {
155                 my $imaps = tcp_server();
156                 $rdr->{4} = $imaps;
157                 push @$cmd, '-limaps://'.$imaps->sockhost.':'.$imaps->sockport;
158                 push @$cmd, "--cert=$cert", "--key=$key";
159                 my $tls_opt = [
160                         SSL_hostname => 'server.local',
161                         SSL_verifycn_name => 'server.local',
162                         SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(),
163                         SSL_ca_file => 'certs/test-ca.pem',
164                 ];
165                 %STARTTLS_OPT = (%OPT, Starttls => $tls_opt);
166                 %IMAPS_OPT = (%OPT, Ssl => $tls_opt,
167                         Server => $imaps->sockhost,
168                         Port => $imaps->sockport
169                 );
170         }
171         print STDERR "# CMD ". join(' ', @$cmd). "\n";
172         my $env = { PI_CONFIG => $pi_config };
173         $td = start_script($cmd, $env, $rdr);
174 };
175 } # BEGIN