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