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 # FIXME: make easier to test both versions
22 my $version = $ENV{PI_VERSION} || 2;
23 require_git('2.6') if $version == 2;
25 my $tmpdir = tempdir('pi-nntpd-XXXXXX', TMPDIR => 1, CLEANUP => 1);
26 my $home = "$tmpdir/pi-home";
27 my $err = "$tmpdir/stderr.log";
28 my $out = "$tmpdir/stdout.log";
29 my $mainrepo = "$tmpdir/main.git";
30 my $group = 'test-nntpd';
31 my $addr = $group . '@example.com';
32 my $nntpd = 'blib/script/public-inbox-nntpd';
33 my $init = 'blib/script/public-inbox-init';
34 use_ok 'PublicInbox::Import';
35 use_ok 'PublicInbox::Inbox';
36 use_ok 'PublicInbox::Git';
38 skip "git 2.6+ required for V2Writable", 1 if $version == 1;
39 use_ok 'PublicInbox::V2Writable';
43 LocalAddr => '127.0.0.1',
49 my $sock = IO::Socket::INET->new(%opts);
52 END { kill 'TERM', $pid if defined $pid };
55 mainrepo => $mainrepo,
58 -primary_address => $addr,
60 $ibx = PublicInbox::Inbox->new($ibx);
62 local $ENV{HOME} = $home;
63 my @cmd = ($init, $group, $mainrepo, 'http://example.com/', $addr);
64 push @cmd, "-V$version";
65 is(system(@cmd), 0, 'init OK');
66 is(system(qw(git config), "--file=$home/.public-inbox/config",
67 "publicinbox.$group.newsgroup", $group),
68 0, 'enabled newsgroup');
73 $im = PublicInbox::V2Writable->new($ibx);
74 } elsif ($version == 1) {
75 my $git = PublicInbox::Git->new($mainrepo);
76 $im = PublicInbox::Import->new($git, 'test', $addr);
78 die "unsupported version: $version";
81 # ensure successful message delivery
83 my $mime = Email::MIME->new(<<EOF);
84 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
85 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
87 Message-Id: <nntp\@example.com>
88 Content-Type: text/plain; charset=utf-8
89 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
90 Date: Thu, 01 Jan 1970 06:06:06 +0000
91 Content-Transfer-Encoding: 8bit
92 References: <ref tab squeezed>
94 This is a test message for El\xc3\xa9anor
98 $mime->header_set('List-Id', "<$list_id>");
99 $len = length($mime->as_string);
103 my $s = PublicInbox::SearchIdx->new($mainrepo, 1);
108 ok($sock, 'sock created');
109 my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
110 $pid = spawn_listener(undef, $cmd, [ $sock ]);
111 ok(defined $pid, 'forked nntpd process successfully');
112 my $host_port = $sock->sockhost . ':' . $sock->sockport;
113 my $n = Net::NNTP->new($host_port);
115 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
116 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
117 is_deeply($n->listgroup($group), [1], 'listgroup OK');
120 PeerAddr => $host_port,
125 my $mid = '<nntp@example.com>';
127 'message-id' => $mid,
128 subject => "Testing for El\xc3\xa9anor",
129 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
130 'from' => "El\xc3\xa9anor <me\@example.com>",
131 'to' => "El\xc3\xa9anor <you\@example.com>",
133 'xref' => hostname . " $group:1",
134 'references' => '<reftabsqueezed>',
137 my $s = IO::Socket::INET->new(%opts);
138 sysread($s, my $buf, 4096);
139 is($buf, "201 " . hostname . " ready - post via email\r\n",
143 ok(syswrite($s, " \r\n"), 'wrote spaces');
144 ok(syswrite($s, "\r\n"), 'wrote nothing');
145 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
146 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
148 $s = IO::Socket::INET->new(%opts);
149 sysread($s, $buf, 4096);
150 is($buf, "201 " . hostname . " ready - post via email\r\n",
154 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
155 $buf = read_til_dot($s);
156 like($buf, qr/\A231 list of /, 'newgroups OK');
158 while (my ($k, $v) = each %xhdr) {
159 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
160 "XHDR $k by message-id works");
161 is_deeply($n->xhdr("$k 1"), { 1 => $v },
162 "$k by article number works");
163 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
164 "$k by article range works");
166 syswrite($s, "HDR $k $mid\r\n");
167 $buf = read_til_dot($s);
168 my @r = split("\r\n", $buf);
169 like($r[0], qr/\A225 /, '225 response for HDR');
170 is($r[1], "0 $v", 'got expected response for HDR');
174 my $nogroup = Net::NNTP->new($host_port);
175 while (my ($k, $v) = each %xhdr) {
176 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
177 "$k by message-id works without group");
181 is_deeply($n->xover('1-'), {
182 '1' => ["Testing for El\xc3\xa9anor",
183 "El\xc3\xa9anor <me\@example.com>",
184 'Thu, 01 Jan 1970 06:06:06 +0000',
185 '<nntp@example.com>',
189 'Xref: '. hostname . ' test-nntpd:1'] },
190 "XOVER range works");
192 is_deeply($n->xover('1'), {
193 '1' => ["Testing for El\xc3\xa9anor",
194 "El\xc3\xa9anor <me\@example.com>",
195 'Thu, 01 Jan 1970 06:06:06 +0000',
196 '<nntp@example.com>',
200 'Xref: '. hostname . ' test-nntpd:1'] },
201 "XOVER by article works");
203 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
204 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
205 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
206 'body really matches');
207 my $art = $n->article(1);
208 is(ref($art), 'ARRAY', 'got array for ARTICLE');
209 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
210 is($n->article(999), undef, 'non-existent num');
211 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
214 syswrite($s, "OVER $mid\r\n");
215 $buf = read_til_dot($s);
216 my @r = split("\r\n", $buf);
217 like($r[0], qr/^224 /, 'got 224 response for OVER');
218 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
219 "El\xc3\xa9anor <me\@example.com>\t" .
220 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
221 "$mid\t<reftabsqueezed>\t$len\t1" .
222 "\tXref: " . hostname . " test-nntpd:0",
223 'OVER by Message-ID works');
224 is($r[2], '.', 'correctly terminated response');
227 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
229 is_deeply($n->xhdr(qw(References 1-)), { 1 => '<reftabsqueezed>' },
230 'XHDR References 1- works)');
231 is_deeply($n->xhdr(qw(list-id 1-)), {},
232 'XHDR on invalid header returns empty');
234 my $mids = $n->newnews(0, '*');
235 is_deeply($mids, ['<nntp@example.com>'], 'NEWNEWS works');
240 ok($date >= $t0, 'valid date after start');
241 ok($date <= $t1, 'valid date before stop');
244 # pipelined requests:
247 syswrite($s, "GROUP $group\r\n");
253 <$s> =~ /\A224 / or _exit(1);
254 <$s> =~ /\A1/ or _exit(2);
255 <$s> eq ".\r\n" or _exit(3);
260 syswrite($s, "XOVER 1\r\n");
262 is($rdr, waitpid($rdr, 0), 'reader done');
263 is($? >> 8, 0, 'no errors');
266 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
267 syswrite($s, 'HDR List-id 1-');
268 select(undef, undef, undef, 0.15);
269 ok(kill('TERM', $pid), 'killed nntpd');
270 select(undef, undef, undef, 0.15);
271 syswrite($s, "\r\n");
274 sysread($s, $buf, 4096, length($buf));
275 } until ($buf =~ /\r\n\z/);
276 my @r = split("\r\n", $buf);
277 like($r[0], qr/^5\d\d /,
278 'got 5xx response for unoptimized HDR');
279 is(scalar @r, 1, 'only one response line');
283 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
286 open my $fh, '<', $err or die "open $err failed: $!";
289 is($?, 0, 'no error in exited process');
290 unlike($eout, qr/wide/i, 'no Wide character warnings');
299 sysread($s, $buf, 4096, length($buf));
300 } until ($buf =~ /\r\n\.\r\n\z/);