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 require './t/common.perl';
11 foreach my $mod (qw(DBD::SQLite Search::Xapian)) {
13 plan skip_all => "$mod missing for nntpd.t" if $@;
15 use_ok 'PublicInbox::V2Writable';
17 my $mainrepo = tempdir('pi-v2writable-XXXXXX', TMPDIR => 1, CLEANUP => 1);
19 mainrepo => $mainrepo,
20 name => 'test-v2writable',
22 -primary_address => 'test@example.com',
24 $ibx = PublicInbox::Inbox->new($ibx);
25 my $mime = PublicInbox::MIME->create(
27 From => 'a@example.com',
28 To => 'test@example.com',
29 Subject => 'this is a subject',
30 'Message-ID' => '<a-mid@b>',
31 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
33 body => "hello world\n",
37 local $ENV{NPROC} = '1';
38 PublicInbox::V2Writable->new($ibx, 1);
40 is($im->{partitions}, 1, 'one partition when forced');
41 ok($im->add($mime), 'ordinary message added');
42 foreach my $f ("$mainrepo/msgmap.sqlite3",
43 glob("$mainrepo/xap*/*"),
44 glob("$mainrepo/xap*/*/*")) {
46 my ($bn) = (split(m!/!, $f))[-1];
47 is($st[2] & 07777, -f _ ? 0660 : 0770,
48 "default sharedRepository respected for $bn");
53 if ('ensure git configs are correct') {
54 my @cmd = (qw(git config), "--file=$mainrepo/all.git/config",
55 qw(core.sharedRepository 0644));
56 is(system(@cmd), 0, "set sharedRepository in all.git");
57 $git0 = PublicInbox::Git->new("$mainrepo/git/0.git");
58 chomp(my $v = $git0->qx(qw(config core.sharedRepository)));
59 is($v, '0644', 'child repo inherited core.sharedRepository');
60 chomp($v = $git0->qx(qw(config --bool repack.writeBitmaps)));
61 is($v, 'true', 'child repo inherited repack.writeBitmaps');
66 local $SIG{__WARN__} = sub { push @warn, @_ };
67 is($im->add($mime), undef, 'obvious duplicate rejected');
68 is(scalar(@warn), 0, 'no warning about resent message');
71 $mime->header_set('Message-Id', '<a-mid@b>', '<c@d>');
72 is($im->add($mime), undef, 'secondary MID ignored if first matches');
73 my $sec = PublicInbox::MIME->new($mime->as_string);
74 $sec->header_set('Date');
75 $sec->header_set('Message-Id', '<a-mid@b>', '<c@d>');
76 ok($im->add($sec), 'secondary MID used if data is different');
77 like(join(' ', @warn), qr/mismatched/, 'warned about mismatch');
78 like(join(' ', @warn), qr/alternative/, 'warned about alternative');
79 is_deeply([ '<a-mid@b>', '<c@d>' ],
80 [ $sec->header_obj->header_raw('Message-Id') ],
81 'no new Message-Id added');
83 my $sane_mid = qr/\A<[\w\-\.]+\@\w+>\z/;
85 $mime->header_set('Message-Id', '<a-mid@b>');
86 $mime->body_set('different');
87 ok($im->add($mime), 'reused mid ok');
88 like(join(' ', @warn), qr/reused/, 'warned about reused MID');
89 my @mids = $mime->header_obj->header_raw('Message-Id');
90 is($mids[0], '<a-mid@b>', 'original mid not changed');
91 like($mids[1], $sane_mid, 'new MID added');
92 is(scalar(@mids), 2, 'only one new MID added');
95 $mime->header_set('Message-Id', '<a-mid@b>');
96 $mime->body_set('this one needs a random mid');
97 my $hdr = $mime->header_obj;
98 my $gen = PublicInbox::Import::digest2mid(content_digest($mime), $hdr);
99 unlike($gen, qr![\+/=]!, 'no URL-unfriendly chars in Message-Id');
100 my $fake = PublicInbox::MIME->new($mime->as_string);
101 $fake->header_set('Message-Id', "<$gen>");
102 ok($im->add($fake), 'fake added easily');
103 is_deeply(\@warn, [], 'no warnings from a faker');
104 ok($im->add($mime), 'random MID made');
105 like(join(' ', @warn), qr/using random/, 'warned about using random');
106 @mids = $mime->header_obj->header_raw('Message-Id');
107 is($mids[0], '<a-mid@b>', 'original mid not changed');
108 like($mids[1], $sane_mid, 'new MID added');
109 is(scalar(@mids), 2, 'only one new MID added');
112 $mime->header_set('Message-Id');
113 ok($im->add($mime), 'random MID made for MID free message');
114 @mids = $mime->header_obj->header_raw('Message-Id');
115 like($mids[0], $sane_mid, 'mid was generated');
116 is(scalar(@mids), 1, 'new generated');
120 $mime->header_set('Message-Id', '<abcde@1>', '<abcde@2>');
121 $mime->header_set('References', '<zz-mid@b>');
122 ok($im->add($mime), 'message with multiple Message-ID');
124 my $srch = $ibx->search;
125 my $mset1 = $srch->reopen->query('m:abcde@1', { mset => 1 });
126 is($mset1->size, 1, 'message found by first MID');
127 my $mset2 = $srch->reopen->query('m:abcde@2', { mset => 1 });
128 is($mset2->size, 1, 'message found by second MID');
129 is((($mset1->items)[0])->get_docid, (($mset2->items)[0])->get_docid,
130 'same document') if ($mset1->size);
136 use Socket qw(SO_KEEPALIVE IPPROTO_TCP TCP_NODELAY);
137 eval { require Danga::Socket };
138 skip "Danga::Socket missing $@", 2 if $@;
139 my $err = "$mainrepo/stderr.log";
140 my $out = "$mainrepo/stdout.log";
142 LocalAddr => '127.0.0.1',
148 my $group = 'inbox.comp.test.v2writable';
149 my $pi_config = "$mainrepo/pi_config";
150 open my $fh, '>', $pi_config or die "open: $!\n";
152 [publicinbox "test-v2writable"]
155 address = test\@example.com
159 close $fh or die "close: $!\n";
160 my $sock = IO::Socket::INET->new(%opts);
161 ok($sock, 'sock created');
164 END { kill 'TERM', $pid if defined $pid };
165 my $nntpd = 'blib/script/public-inbox-nntpd';
166 my $cmd = [ $nntpd, "--stdout=$out", "--stderr=$err" ];
167 $pid = spawn_listener({ PI_CONFIG => $pi_config }, $cmd, [ $sock ]);
168 my $host_port = $sock->sockhost . ':' . $sock->sockport;
169 my $n = Net::NNTP->new($host_port);
171 my $x = $n->xover('1-');
173 foreach my $num (sort { $a <=> $b } keys %$x) {
174 my $mid = $x->{$num}->[3];
175 is($uniq{$mid}++, 0, "MID for $num is unique in XOVER");
176 is_deeply($n->xhdr('Message-ID', $num),
177 { $num => $mid }, "XHDR lookup OK on num $num");
178 is_deeply($n->xhdr('Message-ID', $mid),
179 { $mid => $mid }, "XHDR lookup OK on MID $num");
182 foreach my $mid (@{$n->newnews(0, $group)}) {
183 is($nn{$mid}++, 0, "MID is unique in NEWNEWS");
185 is_deeply([sort keys %nn], [sort keys %uniq]);
188 foreach my $num (@{$n->listgroup($group)}) {
189 is($lg{$num}++, 0, "num is unique in LISTGROUP");
191 is_deeply([sort keys %lg], [sort keys %$x],
192 'XOVER and LISTGROUPS return the same article numbers');
194 my $xref = $n->xhdr('Xref', '1-');
195 is_deeply([sort keys %lg], [sort keys %$xref], 'Xref range OK');
197 my $mids = $n->xhdr('Message-ID', '1-');
198 is_deeply([sort keys %lg], [sort keys %$xref], 'Message-ID range OK');
200 my $rover = $n->xrover('1-');
201 is_deeply([sort keys %lg], [sort keys %$rover], 'XROVER range OK');
204 local $ENV{NPROC} = 2;
205 my @log = qw(log --no-decorate --no-abbrev --no-notes --no-color);
206 my @before = $git0->qx(@log, qw(--pretty=oneline));
207 my $before = $git0->qx(@log, qw(--pretty=raw --raw -r));
208 $im = PublicInbox::V2Writable->new($ibx, 1);
209 is($im->{partitions}, 1, 'detected single partition from previous');
210 my $smsg = $im->remove($mime, 'test removal');
212 my @after = $git0->qx(@log, qw(--pretty=oneline));
213 my $tip = shift @after;
214 like($tip, qr/\A[a-f0-9]+ test removal\n\z/s,
215 'commit message propagated to git');
216 is_deeply(\@after, \@before, 'only one commit written to git');
217 is($ibx->mm->num_for($smsg->mid), undef, 'no longer in Msgmap by mid');
218 my $num = $smsg->{num};
219 like($num, qr/\A\d+\z/, 'numeric number in return message');
220 is($ibx->mm->mid_for($num), undef, 'no longer in Msgmap by num');
221 my $srch = $ibx->search->reopen;
222 my $mset = $srch->query('m:'.$smsg->mid, { mset => 1});
223 is($mset->size, 0, 'no longer found in Xapian');
224 my @log1 = (@log, qw(-1 --pretty=raw --raw -r --no-renames));
225 is($srch->{over_ro}->get_art($num), undef,
226 'removal propagated to Over DB');
228 my $after = $git0->qx(@log1);
229 if ($after =~ m!( [a-f0-9]+ )A\td$!m) {
231 ok(index($before, $oid) > 0, 'no new blob introduced');
233 fail('failed to extract blob from log output');
235 is($im->remove($mime, 'test removal'), undef,
236 'remove is idempotent');
239 $after, 'no git history made with idempotent remove');
241 ok(!$@, '->done is idempotent');
245 ok($im->add($mime), 'add message to be purged');
246 local $SIG{__WARN__} = sub {};
247 ok(my $cmts = $im->purge($mime), 'purged message');
248 like($cmts->[0], qr/\A[a-f0-9]{40}\z/, 'purge returned current commit');
252 is($im->purge($mime), undef, 'no-op returns undef');
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');
264 $mime->header_set('Message-ID', "<$y>");
265 $mime->header_set('References', "<$x>");
266 ok($im->add($mime), 'add excessively long References');
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)');