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);
62 $ibx->with_umask(sub {
63 my $root = PublicInbox::Eml->new(<<'EOF');
64 Date: Fri, 02 Oct 1993 00:00:00 +0000
67 From: John Smith <js@example.com>
69 List-Id: I'm not mad <i.m.just.bored>
73 my $last = PublicInbox::Eml->new(<<'EOF');
74 Date: Sat, 02 Oct 2010 00:00:00 +0000
75 Subject: Re: Hello world
78 From: John Smith <js@example.com>
81 List-Id: there's nothing <left.for.me.to.do>
87 $root_id = $rw->add_message($root);
88 is($root_id, int($root_id), "root_id is an integer: $root_id");
89 $last_id = $rw->add_message($last);
90 is($last_id, int($last_id), "last_id is an integer: $last_id");
95 sort(map { $_->{mid} } @$msgs);
101 my $found = $ro->query('m:root@s');
102 is(scalar(@$found), 1, "message found");
103 is($found->[0]->{mid}, 'root@s', 'mid set correctly') if @$found;
106 my @exp = sort qw(root@s last@s);
108 $res = $ro->query('s:(Hello world)');
109 @res = filter_mids($res);
110 is_deeply(\@res, \@exp, 'got expected results for s:() match');
112 $res = $ro->query('s:"Hello world"');
113 @res = filter_mids($res);
114 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
116 $res = $ro->query('s:"Hello world"', {limit => 1});
117 is(scalar @$res, 1, "limit works");
118 my $first = $res->[0];
120 $res = $ro->query('s:"Hello world"', {offset => 1});
121 is(scalar @$res, 1, "offset works");
122 my $second = $res->[0];
124 isnt($first, $second, "offset returned different result from limit");
128 $ibx->with_umask(sub {
130 my $rmid = '<ghost-message@s>';
131 my $reply_to_ghost = PublicInbox::Eml->new(<<"EOF");
132 Date: Sat, 02 Oct 2010 00:00:00 +0000
134 Message-ID: <ghost-reply\@s>
136 From: Time Traveler <tt\@example.com>
137 To: list\@example.com
142 my $reply_id = $rw->add_message($reply_to_ghost);
143 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
145 my $was_ghost = PublicInbox::Eml->new(<<"EOF");
146 Date: Sat, 02 Oct 2010 00:00:01 +0000
149 From: Laggy Sender <lag\@example.com>
150 To: list\@example.com
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 @$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';
194 my $long = PublicInbox::Eml->new(<<EOF);
195 Date: Sat, 02 Oct 2010 00:00:00 +0000
196 Subject: long message ID
197 References: <root\@s> <last\@s>
198 In-Reply-To: <last\@s>
199 Message-ID: <$long_mid>,
200 From: "Long I.D." <long-id\@example.com>
201 To: list\@example.com
205 my $long_id = $rw->add_message($long);
206 is($long_id, int($long_id), "long_id is an integer: $long_id");
213 my $long_reply_mid = 'reply-to-long@1';
214 my $long_reply = PublicInbox::Eml->new(<<EOF);
215 Subject: I break references
216 Date: Sat, 02 Oct 2010 00:00:00 +0000
217 Message-ID: <$long_reply_mid>
218 In-Reply-To: <$long_mid>
219 From: no1 <no1\@example.com>
220 To: list\@example.com
224 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
228 my $t = $ro->{over_ro}->get_thread('root@s');
229 is(scalar(@$t), 4, "got all 4 messages in thread");
230 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
231 @res = filter_mids($t);
232 is_deeply(\@res, \@exp, "get_thread works");
235 # quote prioritization
236 $ibx->with_umask(sub {
238 $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
239 Date: Sat, 02 Oct 2010 00:00:01 +0000
241 Message-ID: <quote@a>
242 From: Quoter <quoter@example.com>
248 $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
249 Date: Sat, 02 Oct 2010 00:00:02 +0000
251 Message-ID: <nquote@a>
252 From: Non-Quoter<non-quoter@example.com>
258 my $res = $rw->query("theatre");
259 is(scalar(@$res), 2, "got both matches");
261 is($res->[0]->{mid}, 'nquote@a', 'non-quoted scores higher');
262 is($res->[1]->{mid}, 'quote@a', 'quoted result still returned');
264 $res = $rw->query("illusions");
265 is(scalar(@$res), 1, "got a match for quoted text");
266 is($res->[0]->{mid}, 'quote@a',
267 "quoted result returned if nothing else") if scalar(@$res);
270 # circular references
271 $ibx->with_umask(sub {
272 my $s = 'foo://'. ('Circle' x 15).'/foo';
273 my $doc_id = $rw->add_message(PublicInbox::Eml->new(<<EOF));
275 Date: Sat, 02 Oct 2010 00:00:01 +0000
276 Message-ID: <circle\@a>
277 References: <circle\@a>
278 In-Reply-To: <circle\@a>
279 From: Circle <circle\@example.com>
280 To: list\@example.com
284 ok($doc_id > 0, "doc_id defined with circular reference");
285 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
286 is(defined($smsg), 1, 'found m:circl@a');
288 is($smsg->{references}, '', "no references created");
289 is($smsg->{subject}, $s, 'long subject not rewritten');
293 $ibx->with_umask(sub {
294 my $mime = eml_load 't/utf8.eml';
295 my $doc_id = $rw->add_message($mime);
296 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
297 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
298 is(defined($msg), 1, 'found testmessage@example.com');
300 is($mime->header('Subject'), $msg->{subject},
301 'UTF-8 subject preserved');
306 my $msgs = $ro->query('d:19931002..20101002');
307 ok(scalar(@$msgs) > 0, 'got results within range');
308 $msgs = $ro->query('d:20101003..');
309 is(scalar(@$msgs), 0, 'nothing after 20101003');
310 $msgs = $ro->query('d:..19931001');
311 is(scalar(@$msgs), 0, 'nothing before 19931001');
314 # names and addresses
316 my $mset = $ro->query('t:list@example.com', {mset => 1});
317 is($mset->size, 6, 'searched To: successfully');
318 foreach my $m ($mset->items) {
319 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
320 like($smsg->{to}, qr/\blist\@example\.com\b/, 'to appears');
321 my $doc = $m->get_document;
322 my $col = PublicInbox::Search::BYTES();
323 my $bytes = PublicInbox::Smsg::get_val($doc, $col);
324 like($bytes, qr/\A[0-9]+\z/, '$bytes stored as digit');
325 ok($bytes > 0, '$bytes is > 0');
326 is($bytes, $smsg->{bytes}, 'bytes Xapian value matches Over');
329 $mset = $ro->query('tc:list@example.com', {mset => 1});
330 is($mset->size, 6, 'searched To+Cc: successfully');
331 foreach my $m ($mset->items) {
332 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
333 my $tocc = join("\n", $smsg->{to}, $smsg->{cc});
334 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
337 foreach my $pfx ('tcf:', 'c:') {
338 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
339 is($mset->items, 1, "searched $pfx successfully for Cc:");
340 foreach my $m ($mset->items) {
341 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
342 like($smsg->{cc}, qr/\bfoo\@example\.com\b/,
347 foreach my $pfx ('', 'tcf:', 'f:') {
348 my $res = $ro->query($pfx . 'Laggy');
350 "searched $pfx successfully for From:");
351 foreach my $smsg (@$res) {
352 like($smsg->{from_name}, qr/Laggy Sender/,
353 "From appears with $pfx");
361 my $res = $ro->query('b:hello');
362 is(scalar(@$res), 0, 'no match on body search only');
363 $res = $ro->query('bs:smith');
365 'no match on body+subject search for From');
367 $res = $ro->query('q:theatre');
368 is(scalar(@$res), 1, 'only one quoted body');
369 like($res->[0]->{from_name}, qr/\AQuoter/,
370 'got quoted body') if (scalar(@$res));
372 $res = $ro->query('nq:theatre');
373 is(scalar @$res, 1, 'only one non-quoted body');
374 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
375 'got non-quoted body') if (scalar(@$res));
377 foreach my $pfx (qw(b: bs:)) {
378 $res = $ro->query($pfx . 'theatre');
379 is(scalar @$res, 2, "searched both bodies for $pfx");
380 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
381 "non-quoter first for $pfx") if scalar(@$res);
385 $ibx->with_umask(sub {
386 my $amsg = eml_load 't/search-amsg.eml';
387 ok($rw->add_message($amsg), 'added attachment');
390 my $n = $ro->query('n:attached_fart.txt');
391 is(scalar @$n, 1, 'got result for n:');
392 my $res = $ro->query('part_deux.txt');
393 is(scalar @$res, 1, 'got result without n:');
394 is($n->[0]->{mid}, $res->[0]->{mid},
395 'same result with and without') if scalar(@$res);
396 my $txt = $ro->query('"inside another"');
397 is(scalar @$txt, 1, 'found inside another');
398 is($txt->[0]->{mid}, $res->[0]->{mid},
399 'search inside text attachments works') if scalar(@$txt);
402 if (scalar(@$n) >= 1) {
403 my $mid = $n->[0]->{mid};
405 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
406 ok($art, 'article exists in OVER DB');
408 $rw->unindex_blob($amsg);
409 $rw->commit_txn_lazy;
411 skip('$art not defined', 1) unless defined $art;
412 is($ro->{over_ro}->get_art($art->{num}), undef,
413 'gone from OVER DB');
417 my $all_mask = 07777;
418 my $dir_mask = 02770;
420 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
421 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
422 if ($^O =~ /(?:free|open)bsd/i) {
427 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
428 "$git_dir/public-inbox",
429 glob("$git_dir/public-inbox/xapian*/"),
430 glob("$git_dir/public-inbox/xapian*/*")) {
432 my ($bn) = (split(m!/!, $f))[-1];
433 oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
434 "sharedRepository respected for $bn");
437 $ibx->with_umask(sub {
439 my $digits = '10010260936330';
440 my $ua = 'Pine.LNX.4.10';
441 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
442 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
444 my $pine = PublicInbox::Eml->new(<<EOF);
447 From: torvalds\@transmeta
448 To: list\@example.com
451 my $x = $rw->add_message($pine);
452 $rw->commit_txn_lazy;
453 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
454 'searching only digit yielded result');
459 is($ro->query("m:$wild*", { mset => 1})->size, 1,
460 "searching chopped($i) digit yielded result $wild ");
462 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
465 { # List-Id searching
466 my $found = $ro->query('lid:i.m.just.bored');
467 is_deeply([ filter_mids($found) ], [ 'root@s' ],
468 'got expected mid on exact lid: search');
470 $found = $ro->query('lid:just.bored');
471 is_deeply($found, [], 'got nothing on lid: search');
473 $found = $ro->query('lid:*.just.bored');
474 is_deeply($found, [], 'got nothing on lid: search');
476 $found = $ro->query('l:i.m.just.bored');
477 is_deeply([ filter_mids($found) ], [ 'root@s' ],
478 'probabilistic search works on full List-Id contents');
480 $found = $ro->query('l:just.bored');
481 is_deeply([ filter_mids($found) ], [ 'root@s' ],
482 'probabilistic search works on partial List-Id contents');
484 $found = $ro->query('lid:mad');
485 is_deeply($found, [], 'no match on phrase with lid:');
487 $found = $ro->query('lid:bored');
488 is_deeply($found, [], 'no match on partial List-Id with lid:');
490 $found = $ro->query('l:nothing');
491 is_deeply($found, [], 'matched on phrase with l:');
494 $ibx->with_umask(sub {
496 my $doc_id = $rw->add_message(eml_load('t/data/message_embed.eml'));
497 ok($doc_id > 0, 'messages within messages');
498 $rw->commit_txn_lazy;
500 my $n_test_eml = $ro->query('n:test.eml');
501 is(scalar(@$n_test_eml), 1, 'got a result');
502 my $n_embed2x_eml = $ro->query('n:embed2x.eml');
503 is_deeply($n_test_eml, $n_embed2x_eml, '.eml filenames searchable');
504 for my $m (qw(20200418222508.GA13918@dcvr 20200418222020.GA2745@dcvr
505 20200418214114.7575-1-e@yhbt.net)) {
506 is($ro->query("m:$m")->[0]->{mid},
507 '20200418222508.GA13918@dcvr', 'probabilistic m:'.$m);
508 is($ro->query("mid:$m")->[0]->{mid},
509 '20200418222508.GA13918@dcvr', 'boolean mid:'.$m);
511 is($ro->query('dfpost:4dc62c50')->[0]->{mid},
512 '20200418222508.GA13918@dcvr',
513 'diff search reaches inside message/rfc822');
514 is($ro->query('s:"mail header experiments"')->[0]->{mid},
515 '20200418222508.GA13918@dcvr',
516 'Subject search reaches inside message/rfc822');