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)) {
8 plan skip_all => "$mod missing for nntpd.t" if $@;
10 require PublicInbox::SearchIdx;
11 require PublicInbox::Msgmap;
14 use Socket qw(IPPROTO_TCP TCP_NODELAY);
15 use File::Temp qw/tempdir/;
18 require './t/common.perl';
20 # FIXME: make easier to test both versions
21 my $version = $ENV{PI_VERSION} || 2;
22 require_git('2.6') if $version == 2;
24 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
25 my $home = "$tmpdir/pi-home";
26 my $err = "$tmpdir/stderr.log";
27 my $out = "$tmpdir/stdout.log";
28 my $mainrepo = "$tmpdir/main.git";
29 my $group = 'test-nntpd';
30 my $addr = $group . '@example.com';
31 my $nntpd = 'blib/script/public-inbox-nntpd';
32 my $init = 'blib/script/public-inbox-init';
33 use_ok 'PublicInbox::Import';
34 use_ok 'PublicInbox::Inbox';
35 use_ok 'PublicInbox::Git';
37 skip "git 2.6+ required for V2Writable", 1 if $version == 1;
38 use_ok 'PublicInbox::V2Writable';
42 LocalAddr => '127.0.0.1',
48 my $sock = IO::Socket::INET->new(%opts);
51 END { kill 'TERM', $pid if defined $pid };
54 mainrepo => $mainrepo,
57 -primary_address => $addr,
59 $ibx = PublicInbox::Inbox->new($ibx);
61 local $ENV{HOME} = $home;
62 my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
63 push @cmd, "-V$version";
64 is(system(@cmd), 0, 'init OK');
65 is(system(qw(git config), "--file=$home/.public-inbox/config",
66 "publicinbox.$group.newsgroup", $group),
67 0, 'enabled newsgroup');
72 $im = PublicInbox::V2Writable->new($ibx);
73 } elsif ($version == 1) {
74 my $git = PublicInbox::Git->new($mainrepo);
75 $im = PublicInbox::Import->new($git, 'test', $addr);
77 die "unsupported version: $version";
80 # ensure successful message delivery
82 my $mime = Email::MIME->new(<<EOF);
83 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
84 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
86 Message-Id: <nntp\@example.com>
87 Content-Type: text/plain; charset=utf-8
88 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
89 Date: Thu, 01 Jan 1970 06:06:06 +0000
90 Content-Transfer-Encoding: 8bit
91 References: <ref tab squeezed>
93 This is a test message for El\xc3\xa9anor
97 $mime->header_set('List-Id', "<$list_id>");
98 $len = length($mime->as_string);
102 my $s = PublicInbox::SearchIdx->new($mainrepo, 1);
107 ok($sock, 'sock created');
108 my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
109 $pid = spawn_listener(undef, $cmd, [ $sock ]);
110 ok(defined $pid, 'forked nntpd process successfully');
111 my $host_port = $sock->sockhost . ':' . $sock->sockport;
112 my $n = Net::NNTP->new($host_port);
114 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
115 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
116 is_deeply($n->listgroup($group), [1], 'listgroup OK');
119 PeerAddr => $host_port,
124 my $mid = '<nntp@example.com>';
126 'message-id' => $mid,
127 subject => "Testing for El\xc3\xa9anor",
128 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
129 'from' => "El\xc3\xa9anor <me\@example.com>",
130 'to' => "El\xc3\xa9anor <you\@example.com>",
132 'xref' => hostname . " $group:1",
133 'references' => '<reftabsqueezed>',
136 my $s = IO::Socket::INET->new(%opts);
137 sysread($s, my $buf, 4096);
138 is($buf, "201 " . hostname . " ready - post via email\r\n",
142 ok(syswrite($s, " \r\n"), 'wrote spaces');
143 ok(syswrite($s, "\r\n"), 'wrote nothing');
144 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
145 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
147 $s = IO::Socket::INET->new(%opts);
148 sysread($s, $buf, 4096);
149 is($buf, "201 " . hostname . " ready - post via email\r\n",
153 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
154 $buf = read_til_dot($s);
155 like($buf, qr/\A231 list of /, 'newgroups OK');
157 while (my ($k, $v) = each %xhdr) {
158 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
159 "XHDR $k by message-id works");
160 is_deeply($n->xhdr("$k 1"), { 1 => $v },
161 "$k by article number works");
162 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
163 "$k by article range works");
165 syswrite($s, "HDR $k $mid\r\n");
166 $buf = read_til_dot($s);
167 my @r = split("\r\n", $buf);
168 like($r[0], qr/\A225 /, '225 response for HDR');
169 is($r[1], "0 $v", 'got expected response for HDR');
173 my $nogroup = Net::NNTP->new($host_port);
174 while (my ($k, $v) = each %xhdr) {
175 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
176 "$k by message-id works without group");
180 is_deeply($n->xover('1-'), {
181 '1' => ["Testing for El\xc3\xa9anor",
182 "El\xc3\xa9anor <me\@example.com>",
183 'Thu, 01 Jan 1970 06:06:06 +0000',
184 '<nntp@example.com>',
188 'Xref: '. hostname . ' test-nntpd:1'] },
189 "XOVER range works");
191 is_deeply($n->xover('1'), {
192 '1' => ["Testing for El\xc3\xa9anor",
193 "El\xc3\xa9anor <me\@example.com>",
194 'Thu, 01 Jan 1970 06:06:06 +0000',
195 '<nntp@example.com>',
199 'Xref: '. hostname . ' test-nntpd:1'] },
200 "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<reftabsqueezed>\t$len\t1" .
221 "\tXref: " . hostname . " test-nntpd:0",
222 'OVER by Message-ID works');
223 is($r[2], '.', 'correctly terminated response');
226 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
228 is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
229 'XHDR References 1- works)');
230 is_deeply($n->xhdr(qw(list-id 1-)), {},
231 'XHDR on invalid header returns empty');
233 my $mids = $n->newnews(0, '*');
234 is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
239 ok($date >= $t0, 'valid date after start');
240 ok($date <= $t1, 'valid date before stop');
243 # pipelined requests:
246 syswrite($s, "GROUP $group\r\n");
252 <$s> =~ /\A224 / or _exit(1);
253 <$s> =~ /\A1/ or _exit(2);
254 <$s> eq ".\r\n" or _exit(3);
259 syswrite($s, "XOVER 1\r\n");
261 is($rdr, waitpid($rdr, 0), 'reader done');
262 is($? >> 8, 0, 'no errors');
265 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
266 syswrite($s, 'HDR List-id 1-');
267 select(undef, undef, undef, 0.15);
268 ok(kill('TERM', $pid), 'killed nntpd');
269 select(undef, undef, undef, 0.15);
270 syswrite($s, "\r\n");
273 sysread($s, $buf, 4096, length($buf));
274 } until ($buf =~ /\r\n\z/);
275 my @r = split("\r\n", $buf);
276 like($r[0], qr/^5\d\d /,
277 'got 5xx response for unoptimized HDR');
278 is(scalar @r, 1, 'only one response line');
282 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
285 open my $fh, '<', $err or die "open $err failed: $!";
288 is($?, 0, 'no error in exited process');
289 unlike($eout, qr/wide/i, 'no Wide character warnings');
298 sysread($s, $buf, 4096, length($buf));
299 } until ($buf =~ /\r\n\.\r\n\z/);