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 $cfgpfx = "publicinbox.$group";
28 my $nntpd = 'blib/script/public-inbox-nntpd';
29 my $init = 'blib/script/public-inbox-init';
30 use_ok 'PublicInbox::Import';
31 use_ok 'PublicInbox::Git';
34 LocalAddr => '127.0.0.1',
40 my $sock = IO::Socket::INET->new(%opts);
43 END { kill 'TERM', $pid if defined $pid };
45 local $ENV{HOME} = $home;
46 system($init, $group, $maindir, 'http://example.com/', $addr);
49 # ensure successful message delivery
51 my $mime = Email::MIME->new(<<EOF);
52 To: =?utf-8?Q?El=C3=A9anor?= <you\@example.com>
53 From: =?utf-8?Q?El=C3=A9anor?= <me\@example.com>
55 Message-Id: <nntp\@example.com>
56 Content-Type: text/plain; charset=utf-8
57 Subject: Testing for =?utf-8?Q?El=C3=A9anor?=
58 Date: Thu, 01 Jan 1970 06:06:06 +0000
59 Content-Transfer-Encoding: 8bit
61 This is a test message for El\xc3\xa9anor
63 $mime->header_set('List-Id', "<$addr>");
64 $len = length($mime->as_string);
65 my $git = PublicInbox::Git->new($maindir);
66 my $im = PublicInbox::Import->new($git, 'test', $addr);
69 my $s = PublicInbox::SearchIdx->new($maindir, 1);
73 ok($sock, 'sock created');
75 my $fl = fcntl($sock, F_GETFD, 0);
76 ok(! $!, 'no error from fcntl(F_GETFD)');
77 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
81 # pretend to be systemd
82 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
83 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
84 $ENV{LISTEN_PID} = $$;
86 exec $nntpd, "--stdout=$out", "--stderr=$err";
89 ok(defined $pid, 'forked nntpd process successfully');
91 fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
92 ok(! $!, 'no error from fcntl(F_SETFD)');
93 my $host_port = $sock->sockhost . ':' . $sock->sockport;
94 my $n = Net::NNTP->new($host_port);
96 is_deeply($list, { $group => [ qw(1 1 n) ] }, 'LIST works');
97 is_deeply([$n->group($group)], [ qw(0 1 1), $group ], 'GROUP works');
100 PeerAddr => $host_port,
105 my $mid = '<nntp@example.com>';
107 'message-id' => $mid,
108 subject => "Testing for El\xc3\xa9anor",
109 'date' => 'Thu, 01 Jan 1970 06:06:06 +0000',
110 'from' => "El\xc3\xa9anor <me\@example.com>",
111 'to' => "El\xc3\xa9anor <you\@example.com>",
113 'xref' => "example.com $group:1"
116 my $s = IO::Socket::INET->new(%opts);
117 sysread($s, my $buf, 4096);
118 is($buf, "201 server ready - post via email\r\n", 'got greeting');
121 syswrite($s, "NEWGROUPS\t19990424 000000 \033GMT\007\r\n");
122 is(0, sysread($s, $buf, 4096), 'GOT EOF on cntrl');
124 $s = IO::Socket::INET->new(%opts);
125 sysread($s, $buf, 4096);
126 is($buf, "201 server ready - post via email\r\n", 'got greeting');
129 syswrite($s, "NEWGROUPS 19990424 000000 GMT\r\n");
130 $buf = read_til_dot($s);
131 like($buf, qr/\A231 list of /, 'newgroups OK');
133 while (my ($k, $v) = each %xhdr) {
134 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
135 "XHDR $k by message-id works");
136 is_deeply($n->xhdr("$k 1"), { 1 => $v },
137 "$k by article number works");
138 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
139 "$k by article range works");
141 syswrite($s, "HDR $k $mid\r\n");
142 $buf = read_til_dot($s);
143 my @r = split("\r\n", $buf);
144 like($r[0], qr/\A225 /, '225 response for HDR');
145 is($r[1], "0 $v", 'got expected response for HDR');
149 my $nogroup = Net::NNTP->new($host_port);
150 while (my ($k, $v) = each %xhdr) {
151 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
152 "$k by message-id works without group");
156 is_deeply($n->xover('1-'), {
157 '1' => ["Testing for El\xc3\xa9anor",
158 "El\xc3\xa9anor <me\@example.com>",
159 'Thu, 01 Jan 1970 06:06:06 +0000',
160 '<nntp@example.com>',
163 '1' ] }, "XOVER range works");
165 is_deeply($n->xover('1'), {
166 '1' => ["Testing for El\xc3\xa9anor",
167 "El\xc3\xa9anor <me\@example.com>",
168 'Thu, 01 Jan 1970 06:06:06 +0000',
169 '<nntp@example.com>',
172 '1' ] }, "XOVER by article works");
175 syswrite($s, "OVER $mid\r\n");
176 $buf = read_til_dot($s);
177 my @r = split("\r\n", $buf);
178 like($r[0], qr/^224 /, 'got 224 response for OVER');
179 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
180 "El\xc3\xa9anor <me\@example.com>\t" .
181 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
182 "$mid\t\t$len\t1", 'OVER by Message-ID works');
183 is($r[2], '.', 'correctly terminated response');
186 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
188 is_deeply($n->xhdr(qw(References 1-)), { 1 => '' },
189 'XHDR References 1- works (empty string)');
190 is_deeply($n->xhdr(qw(list-id 1-)), {},
191 'XHDR on invalid header returns empty');
194 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
195 syswrite($s, 'HDR List-id 1-');
196 select(undef, undef, undef, 0.15);
197 ok(kill('TERM', $pid), 'killed nntpd');
198 select(undef, undef, undef, 0.15);
199 syswrite($s, "\r\n");
202 sysread($s, $buf, 4096, length($buf));
203 } until ($buf =~ /\r\n\z/);
204 my @r = split("\r\n", $buf);
205 like($r[0], qr/^5\d\d /,
206 'got 5xx response for unoptimized HDR');
207 is(scalar @r, 1, 'only one response line');
210 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
213 open my $fh, '<', $err or die "open $err failed: $!";
216 is($?, 0, 'no error in exited process');
217 unlike($eout, qr/wide/i, 'no Wide character warnings');
226 sysread($s, $buf, 4096, length($buf));
227 } until ($buf =~ /\r\n\.\r\n\z/);