]> Sergey Matveev's repositories - public-inbox.git/blob - t/httpd-unix.t
t/common: start_script replaces spawn_listener
[public-inbox.git] / t / httpd-unix.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 # Tests for binding Unix domain sockets
4 use strict;
5 use warnings;
6 use Test::More;
7 require './t/common.perl';
8 use Errno qw(EADDRINUSE);
9
10 foreach my $mod (qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)) {
11         eval "require $mod";
12         plan skip_all => "$mod missing for httpd-unix.t" if $@;
13 }
14
15 use File::Temp qw/tempdir/;
16 use IO::Socket::UNIX;
17 my $tmpdir = tempdir('httpd-unix-XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 my $unix = "$tmpdir/unix.sock";
19 my $psgi = './t/httpd-corner.psgi';
20 my $out = "$tmpdir/out.log";
21 my $err = "$tmpdir/err.log";
22 my $td;
23
24 my $spawn_httpd = sub {
25         my (@args) = @_;
26         push @args, '-W0';
27         my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
28         $td = start_script($cmd);
29 };
30
31 {
32         require PublicInbox::Daemon;
33         my $l = "$tmpdir/named.sock";
34         my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
35                                         Type => SOCK_STREAM);
36         is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
37 }
38
39 ok(!-S $unix, 'UNIX socket does not exist, yet');
40 $spawn_httpd->("-l$unix");
41 my %o = (Peer => $unix, Type => SOCK_STREAM);
42 for (1..1000) {
43         last if -S $unix && IO::Socket::UNIX->new(%o);
44         select undef, undef, undef, 0.02
45 }
46
47 ok(-S $unix, 'UNIX socket was bound by -httpd');
48 sub check_sock ($) {
49         my ($unix) = @_;
50         my $sock = IO::Socket::UNIX->new(Peer => $unix, Type => SOCK_STREAM);
51         warn "E: $! connecting to $unix\n" unless defined $sock;
52         ok($sock, 'client UNIX socket connected');
53         ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
54                 'wrote req to server');
55         ok($sock->read(my $buf, 4096), 'read response');
56         like($buf, qr!\r\n\r\n127\.0\.0\.1:0\z!,
57                 'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
58 }
59
60 check_sock($unix);
61
62 { # do not clobber existing socket
63         my %err = ( 'linux' => EADDRINUSE );
64         open my $out, '>>', "$tmpdir/1" or die "redirect failed: $!";
65         open my $err, '>>', "$tmpdir/2" or die "redirect failed: $!";
66         my $cmd = ['-httpd', '-l', $unix, '-W0', $psgi];
67         my $ftd = start_script($cmd, undef, { 1 => $out, 2 => $err });
68         $ftd->join;
69         isnt($?, 0, 'httpd failure set $?');
70         SKIP: {
71                 my $ec = $err{$^O} or
72                         skip("not sure if $^O fails with EADDRINUSE", 1);
73                 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
74         };
75         open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
76         local $/;
77         my $e = <$fh>;
78         like($e, qr/no listeners bound/i, 'got error message');
79         is(-s "$tmpdir/1", 0, 'stdout was empty');
80 }
81
82 {
83         is($td->kill, 1, 'terminate existing process');
84         $td->join;
85         is($?, 0, 'existing httpd exited successfully');
86         ok(-S $unix, 'unix socket still exists');
87 }
88
89 SKIP: {
90         eval 'require Net::Server::Daemonize';
91         skip('Net::Server missing for pid-file/daemonization test', 10) if $@;
92
93         # wait for daemonization
94         $spawn_httpd->("-l$unix", '-D', '-P', "$tmpdir/pid");
95         $td->join;
96         is($?, 0, 'daemonized process OK');
97         check_sock($unix);
98
99         ok(-f "$tmpdir/pid", 'pid file written');
100         open my $fh, '<', "$tmpdir/pid" or die "open failed: $!";
101         local $/ = "\n";
102         my $rpid = <$fh>;
103         chomp $rpid;
104         like($rpid, qr/\A\d+\z/s, 'pid file looks like a pid');
105         is(kill('TERM', $rpid), 1, 'signalled daemonized process');
106         for (1..100) {
107                 kill(0, $rpid) or last;
108                 select undef, undef, undef, 0.02;
109         }
110         is(kill(0, $rpid), 0, 'daemonized process exited')
111 }
112
113 done_testing();