]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-corner.t
t/mda: switch to run_script for testing
[public-inbox.git] / t / httpd-corner.t
1 # Copyright (C) 2016-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # note: our HTTP server should be standalone and capable of running
4 # generic PSGI/Plack apps.
5 use strict;
6 use warnings;
7 use Test::More;
8 use Time::HiRes qw(gettimeofday tv_interval);
9 use PublicInbox::Spawn qw(which spawn);
10
11 foreach my $mod (qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)) {
12         eval "require $mod";
13         plan skip_all => "$mod missing for httpd-corner.t" if $@;
14 }
15
16 use Digest::SHA qw(sha1_hex);
17 use File::Temp qw/tempdir/;
18 use IO::Socket;
19 use IO::Socket::UNIX;
20 use Fcntl qw(:seek);
21 use Socket qw(IPPROTO_TCP TCP_NODELAY SOL_SOCKET);
22 use POSIX qw(mkfifo);
23 require './t/common.perl';
24 my $tmpdir = tempdir('httpd-corner-XXXXXX', TMPDIR => 1, CLEANUP => 1);
25 my $fifo = "$tmpdir/fifo";
26 ok(defined mkfifo($fifo, 0777), 'created FIFO');
27 my $err = "$tmpdir/stderr.log";
28 my $out = "$tmpdir/stdout.log";
29 my $httpd = 'blib/script/public-inbox-httpd';
30 my $psgi = "./t/httpd-corner.psgi";
31 my $sock = tcp_server() or die;
32
33 # make sure stdin is not a pipe for lsof test to check for leaking pipes
34 open(STDIN, '<', '/dev/null') or die 'no /dev/null: $!';
35
36 # Make sure we don't clobber socket options set by systemd or similar
37 # using socket activation:
38 my ($defer_accept_val, $accf_arg);
39 if ($^O eq 'linux') {
40         setsockopt($sock, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT(), 5) or die;
41         my $x = getsockopt($sock, IPPROTO_TCP, Socket::TCP_DEFER_ACCEPT());
42         defined $x or die "getsockopt: $!";
43         $defer_accept_val = unpack('i', $x);
44         if ($defer_accept_val <= 0) {
45                 die "unexpected TCP_DEFER_ACCEPT value: $defer_accept_val";
46         }
47 } elsif ($^O eq 'freebsd' && system('kldstat -m accf_data >/dev/null') == 0) {
48         require PublicInbox::Daemon;
49         my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
50         $accf_arg = pack('a16a240', 'dataready', '');
51         setsockopt($sock, SOL_SOCKET, $var, $accf_arg) or die "setsockopt: $!";
52 }
53
54 sub unix_server ($) {
55         my $s = IO::Socket::UNIX->new(
56                 Listen => 1024,
57                 Type => Socket::SOCK_STREAM(),
58                 Local => $_[0],
59         );
60         $s->blocking(0);
61         $s;
62 }
63
64 my $upath = "$tmpdir/s";
65 my $unix = unix_server($upath);
66 ok($unix, 'UNIX socket created');
67 my $pid;
68 END { kill 'TERM', $pid if defined $pid };
69 my $spawn_httpd = sub {
70         my (@args) = @_;
71         my $cmd = [ $httpd, @args, "--stdout=$out", "--stderr=$err", $psgi ];
72         $pid = spawn_listener(undef, $cmd, [ $sock, $unix ]);
73         ok(defined $pid, 'forked httpd process successfully');
74 };
75
76 $spawn_httpd->();
77 if ('test worker death') {
78         my $conn = conn_for($sock, 'killed worker');
79         $conn->write("GET /pid HTTP/1.0\r\n\r\n");
80         ok($conn->read(my $buf, 8192), 'read response');
81         my ($head, $body) = split(/\r\n\r\n/, $buf);
82         like($body, qr/\A[0-9]+\z/, '/pid response');
83         my $pid = $body;
84         is(kill('KILL', $pid), 1, 'killed worker');
85
86         $conn = conn_for($sock, 'respawned worker');
87         $conn->write("GET /pid HTTP/1.0\r\n\r\n");
88         ok($conn->read($buf, 8192), 'read response');
89         ($head, $body) = split(/\r\n\r\n/, $buf);
90         like($body, qr/\A[0-9]+\z/, '/pid response');
91         isnt($body, $pid, 'respawned worker');
92 }
93
94 {
95         my $conn = conn_for($sock, 'streaming callback');
96         $conn->write("GET /callback HTTP/1.0\r\n\r\n");
97         ok($conn->read(my $buf, 8192), 'read response');
98         my ($head, $body) = split(/\r\n\r\n/, $buf);
99         is($body, "hello world\n", 'callback body matches expected');
100 }
101
102 {
103         my $conn = conn_for($sock, 'getline-die');
104         $conn->write("GET /getline-die HTTP/1.1\r\nHost: example.com\r\n\r\n");
105         ok($conn->read(my $buf, 8192), 'read some response');
106         like($buf, qr!HTTP/1\.1 200\b[^\r]*\r\n!, 'got some sort of header');
107         is($conn->read(my $nil, 8192), 0, 'read EOF');
108         $conn = undef;
109         my $after = capture($err);
110         is(scalar(grep(/GETLINE FAIL/, @$after)), 1, 'failure logged');
111         is(scalar(grep(/CLOSE FAIL/, @$after)), 1, 'body->close not called');
112 }
113
114 {
115         my $conn = conn_for($sock, 'close-die');
116         $conn->write("GET /close-die HTTP/1.1\r\nHost: example.com\r\n\r\n");
117         ok($conn->read(my $buf, 8192), 'read some response');
118         like($buf, qr!HTTP/1\.1 200\b[^\r]*\r\n!, 'got some sort of header');
119         is($conn->read(my $nil, 8192), 0, 'read EOF');
120         $conn = undef;
121         my $after = capture($err);
122         is(scalar(grep(/GETLINE FAIL/, @$after)), 0, 'getline not failed');
123         is(scalar(grep(/CLOSE FAIL/, @$after)), 1, 'body->close not called');
124 }
125
126 SKIP: {
127         my $conn = conn_for($sock, 'excessive header');
128         $SIG{PIPE} = 'IGNORE';
129         $conn->write("GET /callback HTTP/1.0\r\n");
130         foreach my $i (1..500000) {
131                 last unless $conn->write("X-xxxxxJunk-$i: omg\r\n");
132         }
133         ok(!$conn->write("\r\n"), 'broken request');
134         ok($conn->read(my $buf, 8192), 'read response');
135         my ($head, $body) = split(/\r\n\r\n/, $buf);
136         like($head, qr/\b400\b/, 'got 400 response');
137 }
138
139 {
140         my $conn = conn_for($sock, 'excessive body Content-Length');
141         $SIG{PIPE} = 'IGNORE';
142         my $n = (10 * 1024 * 1024) + 1;
143         $conn->write("PUT /sha1 HTTP/1.0\r\nContent-Length: $n\r\n\r\n");
144         ok($conn->read(my $buf, 8192), 'read response');
145         my ($head, $body) = split(/\r\n\r\n/, $buf);
146         like($head, qr/\b413\b/, 'got 413 response');
147 }
148
149 {
150         my $conn = conn_for($sock, 'excessive body chunked');
151         $SIG{PIPE} = 'IGNORE';
152         my $n = (10 * 1024 * 1024) + 1;
153         $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding: chunked\r\n");
154         $conn->write("\r\n".sprintf("%x\r\n", $n));
155         ok($conn->read(my $buf, 8192), 'read response');
156         my ($head, $body) = split(/\r\n\r\n/, $buf);
157         like($head, qr/\b413\b/, 'got 413 response');
158 }
159
160 {
161         my $conn = conn_for($sock, 'chunk with pipeline');
162         my $n = 10;
163         my $payload = 'b'x$n;
164         $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding: chunked\r\n");
165         $conn->write("\r\n".sprintf("%x\r\n", $n));
166         $conn->write($payload . "\r\n0\r\n\r\nGET /empty HTTP/1.0\r\n\r\n");
167         $conn->read(my $buf, 4096);
168         my $lim = 0;
169         $lim++ while ($conn->read($buf, 4096, length($buf)) && $lim < 9);
170         my $exp = sha1_hex($payload);
171         like($buf, qr!\r\n\r\n${exp}HTTP/1\.0 200 OK\r\n!s,
172                 'chunk parser can handled pipelined requests');
173 }
174
175 # Unix domain sockets
176 {
177         my $u = IO::Socket::UNIX->new(Type => SOCK_STREAM, Peer => $upath);
178         ok($u, 'unix socket connected');
179         $u->write("GET /host-port HTTP/1.0\r\n\r\n");
180         $u->read(my $buf, 4096);
181         like($buf, qr!\r\n\r\n127\.0\.0\.1:0\z!,
182                 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
183 }
184
185 sub conn_for {
186         my ($dest, $msg) = @_;
187         my $conn = tcp_connect($dest);
188         ok($conn, "connected for $msg");
189         setsockopt($conn, IPPROTO_TCP, TCP_NODELAY, 1);
190         return $conn;
191 }
192
193 {
194         my $conn = conn_for($sock, 'host-port');
195         $conn->write("GET /host-port HTTP/1.0\r\n\r\n");
196         $conn->read(my $buf, 4096);
197         my ($head, $body) = split(/\r\n\r\n/, $buf);
198         my ($addr, $port) = split(/:/, $body);
199         is($addr, $conn->sockhost, 'host matches addr');
200         is($port, $conn->sockport, 'port matches');
201 }
202
203 # graceful termination
204 {
205         my $conn = conn_for($sock, 'graceful termination via slow header');
206         $conn->write("GET /slow-header HTTP/1.0\r\n" .
207                         "X-Check-Fifo: $fifo\r\n\r\n");
208         open my $f, '>', $fifo or die "open $fifo: $!\n";
209         $f->autoflush(1);
210         ok(print($f "hello\n"), 'wrote something to fifo');
211         my $kpid = $pid;
212         $pid = undef;
213         is(kill('TERM', $kpid), 1, 'started graceful shutdown');
214         ok(print($f "world\n"), 'wrote else to fifo');
215         close $f or die "close fifo: $!\n";
216         $conn->read(my $buf, 8192);
217         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
218         like($head, qr!\AHTTP/1\.[01] 200 OK!, 'got 200 for slow-header');
219         is($body, "hello\nworld\n", 'read expected body');
220         is(waitpid($kpid, 0), $kpid, 'reaped httpd');
221         is($?, 0, 'no error');
222         $spawn_httpd->('-W0');
223 }
224
225 {
226         my $conn = conn_for($sock, 'graceful termination via slow-body');
227         $conn->write("GET /slow-body HTTP/1.0\r\n" .
228                         "X-Check-Fifo: $fifo\r\n\r\n");
229         open my $f, '>', $fifo or die "open $fifo: $!\n";
230         $f->autoflush(1);
231         my $buf;
232         $conn->sysread($buf, 8192);
233         like($buf, qr!\AHTTP/1\.[01] 200 OK!, 'got 200 for slow-body');
234         like($buf, qr!\r\n\r\n!, 'finished HTTP response header');
235
236         foreach my $c ('a'..'c') {
237                 $c .= "\n";
238                 ok(print($f $c), 'wrote line to fifo');
239                 $conn->sysread($buf, 8192);
240                 is($buf, $c, 'got trickle for reading');
241         }
242         my $kpid = $pid;
243         $pid = undef;
244         is(kill('TERM', $kpid), 1, 'started graceful shutdown');
245         ok(print($f "world\n"), 'wrote else to fifo');
246         close $f or die "close fifo: $!\n";
247         $conn->sysread($buf, 8192);
248         is($buf, "world\n", 'read expected body');
249         is($conn->sysread($buf, 8192), 0, 'got EOF from server');
250         is(waitpid($kpid, 0), $kpid, 'reaped httpd');
251         is($?, 0, 'no error');
252         $spawn_httpd->('-W0');
253 }
254
255 sub delay { select(undef, undef, undef, shift || rand(0.02)) }
256
257 my $str = 'abcdefghijklmnopqrstuvwxyz';
258 my $len = length $str;
259 is($len, 26, 'got the alphabet');
260 my $check_self = sub {
261         my ($conn) = @_;
262         $conn->read(my $buf, 4096);
263         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
264         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
265         is($body, sha1_hex($str), 'read expected body');
266 };
267
268 SKIP: {
269         which('curl') or skip('curl(1) missing', 4);
270         my $base = 'http://' . $sock->sockhost . ':' . $sock->sockport;
271         my $url = "$base/sha1";
272         my ($r, $w);
273         pipe($r, $w) or die "pipe: $!";
274         my $cmd = [qw(curl --tcp-nodelay --no-buffer -T- -HExpect: -sS), $url];
275         open my $cout, '+>', undef or die;
276         open my $cerr, '>', undef or die;
277         my $rdr = { 0 => fileno($r), 1 => fileno($cout), 2 => fileno($cerr) };
278         my $pid = spawn($cmd, undef, $rdr);
279         close $r or die "close read pipe: $!";
280         foreach my $c ('a'..'z') {
281                 print $w $c or die "failed to write to curl: $!";
282                 delay();
283         }
284         close $w or die "close write pipe: $!";
285         waitpid($pid, 0);
286         is($?, 0, 'curl exited successfully');
287         is(-s $cerr, 0, 'no errors from curl');
288         $cout->seek(0, SEEK_SET);
289         is(<$cout>, sha1_hex($str), 'read expected body');
290
291         open my $fh, '-|', qw(curl -sS), "$base/async-big" or die $!;
292         my $n = 0;
293         my $non_zero = 0;
294         while (1) {
295                 my $r = sysread($fh, my $buf, 4096) or last;
296                 $n += $r;
297                 $buf =~ /\A\0+\z/ or $non_zero++;
298         }
299         close $fh or die "curl errored out \$?=$?";
300         is($n, 30 * 1024 * 1024, 'got expected output from curl');
301         is($non_zero, 0, 'read all zeros');
302 }
303
304 {
305         my $conn = conn_for($sock, '1.1 pipeline together');
306         $conn->write("PUT /sha1 HTTP/1.1\r\nUser-agent: hello\r\n\r\n" .
307                         "PUT /sha1 HTTP/1.1\r\n\r\n");
308         my $buf = '';
309         my @r;
310         until (scalar(@r) >= 2) {
311                 my $r = $conn->sysread(my $tmp, 4096);
312                 die $! unless defined $r;
313                 die "EOF <$buf>" unless $r;
314                 $buf .= $tmp;
315                 @r = ($buf =~ /\r\n\r\n([a-f0-9]{40})/g);
316         }
317         is(2, scalar @r, 'got 2 responses');
318         my $i = 3;
319         foreach my $hex (@r) {
320                 is($hex, sha1_hex(''), "read expected body $i");
321                 $i++;
322         }
323 }
324
325 {
326         my $conn = conn_for($sock, 'no TCP_CORK on empty body');
327         $conn->write("GET /empty HTTP/1.1\r\nHost:example.com\r\n\r\n");
328         my $buf = '';
329         my $t0 = [ gettimeofday ];
330         until ($buf =~ /\r\n\r\n/s) {
331                 $conn->sysread($buf, 4096, length($buf));
332         }
333         my $elapsed = tv_interval($t0, [ gettimeofday ]);
334         ok($elapsed < 0.190, 'no 200ms TCP cork delay on empty body');
335 }
336
337 {
338         my $conn = conn_for($sock, 'graceful termination during slow request');
339         $conn->write("PUT /sha1 HTTP/1.0\r\n");
340         delay();
341         $conn->write("Content-Length: $len\r\n");
342         delay();
343         $conn->write("\r\n");
344         my $kpid = $pid;
345         $pid = undef;
346         is(kill('TERM', $kpid), 1, 'started graceful shutdown');
347         delay();
348         my $n = 0;
349         foreach my $c ('a'..'z') {
350                 $n += $conn->write($c);
351         }
352         is($n, $len, 'wrote alphabet');
353         $check_self->($conn);
354         is(waitpid($kpid, 0), $kpid, 'reaped httpd');
355         is($?, 0, 'no error');
356         $spawn_httpd->('-W0');
357 }
358
359 # various DoS attacks against the chunk parser:
360 {
361         local $SIG{PIPE} = 'IGNORE';
362         my $conn = conn_for($sock, '1.1 chunk header excessive');
363         $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
364         my $n = 0;
365         my $w;
366         while ($w = $conn->write('ffffffff')) {
367                 $n += $w;
368         }
369         ok($!, 'got error set in $!');
370         is($w, undef, 'write error happened');
371         ok($n > 0, 'was able to write');
372         my $r = $conn->read(my $buf, 66666);
373         ok($r > 0, 'got non-empty response');
374         like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
375
376         $conn = conn_for($sock, '1.1 chunk trailer excessive');
377         $conn->write("PUT /sha1 HTTP/1.1\r\nTransfer-Encoding:chunked\r\n\r\n");
378         is($conn->syswrite("1\r\na"), 4, 'wrote first header + chunk');
379         delay();
380         $n = 0;
381         while ($w = $conn->write("\r")) {
382                 $n += $w;
383         }
384         ok($!, 'got error set in $!');
385         ok($n > 0, 'wrote part of chunk end (\r)');
386         $r = $conn->read($buf, 66666);
387         ok($r > 0, 'got non-empty response');
388         like($buf, qr!HTTP/1\.\d 400 !, 'got 400 response');
389 }
390
391 {
392         my $conn = conn_for($sock, '1.1 chunked close trickle');
393         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
394         $conn->write("Transfer-encoding: chunked\r\n\r\n");
395         foreach my $x ('a'..'z') {
396                 delay();
397                 $conn->write('1');
398                 delay();
399                 $conn->write("\r");
400                 delay();
401                 $conn->write("\n");
402                 delay();
403                 $conn->write($x);
404                 delay();
405                 $conn->write("\r");
406                 delay();
407                 $conn->write("\n");
408         }
409         $conn->write('0');
410         delay();
411         $conn->write("\r");
412         delay();
413         $conn->write("\n");
414         delay();
415         $conn->write("\r");
416         delay();
417         $conn->write("\n");
418         delay();
419         $check_self->($conn);
420 }
421
422 {
423         my $conn = conn_for($sock, '1.1 chunked close');
424         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
425         my $xlen = sprintf('%x', $len);
426         $conn->write("Transfer-Encoding: chunked\r\n\r\n$xlen\r\n" .
427                 "$str\r\n0\r\n\r\n");
428         $check_self->($conn);
429 }
430
431 {
432         my $conn = conn_for($sock, 'chunked body + pipeline');
433         $conn->write("PUT /sha1 HTTP/1.1\r\n" .
434                         "Transfer-Encoding: chunked\r\n");
435         delay();
436         $conn->write("\r\n1\r\n");
437         delay();
438         $conn->write('a');
439         delay();
440         $conn->write("\r\n0\r\n\r\nPUT /sha1 HTTP/1.1\r\n");
441         delay();
442
443         my $buf = '';
444         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
445                 $conn->sysread(my $tmp, 4096);
446                 $buf .= $tmp;
447         }
448         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
449         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
450         is($body, sha1_hex('a'), 'read expected body');
451
452         $conn->write("Connection: close\r\n");
453         $conn->write("Content-Length: $len\r\n\r\n$str");
454         $check_self->($conn);
455 }
456
457 {
458         my $conn = conn_for($sock, 'trickle header, one-shot body + pipeline');
459         $conn->write("PUT /sha1 HTTP/1.0\r\n" .
460                         "Connection: keep-alive\r\n");
461         delay();
462         $conn->write("Content-Length: $len\r\n\r\n${str}PUT");
463         my $buf = '';
464         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
465                 $conn->sysread(my $tmp, 4096);
466                 $buf .= $tmp;
467         }
468         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
469         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
470         is($body, sha1_hex($str), 'read expected body');
471
472         $conn->write(" /sha1 HTTP/1.0\r\nContent-Length: $len\r\n\r\n$str");
473         $check_self->($conn);
474 }
475
476 {
477         my $conn = conn_for($sock, 'trickle body');
478         $conn->write("PUT /sha1 HTTP/1.0\r\n");
479         $conn->write("Content-Length: $len\r\n\r\n");
480         my $beg = substr($str, 0, 10);
481         my $end = substr($str, 10);
482         is($beg . $end, $str, 'substr setup correct');
483         delay();
484         $conn->write($beg);
485         delay();
486         $conn->write($end);
487         $check_self->($conn);
488 }
489
490 {
491         my $conn = conn_for($sock, 'one-shot write');
492         $conn->write("PUT /sha1 HTTP/1.0\r\n" .
493                         "Content-Length: $len\r\n\r\n$str");
494         $check_self->($conn);
495 }
496
497 {
498         my $conn = conn_for($sock, 'trickle header, one-shot body');
499         $conn->write("PUT /sha1 HTTP/1.0\r\n");
500         delay();
501         $conn->write("Content-Length: $len\r\n\r\n$str");
502         $check_self->($conn);
503 }
504
505 {
506         my $conn = conn_for($sock, '1.1 Connnection: close');
507         $conn->write("PUT /sha1 HTTP/1.1\r\nConnection:close\r\n");
508         delay();
509         $conn->write("Content-Length: $len\r\n\r\n$str");
510         $check_self->($conn);
511 }
512
513 {
514         my $conn = conn_for($sock, '1.1 pipeline start');
515         $conn->write("PUT /sha1 HTTP/1.1\r\n\r\nPUT");
516         my $buf = '';
517         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
518                 $conn->sysread(my $tmp, 4096);
519                 $buf .= $tmp;
520         }
521         my ($head, $body) = split(/\r\n\r\n/, $buf, 2);
522         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
523         is($body, sha1_hex(''), 'read expected body');
524
525         # 2nd request
526         $conn->write(" /sha1 HTTP/1.1\r\n\r\n");
527         $buf = '';
528         until ($buf =~ /\r\n\r\n[a-f0-9]{40}\z/) {
529                 $conn->sysread(my $tmp, 4096);
530                 $buf .= $tmp;
531         }
532         ($head, $body) = split(/\r\n\r\n/, $buf, 2);
533         like($head, qr/\r\nContent-Length: 40\r\n/s, 'got expected length');
534         is($body, sha1_hex(''), 'read expected body #2');
535 }
536
537 SKIP: {
538         skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
539         my $var = Socket::TCP_DEFER_ACCEPT();
540         defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
541         is(unpack('i', $x), $defer_accept_val,
542                 'TCP_DEFER_ACCEPT unchanged if previously set');
543 };
544 SKIP: {
545         skip 'SO_ACCEPTFILTER is FreeBSD-only', 1 if $^O ne 'freebsd';
546         skip 'accf_data not loaded: kldload accf_data' if !defined $accf_arg;
547         my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
548         defined(my $x = getsockopt($sock, SOL_SOCKET, $var)) or die;
549         is($x, $accf_arg, 'SO_ACCEPTFILTER unchanged if previously set');
550 };
551 SKIP: {
552         skip 'only testing lsof(8) output on Linux', 1 if $^O ne 'linux';
553         skip 'no lsof in PATH', 1 unless which('lsof');
554         my @lsof = `lsof -p $pid`;
555         is_deeply([grep(/\bdeleted\b/, @lsof)], [], 'no lingering deleted inputs');
556         is_deeply([grep(/\bpipe\b/, @lsof)], [], 'no extra pipes with -W0');
557 };
558
559 done_testing();
560
561 sub capture {
562         my ($f) = @_;
563         open my $fh, '+<', $f or die "failed to open $f: $!\n";
564         local $/ = "\n";
565         my @r = <$fh>;
566         truncate($fh, 0) or die "truncate failed on $f: $!\n";
567         \@r
568 }
569
570 1;