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