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>
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/;
12 use Socket qw(IPPROTO_TCP SOL_SOCKET);
13 require './t/common.perl';
15 # FIXME: too much setup
16 my $tmpdir = tempdir('pi-httpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
17 my $home = "$tmpdir/pi-home";
18 my $err = "$tmpdir/stderr.log";
19 my $out = "$tmpdir/stdout.log";
20 my $maindir = "$tmpdir/main.git";
21 my $group = 'test-httpd';
22 my $addr = $group . '@example.com';
23 my $cfgpfx = "publicinbox.$group";
24 my $httpd = 'blib/script/public-inbox-httpd';
25 my $init = 'blib/script/public-inbox-init';
26 my $sock = tcp_server();
28 use_ok 'PublicInbox::Git';
29 use_ok 'PublicInbox::Import';
31 END { kill 'TERM', $pid if defined $pid };
33 local $ENV{HOME} = $home;
34 ok(!system($init, $group, $maindir, 'http://example.com/', $addr),
37 # ensure successful message delivery
39 my $mime = Email::MIME->new(<<EOF);
40 From: Me <me\@example.com>
41 To: You <you\@example.com>
43 Message-Id: <nntp\@example.com>
45 Date: Thu, 01 Jan 1970 06:06:06 +0000
50 my $git = PublicInbox::Git->new($maindir);
51 my $im = PublicInbox::Import->new($git, 'test', $addr);
55 ok($sock, 'sock created');
56 my $cmd = [ $httpd, "--stdout=$out", "--stderr=$err" ];
57 $pid = spawn_listener(undef, $cmd, [$sock]);
58 my $host = $sock->sockhost;
59 my $port = $sock->sockport;
60 my $conn = tcp_connect($sock);
61 ok($conn, 'connected');
62 ok($conn->write("GET / HTTP/1.0\r\n\r\n"), 'wrote data to socket');
65 ok($conn->read($buf, 4096), 'read some bytes');
66 like($buf, qr!\AHTTP/1\.[01] 404\b!, 'got 404 response');
67 is($conn->read($buf, 1), 0, "EOF");
70 is(system(qw(git clone -q --mirror),
71 "http://$host:$port/$group", "$tmpdir/clone.git"),
72 0, 'smart clone successful');
74 # ensure dumb cloning works, too:
75 is(system('git', "--git-dir=$maindir",
76 qw(config http.uploadpack false)),
77 0, 'disable http.uploadpack');
78 is(system(qw(git clone -q --mirror),
79 "http://$host:$port/$group", "$tmpdir/dumb.git"),
80 0, 'clone successful');
82 ok(kill('TERM', $pid), 'killed httpd');
86 is(system('git', "--git-dir=$tmpdir/clone.git",
87 qw(fsck --no-verbose)), 0,
88 'fsck on cloned directory successful');
92 skip 'TCP_DEFER_ACCEPT is Linux-only', 1 if $^O ne 'linux';
93 my $var = Socket::TCP_DEFER_ACCEPT();
94 defined(my $x = getsockopt($sock, IPPROTO_TCP, $var)) or die;
95 ok(unpack('i', $x) > 0, 'TCP_DEFER_ACCEPT set');
98 skip 'SO_ACCEPTFILTER is FreeBSD-only', 1 if $^O ne 'freebsd';
99 if (system('kldstat -m accf_http >/dev/null') != 0) {
100 skip 'accf_http not loaded: kldload accf_http', 1;
102 require PublicInbox::Daemon;
103 my $var = PublicInbox::Daemon::SO_ACCEPTFILTER();
104 my $x = getsockopt($sock, SOL_SOCKET, $var);
105 like($x, qr/\Ahttpready\0+\z/, 'got httpready accf for HTTP');