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