1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (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 $maindir = "$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::Git';
33 LocalAddr => '127.0.0.1',
39 my $sock = IO::Socket::INET->new(%opts);
42 END { kill 'TERM', $pid if defined $pid };
44 local $ENV{HOME} = $home;
45 system($init, $group, $maindir, 'http://example.com/', $addr);
46 is(system(qw(git config), "--file=$home/.public-inbox/config",
47 "publicinbox.$group.newsgroup", $group),
48 0, 'enabled newsgroup');
51 # ensure successful message delivery
53 my $mime = Email::MIME->new(<<EOF);
54 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
55 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
57 Message-Id: <nntp\@example.com>
58 Content-Type: text/plain; charset=utf-8
59 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
60 Date: Thu, 01 Jan 1970 06:06:06 +0000
61 Content-Transfer-Encoding: 8bit
63 This is a test message for El\xc3\xa9anor
67 $mime->header_set('List-Id', "<$list_id>");
68 $len = length($mime->as_string);
69 my $git = PublicInbox::Git->new($maindir);
70 my $im = PublicInbox::Import->new($git, 'test', $addr);
73 my $s = PublicInbox::SearchIdx->new($maindir, 1);
77 ok($sock, 'sock created');
79 my $fl = fcntl($sock, F_GETFD, 0);
80 ok(! $!, 'no error from fcntl(F_GETFD)');
81 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
85 # pretend to be systemd
86 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
87 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
88 $ENV{LISTEN_PID} = $$;
90 exec $nntpd, "--stdout=$out", "--stderr=$err";
93 ok(defined $pid, 'forked nntpd process successfully');
95 fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
96 ok(! $!, 'no error from fcntl(F_SETFD)');
97 my $host_port = $sock->sockhost . ':' . $sock->sockport;
98 my $n = Net::NNTP->new($host_port);
100 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
101 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
104 PeerAddr => $host_port,
109 my $mid = '<nntp@example.com>';
111 'message-id' => $mid,
112 subject => "Testing for El\xc3\xa9anor",
113 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
114 'from' => "El\xc3\xa9anor <me\@example.com>",
115 'to' => "El\xc3\xa9anor <you\@example.com>",
117 'xref' => "example.com $group:1"
120 my $s = IO::Socket::INET->new(%opts);
121 sysread($s, my $buf, 4096);
122 is($buf, "201 server ready - post via email\r\n", 'got greeting');
125 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
126 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
128 $s = IO::Socket::INET->new(%opts);
129 sysread($s, $buf, 4096);
130 is($buf, "201 server ready - post via email\r\n", 'got greeting');
133 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
134 $buf = read_til_dot($s);
135 like($buf, qr/\A231 list of /, 'newgroups OK');
137 while (my ($k, $v) = each %xhdr) {
138 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
139 "XHDR $k by message-id works");
140 is_deeply($n->xhdr("$k 1"), { 1 => $v },
141 "$k by article number works");
142 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
143 "$k by article range works");
145 syswrite($s, "HDR $k $mid\r\n");
146 $buf = read_til_dot($s);
147 my @r = split("\r\n", $buf);
148 like($r[0], qr/\A225 /, '225 response for HDR');
149 is($r[1], "0 $v", 'got expected response for HDR');
153 my $nogroup = Net::NNTP->new($host_port);
154 while (my ($k, $v) = each %xhdr) {
155 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
156 "$k by message-id works without group");
160 is_deeply($n->xover('1-'), {
161 '1' => ["Testing for El\xc3\xa9anor",
162 "El\xc3\xa9anor <me\@example.com>",
163 'Thu, 01 Jan 1970 06:06:06 +0000',
164 '<nntp@example.com>',
167 '1' ] }, "XOVER range works");
169 is_deeply($n->xover('1'), {
170 '1' => ["Testing for El\xc3\xa9anor",
171 "El\xc3\xa9anor <me\@example.com>",
172 'Thu, 01 Jan 1970 06:06:06 +0000',
173 '<nntp@example.com>',
176 '1' ] }, "XOVER by article works");
178 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
179 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
180 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
181 'body really matches');
182 my $art = $n->article(1);
183 is(ref($art), 'ARRAY', 'got array for ARTICLE');
184 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
185 is($n->article(999), undef, 'non-existent num');
186 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
189 syswrite($s, "OVER $mid\r\n");
190 $buf = read_til_dot($s);
191 my @r = split("\r\n", $buf);
192 like($r[0], qr/^224 /, 'got 224 response for OVER');
193 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
194 "El\xc3\xa9anor <me\@example.com>\t" .
195 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
196 "$mid\t\t$len\t1", 'OVER by Message-ID works');
197 is($r[2], '.', 'correctly terminated response');
200 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
202 is_deeply($n->xhdr(qw(References 1-)), { 1 => '' },
203 'XHDR References 1- works (empty string)');
204 is_deeply($n->xhdr(qw(list-id 1-)), {},
205 'XHDR on invalid header returns empty');
211 ok($date >= $t0, 'valid date after start');
212 ok($date <= $t1, 'valid date before stop');
216 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
217 syswrite($s, 'HDR List-id 1-');
218 select(undef, undef, undef, 0.15);
219 ok(kill('TERM', $pid), 'killed nntpd');
220 select(undef, undef, undef, 0.15);
221 syswrite($s, "\r\n");
224 sysread($s, $buf, 4096, length($buf));
225 } until ($buf =~ /\r\n\z/);
226 my @r = split("\r\n", $buf);
227 like($r[0], qr/^5\d\d /,
228 'got 5xx response for unoptimized HDR');
229 is(scalar @r, 1, 'only one response line');
233 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
236 open my $fh, '<', $err or die "open $err failed: $!";
239 is($?, 0, 'no error in exited process');
240 unlike($eout, qr/wide/i, 'no Wide character warnings');
249 sysread($s, $buf, 4096, length($buf));
250 } until ($buf =~ /\r\n\.\r\n\z/);