1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 foreach my $mod (qw(DBD::SQLite Search::Xapian Danga::Socket)) {
8 plan skip_all => "$mod missing for nntpd.t" if $@;
10 require PublicInbox::SearchIdx;
11 require PublicInbox::Msgmap;
15 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
16 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
17 use File::Temp qw/tempdir/;
20 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
21 my $home = "$tmpdir/pi-home";
22 my $err = "$tmpdir/stderr.log";
23 my $out = "$tmpdir/stdout.log";
24 my $mainrepo = "$tmpdir/main.git";
25 my $group = 'test-nntpd';
26 my $addr = $group . '@example.com';
27 my $nntpd = 'blib/script/public-inbox-nntpd';
28 my $init = 'blib/script/public-inbox-init';
29 use_ok 'PublicInbox::Import';
30 use_ok 'PublicInbox::Inbox';
31 use_ok 'PublicInbox::Git';
32 use_ok 'PublicInbox::V2Writable';
34 # XXX FIXME: make it easier to test both versions
35 my $version = int($ENV{PI_VERSION} || 1);
37 LocalAddr => '127.0.0.1',
43 my $sock = IO::Socket::INET->new(%opts);
46 END { kill 'TERM', $pid if defined $pid };
49 mainrepo => $mainrepo,
52 -primary_address => $addr,
54 $ibx = PublicInbox::Inbox->new($ibx);
56 local $ENV{HOME} = $home;
57 my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
58 push @cmd, "-V$version";
59 is(system(@cmd), 0, 'init OK');
60 is(system(qw(git config), "--file=$home/.public-inbox/config",
61 "publicinbox.$group.newsgroup", $group),
62 0, 'enabled newsgroup');
67 $im = PublicInbox::V2Writable->new($ibx);
68 } elsif ($version == 1) {
69 my $git = PublicInbox::Git->new($mainrepo);
70 $im = PublicInbox::Import->new($git, 'test', $addr);
72 die "unsupported version: $version";
75 # ensure successful message delivery
77 my $mime = Email::MIME->new(<<EOF);
78 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
79 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
81 Message-Id: <nntp\@example.com>
82 Content-Type: text/plain; charset=utf-8
83 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
84 Date: Thu, 01 Jan 1970 06:06:06 +0000
85 Content-Transfer-Encoding: 8bit
87 This is a test message for El\xc3\xa9anor
91 $mime->header_set('List-Id', "<$list_id>");
92 $len = length($mime->as_string);
96 my $s = PublicInbox::SearchIdx->new($mainrepo, 1);
101 ok($sock, 'sock created');
103 my $fl = fcntl($sock, F_GETFD, 0);
104 ok(! $!, 'no error from fcntl(F_GETFD)');
105 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
109 # pretend to be systemd
110 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
111 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
112 $ENV{LISTEN_PID} = $$;
113 $ENV{LISTEN_FDS} = 1;
114 exec $nntpd, "--stdout=$out", "--stderr=$err";
117 ok(defined $pid, 'forked nntpd process successfully');
119 fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
120 ok(! $!, 'no error from fcntl(F_SETFD)');
121 my $host_port = $sock->sockhost . ':' . $sock->sockport;
122 my $n = Net::NNTP->new($host_port);
124 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
125 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
128 PeerAddr => $host_port,
133 my $mid = '<nntp@example.com>';
135 'message-id' => $mid,
136 subject => "Testing for El\xc3\xa9anor",
137 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
138 'from' => "El\xc3\xa9anor <me\@example.com>",
139 'to' => "El\xc3\xa9anor <you\@example.com>",
141 'xref' => "example.com $group:1"
144 my $s = IO::Socket::INET->new(%opts);
145 sysread($s, my $buf, 4096);
146 is($buf, "201 server ready - post via email\r\n", 'got greeting');
149 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
150 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
152 $s = IO::Socket::INET->new(%opts);
153 sysread($s, $buf, 4096);
154 is($buf, "201 server ready - post via email\r\n", 'got greeting');
157 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
158 $buf = read_til_dot($s);
159 like($buf, qr/\A231 list of /, 'newgroups OK');
161 while (my ($k, $v) = each %xhdr) {
162 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
163 "XHDR $k by message-id works");
164 is_deeply($n->xhdr("$k 1"), { 1 => $v },
165 "$k by article number works");
166 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
167 "$k by article range works");
169 syswrite($s, "HDR $k $mid\r\n");
170 $buf = read_til_dot($s);
171 my @r = split("\r\n", $buf);
172 like($r[0], qr/\A225 /, '225 response for HDR');
173 is($r[1], "0 $v", 'got expected response for HDR');
177 my $nogroup = Net::NNTP->new($host_port);
178 while (my ($k, $v) = each %xhdr) {
179 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
180 "$k by message-id works without group");
184 is_deeply($n->xover('1-'), {
185 '1' => ["Testing for El\xc3\xa9anor",
186 "El\xc3\xa9anor <me\@example.com>",
187 'Thu, 01 Jan 1970 06:06:06 +0000',
188 '<nntp@example.com>',
191 '1' ] }, "XOVER range works");
193 is_deeply($n->xover('1'), {
194 '1' => ["Testing for El\xc3\xa9anor",
195 "El\xc3\xa9anor <me\@example.com>",
196 'Thu, 01 Jan 1970 06:06:06 +0000',
197 '<nntp@example.com>',
200 '1' ] }, "XOVER by article works");
202 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
203 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
204 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
205 'body really matches');
206 my $art = $n->article(1);
207 is(ref($art), 'ARRAY', 'got array for ARTICLE');
208 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
209 is($n->article(999), undef, 'non-existent num');
210 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
213 syswrite($s, "OVER $mid\r\n");
214 $buf = read_til_dot($s);
215 my @r = split("\r\n", $buf);
216 like($r[0], qr/^224 /, 'got 224 response for OVER');
217 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
218 "El\xc3\xa9anor <me\@example.com>\t" .
219 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
220 "$mid\t\t$len\t1", 'OVER by Message-ID works');
221 is($r[2], '.', 'correctly terminated response');
224 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
226 is_deeply($n->xhdr(qw(References 1-)), { 1 => '' },
227 'XHDR References 1- works (empty string)');
228 is_deeply($n->xhdr(qw(list-id 1-)), {},
229 'XHDR on invalid header returns empty');
231 my $mids = $n->newnews(0, '*');
232 is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
237 ok($date >= $t0, 'valid date after start');
238 ok($date <= $t1, 'valid date before stop');
242 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
243 syswrite($s, 'HDR List-id 1-');
244 select(undef, undef, undef, 0.15);
245 ok(kill('TERM', $pid), 'killed nntpd');
246 select(undef, undef, undef, 0.15);
247 syswrite($s, "\r\n");
250 sysread($s, $buf, 4096, length($buf));
251 } until ($buf =~ /\r\n\z/);
252 my @r = split("\r\n", $buf);
253 like($r[0], qr/^5\d\d /,
254 'got 5xx response for unoptimized HDR');
255 is(scalar @r, 1, 'only one response line');
259 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
262 open my $fh, '<', $err or die "open $err failed: $!";
265 is($?, 0, 'no error in exited process');
266 unlike($eout, qr/wide/i, 'no Wide character warnings');
275 sysread($s, $buf, 4096, length($buf));
276 } until ($buf =~ /\r\n\.\r\n\z/);