]> Sergey Matveev's repositories - public-inbox.git/blob - t/nntpd-validate.t
tests: recommend running create-certs.pl with $^X
[public-inbox.git] / t / nntpd-validate.t
1 # Copyright (C) 2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Integration test to validate compression.
5 use strict;
6 use warnings;
7 use File::Temp qw(tempdir);
8 use Test::More;
9 use Symbol qw(gensym);
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};
14
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 $@;
18 }
19
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)) {
22         eval "require $mod";
23         plan skip_all => "$mod missing for $0" if $@;
24 }
25
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';
30 }
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";
36 }
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);
41 my $batch = 1000;
42 END {
43         foreach ($pid, $tail_pid) {
44                 kill 'TERM', $_ if defined $_;
45         }
46 };
47 if (($ENV{NNTP_TEST_URL} // '') =~ m!\Anntp://([^/]+)/([^/]+)\z!) {
48         ($host_port, $group) = ($1, $2);
49         $host_port .= ":119" unless index($host_port, ':') > 0;
50 } else {
51         make_local_server();
52 }
53 my $test_article = $ENV{TEST_ARTICLE} // 0;
54 my $test_xover = $ENV{TEST_XOVER} // 1;
55
56 if ($test_tls) {
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');
61 }
62 if ($test_compress) {
63         my $nntp = Net::NNTP->new($host_port, %OPT);
64         ok($nntp->compress, 'COMPRESS works');
65         ok($nntp->quit, 'QUIT after compress OK');
66 }
67
68 sub do_get_all {
69         my ($methods) = @_;
70         my $desc = join(',', @$methods);
71         my $t0 = clock_gettime(CLOCK_MONOTONIC);
72         my $dig = Digest::SHA->new(1);
73         my $digfh = gensym;
74         my $tmpfh;
75         if ($keep_tmp) {
76                 open $tmpfh, '>', "$tmpdir/$desc.raw" or die $!;
77         }
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) {
83                 my $res = $nntp->$m;
84                 print STDERR "# $m got $res ($desc)\n" if !$res;
85         }
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";
90                 return undef;
91         }
92         my $i;
93         for ($i = $first; $i < $last; $i += $batch) {
94                 my $j = $i + $batch - 1;
95                 $j = $last if $j > $last;
96                 if ($test_xover) {
97                         my $xover = $nntp->xover("$i-$j");
98                         for my $n (sort { $a <=> $b } keys %$xover) {
99                                 my $line = join("\t", @{$xover->{$n}});
100                                 $line =~ tr/\r//d;
101                                 $dig->add("$n\t".$line);
102                         }
103                 }
104                 if ($test_article) {
105                         for my $n ($i..$j) {
106                                 $nntp->article($n, $digfh) and next;
107                                 next if $nntp->code == 423;
108                                 my $res = $nntp->code.' '.  $nntp->message;
109
110                                 $res =~ tr/\r\n//d;
111                                 print STDERR "# Article $n ($desc): $res\n";
112                         }
113                 }
114         }
115
116         # hacky bytes_read thing added to Net::NNTP for testing:
117         my $bytes_read = '';
118         if ($nntp->can('bytes_read')) {
119                 $bytes_read .= ' '.$nntp->bytes_read.'b';
120         }
121         my $q = $nntp->quit;
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";
126         $res;
127 }
128 my @tests = ([]);
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);
133 for my $m (@tests) {
134         my $key = join(',', @$m);
135         push @keys, $key;
136         diag "$key start";
137         $thr{$key} = threads->create(\&do_get_all, $m);
138 }
139
140 $res{$_} = $thr{$_}->join for @keys;
141 my $plain = $res{''};
142 ok($plain, "plain got $plain");
143 is($res{$_}, $plain, "$_ matches '' result") for @keys;
144
145 done_testing();
146
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";
154         {
155                 open my $fh, '>', $pi_config or die "open($pi_config): $!";
156                 print $fh <<"" or die "print $pi_config: $!";
157 [publicinbox "test"]
158         newsgroup = $group
159         mainrepo = $inbox_dir
160         address = test\@example.com
161
162                 close $fh or die "close($pi_config): $!";
163         }
164         my ($out, $err) = ("$tmpdir/out", "$tmpdir/err");
165         for ($out, $err) {
166                 open my $fh, '>', $_ or die "truncate: $!";
167         }
168         if (my $tail_cmd = $ENV{TAIL}) { # don't assume GNU tail
169                 $tail_pid = fork;
170                 if (defined $tail_pid && $tail_pid == 0) {
171                         open STDOUT, '>&STDERR' or die ">&2 failed: $!";
172                         exec(split(' ', $tail_cmd), $out, $err);
173                 }
174         }
175         my $sock = tcp_server();
176         ok($sock, 'sock created');
177         $host_port = $sock->sockhost . ':' . $sock->sockport;
178
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";
183         if ($test_tls) {
184                 push @$cmd, "--cert=$cert", "--key=$key";
185                 %OPT = (
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',
190                 );
191         }
192         print STDERR "# CMD ". join(' ', @$cmd). "\n";
193         $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [$sock]);
194 }
195
196 package DigestPipe;
197 use strict;
198 use warnings;
199
200 sub TIEHANDLE {
201         my ($class, $self) = @_;
202         bless $self, $class;
203 }
204
205 sub PRINT {
206         my $self = shift;
207         my $data = join('', @_);
208         # Net::NNTP emit different line-endings depending on TLS or not...:
209         $data =~ tr/\r//d;
210         $self->{dig}->add($data);
211         if (my $tmpfh = $self->{tmpfh}) {
212                 print $tmpfh $data;
213         }
214         1;
215 }
216 1;