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