]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2writable.t
nntp: simplify the long_response API
[public-inbox.git] / t / v2writable.t
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::MIME;
7 use PublicInbox::ContentId qw(content_digest);
8 use File::Temp qw/tempdir/;
9 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
10         eval "require $mod";
11         plan skip_all => "$mod missing for nntpd.t" if $@;
12 }
13 use_ok 'PublicInbox::V2Writable';
14 my $mainrepo = tempdir('pi-v2writable-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $ibx = {
16         mainrepo => $mainrepo,
17         name => 'test-v2writable',
18         version => 2,
19         -primary_address => 'test@example.com',
20 };
21 $ibx = PublicInbox::Inbox->new($ibx);
22 my $mime = PublicInbox::MIME->create(
23         header => [
24                 From => 'a@example.com',
25                 To => 'test@example.com',
26                 Subject => 'this is a subject',
27                 'Message-ID' => '<a-mid@b>',
28                 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
29         ],
30         body => "hello world\n",
31 );
32
33 my $im = eval {
34         local $ENV{NPROC} = '1';
35         PublicInbox::V2Writable->new($ibx, 1);
36 };
37 is($im->{partitions}, 1, 'one partition when forced');
38 ok($im->add($mime), 'ordinary message added');
39 my $git0;
40
41 if ('ensure git configs are correct') {
42         my @cmd = (qw(git config), "--file=$mainrepo/all.git/config",
43                 qw(core.sharedRepository 0644));
44         is(system(@cmd), 0, "set sharedRepository in all.git");
45         $git0 = PublicInbox::Git->new("$mainrepo/git/0.git");
46         chomp(my $v = $git0->qx(qw(config core.sharedRepository)));
47         is($v, '0644', 'child repo inherited core.sharedRepository');
48         chomp($v = $git0->qx(qw(config --bool repack.writeBitmaps)));
49         is($v, 'true', 'child repo inherited repack.writeBitmaps');
50 }
51
52 {
53         my @warn;
54         local $SIG{__WARN__} = sub { push @warn, @_ };
55         is($im->add($mime), undef, 'obvious duplicate rejected');
56         is(scalar(@warn), 0, 'no warning about resent message');
57
58         @warn = ();
59         $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
60         is($im->add($mime), undef, 'secondary MID ignored if first matches');
61         my $sec = PublicInbox::MIME->new($mime->as_string);
62         $sec->header_set('Date');
63         $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
64         ok($im->add($sec), 'secondary MID used if data is different');
65         like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
66         like(join(' ', @warn), qr/alternative/, 'warned about alternative');
67         is_deeply([ '<a-mid@b>', '<c@d>' ],
68                 [ $sec->header_obj->header_raw('Message-Id') ],
69                 'no new Message-Id added');
70
71         my $sane_mid = qr/\A<[\w\-]+\@localhost>\z/;
72         @warn = ();
73         $mime->header_set('Message-Id', '<a-mid@b>');
74         $mime->body_set('different');
75         ok($im->add($mime), 'reused mid ok');
76         like(join(' ', @warn), qr/reused/, 'warned about reused MID');
77         my @mids = $mime->header_obj->header_raw('Message-Id');
78         is($mids[0], '<a-mid@b>', 'original mid not changed');
79         like($mids[1], $sane_mid, 'new MID added');
80         is(scalar(@mids), 2, 'only one new MID added');
81
82         @warn = ();
83         $mime->header_set('Message-Id', '<a-mid@b>');
84         $mime->body_set('this one needs a random mid');
85         my $gen = PublicInbox::Import::digest2mid(content_digest($mime));
86         unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
87         my $fake = PublicInbox::MIME->new($mime->as_string);
88         $fake->header_set('Message-Id', "<$gen>");
89         ok($im->add($fake), 'fake added easily');
90         is_deeply(\@warn, [], 'no warnings from a faker');
91         ok($im->add($mime), 'random MID made');
92         like(join(' ', @warn), qr/using random/, 'warned about using random');
93         @mids = $mime->header_obj->header_raw('Message-Id');
94         is($mids[0], '<a-mid@b>', 'original mid not changed');
95         like($mids[1], $sane_mid, 'new MID added');
96         is(scalar(@mids), 2, 'only one new MID added');
97
98         @warn = ();
99         $mime->header_set('Message-Id');
100         ok($im->add($mime), 'random MID made for MID free message');
101         @mids = $mime->header_obj->header_raw('Message-Id');
102         like($mids[0], $sane_mid, 'mid was generated');
103         is(scalar(@mids), 1, 'new generated');
104 }
105
106 {
107         $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
108         $mime->header_set('References', '<zz-mid@b>');
109         ok($im->add($mime), 'message with multiple Message-ID');
110         $im->done;
111         my @found;
112         my $srch = $ibx->search;
113         $srch->reopen->each_smsg_by_mid('abcde@1', sub { push @found, @_; 1 });
114         is(scalar(@found), 1, 'message found by first MID');
115         $srch->reopen->each_smsg_by_mid('abcde@2', sub { push @found, @_; 1 });
116         is(scalar(@found), 2, 'message found by second MID');
117         is($found[0]->{doc_id}, $found[1]->{doc_id}, 'same document');
118         ok($found[1]->{doc_id} > 0, 'doc_id is positive');
119 }
120
121 SKIP: {
122         use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
123         use Net::NNTP;
124         use IO::Socket;
125         use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
126         eval { require Danga::Socket };
127         skip "Danga::Socket missing $@", 2 if $@;
128         my $err = "$mainrepo/stderr.log";
129         my $out = "$mainrepo/stdout.log";
130         my %opts = (
131                 LocalAddr => '127.0.0.1',
132                 ReuseAddr => 1,
133                 Proto => 'tcp',
134                 Type => SOCK_STREAM,
135                 Listen => 1024,
136         );
137         my $group = 'inbox.comp.test.v2writable';
138         my $pi_config = "$mainrepo/pi_config";
139         open my $fh, '>', $pi_config or die "open: $!\n";
140         print $fh <<EOF
141 [publicinbox "test-v2writable"]
142         mainrepo = $mainrepo
143         version = 2
144         address = test\@example.com
145         newsgroup = $group
146 EOF
147         ;
148         close $fh or die "close: $!\n";
149         my $sock = IO::Socket::INET->new(%opts);
150         ok($sock, 'sock created');
151         my $pid;
152         my $len;
153         END { kill 'TERM', $pid if defined $pid };
154         $! = 0;
155         my $fl = fcntl($sock, F_GETFD, 0);
156         ok(! $!, 'no error from fcntl(F_GETFD)');
157         is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
158         $pid = fork;
159         if ($pid == 0) {
160                 use POSIX qw(dup2);
161                 $ENV{PI_CONFIG} = $pi_config;
162                 # pretend to be systemd
163                 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
164                 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
165                 $ENV{LISTEN_PID} = $$;
166                 $ENV{LISTEN_FDS} = 1;
167                 my $nntpd = 'blib/script/public-inbox-nntpd';
168                 exec $nntpd, "--stdout=$out", "--stderr=$err";
169                 die "FAIL: $!\n";
170         }
171         ok(defined $pid, 'forked nntpd process successfully');
172         $! = 0;
173         fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
174         ok(! $!, 'no error from fcntl(F_SETFD)');
175         my $host_port = $sock->sockhost . ':' . $sock->sockport;
176         my $n = Net::NNTP->new($host_port);
177         $n->group($group);
178         my $x = $n->xover('1-');
179         my %uniq;
180         foreach my $num (sort { $a <=> $b } keys %$x) {
181                 my $mid = $x->{$num}->[3];
182                 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
183                 is_deeply($n->xhdr('Message-ID', $num),
184                          { $num => $mid }, "XHDR lookup OK on num $num");
185                 is_deeply($n->xhdr('Message-ID', $mid),
186                          { $mid => $mid }, "XHDR lookup OK on MID $num");
187         }
188         my %nn;
189         foreach my $mid (@{$n->newnews(0, $group)}) {
190                 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
191         }
192         is_deeply([sort keys %nn], [sort keys %uniq]);
193
194         my %lg;
195         foreach my $num (@{$n->listgroup($group)}) {
196                 is($lg{$num}++, 0, "num is unique in LISTGROUP");
197         }
198         is_deeply([sort keys %lg], [sort keys %$x],
199                 'XOVER and LISTGROUPS return the same article numbers');
200
201         my $xref = $n->xhdr('Xref', '1-');
202         is_deeply([sort keys %lg], [sort keys %$xref], 'Xref range OK');
203
204         my $mids = $n->xhdr('Message-ID', '1-');
205         is_deeply([sort keys %lg], [sort keys %$xref], 'Message-ID range OK');
206
207         my $rover = $n->xrover('1-');
208         is_deeply([sort keys %lg], [sort keys %$rover], 'XROVER range OK');
209 };
210 {
211         local $ENV{NPROC} = 2;
212         my @before = $git0->qx(qw(log --pretty=oneline));
213         my $before = $git0->qx(qw(log --pretty=raw --raw -r --no-abbrev));
214         $im = PublicInbox::V2Writable->new($ibx, 1);
215         is($im->{partitions}, 1, 'detected single partition from previous');
216         my $smsg = $im->remove($mime, 'test removal');
217         my @after = $git0->qx(qw(log --pretty=oneline));
218         $im->done;
219         my $tip = shift @after;
220         like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
221                 'commit message propaged to git');
222         is_deeply(\@after, \@before, 'only one commit written to git');
223         is($ibx->mm->num_for($smsg->mid), undef, 'no longer in Msgmap by mid');
224         like($smsg->num, qr/\A\d+\z/, 'numeric number in return message');
225         is($ibx->mm->mid_for($smsg->num), undef, 'no longer in Msgmap by num');
226         my $srch = $ibx->search->reopen;
227         my @found = ();
228         $srch->each_smsg_by_mid($smsg->mid, sub { push @found, @_; 1 });
229         is(scalar(@found), 0, 'no longer found in Xapian skeleton');
230         my @log1 = qw(log -1 --pretty=raw --raw -r --no-abbrev --no-renames);
231
232         my $after = $git0->qx(@log1);
233         if ($after =~ m!( [a-f0-9]+ )A\td$!m) {
234                 my $oid = $1;
235                 ok(index($before, $oid) > 0, 'no new blob introduced');
236         } else {
237                 fail('failed to extract blob from log output');
238         }
239         is($im->remove($mime, 'test removal'), undef,
240                 'remove is idempotent');
241         $im->done;
242         is($git0->qx(@log1),
243                 $after, 'no git history made with idempotent remove');
244         eval { $im->done };
245         ok(!$@, '->done is idempotent');
246 }
247
248 {
249         ok($im->add($mime), 'add message to be purged');
250         local $SIG{__WARN__} = sub {};
251         ok($im->purge($mime), 'purged message');
252         $im->done;
253 }
254
255 {
256         my @warn;
257         my $x = 'x'x250;
258         my $y = 'y'x250;
259         local $SIG{__WARN__} = sub { push @warn, @_ };
260         $mime->header_set('Subject', 'long mid');
261         $mime->header_set('Message-ID', "<$x>");
262         ok($im->add($mime), 'add excessively long Message-ID');
263
264         $mime->header_set('Message-ID', "<$y>");
265         $mime->header_set('References', "<$x>");
266         ok($im->add($mime), 'add excessively long References');
267         $im->barrier;
268
269         my $msgs = $ibx->search->reopen->get_thread('x'x244);
270         is(2, scalar(@$msgs), 'got both messages');
271         is($msgs->[0]->{mid}, 'x'x244, 'stored truncated mid');
272         is($msgs->[1]->{references}, '<'.('x'x244).'>', 'stored truncated ref');
273         is($msgs->[1]->{mid}, 'y'x244, 'stored truncated mid(2)');
274         $im->done;
275 }
276
277 done_testing();