]> Sergey Matveev's repositories - public-inbox.git/blob - t/v2writable.t
import: (v2) delete writes the blob into history in subdir
[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         my $fh = $git0->popen(qw(config core.sharedRepository));
47         my $v = eval { local $/; <$fh> };
48         chomp $v;
49         is($v, '0644', 'child repo inherited core.sharedRepository');
50         $fh = $git0->popen(qw(config --bool repack.writeBitmaps));
51         $v = eval { local $/; <$fh> };
52         chomp $v;
53         is($v, 'true', 'child repo inherited repack.writeBitmaps');
54 }
55
56 {
57         my @warn;
58         local $SIG{__WARN__} = sub { push @warn, @_ };
59         is($im->add($mime), undef, 'obvious duplicate rejected');
60         like(join(' ', @warn), qr/resent/, 'warned about resent message');
61
62         @warn = ();
63         $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
64         ok($im->add($mime), 'secondary MID used');
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                 [ $mime->header_obj->header_raw('Message-Id') ],
69                 'no new Message-Id added');
70
71         @warn = ();
72         $mime->header_set('Message-Id', '<a-mid@b>');
73         $mime->body_set('different');
74         ok($im->add($mime), 'reused mid ok');
75         like(join(' ', @warn), qr/reused/, 'warned about reused MID');
76         my @mids = $mime->header_obj->header_raw('Message-Id');
77         is($mids[1], '<a-mid@b>', 'original mid not changed');
78         like($mids[0], qr/\A<\w+\@localhost>\z/, 'new MID added');
79         is(scalar(@mids), 2, 'only one new MID added');
80
81         @warn = ();
82         $mime->header_set('Message-Id', '<a-mid@b>');
83         $mime->body_set('this one needs a random mid');
84         my $gen = content_digest($mime)->hexdigest . '@localhost';
85         my $fake = PublicInbox::MIME->new($mime->as_string);
86         $fake->header_set('Message-Id', $gen);
87         ok($im->add($fake), 'fake added easily');
88         is_deeply(\@warn, [], 'no warnings from a faker');
89         ok($im->add($mime), 'random MID made');
90         like(join(' ', @warn), qr/using random/, 'warned about using random');
91         @mids = $mime->header_obj->header_raw('Message-Id');
92         is($mids[1], '<a-mid@b>', 'original mid not changed');
93         like($mids[0], qr/\A<\w+\@localhost>\z/, 'new MID added');
94         is(scalar(@mids), 2, 'only one new MID added');
95
96         @warn = ();
97         $mime->header_set('Message-Id');
98         ok($im->add($mime), 'random MID made for MID free message');
99         @mids = $mime->header_obj->header_raw('Message-Id');
100         like($mids[0], qr/\A<\w+\@localhost>\z/, 'mid was generated');
101         is(scalar(@mids), 1, 'new generated');
102 }
103
104 {
105         $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
106         ok($im->add($mime), 'message with multiple Message-ID');
107         $im->done;
108         my @found;
109         $ibx->search->reopen;
110         $ibx->search->each_smsg_by_mid('abcde@1', sub { push @found, @_; 1 });
111         is(scalar(@found), 1, 'message found by first MID');
112         $ibx->search->each_smsg_by_mid('abcde@2', sub { push @found, @_; 1 });
113         is(scalar(@found), 2, 'message found by second MID');
114         is($found[0]->{doc_id}, $found[1]->{doc_id}, 'same document');
115         ok($found[1]->{doc_id} > 0, 'doc_id is positive');
116 }
117
118 SKIP: {
119         use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
120         use Net::NNTP;
121         use IO::Socket;
122         use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
123         eval { require Danga::Socket };
124         skip "Danga::Socket missing $@", 2 if $@;
125         my $err = "$mainrepo/stderr.log";
126         my $out = "$mainrepo/stdout.log";
127         my %opts = (
128                 LocalAddr => '127.0.0.1',
129                 ReuseAddr => 1,
130                 Proto => 'tcp',
131                 Type => SOCK_STREAM,
132                 Listen => 1024,
133         );
134         my $group = 'inbox.comp.test.v2writable';
135         my $pi_config = "$mainrepo/pi_config";
136         open my $fh, '>', $pi_config or die "open: $!\n";
137         print $fh <<EOF
138 [publicinbox "test-v2writable"]
139         mainrepo = $mainrepo
140         version = 2
141         address = test\@example.com
142         newsgroup = $group
143 EOF
144         ;
145         close $fh or die "close: $!\n";
146         my $sock = IO::Socket::INET->new(%opts);
147         ok($sock, 'sock created');
148         my $pid;
149         my $len;
150         END { kill 'TERM', $pid if defined $pid };
151         $! = 0;
152         my $fl = fcntl($sock, F_GETFD, 0);
153         ok(! $!, 'no error from fcntl(F_GETFD)');
154         is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
155         $pid = fork;
156         if ($pid == 0) {
157                 use POSIX qw(dup2);
158                 $ENV{PI_CONFIG} = $pi_config;
159                 # pretend to be systemd
160                 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
161                 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
162                 $ENV{LISTEN_PID} = $$;
163                 $ENV{LISTEN_FDS} = 1;
164                 my $nntpd = 'blib/script/public-inbox-nntpd';
165                 exec $nntpd, "--stdout=$out", "--stderr=$err";
166                 die "FAIL: $!\n";
167         }
168         ok(defined $pid, 'forked nntpd process successfully');
169         $! = 0;
170         fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
171         ok(! $!, 'no error from fcntl(F_SETFD)');
172         my $host_port = $sock->sockhost . ':' . $sock->sockport;
173         my $n = Net::NNTP->new($host_port);
174         $n->group($group);
175         my $x = $n->xover('1-');
176         my %uniq;
177         foreach my $num (sort { $a <=> $b } keys %$x) {
178                 my $mid = $x->{$num}->[3];
179                 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
180                 is_deeply($n->xhdr('Message-ID', $num),
181                          { $num => $mid }, "XHDR lookup OK on num $num");
182                 is_deeply($n->xhdr('Message-ID', $mid),
183                          { $mid => $mid }, "XHDR lookup OK on MID $num");
184         }
185         my %nn;
186         foreach my $mid (@{$n->newnews(0, $group)}) {
187                 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
188         }
189         is_deeply([sort keys %nn], [sort keys %uniq]);
190 };
191 {
192         local $ENV{NPROC} = 2;
193         my @before = $git0->qx(qw(log --pretty=oneline));
194         my $before = $git0->qx(qw(log --pretty=raw --raw -r --no-abbrev));
195         $im = PublicInbox::V2Writable->new($ibx, 1);
196         is($im->{partitions}, 1, 'detected single partition from previous');
197         my $smsg = $im->remove($mime, 'test removal');
198         my @after = $git0->qx(qw(log --pretty=oneline));
199         $im->done;
200         my $tip = shift @after;
201         like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
202                 'commit message propaged to git');
203         is_deeply(\@after, \@before, 'only one commit written to git');
204         is($ibx->mm->num_for($smsg->mid), undef, 'no longer in Msgmap by mid');
205         like($smsg->num, qr/\A\d+\z/, 'numeric number in return message');
206         is($ibx->mm->mid_for($smsg->num), undef, 'no longer in Msgmap by num');
207         my $srch = $ibx->search->reopen;
208         my @found = ();
209         $srch->each_smsg_by_mid($smsg->mid, sub { push @found, @_; 1 });
210         is(scalar(@found), 0, 'no longer found in Xapian skeleton');
211
212         my $after = $git0->qx(qw(log -1 --pretty=raw --raw -r --no-abbrev));
213         if ($after =~ m!( [a-f0-9]+ )A\td$!) {
214                 my $oid = $1;
215                 ok(index($before, $oid) > 0, 'no new blob introduced');
216         } else {
217                 fail('failed to extract blob from log output');
218         }
219 }
220
221 done_testing();