user/dev discussion of public-inbox itself
 help / color / Atom feed
2ef64dfcd0968921ef93f841ac646bb17a2fafae blob 3399 bytes (raw)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
 
# Copyright (C) 2016-2018 all contributors <meta@public-inbox.org>
# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
# Tests for binding Unix domain sockets
use strict;
use warnings;
use Test::More;

foreach my $mod (qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)) {
	eval "require $mod";
	plan skip_all => "$mod missing for httpd-unix.t" if $@;
}

use File::Temp qw/tempdir/;
use IO::Socket::UNIX;
use Cwd qw/getcwd/;
my $tmpdir = tempdir('httpd-unix-XXXXXX', TMPDIR => 1, CLEANUP => 1);
my $unix = "$tmpdir/unix.sock";
my $httpd = 'blib/script/public-inbox-httpd';
my $psgi = getcwd() . '/t/httpd-corner.psgi';
my $out = "$tmpdir/out.log";
my $err = "$tmpdir/err.log";

my $pid;
END { kill 'TERM', $pid if defined $pid };

my $spawn_httpd = sub {
	my (@args) = @_;
	$pid = fork;
	if ($pid == 0) {
		exec $httpd, @args, "--stdout=$out", "--stderr=$err", $psgi;
		die "FAIL: $!\n";
	}
	ok(defined $pid, 'forked httpd process successfully');
};

{
	require PublicInbox::Daemon;
	my $l = "$tmpdir/named.sock";
	my $s = IO::Socket::UNIX->new(Listen => 5, Local => $l,
					Type => SOCK_STREAM);
	is(PublicInbox::Daemon::sockname($s), $l, 'sockname works for UNIX');
}

ok(!-S $unix, 'UNIX socket does not exist, yet');
$spawn_httpd->("-l$unix");
for (1..1000) {
	last if -S $unix;
	select undef, undef, undef, 0.02
}

ok(-S $unix, 'UNIX socket was bound by -httpd');
sub check_sock ($) {
	my ($unix) = @_;
	my $sock = IO::Socket::UNIX->new(Peer => $unix, Type => SOCK_STREAM);
	warn "E: $! connecting to $unix\n" unless defined $sock;
	ok($sock, 'client UNIX socket connected');
	ok($sock->write("GET /host-port HTTP/1.0\r\n\r\n"),
		'wrote req to server');
	ok($sock->read(my $buf, 4096), 'read response');
	like($buf, qr!\r\n\r\n127\.0\.0\.1:0\z!,
		'set REMOTE_ADDR and REMOTE_PORT for Unix socket');
}

check_sock($unix);

{ # do not clobber existing socket
	my $fpid = fork;
	if ($fpid == 0) {
		open STDOUT, '>>', "$tmpdir/1" or die "redirect failed: $!";
		open STDERR, '>>', "$tmpdir/2" or die "redirect failed: $!";
		exec $httpd, '-l', $unix, '-W0', $psgi;
		die "FAIL: $!\n";
	}
	is($fpid, waitpid($fpid, 0), 'second httpd exits');
	isnt($?, 0, 'httpd failed with failure to bind');
	open my $fh, "$tmpdir/2" or die "failed to open $tmpdir/2: $!";
	local $/;
	my $e = <$fh>;
	like($e, qr/no listeners bound/i, 'got error message');
	is(-s "$tmpdir/1", 0, 'stdout was empty');
}

{
	my $kpid = $pid;
	$pid = undef;
	is(kill('TERM', $kpid), 1, 'terminate existing process');
	is(waitpid($kpid, 0), $kpid, 'existing httpd terminated');
	is($?, 0, 'existing httpd exited successfully');
	ok(-S $unix, 'unix socket still exists');
}

SKIP: {
	eval 'require Net::Server::Daemonize';
	skip('Net::Server missing for pid-file/daemonization test', 10) if $@;

	# wait for daemonization
	$spawn_httpd->("-l$unix", '-D', '-P', "$tmpdir/pid");
	my $kpid = $pid;
	$pid = undef;
	is(waitpid($kpid, 0), $kpid, 'existing httpd terminated');
	check_sock($unix);

	ok(-f "$tmpdir/pid", 'pid file written');
	open my $fh, '<', "$tmpdir/pid" or die "open failed: $!";
	local $/ = "\n";
	my $rpid = <$fh>;
	chomp $rpid;
	like($rpid, qr/\A\d+\z/s, 'pid file looks like a pid');
	is(kill('TERM', $rpid), 1, 'signalled daemonized process');
	for (1..100) {
		kill(0, $rpid) or last;
		select undef, undef, undef, 0.02;
	}
	is(kill(0, $rpid), 0, 'daemonized process exited')
}

done_testing();
debug log:

solving 2ef64df ...
found 2ef64df in https://80x24.org/public-inbox.git

user/dev discussion of public-inbox itself

Archives are clonable:
	git clone --mirror http://public-inbox.org/meta
	git clone --mirror http://czquwvybam4bgbro.onion/meta
	git clone --mirror http://hjrcffqmbrq6wope.onion/meta
	git clone --mirror http://ou63pmih66umazou.onion/meta

Newsgroups are available over NNTP:
	nntp://news.public-inbox.org/inbox.comp.mail.public-inbox.meta
	nntp://ou63pmih66umazou.onion/inbox.comp.mail.public-inbox.meta
	nntp://czquwvybam4bgbro.onion/inbox.comp.mail.public-inbox.meta
	nntp://hjrcffqmbrq6wope.onion/inbox.comp.mail.public-inbox.meta
	nntp://news.gmane.org/gmane.mail.public-inbox.general

 note: .onion URLs require Tor: https://www.torproject.org/

AGPL code for this site: git clone https://public-inbox.org/ public-inbox