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>
7 foreach my $mod (qw(Plack::Util Plack::Builder HTTP::Date HTTP::Status)) {
9 plan skip_all => "$mod missing for httpd.t" if $@;
11 use File::Temp qw/tempdir/;
13 use Socket qw(IPPROTO_TCP);
14 require './t/common.perl';
16 # FIXME: too much setup
17 my $tmpdir = tempdir('pi-httpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
18 my $home = "$tmpdir/pi-home";
19 my $err = "$tmpdir/stderr.log";
20 my $out = "$tmpdir/stdout.log";
21 my $maindir = "$tmpdir/main.git";
22 my $group = 'test-httpd';
23 my $addr = $group . '@example.com';
24 my $cfgpfx = "publicinbox.$group";
25 my $httpd = 'blib/script/public-inbox-httpd';
26 my $init = 'blib/script/public-inbox-init';
27 my $sock = tcp_server();
29 use_ok 'PublicInbox::Git';
30 use_ok 'PublicInbox::Import';
32 END { kill 'TERM', $pid if defined $pid };
34 local $ENV{HOME} = $home;
35 ok(!system($init, $group, $maindir, 'http://example.com/', $addr),
38 # ensure successful message delivery
40 my $mime = Email::MIME->new(<<EOF);
41 From: Me <me\@example.com>
42 To: You <you\@example.com>
44 Message-Id: <nntp\@example.com>
46 Date: Thu, 01 Jan 1970 06:06:06 +0000
51 my $git = PublicInbox::Git->new($maindir);
52 my $im = PublicInbox::Import->new($git, 'test', $addr);
56 ok($sock, 'sock created');
57 my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ];
58 $pid = spawn_listener(undef, $cmd, [$sock]);
59 my $host = $sock->sockhost;
60 my $port = $sock->sockport;
61 my $conn = IO::Socket::INET->new(PeerAddr => $host,
65 ok($conn, 'connected');
66 ok($conn->write("GET / HTTP/1.0\r\n\r\n"), 'wrote data to socket');
69 ok($conn->read($buf, 4096), 'read some bytes');
70 like($buf, qr!\AHTTP/1\.[01] 404\b!, 'got 404 response');
71 is($conn->read($buf, 1), 0, "EOF");
74 is(system(qw(git clone -q --mirror),
75 "http://$host:$port/$group", "$tmpdir/clone.git"),
76 0, 'smart clone successful');
78 # ensure dumb cloning works, too:
79 is(system('git', "--git-dir=$maindir",
80 qw(config http.uploadpack false)),
81 0, 'disable http.uploadpack');
82 is(system(qw(git clone -q --mirror),
83 "http://$host:$port/$group", "$tmpdir/dumb.git"),
84 0, 'clone successful');
86 ok(kill('TERM', $pid), 'killed httpd');
90 is(system('git', "--git-dir=$tmpdir/clone.git",
91 qw(fsck --no-verbose)), 0,
92 'fsck on cloned directory successful');
96 skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
97 my $var = Socket::TCP_DEFER_ACCEPT();
98 defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
99 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set');
102 skip 'SO_ACCEPTFILTER is FreeBSD-only', 1 if $^O ne 'freebsd';
103 if (system('kldstat -m accf_http >/dev/null') != 0) {
104 skip 'accf_http not loaded: kldload accf_http', 1;
106 require PublicInbox::Daemon;
107 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
108 my $x = getsockopt($sock, SOL_SOCKET, $var);
109 like($x, qr/\Ahttpready\0+\z/, 'got httpready accf for HTTP');