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 Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
16 use File::Temp qw/tempdir/;
19 require './t/common.perl';
21 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
22 my $home = "$tmpdir/pi-home";
23 my $err = "$tmpdir/stderr.log";
24 my $out = "$tmpdir/stdout.log";
25 my $mainrepo = "$tmpdir/main.git";
26 my $group = 'test-nntpd';
27 my $addr = $group . '@example.com';
28 my $nntpd = 'blib/script/public-inbox-nntpd';
29 my $init = 'blib/script/public-inbox-init';
30 use_ok 'PublicInbox::Import';
31 use_ok 'PublicInbox::Inbox';
32 use_ok 'PublicInbox::Git';
33 use_ok 'PublicInbox::V2Writable';
35 # XXX FIXME: make it easier to test both versions
36 my $version = int($ENV{PI_VERSION} || 1);
38 LocalAddr => '127.0.0.1',
44 my $sock = IO::Socket::INET->new(%opts);
47 END { kill 'TERM', $pid if defined $pid };
50 mainrepo => $mainrepo,
53 -primary_address => $addr,
55 $ibx = PublicInbox::Inbox->new($ibx);
57 local $ENV{HOME} = $home;
58 my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
59 push @cmd, "-V$version";
60 is(system(@cmd), 0, 'init OK');
61 is(system(qw(git config), "--file=$home/.public-inbox/config",
62 "publicinbox.$group.newsgroup", $group),
63 0, 'enabled newsgroup');
68 $im = PublicInbox::V2Writable->new($ibx);
69 } elsif ($version == 1) {
70 my $git = PublicInbox::Git->new($mainrepo);
71 $im = PublicInbox::Import->new($git, 'test', $addr);
73 die "unsupported version: $version";
76 # ensure successful message delivery
78 my $mime = Email::MIME->new(<<EOF);
79 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
80 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
82 Message-Id: <nntp\@example.com>
83 Content-Type: text/plain; charset=utf-8
84 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
85 Date: Thu, 01 Jan 1970 06:06:06 +0000
86 Content-Transfer-Encoding: 8bit
87 References: <ref tab squeezed>
89 This is a test message for El\xc3\xa9anor
93 $mime->header_set('List-Id', "<$list_id>");
94 $len = length($mime->as_string);
98 my $s = PublicInbox::SearchIdx->new($mainrepo, 1);
103 ok($sock, 'sock created');
104 my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
105 $pid = spawn_listener(undef, $cmd, [ $sock ]);
106 ok(defined $pid, 'forked nntpd process successfully');
107 my $host_port = $sock->sockhost . ':' . $sock->sockport;
108 my $n = Net::NNTP->new($host_port);
110 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
111 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
112 is_deeply($n->listgroup($group), [1], 'listgroup OK');
115 PeerAddr => $host_port,
120 my $mid = '<nntp@example.com>';
122 'message-id' => $mid,
123 subject => "Testing for El\xc3\xa9anor",
124 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
125 'from' => "El\xc3\xa9anor <me\@example.com>",
126 'to' => "El\xc3\xa9anor <you\@example.com>",
128 'xref' => hostname . " $group:1",
129 'references' => '<reftabsqueezed>',
132 my $s = IO::Socket::INET->new(%opts);
133 sysread($s, my $buf, 4096);
134 is($buf, "201 " . hostname . " ready - post via email\r\n",
138 ok(syswrite($s, " \r\n"), 'wrote spaces');
139 ok(syswrite($s, "\r\n"), 'wrote nothing');
140 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
141 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
143 $s = IO::Socket::INET->new(%opts);
144 sysread($s, $buf, 4096);
145 is($buf, "201 " . hostname . " ready - post via email\r\n",
149 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
150 $buf = read_til_dot($s);
151 like($buf, qr/\A231 list of /, 'newgroups OK');
153 while (my ($k, $v) = each %xhdr) {
154 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
155 "XHDR $k by message-id works");
156 is_deeply($n->xhdr("$k 1"), { 1 => $v },
157 "$k by article number works");
158 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
159 "$k by article range works");
161 syswrite($s, "HDR $k $mid\r\n");
162 $buf = read_til_dot($s);
163 my @r = split("\r\n", $buf);
164 like($r[0], qr/\A225 /, '225 response for HDR');
165 is($r[1], "0 $v", 'got expected response for HDR');
169 my $nogroup = Net::NNTP->new($host_port);
170 while (my ($k, $v) = each %xhdr) {
171 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
172 "$k by message-id works without group");
176 is_deeply($n->xover('1-'), {
177 '1' => ["Testing for El\xc3\xa9anor",
178 "El\xc3\xa9anor <me\@example.com>",
179 'Thu, 01 Jan 1970 06:06:06 +0000',
180 '<nntp@example.com>',
184 'Xref: '. hostname . ' test-nntpd:1'] },
185 "XOVER range works");
187 is_deeply($n->xover('1'), {
188 '1' => ["Testing for El\xc3\xa9anor",
189 "El\xc3\xa9anor <me\@example.com>",
190 'Thu, 01 Jan 1970 06:06:06 +0000',
191 '<nntp@example.com>',
195 'Xref: '. hostname . ' test-nntpd:1'] },
196 "XOVER by article works");
198 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
199 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
200 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
201 'body really matches');
202 my $art = $n->article(1);
203 is(ref($art), 'ARRAY', 'got array for ARTICLE');
204 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
205 is($n->article(999), undef, 'non-existent num');
206 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
209 syswrite($s, "OVER $mid\r\n");
210 $buf = read_til_dot($s);
211 my @r = split("\r\n", $buf);
212 like($r[0], qr/^224 /, 'got 224 response for OVER');
213 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
214 "El\xc3\xa9anor <me\@example.com>\t" .
215 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
216 "$mid\t<reftabsqueezed>\t$len\t1" .
217 "\tXref: " . hostname . " test-nntpd:0",
218 'OVER by Message-ID works');
219 is($r[2], '.', 'correctly terminated response');
222 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
224 is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
225 'XHDR References 1- works)');
226 is_deeply($n->xhdr(qw(list-id 1-)), {},
227 'XHDR on invalid header returns empty');
229 my $mids = $n->newnews(0, '*');
230 is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
235 ok($date >= $t0, 'valid date after start');
236 ok($date <= $t1, 'valid date before stop');
239 # pipelined requests:
242 syswrite($s, "GROUP $group\r\n");
248 <$s> =~ /\A224 / or _exit(1);
249 <$s> =~ /\A1/ or _exit(2);
250 <$s> eq ".\r\n" or _exit(3);
255 syswrite($s, "XOVER 1\r\n");
257 is($rdr, waitpid($rdr, 0), 'reader done');
258 is($? >> 8, 0, 'no errors');
261 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
262 syswrite($s, 'HDR List-id 1-');
263 select(undef, undef, undef, 0.15);
264 ok(kill('TERM', $pid), 'killed nntpd');
265 select(undef, undef, undef, 0.15);
266 syswrite($s, "\r\n");
269 sysread($s, $buf, 4096, length($buf));
270 } until ($buf =~ /\r\n\z/);
271 my @r = split("\r\n", $buf);
272 like($r[0], qr/^5\d\d /,
273 'got 5xx response for unoptimized HDR');
274 is(scalar @r, 1, 'only one response line');
278 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
281 open my $fh, '<', $err or die "open $err failed: $!";
284 is($?, 0, 'no error in exited process');
285 unlike($eout, qr/wide/i, 'no Wide character warnings');
294 sysread($s, $buf, 4096, length($buf));
295 } until ($buf =~ /\r\n\.\r\n\z/);