use strict;
use warnings;
use Test::More;
-eval { require PublicInbox::SearchIdx };
-plan skip_all => "Xapian missing for nntpd" if $@;
-eval { require PublicInbox::Msgmap };
-plan skip_all => "DBD::SQLite missing for nntpd" if $@;
+foreach my $mod (qw(DBD::SQLite Search::Xapian Danga::Socket)) {
+ eval "require $mod";
+ plan skip_all => "$mod missing for nntpd.t" if $@;
+}
+require PublicInbox::SearchIdx;
+require PublicInbox::Msgmap;
use Cwd;
use Email::Simple;
use IO::Socket;
use File::Temp qw/tempdir/;
use Net::NNTP;
use IPC::Run qw(run);
-use Data::Dumper;
my $tmpdir = tempdir(CLEANUP => 1);
my $home = "$tmpdir/pi-home";
Cc: $addr
Message-Id: <nntp\@example.com>
Subject: hihi
-Date: Thu, 01 Jan 1970 00:00:00 +0000
+Date: Thu, 01 Jan 1970 06:06:06 +0000
nntp
EOF
is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
+ %opts = (
+ PeerAddr => $host_port,
+ Proto => 'tcp',
+ Type => SOCK_STREAM,
+ Timeout => 1,
+ );
my $mid = '<nntp@example.com>';
my %xhdr = (
'message-id' => $mid,
'subject' => 'hihi',
- 'date' => 'Thu, 01 Jan 1970 00:00:00 +0000',
+ 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
'from' => 'Me <me@example.com>',
'to' => 'You <you@example.com>',
'cc' => $addr,
'xref' => "example.com $group:1"
);
+
+ my $s = IO::Socket::INET->new(%opts);
+ sysread($s, my $buf, 4096);
+ is($buf, "201 server ready - post via email\r\n", 'got greeting');
+ $s->autoflush(1);
+
while (my ($k, $v) = each %xhdr) {
is_deeply($n->xhdr("$k $mid"), { $mid => $v },
- "$k by message-id works");
+ "XHDR $k by message-id works");
is_deeply($n->xhdr("$k 1"), { 1 => $v },
"$k by article number works");
+ is_deeply($n->xhdr("$k 1-"), { 1 => $v },
+ "$k by article range works");
+ next;
+ $buf = '';
+ syswrite($s, "HDR $k $mid\r\n");
+ do {
+ sysread($s, $buf, 4096, length($buf));
+ } until ($buf =~ /\r\n\.\r\n\z/);
+ my @r = split("\r\n", $buf);
+ like($r[0], qr/\A224 /, '224 response for HDR');
+ is($r[1], "0 $v", 'got expected response for HDR');
}
{
}
}
- # TODO: upgrades and such
+ is_deeply($n->xover('1-'), {
+ '1' => ['hihi',
+ 'Me <me@example.com>',
+ 'Thu, 01 Jan 1970 06:06:06 +0000',
+ '<nntp@example.com>',
+ '',
+ '202',
+ '1' ] }, "XOVER range works");
+
+ is_deeply($n->xover('1'), {
+ '1' => ['hihi',
+ 'Me <me@example.com>',
+ 'Thu, 01 Jan 1970 06:06:06 +0000',
+ '<nntp@example.com>',
+ '',
+ '202',
+ '1' ] }, "XOVER by article works");
+
+ {
+ syswrite($s, "OVER $mid\r\n");
+ $buf = '';
+ do {
+ sysread($s, $buf, 4096, length($buf));
+ } until ($buf =~ /\r\n\.\r\n\z/);
+ my @r = split("\r\n", $buf);
+ like($r[0], qr/^224 /, 'got 224 response for OVER');
+ is($r[1], "0\thihi\tMe <me\@example.com>\t" .
+ "Thu, 01 Jan 1970 06:06:06 +0000\t" .
+ "$mid\t\t202\t1", 'OVER by Message-ID works');
+ is($r[2], '.', 'correctly terminated response');
+ }
+
+ is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
+ 'XHDR Cc 1- works');
+ is_deeply($n->xhdr(qw(References 1-)), { 1 => '' },
+ 'XHDR References 1- works (empty string)');
+ is_deeply($n->xhdr(qw(list-id 1-)), {},
+ 'XHDR on invalid header returns empty');
+
+ {
+ syswrite($s, "HDR List-id 1-\r\n");
+ $buf = '';
+ do {
+ sysread($s, $buf, 4096, length($buf));
+ } until ($buf =~ /\r\n\z/);
+ my @r = split("\r\n", $buf);
+ like($r[0], qr/^5\d\d /,
+ 'got 5xx response for unoptimized HDR');
+ is(scalar @r, 1, 'only one response line');
+ }
ok(kill('TERM', $pid), 'killed nntpd');
$pid = undef;