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