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