2 # Copyright (C) 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.
8 use PublicInbox::DS qw(now);
11 use PublicInbox::TestCommon;
12 my $inbox_dir = $ENV{GIANT_INBOX_DIR};
13 plan skip_all => "GIANT_INBOX_DIR not defined for $0" unless $inbox_dir;
14 # how many emails to read into memory at once per-process
15 my $BATCH = $ENV{TEST_BATCH} // 100;
16 my $REPEAT = $ENV{TEST_REPEAT} // 1;
17 diag "TEST_BATCH=$BATCH TEST_REPEAT=$REPEAT";
19 require_mods(qw(Mail::IMAPClient -imapd));
20 my $imap_client = 'Mail::IMAPClient';
21 my $can_compress = $imap_client->can('compress');
22 if ($can_compress) { # hope this gets fixed upstream, soon
23 require PublicInbox::IMAPClient;
24 $imap_client = 'PublicInbox::IMAPClient';
27 my $test_tls = $ENV{TEST_SKIP_TLS} ? 0 : eval { require IO::Socket::SSL };
28 my ($cert, $key) = qw(certs/server-cert.pem certs/server-key.pem);
29 if ($test_tls && !-r $key || !-r $cert) {
31 "certs/ missing for $0, run $^X ./certs/create-certs.perl";
33 my ($tmpdir, $for_destroy) = tmpdir();
34 my %OPT = qw(User u Password p);
35 my (%STARTTLS_OPT, %IMAPS_OPT, $td, $newsgroup, $mailbox, $make_local_server);
36 if (($ENV{IMAP_TEST_URL} // '') =~ m!\Aimap://([^/]+)/(.+)\z!) {
37 ($OPT{Server}, $mailbox) = ($1, $2);
38 $OPT{Server} =~ s/:([0-9]+)\z// and $OPT{Port} = $1 + 0;
40 %IMAPS_OPT = (%OPT, Port => 993) if $OPT{Port} == 143;
42 require_mods(qw(DBD::SQLite));
43 $make_local_server->();
44 $mailbox = "$newsgroup.0";
47 my %opts = (imap => \%OPT, 'imap+compress' => { %OPT, Compress => 1 });
49 my $mic = $imap_client->new(%OPT) or BAIL_OUT "new $!";
50 $mic->examine($mailbox) or BAIL_OUT "examine: $!";
51 my $next = $mic->uidnext($mailbox) or BAIL_OUT "uidnext: $!";
55 if (scalar keys %STARTTLS_OPT) {
56 $opts{starttls} = \%STARTTLS_OPT;
57 $opts{'starttls+compress'} = { %STARTTLS_OPT, Compress => 1 };
59 if (scalar keys %IMAPS_OPT) {
60 $opts{imaps} = \%IMAPS_OPT;
61 $opts{'imaps+compress'} = { %IMAPS_OPT, Compress => 1 };
64 my $do_get_all = sub {
65 my ($desc, $opt) = @_;
66 local $SIG{__DIE__} = sub { print STDERR $desc, ': ', @_; _exit(1) };
68 my $dig = PublicInbox::SHA->new(1);
69 my $mic = $imap_client->new(%$opt);
70 $mic->examine($mailbox) or die "examine: $!";
74 until ($uid_base > $uid_max) {
75 my $end = $uid_base + $BATCH;
76 my $ret = $mic->fetch_hash("$uid_base:$end", 'BODY[]') or last;
77 for my $uid ($uid_base..$end) {
79 my $h = delete $ret->{$uid} or next;
80 my $body = delete $h->{'BODY[]'} or
81 die "no BODY[] for UID=$uid";
83 $bytes += length($body);
88 $mic->logout or die "logout failed: $!";
89 my $elapsed = sprintf('%0.3f', now() - $t0);
90 my $res = $dig->hexdigest;
91 print STDERR "# $desc $res (${elapsed}s) $bytes bytes, NR=$nr\n";
97 while (my ($desc, $opt) = each %opts) {
98 pipe(my ($r, $w)) or die;
102 my $res = $do_get_all->($desc, $opt);
103 print $w $res or die;
108 $pids{$pid} = [ $desc, $r ];
112 while (scalar keys %pids) {
113 my $pid = waitpid(-1, 0) or next;
114 my $child = delete $pids{$pid} or next;
115 my ($desc, $rpipe) = @$child;
116 is($?, 0, "$desc done");
117 my $sum = do { local $/; <$rpipe> };
118 push @{$res{$sum}}, $desc;
120 is(scalar keys %res, 1, 'all got the same result');
123 is($?, 0, 'no error on -imapd exit');
128 $make_local_server = sub {
129 require PublicInbox::Inbox;
130 $newsgroup = 'inbox.test';
131 my $ibx = { inboxdir => $inbox_dir, newsgroup => $newsgroup };
132 $ibx = PublicInbox::Inbox->new($ibx);
133 my $pi_config = "$tmpdir/config";
135 open my $fh, '>', $pi_config or die "open($pi_config): $!";
136 print $fh <<"" or die "print $pi_config: $!";
138 newsgroup = $newsgroup
139 inboxdir = $inbox_dir
140 address = test\@example.com
142 close $fh or die "close($pi_config): $!";
144 my ($out, $err) = ("$tmpdir/out", "$tmpdir/err");
146 open my $fh, '>', $_ or die "truncate: $!";
148 my $imap = tcp_server();
149 my $rdr = { 3 => $imap };
150 $OPT{Server} = $imap->sockhost;
151 $OPT{Port} = $imap->sockport;
153 # not using multiple workers, here, since we want to increase
154 # the chance of tripping concurrency bugs within PublicInbox/IMAP*.pm
155 my $cmd = [ '-imapd', "--stdout=$out", "--stderr=$err", '-W0' ];
156 push @$cmd, '-limap://'.tcp_host_port($imap);
158 my $imaps = tcp_server();
160 push @$cmd, '-limaps://'.tcp_host_port($imaps);
161 push @$cmd, "--cert=$cert", "--key=$key";
163 SSL_hostname => 'server.local',
164 SSL_verifycn_name => 'server.local',
165 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(),
166 SSL_ca_file => 'certs/test-ca.pem',
168 %STARTTLS_OPT = (%OPT, Starttls => $tls_opt);
169 %IMAPS_OPT = (%OPT, Ssl => $tls_opt,
170 Server => $imaps->sockhost,
171 Port => $imaps->sockport
174 print STDERR "# CMD ". join(' ', @$cmd). "\n";
175 my $env = { PI_CONFIG => $pi_config };
176 $td = start_script($cmd, $env, $rdr);