1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
7 use PublicInbox::ContentId qw(content_digest);
8 use File::Temp qw/tempdir/;
9 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
11 plan skip_all => "$mod missing for nntpd.t" if $@;
13 use_ok 'PublicInbox::V2Writable';
14 my $mainrepo = tempdir('pi-v2writable-XXXXXX', TMPDIR => 1, CLEANUP => 1);
16 mainrepo => $mainrepo,
17 name => 'test-v2writable',
19 -primary_address => 'test@example.com',
21 $ibx = PublicInbox::Inbox->new($ibx);
22 my $mime = PublicInbox::MIME->create(
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',
30 body => "hello world\n",
34 local $ENV{NPROC} = '1';
35 PublicInbox::V2Writable->new($ibx, 1);
37 is($im->{partitions}, 1, 'one partition when forced');
38 ok($im->add($mime), 'ordinary message added');
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> };
49 is($v, '0644', 'child repo inherited core.sharedRepository');
50 $fh = $git0->popen(qw(config --bool repack.writeBitmaps));
51 $v = eval { local $/; <$fh> };
53 is($v, 'true', 'child repo inherited repack.writeBitmaps');
58 local $SIG{__WARN__} = sub { push @warn, @_ };
59 is($im->add($mime), undef, 'obvious duplicate rejected');
60 is(scalar(@warn), 0, 'no warning about resent message');
63 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
64 is($im->add($mime), undef, 'secondary MID ignored if first matches');
65 my $sec = PublicInbox::MIME->new($mime->as_string);
66 $sec->header_set('Date');
67 $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
68 ok($im->add($sec), 'secondary MID used if data is different');
69 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
70 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
71 is_deeply([ '<a-mid@b>', '<c@d>' ],
72 [ $sec->header_obj->header_raw('Message-Id') ],
73 'no new Message-Id added');
75 my $sane_mid = qr/\A<[\w\-]+\@localhost>\z/;
77 $mime->header_set('Message-Id', '<a-mid@b>');
78 $mime->body_set('different');
79 ok($im->add($mime), 'reused mid ok');
80 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
81 my @mids = $mime->header_obj->header_raw('Message-Id');
82 is($mids[1], '<a-mid@b>', 'original mid not changed');
83 like($mids[0], $sane_mid, 'new MID added');
84 is(scalar(@mids), 2, 'only one new MID added');
87 $mime->header_set('Message-Id', '<a-mid@b>');
88 $mime->body_set('this one needs a random mid');
89 my $gen = PublicInbox::Import::digest2mid(content_digest($mime));
90 unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
91 my $fake = PublicInbox::MIME->new($mime->as_string);
92 $fake->header_set('Message-Id', "<$gen>");
93 ok($im->add($fake), 'fake added easily');
94 is_deeply(\@warn, [], 'no warnings from a faker');
95 ok($im->add($mime), 'random MID made');
96 like(join(' ', @warn), qr/using random/, 'warned about using random');
97 @mids = $mime->header_obj->header_raw('Message-Id');
98 is($mids[1], '<a-mid@b>', 'original mid not changed');
99 like($mids[0], $sane_mid, 'new MID added');
100 is(scalar(@mids), 2, 'only one new MID added');
103 $mime->header_set('Message-Id');
104 ok($im->add($mime), 'random MID made for MID free message');
105 @mids = $mime->header_obj->header_raw('Message-Id');
106 like($mids[0], $sane_mid, 'mid was generated');
107 is(scalar(@mids), 1, 'new generated');
111 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
112 ok($im->add($mime), 'message with multiple Message-ID');
115 my $srch = $ibx->search;
116 $srch->reopen->each_smsg_by_mid('abcde@1', sub { push @found, @_; 1 });
117 is(scalar(@found), 1, 'message found by first MID');
118 $srch->reopen->each_smsg_by_mid('abcde@2', sub { push @found, @_; 1 });
119 is(scalar(@found), 2, 'message found by second MID');
120 is($found[0]->{doc_id}, $found[1]->{doc_id}, 'same document');
121 ok($found[1]->{doc_id} > 0, 'doc_id is positive');
125 use Fcntl qw(FD_CLOEXEC F_SETFD F_GETFD);
128 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
129 eval { require Danga::Socket };
130 skip "Danga::Socket missing $@", 2 if $@;
131 my $err = "$mainrepo/stderr.log";
132 my $out = "$mainrepo/stdout.log";
134 LocalAddr => '127.0.0.1',
140 my $group = 'inbox.comp.test.v2writable';
141 my $pi_config = "$mainrepo/pi_config";
142 open my $fh, '>', $pi_config or die "open: $!\n";
144 [publicinbox "test-v2writable"]
147 address = test\@example.com
151 close $fh or die "close: $!\n";
152 my $sock = IO::Socket::INET->new(%opts);
153 ok($sock, 'sock created');
156 END { kill 'TERM', $pid if defined $pid };
158 my $fl = fcntl($sock, F_GETFD, 0);
159 ok(! $!, 'no error from fcntl(F_GETFD)');
160 is($fl, FD_CLOEXEC, 'cloexec set by default (Perl behavior)');
164 $ENV{PI_CONFIG} = $pi_config;
165 # pretend to be systemd
166 fcntl($sock, F_SETFD, $fl &= ~FD_CLOEXEC);
167 dup2(fileno($sock), 3) or die "dup2 failed: $!\n";
168 $ENV{LISTEN_PID} = $$;
169 $ENV{LISTEN_FDS} = 1;
170 my $nntpd = 'blib/script/public-inbox-nntpd';
171 exec $nntpd, "--stdout=$out", "--stderr=$err";
174 ok(defined $pid, 'forked nntpd process successfully');
176 fcntl($sock, F_SETFD, $fl |= FD_CLOEXEC);
177 ok(! $!, 'no error from fcntl(F_SETFD)');
178 my $host_port = $sock->sockhost . ':' . $sock->sockport;
179 my $n = Net::NNTP->new($host_port);
181 my $x = $n->xover('1-');
183 foreach my $num (sort { $a <=> $b } keys %$x) {
184 my $mid = $x->{$num}->[3];
185 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
186 is_deeply($n->xhdr('Message-ID', $num),
187 { $num => $mid }, "XHDR lookup OK on num $num");
188 is_deeply($n->xhdr('Message-ID', $mid),
189 { $mid => $mid }, "XHDR lookup OK on MID $num");
192 foreach my $mid (@{$n->newnews(0, $group)}) {
193 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
195 is_deeply([sort keys %nn], [sort keys %uniq]);
198 local $ENV{NPROC} = 2;
199 my @before = $git0->qx(qw(log --pretty=oneline));
200 my $before = $git0->qx(qw(log --pretty=raw --raw -r --no-abbrev));
201 $im = PublicInbox::V2Writable->new($ibx, 1);
202 is($im->{partitions}, 1, 'detected single partition from previous');
203 my $smsg = $im->remove($mime, 'test removal');
204 my @after = $git0->qx(qw(log --pretty=oneline));
206 my $tip = shift @after;
207 like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
208 'commit message propaged to git');
209 is_deeply(\@after, \@before, 'only one commit written to git');
210 is($ibx->mm->num_for($smsg->mid), undef, 'no longer in Msgmap by mid');
211 like($smsg->num, qr/\A\d+\z/, 'numeric number in return message');
212 is($ibx->mm->mid_for($smsg->num), undef, 'no longer in Msgmap by num');
213 my $srch = $ibx->search->reopen;
215 $srch->each_smsg_by_mid($smsg->mid, sub { push @found, @_; 1 });
216 is(scalar(@found), 0, 'no longer found in Xapian skeleton');
218 my $after = $git0->qx(qw(log -1 --pretty=raw --raw -r --no-abbrev));
219 if ($after =~ m!( [a-f0-9]+ )A\t_/D$!) {
221 ok(index($before, $oid) > 0, 'no new blob introduced');
223 fail('failed to extract blob from log output');
225 is($im->remove($mime, 'test removal'), undef,
226 'remove is idempotent');
228 is($git0->qx(qw(log -1 --pretty=raw --raw -r --no-abbrev)),
229 $after, 'no git history made with idempotent remove');
231 ok(!$@, '->done is idempotent');