1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite Search::Xapian));
8 require PublicInbox::SearchIdx;
9 require PublicInbox::Inbox;
11 my ($tmpdir, $for_destroy) = tmpdir();
12 my $git_dir = "$tmpdir/a.git";
13 my $ibx = PublicInbox::Inbox->new({ inboxdir => $git_dir });
14 my ($root_id, $last_id);
16 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)")
17 or BAIL_OUT("`git init --shared' failed, weird FS or seccomp?");
18 eval { PublicInbox::Search->new($ibx)->xdb };
19 ok($@, "exception raised on non-existent DB");
21 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
22 $ibx->with_umask(sub {
27 my $ro = PublicInbox::Search->new($ibx);
29 $rw->commit_txn_lazy if $rw;
30 $rw = PublicInbox::SearchIdx->new($ibx, 1);
31 $rw->{qp_flags} = 0; # quiet a warning
36 my ($got, $exp, $msg) = @_;
37 is(sprintf('0%03o', $got), sprintf('0%03o', $exp), $msg);
41 # git repository perms
42 oct_is($ibx->_git_config_perm(),
43 &PublicInbox::InboxWritable::PERM_GROUP,
44 'undefined permission is group');
46 [ '0644', 0022, '644 => umask(0022)' ],
47 [ '0600', 0077, '600 => umask(0077)' ],
48 [ '0640', 0027, '640 => umask(0027)' ],
49 [ 'group', 0007, 'group => umask(0007)' ],
50 [ 'everybody', 0002, 'everybody => umask(0002)' ],
51 [ 'umask', umask, 'umask => existing umask' ],
54 my ($perm, $exp, $msg) = @$_;
55 my $got = PublicInbox::InboxWritable::_umask_for(
56 PublicInbox::InboxWritable->_git_config_perm($perm));
57 oct_is($got, $exp, $msg);
61 $ibx->with_umask(sub {
62 my $root = Email::MIME->create(
64 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
65 Subject => 'Hello world',
66 'Message-ID' => '<root@s>',
67 From => 'John Smith <js@example.com>',
68 To => 'list@example.com',
71 my $last = Email::MIME->create(
73 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
74 Subject => 'Re: Hello world',
75 'In-Reply-To' => '<root@s>',
76 'Message-ID' => '<last@s>',
77 From => 'John Smith <js@example.com>',
78 To => 'list@example.com',
79 Cc => 'foo@example.com',
81 body => "goodbye forever :<\n");
85 $root_id = $rw->add_message($root);
86 is($root_id, int($root_id), "root_id is an integer: $root_id");
87 $last_id = $rw->add_message($last);
88 is($last_id, int($last_id), "last_id is an integer: $last_id");
93 sort(map { $_->mid } @$msgs);
99 my $found = $ro->query('m:root@s');
100 is(scalar(@$found), 1, "message found");
101 is($found->[0]->mid, 'root@s', 'mid set correctly') if scalar(@$found);
104 my @exp = sort qw(root@s last@s);
106 $res = $ro->query('s:(Hello world)');
107 @res = filter_mids($res);
108 is_deeply(\@res, \@exp, 'got expected results for s:() match');
110 $res = $ro->query('s:"Hello world"');
111 @res = filter_mids($res);
112 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
114 $res = $ro->query('s:"Hello world"', {limit => 1});
115 is(scalar @$res, 1, "limit works");
116 my $first = $res->[0];
118 $res = $ro->query('s:"Hello world"', {offset => 1});
119 is(scalar @$res, 1, "offset works");
120 my $second = $res->[0];
122 isnt($first, $second, "offset returned different result from limit");
126 $ibx->with_umask(sub {
128 my $rmid = '<ghost-message@s>';
129 my $reply_to_ghost = Email::MIME->create(
131 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
132 Subject => 'Re: ghosts',
133 'Message-ID' => '<ghost-reply@s>',
134 'In-Reply-To' => $rmid,
135 From => 'Time Traveler <tt@example.com>',
136 To => 'list@example.com',
141 my $reply_id = $rw->add_message($reply_to_ghost);
142 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
144 my $was_ghost = Email::MIME->create(
146 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
148 'Message-ID' => $rmid,
149 From => 'Laggy Sender <lag@example.com>',
150 To => 'list@example.com',
152 body => "are real\n");
154 my $ghost_id = $rw->add_message($was_ghost);
155 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
156 my $msgs = $rw->{over}->get_thread('ghost-message@s');
157 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
158 foreach (qw(sid tid)) {
159 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
161 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
162 ok($_->{num} > 0, 'positive art num') foreach @$msgs
165 # search thread on ghost
171 my $res = $ro->query('ghost');
172 my @exp = sort qw(ghost-message@s ghost-reply@s);
173 my @res = filter_mids($res);
174 is_deeply(\@res, \@exp, 'got expected results for Subject match');
177 $res = $ro->query('goodbye');
178 is(scalar(@$res), 1, "goodbye message found");
179 is($res->[0]->mid, 'last@s', 'got goodbye message body') if scalar(@$res);
182 $res = $ro->query('dt:20101002000001..20101002000001');
183 @res = filter_mids($res);
184 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
185 $res = $ro->query('dt:20101002000002..20101002000002');
186 is_deeply($res, [], 'exact Date: match down to the second');
190 $ibx->with_umask(sub {
193 my $long_mid = 'last' . ('x' x 60). '@s';
195 my $long = Email::MIME->create(
197 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
198 Subject => 'long message ID',
199 'References' => '<root@s> <last@s>',
200 'In-Reply-To' => '<last@s>',
201 'Message-ID' => "<$long_mid>",
202 From => '"Long I.D." <long-id@example.com>',
203 To => 'list@example.com',
206 my $long_id = $rw->add_message($long);
207 is($long_id, int($long_id), "long_id is an integer: $long_id");
214 my $long_reply_mid = 'reply-to-long@1';
215 my $long_reply = Email::MIME->create(
217 Subject => 'I break references',
218 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
219 'Message-ID' => "<$long_reply_mid>",
221 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
222 'In-Reply-To' => "<$long_mid>",
223 From => 'no1 <no1@example.com>',
224 To => 'list@example.com',
226 body => "no References\n");
227 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
231 my $t = $ro->{over_ro}->get_thread('root@s');
232 is(scalar(@$t), 4, "got all 4 messages in thread");
233 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
234 @res = filter_mids($t);
235 is_deeply(\@res, \@exp, "get_thread works");
238 # quote prioritization
239 $ibx->with_umask(sub {
241 $rw->add_message(Email::MIME->create(
243 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
245 'Message-ID' => '<quote@a>',
246 From => 'Quoter <quoter@example.com>',
247 To => 'list@example.com',
249 body => "> theatre illusions\nfade\n"));
251 $rw->add_message(Email::MIME->create(
253 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
255 'Message-ID' => '<nquote@a>',
256 From => 'Non-Quoter<non-quoter@example.com>',
257 To => 'list@example.com',
259 body => "theatre\nfade\n"));
260 my $res = $rw->query("theatre");
261 is(scalar(@$res), 2, "got both matches");
262 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
263 is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
265 $res = $rw->query("illusions");
266 is(scalar(@$res), 1, "got a match for quoted text");
267 is($res->[0]->mid, 'quote@a',
268 "quoted result returned if nothing else") if scalar(@$res);
271 # circular references
272 $ibx->with_umask(sub {
273 my $s = 'foo://'. ('Circle' x 15).'/foo';
274 my $doc_id = $rw->add_message(Email::MIME->create(
275 header => [ Subject => $s ],
277 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
278 'Message-ID' => '<circle@a>',
279 'References' => '<circle@a>',
280 'In-Reply-To' => '<circle@a>',
281 From => 'Circle <circle@example.com>',
282 To => 'list@example.com',
285 ok($doc_id > 0, "doc_id defined with circular reference");
286 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
287 is(defined($smsg), 1, 'found m:circl@a');
288 is($smsg->references, '', "no references created") if defined($smsg);
289 is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
292 $ibx->with_umask(sub {
294 my $mbox = 't/utf8.mbox';
295 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
299 $str =~ s/\AFrom [^\n]+\n//s;
300 my $mime = Email::MIME->new($str);
301 my $doc_id = $rw->add_message($mime);
302 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
303 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
304 is(defined($msg), 1, 'found testmessage@example.com');
305 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
309 my $msgs = $ro->query('d:19931002..20101002');
310 ok(scalar(@$msgs) > 0, 'got results within range');
311 $msgs = $ro->query('d:20101003..');
312 is(scalar(@$msgs), 0, 'nothing after 20101003');
313 $msgs = $ro->query('d:..19931001');
314 is(scalar(@$msgs), 0, 'nothing before 19931001');
317 # names and addresses
319 my $mset = $ro->query('t:list@example.com', {mset => 1});
320 is($mset->size, 6, 'searched To: successfully');
321 foreach my $m ($mset->items) {
322 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
323 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
326 $mset = $ro->query('tc:list@example.com', {mset => 1});
327 is($mset->size, 6, 'searched To+Cc: successfully');
328 foreach my $m ($mset->items) {
329 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
330 my $tocc = join("\n", $smsg->to, $smsg->cc);
331 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
334 foreach my $pfx ('tcf:', 'c:') {
335 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
336 is($mset->items, 1, "searched $pfx successfully for Cc:");
337 foreach my $m ($mset->items) {
338 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
339 like($smsg->cc, qr/\bfoo\@example\.com\b/,
344 foreach my $pfx ('', 'tcf:', 'f:') {
345 my $res = $ro->query($pfx . 'Laggy');
347 "searched $pfx successfully for From:");
348 foreach my $smsg (@$res) {
349 like($smsg->from_name, qr/Laggy Sender/,
350 "From appears with $pfx");
358 my $res = $ro->query('b:hello');
359 is(scalar(@$res), 0, 'no match on body search only');
360 $res = $ro->query('bs:smith');
362 'no match on body+subject search for From');
364 $res = $ro->query('q:theatre');
365 is(scalar(@$res), 1, 'only one quoted body');
366 like($res->[0]->from_name, qr/\AQuoter/,
367 'got quoted body') if (scalar(@$res));
369 $res = $ro->query('nq:theatre');
370 is(scalar @$res, 1, 'only one non-quoted body');
371 like($res->[0]->from_name, qr/\ANon-Quoter/,
372 'got non-quoted body') if (scalar(@$res));
374 foreach my $pfx (qw(b: bs:)) {
375 $res = $ro->query($pfx . 'theatre');
376 is(scalar @$res, 2, "searched both bodies for $pfx");
377 like($res->[0]->from_name, qr/\ANon-Quoter/,
378 "non-quoter first for $pfx") if scalar(@$res);
382 $ibx->with_umask(sub {
383 my $part1 = Email::MIME->create(
385 content_type => 'text/plain',
386 disposition => 'attachment',
387 charset => 'US-ASCII',
388 encoding => 'quoted-printable',
389 filename => 'attached_fart.txt',
391 body_str => 'inside the attachment',
393 my $part2 = Email::MIME->create(
395 content_type => 'text/plain',
396 disposition => 'attachment',
397 charset => 'US-ASCII',
398 encoding => 'quoted-printable',
399 filename => 'part_deux.txt',
401 body_str => 'inside another',
403 my $amsg = Email::MIME->create(
405 Subject => 'see attachment',
406 'Message-ID' => '<file@attached>',
407 From => 'John Smith <js@example.com>',
408 To => 'list@example.com',
410 parts => [ $part1, $part2 ],
412 ok($rw->add_message($amsg), 'added attachment');
415 my $n = $ro->query('n:attached_fart.txt');
416 is(scalar @$n, 1, 'got result for n:');
417 my $res = $ro->query('part_deux.txt');
418 is(scalar @$res, 1, 'got result without n:');
419 is($n->[0]->mid, $res->[0]->mid,
420 'same result with and without') if scalar(@$res);
421 my $txt = $ro->query('"inside another"');
422 is(scalar @$txt, 1, 'found inside another');
423 is($txt->[0]->mid, $res->[0]->mid,
424 'search inside text attachments works') if scalar(@$txt);
427 if (scalar(@$n) >= 1) {
428 my $mid = $n->[0]->mid;
430 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
431 ok($art, 'article exists in OVER DB');
433 $rw->unindex_blob($amsg);
434 $rw->commit_txn_lazy;
436 skip('$art not defined', 1) unless defined $art;
437 is($ro->{over_ro}->get_art($art->{num}), undef,
438 'gone from OVER DB');
442 my $all_mask = 07777;
443 my $dir_mask = 02770;
445 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
446 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
447 if ($^O =~ /(?:free|open)bsd/i) {
452 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
453 "$git_dir/public-inbox",
454 glob("$git_dir/public-inbox/xapian*/"),
455 glob("$git_dir/public-inbox/xapian*/*")) {
457 my ($bn) = (split(m!/!, $f))[-1];
458 oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
459 "sharedRepository respected for $bn");
462 $ibx->with_umask(sub {
464 my $digits = '10010260936330';
465 my $ua = 'Pine.LNX.4.10';
466 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
467 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
469 my $pine = Email::MIME->create(
472 'Message-ID' => "<$mid>",
473 From => 'torvalds@transmeta',
474 To => 'list@example.com',
478 my $x = $rw->add_message($pine);
479 $rw->commit_txn_lazy;
480 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
481 'searching only digit yielded result');
486 is($ro->query("m:$wild*", { mset => 1})->size, 1,
487 "searching chopped($i) digit yielded result $wild ");
489 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);