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