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 while (my ($k, $v) = each %xhdr) {
122 is_deeply($n->xhdr("$k $mid"), { $mid => $v },
123 "XHDR $k by message-id works");
124 is_deeply($n->xhdr("$k 1"), { 1 => $v },
125 "$k by article number works");
126 is_deeply($n->xhdr("$k 1-"), { 1 => $v },
127 "$k by article range works");
129 syswrite($s, "HDR $k $mid\r\n");
131 sysread($s, $buf, 4096, length($buf));
132 } until ($buf =~ /\r\n\.\r\n\z/);
133 my @r = split("\r\n", $buf);
134 like($r[0], qr/\A225 /, '225 response for HDR');
135 is($r[1], "0 $v", 'got expected response for HDR');
139 my $nogroup = Net::NNTP->new($host_port);
140 while (my ($k, $v) = each %xhdr) {
141 is_deeply($nogroup->xhdr("$k $mid"), { $mid => $v },
142 "$k by message-id works without group");
146 is_deeply($n->xover('1-'), {
147 '1' => ["Testing for El\xc3\xa9anor",
148 "El\xc3\xa9anor <me\@example.com>",
149 'Thu, 01 Jan 1970 06:06:06 +0000',
150 '<nntp@example.com>',
153 '1' ] }, "XOVER range works");
155 is_deeply($n->xover('1'), {
156 '1' => ["Testing for El\xc3\xa9anor",
157 "El\xc3\xa9anor <me\@example.com>",
158 'Thu, 01 Jan 1970 06:06:06 +0000',
159 '<nntp@example.com>',
162 '1' ] }, "XOVER by article works");
165 syswrite($s, "OVER $mid\r\n");
168 sysread($s, $buf, 4096, length($buf));
169 } until ($buf =~ /\r\n\.\r\n\z/);
170 my @r = split("\r\n", $buf);
171 like($r[0], qr/^224 /, 'got 224 response for OVER');
172 is($r[1], "0\tTesting for El\xc3\xa9anor\t" .
173 "El\xc3\xa9anor <me\@example.com>\t" .
174 "Thu, 01 Jan 1970 06:06:06 +0000\t" .
175 "$mid\t\t$len\t1", 'OVER by Message-ID works');
176 is($r[2], '.', 'correctly terminated response');
179 is_deeply($n->xhdr(qw(Cc 1-)), { 1 => 'test-nntpd@example.com' },
181 is_deeply($n->xhdr(qw(References 1-)), { 1 => '' },
182 'XHDR References 1- works (empty string)');
183 is_deeply($n->xhdr(qw(list-id 1-)), {},
184 'XHDR on invalid header returns empty');
187 setsockopt($s, IPPROTO_TCP, TCP_NODELAY, 1);
188 syswrite($s, 'HDR List-id 1-');
189 select(undef, undef, undef, 0.15);
190 ok(kill('TERM', $pid), 'killed nntpd');
191 select(undef, undef, undef, 0.15);
192 syswrite($s, "\r\n");
195 sysread($s, $buf, 4096, length($buf));
196 } until ($buf =~ /\r\n\z/);
197 my @r = split("\r\n", $buf);
198 like($r[0], qr/^5\d\d /,
199 'got 5xx response for unoptimized HDR');
200 is(scalar @r, 1, 'only one response line');
203 is($pid, waitpid($pid, 0), 'nntpd exited successfully');
206 open my $fh, '<', $err or die "open $err failed: $!";
209 is($?, 0, 'no error in exited process');
210 unlike($eout, qr/wide/i, 'no Wide character warnings');