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);
48 END { kill 'TERM', $pid if defined $pid };
50 local $ENV{HOME} = $home;
51 system($init, $group, $maindir, 'http://example.com/', $addr);
53 # ensure successful message delivery
55 local $ENV{ORIGINAL_RECIPIENT} = $addr;
56 my $simple = Email::Simple->new(<<EOF);
57 From: Me <me\@example.com>
58 To: You <you\@example.com>
60 Message-Id: <nntp\@example.com>
62 Date: Thu, 01 Jan 1970 06:06:06 +0000
66 my $in = $simple->as_string;
67 local $ENV{PATH} = $main_path;
68 IPC::Run::run([$mda], \$in);
69 is(0, $?, 'ran MDA correctly');
70 is(0, system($index, $maindir), 'indexed git dir');
73 ok($sock, 'sock created');
75 my $fl = fcntl($sock, F_GETFD, 0);
76 ok(! $!, 'no error from fcntl(F_GETFD)');
77 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
81 # pretend to be systemd
82 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
83 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
84 $ENV{LISTEN_PID} = $$;
86 exec $nntpd, "--stdout=$out", "--stderr=$err";
89 ok(defined $pid, 'forked nntpd process successfully');
91 fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
92 ok(! $!, 'no error from fcntl(F_SETFD)');
93 my $host_port = $sock->sockhost . ':' . $sock->sockport;
94 my $n = Net::NNTP->new($host_port);
96 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
97 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
100 PeerAddr => $host_port,
105 my $mid = '<nntp@example.com>';
107 'message-id' => $mid,
109 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
110 'from' => 'Me <me@example.com>',
111 'to' => 'You <you@example.com>',
113 'xref' => "example.com $group:1"
116 my $s = IO::Socket::INET->new(%opts);
117 sysread($s, my $buf, 4096);
118 is($buf, "201 server ready - post via email\r\n", 'got greeting');
121 while (my ($k, $v) = each %xhdr) {
122 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
123 "XHDR $k by message-id works");
124 is_deeply($n->xhdr("$k 1"), { 1 => $v },
125 "$k by article number works");
126 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
127 "$k by article range works");
130 syswrite($s, "HDR $k $mid\r\n");
132 sysread($s, $buf, 4096, length($buf));
133 } until ($buf =~ /^[^2]../ || $buf =~ /\r\n\.\r\n\z/);
134 my @r = split("\r\n", $buf);
135 like($r[0], qr/\A224 /, '224 response for HDR');
136 is($r[1], "0 $v", 'got expected response for HDR');
140 my $nogroup = Net::NNTP->new($host_port);
141 while (my ($k, $v) = each %xhdr) {
142 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
143 "$k by message-id works without group");
147 is_deeply($n->xover('1-'), {
149 'Me <me@example.com>',
150 'Thu, 01 Jan 1970 06:06:06 +0000',
151 '<nntp@example.com>',
154 '1' ] }, "XOVER works");
156 ok(kill('TERM', $pid), 'killed nntpd');