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
7 require './t/common.perl';
8 use Errno qw(EADDRINUSE);
10 foreach my $mod (qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)) {
12 plan skip_all => "$mod missing for httpd-unix.t" if $@;
15 use File::Temp qw/tempdir/;
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";
24 my $spawn_httpd = sub {
27 my $cmd = [ '-httpd', @args, "--stdout=$out", "--stderr=$err", $psgi ];
28 $td = start_script($cmd);
32 require PublicInbox::Daemon;
33 my $l = "$tmpdir/named.sock";
34 my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
36 is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
39 ok(!-S $unix, 'UNIX socket does not exist, yet');
40 $spawn_httpd->("-l$unix");
41 my %o = (Peer => $unix, Type => SOCK_STREAM);
43 last if -S $unix && IO::Socket::UNIX->new(%o);
44 select undef, undef, undef, 0.02
47 ok(-S $unix, 'UNIX socket was bound by -httpd');
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');
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 });
69 isnt($?, 0, 'httpd failure set $?');
72 skip("not sure if $^O fails with EADDRINUSE", 1);
73 is($? >> 8, $ec, 'httpd failed with EADDRINUSE');
75 open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
78 like($e, qr/no listeners bound/i, 'got error message');
79 is(-s "$tmpdir/1", 0, 'stdout was empty');
83 is($td->kill, 1, 'terminate existing process');
85 is($?, 0, 'existing httpd exited successfully');
86 ok(-S $unix, 'unix socket still exists');
90 eval 'require Net::Server::Daemonize';
91 skip('Net::Server missing for pid-file/daemonization test', 10) if $@;
93 # wait for daemonization
94 $spawn_httpd->("-l$unix", '-D', '-P', "$tmpdir/pid");
96 is($?, 0, 'daemonized process OK');
99 ok(-f "$tmpdir/pid", 'pid file written');
100 open my $fh, '<', "$tmpdir/pid" or die "open failed: $!";
104 like($rpid, qr/\A\d+\z/s, 'pid file looks like a pid');
105 is(kill('TERM', $rpid), 1, 'signalled daemonized process');
107 kill(0, $rpid) or last;
108 select undef, undef, undef, 0.02;
110 is(kill(0, $rpid), 0, 'daemonized process exited')