1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
6 eval { require PublicInbox::SearchIdx };
7 plan skip_all => "Xapian missing for nntpd" if $@;
8 eval { require PublicInbox::Msgmap };
9 plan skip_all => "DBD::SQLite missing for nntpd" if $@;
13 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
14 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
15 use File::Temp qw/tempdir/;
20 my $tmpdir = tempdir(CLEANUP => 1);
21 my $home = "$tmpdir/pi-home";
22 my $err = "$tmpdir/stderr.log";
23 my $out = "$tmpdir/stdout.log";
24 my $pi_home = "$home/.public-inbox";
25 my $pi_config = "$pi_home/config";
26 my $maindir = "$tmpdir/main.git";
27 my $main_bin = getcwd()."/t/main-bin";
28 my $main_path = "$main_bin:$ENV{PATH}"; # for spamc ham mock
29 my $group = 'test-nntpd';
30 my $addr = $group . '@example.com';
31 my $cfgpfx = "publicinbox.$group";
32 my $failbox = "$home/fail.mbox";
33 local $ENV{PI_EMERGENCY} = $failbox;
34 my $mda = 'blib/script/public-inbox-mda';
35 my $nntpd = 'blib/script/public-inbox-nntpd';
36 my $init = 'blib/script/public-inbox-init';
37 my $index = 'blib/script/public-inbox-index';
40 LocalAddr => '127.0.0.1',
46 my $sock = IO::Socket::INET->new(%opts);
47 plan skip_all => 'sock fd!=3, cannot test nntpd integration' if fileno($sock) != 3;
49 END { kill 'TERM', $pid if defined $pid };
51 local $ENV{HOME} = $home;
52 system($init, $group, $maindir, 'http://example.com/', $addr);
54 # ensure successful message delivery
56 local $ENV{ORIGINAL_RECIPIENT} = $addr;
57 my $simple = Email::Simple->new(<<EOF);
58 From: Me <me\@example.com>
59 To: You <you\@example.com>
61 Message-Id: <nntp\@example.com>
63 Date: Thu, 01 Jan 1970 00:00:00 +0000
67 my $in = $simple->as_string;
68 local $ENV{PATH} = $main_path;
69 IPC::Run::run([$mda], \$in);
70 is(0, $?, 'ran MDA correctly');
71 is(0, system($index, $maindir), 'indexed git dir');
74 ok($sock, 'sock created');
76 my $fl = fcntl($sock, F_GETFD, 0);
77 ok(! $!, 'no error from fcntl(F_GETFD)');
78 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
81 # pretend to be systemd
82 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
83 $ENV{LISTEN_PID} = $$;
85 exec $nntpd, "--stdout=$out", "--stderr=$err";
88 ok(defined $pid, 'forked nntpd process successfully');
90 ok(! $!, 'no error from fcntl(F_SETFD)');
91 fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
92 my $n = Net::NNTP->new($sock->sockhost . ':' . $sock->sockport);
94 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
95 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
97 # TODO: upgrades and such
99 ok(kill('TERM', $pid), 'killed nntpd');