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