1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Integration test to validate compression.
7 use File::Temp qw(tempdir);
10 use Time::HiRes qw(clock_gettime CLOCK_MONOTONIC);
11 my $inbox_dir = $ENV{GIANT_INBOX_DIR};
12 plan skip_all => "GIANT_INBOX_DIR not defined for $0" unless $inbox_dir;
13 my $mid = $ENV{TEST_MID};
15 # This test is also an excuse for me to experiment with Perl threads :P
16 unless (eval 'use threads; 1') {
17 plan skip_all => "$0 requires a threaded perl" if $@;
20 # Net::NNTP is part of the standard library, but distros may split it off...
21 foreach my $mod (qw(DBD::SQLite Net::NNTP Compress::Raw::Zlib)) {
23 plan skip_all => "$mod missing for $0" if $@;
26 my $test_compress = Net::NNTP->can('compress');
27 if (!$test_compress) {
28 diag 'Your Net::NNTP does not yet support compression';
29 diag 'See: https://rt.cpan.org/Ticket/Display.html?id=129967';
31 my $test_tls = $ENV{TEST_SKIP_TLS} ? 0 : eval { require IO::Socket::SSL };
32 my $cert = 'certs/server-cert.pem';
33 my $key = 'certs/server-key.pem';
34 if ($test_tls && !-r $key || !-r $cert) {
35 plan skip_all => "certs/ missing for $0, run $^X ./certs/create-certs.perl";
37 require './t/common.perl';
38 my $keep_tmp = !!$ENV{TEST_KEEP_TMP};
39 my $tmpdir = tempdir('nntpd-validate-XXXXXX',TMPDIR => 1,CLEANUP => $keep_tmp);
40 my (%OPT, $pid, $tail_pid, $host_port, $group);
43 foreach ($pid, $tail_pid) {
44 kill 'TERM', $_ if defined $_;
47 if (($ENV{NNTP_TEST_URL} // '') =~ m!\Anntp://([^/]+)/([^/]+)\z!) {
48 ($host_port, $group) = ($1, $2);
49 $host_port .= ":119" unless index($host_port, ':') > 0;
53 my $test_article = $ENV{TEST_ARTICLE} // 0;
54 my $test_xover = $ENV{TEST_XOVER} // 1;
57 my $nntp = Net::NNTP->new($host_port, %OPT);
58 ok($nntp->starttls, 'STARTTLS works');
59 ok($nntp->compress, 'COMPRESS works') if $test_compress;
60 ok($nntp->quit, 'QUIT after starttls OK');
63 my $nntp = Net::NNTP->new($host_port, %OPT);
64 ok($nntp->compress, 'COMPRESS works');
65 ok($nntp->quit, 'QUIT after compress OK');
70 my $desc = join(',', @$methods);
71 my $t0 = clock_gettime(CLOCK_MONOTONIC);
72 my $dig = Digest::SHA->new(1);
76 open $tmpfh, '>', "$tmpdir/$desc.raw" or die $!;
78 my $tmp = { dig => $dig, tmpfh => $tmpfh };
79 tie *$digfh, 'DigestPipe', $tmp;
80 my $nntp = Net::NNTP->new($host_port, %OPT);
81 $nntp->article("<$mid>", $digfh) if $mid;
82 foreach my $m (@$methods) {
84 print STDERR "# $m got $res ($desc)\n" if !$res;
86 $nntp->article("<$mid>", $digfh) if $mid;
87 my ($num, $first, $last) = $nntp->group($group);
88 unless (defined $num && defined $first && defined $last) {
89 warn "Invalid group\n";
93 for ($i = $first; $i < $last; $i += $batch) {
94 my $j = $i + $batch - 1;
95 $j = $last if $j > $last;
97 my $xover = $nntp->xover("$i-$j");
98 for my $n (sort { $a <=> $b } keys %$xover) {
99 my $line = join("\t", @{$xover->{$n}});
101 $dig->add("$n\t".$line);
106 $nntp->article($n, $digfh) and next;
107 next if $nntp->code == 423;
108 my $res = $nntp->code.' '. $nntp->message;
111 print STDERR "# Article $n ($desc): $res\n";
116 # hacky bytes_read thing added to Net::NNTP for testing:
118 if ($nntp->can('bytes_read')) {
119 $bytes_read .= ' '.$nntp->bytes_read.'b';
122 print STDERR "# quit failed: ".$nntp->code."\n" if !$q;
123 my $elapsed = sprintf('%0.3f', clock_gettime(CLOCK_MONOTONIC) - $t0);
124 my $res = $dig->hexdigest;
125 print STDERR "# $desc - $res (${elapsed}s)$bytes_read\n";
129 push @tests, [ 'compress' ] if $test_compress;
130 push @tests, [ 'starttls' ] if $test_tls;
131 push @tests, [ 'starttls', 'compress' ] if $test_tls && $test_compress;
132 my (@keys, %thr, %res);
134 my $key = join(',', @$m);
137 $thr{$key} = threads->create(\&do_get_all, $m);
140 $res{$_} = $thr{$_}->join for @keys;
141 my $plain = $res{''};
142 ok($plain, "plain got $plain");
143 is($res{$_}, $plain, "$_ matches '' result") for @keys;
147 sub make_local_server {
148 require PublicInbox::Inbox;
149 $group = 'inbox.test.perf.nntpd';
150 my $ibx = { mainrepo => $inbox_dir, newsgroup => $group };
151 $ibx = PublicInbox::Inbox->new($ibx);
152 my $nntpd = 'blib/script/public-inbox-nntpd';
153 my $pi_config = "$tmpdir/config";
155 open my $fh, '>', $pi_config or die "open($pi_config): $!";
156 print $fh <<"" or die "print $pi_config: $!";
159 mainrepo = $inbox_dir
160 address = test\@example.com
162 close $fh or die "close($pi_config): $!";
164 my ($out, $err) = ("$tmpdir/out", "$tmpdir/err");
166 open my $fh, '>', $_ or die "truncate: $!";
168 if (my $tail_cmd = $ENV{TAIL}) { # don't assume GNU tail
170 if (defined $tail_pid && $tail_pid == 0) {
171 open STDOUT, '>&STDERR' or die ">&2 failed: $!";
172 exec(split(' ', $tail_cmd), $out, $err);
175 my $sock = tcp_server();
176 ok($sock, 'sock created');
177 $host_port = $sock->sockhost . ':' . $sock->sockport;
179 # not using multiple workers, here, since we want to increase
180 # the chance of tripping concurrency bugs within PublicInbox/NNTP*.pm
181 my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err", '-W0' ];
182 push @$cmd, "-lnntp://$host_port";
184 push @$cmd, "--cert=$cert", "--key=$key";
186 SSL_hostname => 'server.local',
187 SSL_verifycn_name => 'server.local',
188 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(),
189 SSL_ca_file => 'certs/test-ca.pem',
192 print STDERR "# CMD ". join(' ', @$cmd). "\n";
193 $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [$sock]);
201 my ($class, $self) = @_;
207 my $data = join('', @_);
208 # Net::NNTP emit different line-endings depending on TLS or not...:
210 $self->{dig}->add($data);
211 if (my $tmpfh = $self->{tmpfh}) {