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