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;
10 require PublicInbox::InboxWritable;
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $git_dir = "$tmpdir/a.git";
14 my $ibx = PublicInbox::Inbox->new({ inboxdir => $git_dir });
15 my ($root_id, $last_id);
17 is(0, xsys(qw(git init --shared -q --bare), $git_dir), "git init (main)")
18 or BAIL_OUT("`git init --shared' failed, weird FS or seccomp?");
19 eval { PublicInbox::Search->new($ibx)->xdb };
20 ok($@, "exception raised on non-existent DB");
22 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
23 $ibx->with_umask(sub {
28 my $ro = PublicInbox::Search->new($ibx);
30 $rw->commit_txn_lazy if $rw;
31 $rw = PublicInbox::SearchIdx->new($ibx, 1);
32 $rw->{qp_flags} = 0; # quiet a warning
37 my ($got, $exp, $msg) = @_;
38 is(sprintf('0%03o', $got), sprintf('0%03o', $exp), $msg);
42 # git repository perms
43 oct_is($ibx->_git_config_perm(),
44 &PublicInbox::InboxWritable::PERM_GROUP,
45 'undefined permission is group');
47 [ '0644', 0022, '644 => umask(0022)' ],
48 [ '0600', 0077, '600 => umask(0077)' ],
49 [ '0640', 0027, '640 => umask(0027)' ],
50 [ 'group', 0007, 'group => umask(0007)' ],
51 [ 'everybody', 0002, 'everybody => umask(0002)' ],
52 [ 'umask', umask, 'umask => existing umask' ],
55 my ($perm, $exp, $msg) = @$_;
56 my $got = PublicInbox::InboxWritable::_umask_for(
57 PublicInbox::InboxWritable->_git_config_perm($perm));
58 oct_is($got, $exp, $msg);
63 my $crlf_adjust = \&PublicInbox::SearchIdx::crlf_adjust;
64 is($crlf_adjust->("hi\r\nworld\r\n"), 0, 'no adjustment needed');
65 is($crlf_adjust->("hi\nworld\n"), 2, 'LF-only counts two CR');
66 is($crlf_adjust->("hi\r\nworld\n"), 1, 'CRLF/LF-mix 1 counts 1 CR');
67 is($crlf_adjust->("hi\nworld\r\n"), 1, 'CRLF/LF-mix 2 counts 1 CR');
70 $ibx->with_umask(sub {
71 my $root = PublicInbox::Eml->new(<<'EOF');
72 Date: Fri, 02 Oct 1993 00:00:00 +0000
75 From: John Smith <js@example.com>
77 List-Id: I'm not mad <i.m.just.bored>
81 my $last = PublicInbox::Eml->new(<<'EOF');
82 Date: Sat, 02 Oct 2010 00:00:00 +0000
83 Subject: Re: Hello world
86 From: John Smith <js@example.com>
89 List-Id: there's nothing <left.for.me.to.do>
95 $root_id = $rw->add_message($root);
96 is($root_id, int($root_id), "root_id is an integer: $root_id");
97 $last_id = $rw->add_message($last);
98 is($last_id, int($last_id), "last_id is an integer: $last_id");
103 sort(map { $_->{mid} } @$msgs);
109 my $found = $ro->query('m:root@s');
110 is(scalar(@$found), 1, "message found");
111 is($found->[0]->{mid}, 'root@s', 'mid set correctly') if @$found;
114 my @exp = sort qw(root@s last@s);
116 $res = $ro->query('s:(Hello world)');
117 @res = filter_mids($res);
118 is_deeply(\@res, \@exp, 'got expected results for s:() match');
120 $res = $ro->query('s:"Hello world"');
121 @res = filter_mids($res);
122 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
124 $res = $ro->query('s:"Hello world"', {limit => 1});
125 is(scalar @$res, 1, "limit works");
126 my $first = $res->[0];
128 $res = $ro->query('s:"Hello world"', {offset => 1});
129 is(scalar @$res, 1, "offset works");
130 my $second = $res->[0];
132 isnt($first, $second, "offset returned different result from limit");
136 $ibx->with_umask(sub {
138 my $rmid = '<ghost-message@s>';
139 my $reply_to_ghost = PublicInbox::Eml->new(<<"EOF");
140 Date: Sat, 02 Oct 2010 00:00:00 +0000
142 Message-ID: <ghost-reply\@s>
144 From: Time Traveler <tt\@example.com>
145 To: list\@example.com
150 my $reply_id = $rw->add_message($reply_to_ghost);
151 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
153 my $was_ghost = PublicInbox::Eml->new(<<"EOF");
154 Date: Sat, 02 Oct 2010 00:00:01 +0000
157 From: Laggy Sender <lag\@example.com>
158 To: list\@example.com
162 my $ghost_id = $rw->add_message($was_ghost);
163 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
164 my $msgs = $rw->{over}->get_thread('ghost-message@s');
165 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
166 foreach (qw(sid tid)) {
167 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
169 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
170 ok($_->{num} > 0, 'positive art num') foreach @$msgs
173 # search thread on ghost
179 my $res = $ro->query('ghost');
180 my @exp = sort qw(ghost-message@s ghost-reply@s);
181 my @res = filter_mids($res);
182 is_deeply(\@res, \@exp, 'got expected results for Subject match');
185 $res = $ro->query('goodbye');
186 is(scalar(@$res), 1, "goodbye message found");
187 is($res->[0]->{mid}, 'last@s', 'got goodbye message body') if @$res;
190 $res = $ro->query('dt:20101002000001..20101002000001');
191 @res = filter_mids($res);
192 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
193 $res = $ro->query('dt:20101002000002..20101002000002');
194 is_deeply($res, [], 'exact Date: match down to the second');
198 $ibx->with_umask(sub {
201 my $long_mid = 'last' . ('x' x 60). '@s';
202 my $long = PublicInbox::Eml->new(<<EOF);
203 Date: Sat, 02 Oct 2010 00:00:00 +0000
204 Subject: long message ID
205 References: <root\@s> <last\@s>
206 In-Reply-To: <last\@s>
207 Message-ID: <$long_mid>,
208 From: "Long I.D." <long-id\@example.com>
209 To: list\@example.com
213 my $long_id = $rw->add_message($long);
214 is($long_id, int($long_id), "long_id is an integer: $long_id");
221 my $long_reply_mid = 'reply-to-long@1';
222 my $long_reply = PublicInbox::Eml->new(<<EOF);
223 Subject: I break references
224 Date: Sat, 02 Oct 2010 00:00:00 +0000
225 Message-ID: <$long_reply_mid>
226 In-Reply-To: <$long_mid>
227 From: no1 <no1\@example.com>
228 To: list\@example.com
232 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
236 my $t = $ro->{over_ro}->get_thread('root@s');
237 is(scalar(@$t), 4, "got all 4 messages in thread");
238 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
239 @res = filter_mids($t);
240 is_deeply(\@res, \@exp, "get_thread works");
243 # quote prioritization
244 $ibx->with_umask(sub {
246 $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
247 Date: Sat, 02 Oct 2010 00:00:01 +0000
249 Message-ID: <quote@a>
250 From: Quoter <quoter@example.com>
256 $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
257 Date: Sat, 02 Oct 2010 00:00:02 +0000
259 Message-ID: <nquote@a>
260 From: Non-Quoter<non-quoter@example.com>
266 my $res = $rw->query("theatre");
267 is(scalar(@$res), 2, "got both matches");
269 is($res->[0]->{mid}, 'nquote@a', 'non-quoted scores higher');
270 is($res->[1]->{mid}, 'quote@a', 'quoted result still returned');
272 $res = $rw->query("illusions");
273 is(scalar(@$res), 1, "got a match for quoted text");
274 is($res->[0]->{mid}, 'quote@a',
275 "quoted result returned if nothing else") if scalar(@$res);
278 # circular references
279 $ibx->with_umask(sub {
280 my $s = 'foo://'. ('Circle' x 15).'/foo';
281 my $doc_id = $rw->add_message(PublicInbox::Eml->new(<<EOF));
283 Date: Sat, 02 Oct 2010 00:00:01 +0000
284 Message-ID: <circle\@a>
285 References: <circle\@a>
286 In-Reply-To: <circle\@a>
287 From: Circle <circle\@example.com>
288 To: list\@example.com
292 ok($doc_id > 0, "doc_id defined with circular reference");
293 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
294 is(defined($smsg), 1, 'found m:circl@a');
296 is($smsg->{references}, '', "no references created");
297 is($smsg->{subject}, $s, 'long subject not rewritten');
301 $ibx->with_umask(sub {
302 my $mime = eml_load 't/utf8.eml';
303 my $doc_id = $rw->add_message($mime);
304 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
305 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
306 is(defined($msg), 1, 'found testmessage@example.com');
308 is($mime->header('Subject'), $msg->{subject},
309 'UTF-8 subject preserved');
314 my $msgs = $ro->query('d:19931002..20101002');
315 ok(scalar(@$msgs) > 0, 'got results within range');
316 $msgs = $ro->query('d:20101003..');
317 is(scalar(@$msgs), 0, 'nothing after 20101003');
318 $msgs = $ro->query('d:..19931001');
319 is(scalar(@$msgs), 0, 'nothing before 19931001');
322 # names and addresses
324 my $mset = $ro->query('t:list@example.com', {mset => 1});
325 is($mset->size, 6, 'searched To: successfully');
326 foreach my $m ($mset->items) {
327 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
328 like($smsg->{to}, qr/\blist\@example\.com\b/, 'to appears');
329 my $doc = $m->get_document;
330 my $col = PublicInbox::Search::BYTES();
331 my $bytes = PublicInbox::Smsg::get_val($doc, $col);
332 like($bytes, qr/\A[0-9]+\z/, '$bytes stored as digit');
333 ok($bytes > 0, '$bytes is > 0');
334 is($bytes, $smsg->{bytes}, 'bytes Xapian value matches Over');
336 $col = PublicInbox::Search::UID();
337 my $uid = PublicInbox::Smsg::get_val($doc, $col);
338 is($uid, $smsg->{num}, 'UID column matches {num}');
339 is($uid, $m->get_docid, 'UID column matches docid');
342 $mset = $ro->query('tc:list@example.com', {mset => 1});
343 is($mset->size, 6, 'searched To+Cc: successfully');
344 foreach my $m ($mset->items) {
345 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
346 my $tocc = join("\n", $smsg->{to}, $smsg->{cc});
347 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
350 foreach my $pfx ('tcf:', 'c:') {
351 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
352 is($mset->items, 1, "searched $pfx successfully for Cc:");
353 foreach my $m ($mset->items) {
354 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
355 like($smsg->{cc}, qr/\bfoo\@example\.com\b/,
360 foreach my $pfx ('', 'tcf:', 'f:') {
361 my $res = $ro->query($pfx . 'Laggy');
363 "searched $pfx successfully for From:");
364 foreach my $smsg (@$res) {
365 like($smsg->{from_name}, qr/Laggy Sender/,
366 "From appears with $pfx");
374 my $res = $ro->query('b:hello');
375 is(scalar(@$res), 0, 'no match on body search only');
376 $res = $ro->query('bs:smith');
378 'no match on body+subject search for From');
380 $res = $ro->query('q:theatre');
381 is(scalar(@$res), 1, 'only one quoted body');
382 like($res->[0]->{from_name}, qr/\AQuoter/,
383 'got quoted body') if (scalar(@$res));
385 $res = $ro->query('nq:theatre');
386 is(scalar @$res, 1, 'only one non-quoted body');
387 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
388 'got non-quoted body') if (scalar(@$res));
390 foreach my $pfx (qw(b: bs:)) {
391 $res = $ro->query($pfx . 'theatre');
392 is(scalar @$res, 2, "searched both bodies for $pfx");
393 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
394 "non-quoter first for $pfx") if scalar(@$res);
398 $ibx->with_umask(sub {
399 my $amsg = eml_load 't/search-amsg.eml';
401 my $smsg = bless { blob => $oid }, 'PublicInbox::Smsg';
402 ok($rw->add_message($amsg, $smsg), 'added attachment');
405 my $n = $ro->query('n:attached_fart.txt');
406 is(scalar @$n, 1, 'got result for n:');
407 my $res = $ro->query('part_deux.txt');
408 is(scalar @$res, 1, 'got result without n:');
409 is($n->[0]->{mid}, $res->[0]->{mid},
410 'same result with and without') if scalar(@$res);
411 my $txt = $ro->query('"inside another"');
412 is(scalar @$txt, 1, 'found inside another');
413 is($txt->[0]->{mid}, $res->[0]->{mid},
414 'search inside text attachments works') if scalar(@$txt);
417 if (scalar(@$n) >= 1) {
418 my $mid = $n->[0]->{mid};
420 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
421 ok($art, 'article exists in OVER DB');
424 $rw->unindex_eml($oid, $amsg);
425 $rw->commit_txn_lazy;
427 skip('$art not defined', 1) unless defined $art;
428 is($ro->{over_ro}->get_art($art->{num}), undef,
429 'gone from OVER DB');
433 my $all_mask = 07777;
434 my $dir_mask = 02770;
436 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
437 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
438 if ($^O =~ /(?:free|open)bsd/i) {
443 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
444 "$git_dir/public-inbox",
445 glob("$git_dir/public-inbox/xapian*/"),
446 glob("$git_dir/public-inbox/xapian*/*")) {
448 my ($bn) = (split(m!/!, $f))[-1];
449 oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
450 "sharedRepository respected for $bn");
453 $ibx->with_umask(sub {
455 my $digits = '10010260936330';
456 my $ua = 'Pine.LNX.4.10';
457 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
458 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
460 my $pine = PublicInbox::Eml->new(<<EOF);
463 From: torvalds\@transmeta
464 To: list\@example.com
467 my $x = $rw->add_message($pine);
468 $rw->commit_txn_lazy;
469 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
470 'searching only digit yielded result');
475 is($ro->query("m:$wild*", { mset => 1})->size, 1,
476 "searching chopped($i) digit yielded result $wild ");
478 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
481 { # List-Id searching
482 my $found = $ro->query('lid:i.m.just.bored');
483 is_deeply([ filter_mids($found) ], [ 'root@s' ],
484 'got expected mid on exact lid: search');
486 $found = $ro->query('lid:just.bored');
487 is_deeply($found, [], 'got nothing on lid: search');
489 $found = $ro->query('lid:*.just.bored');
490 is_deeply($found, [], 'got nothing on lid: search');
492 $found = $ro->query('l:i.m.just.bored');
493 is_deeply([ filter_mids($found) ], [ 'root@s' ],
494 'probabilistic search works on full List-Id contents');
496 $found = $ro->query('l:just.bored');
497 is_deeply([ filter_mids($found) ], [ 'root@s' ],
498 'probabilistic search works on partial List-Id contents');
500 $found = $ro->query('lid:mad');
501 is_deeply($found, [], 'no match on phrase with lid:');
503 $found = $ro->query('lid:bored');
504 is_deeply($found, [], 'no match on partial List-Id with lid:');
506 $found = $ro->query('l:nothing');
507 is_deeply($found, [], 'matched on phrase with l:');
510 $ibx->with_umask(sub {
512 my $doc_id = $rw->add_message(eml_load('t/data/message_embed.eml'));
513 ok($doc_id > 0, 'messages within messages');
514 $rw->commit_txn_lazy;
516 my $n_test_eml = $ro->query('n:test.eml');
517 is(scalar(@$n_test_eml), 1, 'got a result');
518 my $n_embed2x_eml = $ro->query('n:embed2x.eml');
519 is_deeply($n_test_eml, $n_embed2x_eml, '.eml filenames searchable');
520 for my $m (qw(20200418222508.GA13918@dcvr 20200418222020.GA2745@dcvr
521 20200418214114.7575-1-e@yhbt.net)) {
522 is($ro->query("m:$m")->[0]->{mid},
523 '20200418222508.GA13918@dcvr', 'probabilistic m:'.$m);
524 is($ro->query("mid:$m")->[0]->{mid},
525 '20200418222508.GA13918@dcvr', 'boolean mid:'.$m);
527 is($ro->query('dfpost:4dc62c50')->[0]->{mid},
528 '20200418222508.GA13918@dcvr',
529 'diff search reaches inside message/rfc822');
530 is($ro->query('s:"mail header experiments"')->[0]->{mid},
531 '20200418222508.GA13918@dcvr',
532 'Subject search reaches inside message/rfc822');