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
65 $mime->header_set('List-Id', "<$addr>");
66 $len = length($mime->as_string);
67 my $git = PublicInbox::Git->new($maindir);
68 my $im = PublicInbox::Import->new($git, 'test', $addr);
71 my $s = PublicInbox::SearchIdx->new($maindir, 1);
75 ok($sock, 'sock created');
77 my $fl = fcntl($sock, F_GETFD, 0);
78 ok(! $!, 'no error from fcntl(F_GETFD)');
79 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
83 # pretend to be systemd
84 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
85 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
86 $ENV{LISTEN_PID} = $$;
88 exec $nntpd, "--stdout=$out", "--stderr=$err";
91 ok(defined $pid, 'forked nntpd process successfully');
93 fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
94 ok(! $!, 'no error from fcntl(F_SETFD)');
95 my $host_port = $sock->sockhost . ':' . $sock->sockport;
96 my $n = Net::NNTP->new($host_port);
98 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
99 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
102 PeerAddr => $host_port,
107 my $mid = '<nntp@example.com>';
109 'message-id' => $mid,
110 subject => "Testing for El\xc3\xa9anor",
111 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
112 'from' => "El\xc3\xa9anor <me\@example.com>",
113 'to' => "El\xc3\xa9anor <you\@example.com>",
115 'xref' => "example.com $group:1"
118 my $s = IO::Socket::INET->new(%opts);
119 sysread($s, my $buf, 4096);
120 is($buf, "201 server ready - post via email\r\n", 'got greeting');
123 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
124 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
126 $s = IO::Socket::INET->new(%opts);
127 sysread($s, $buf, 4096);
128 is($buf, "201 server ready - post via email\r\n", 'got greeting');
131 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
132 $buf = read_til_dot($s);
133 like($buf, qr/\A231 list of /, 'newgroups OK');
135 while (my ($k, $v) = each %xhdr) {
136 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
137 "XHDR $k by message-id works");
138 is_deeply($n->xhdr("$k 1"), { 1 => $v },
139 "$k by article number works");
140 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
141 "$k by article range works");
143 syswrite($s, "HDR $k $mid\r\n");
144 $buf = read_til_dot($s);
145 my @r = split("\r\n", $buf);
146 like($r[0], qr/\A225 /, '225 response for HDR');
147 is($r[1], "0 $v", 'got expected response for HDR');
151 my $nogroup = Net::NNTP->new($host_port);
152 while (my ($k, $v) = each %xhdr) {
153 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
154 "$k by message-id works without group");
158 is_deeply($n->xover('1-'), {
159 '1' => ["Testing for El\xc3\xa9anor",
160 "El\xc3\xa9anor <me\@example.com>",
161 'Thu, 01 Jan 1970 06:06:06 +0000',
162 '<nntp@example.com>',
165 '1' ] }, "XOVER range works");
167 is_deeply($n->xover('1'), {
168 '1' => ["Testing for El\xc3\xa9anor",
169 "El\xc3\xa9anor <me\@example.com>",
170 'Thu, 01 Jan 1970 06:06:06 +0000',
171 '<nntp@example.com>',
174 '1' ] }, "XOVER by article works");
176 is_deeply($n->head(1), $n->head('<nntp@example.com>'), 'HEAD OK');
177 is_deeply($n->body(1), $n->body('<nntp@example.com>'), 'BODY OK');
178 is($n->body(1)->[0], "This is a test message for El\xc3\xa9anor\n",
179 'body really matches');
180 my $art = $n->article(1);
181 is(ref($art), 'ARRAY', 'got array for ARTICLE');
182 is_deeply($art, $n->article('<nntp@example.com>'), 'ARTICLE OK');
183 is($n->article(999), undef, 'non-existent num');
184 is($n->article('<non-existent@example>'), undef, 'non-existent mid');
187 syswrite($s, "OVER $mid\r\n");
188 $buf = read_til_dot($s);
189 my @r = split("\r\n", $buf);
190 like($r[0], qr/^224 /, 'got 224 response for OVER');
191 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
192 "El\xc3\xa9anor <me\@example.com>\t" .
193 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
194 "$mid\t\t$len\t1", 'OVER by Message-ID works');
195 is($r[2], '.', 'correctly terminated response');
198 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
200 is_deeply($n->xhdr(qw(References 1-)), { 1 => '' },
201 'XHDR References 1- works (empty string)');
202 is_deeply($n->xhdr(qw(list-id 1-)), {},
203 'XHDR on invalid header returns empty');
206 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
207 syswrite($s, 'HDR List-id 1-');
208 select(undef, undef, undef, 0.15);
209 ok(kill('TERM', $pid), 'killed nntpd');
210 select(undef, undef, undef, 0.15);
211 syswrite($s, "\r\n");
214 sysread($s, $buf, 4096, length($buf));
215 } until ($buf =~ /\r\n\z/);
216 my @r = split("\r\n", $buf);
217 like($r[0], qr/^5\d\d /,
218 'got 5xx response for unoptimized HDR');
219 is(scalar @r, 1, 'only one response line');
222 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
225 open my $fh, '<', $err or die "open $err failed: $!";
228 is($?, 0, 'no error in exited process');
229 unlike($eout, qr/wide/i, 'no Wide character warnings');
238 sysread($s, $buf, 4096, length($buf));
239 } until ($buf =~ /\r\n\.\r\n\z/);