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');
323 $mset = $ro->query('tc:list@example.com', {mset => 1});
324 is($mset->size, 6, 'searched To+Cc: successfully');
325 foreach my $m ($mset->items) {
326 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
327 my $tocc = join("\n", $smsg->{to}, $smsg->{cc});
328 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
331 foreach my $pfx ('tcf:', 'c:') {
332 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
333 is($mset->items, 1, "searched $pfx successfully for Cc:");
334 foreach my $m ($mset->items) {
335 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
336 like($smsg->{cc}, qr/\bfoo\@example\.com\b/,
341 foreach my $pfx ('', 'tcf:', 'f:') {
342 my $res = $ro->query($pfx . 'Laggy');
344 "searched $pfx successfully for From:");
345 foreach my $smsg (@$res) {
346 like($smsg->{from_name}, qr/Laggy Sender/,
347 "From appears with $pfx");
355 my $res = $ro->query('b:hello');
356 is(scalar(@$res), 0, 'no match on body search only');
357 $res = $ro->query('bs:smith');
359 'no match on body+subject search for From');
361 $res = $ro->query('q:theatre');
362 is(scalar(@$res), 1, 'only one quoted body');
363 like($res->[0]->{from_name}, qr/\AQuoter/,
364 'got quoted body') if (scalar(@$res));
366 $res = $ro->query('nq:theatre');
367 is(scalar @$res, 1, 'only one non-quoted body');
368 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
369 'got non-quoted body') if (scalar(@$res));
371 foreach my $pfx (qw(b: bs:)) {
372 $res = $ro->query($pfx . 'theatre');
373 is(scalar @$res, 2, "searched both bodies for $pfx");
374 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
375 "non-quoter first for $pfx") if scalar(@$res);
379 $ibx->with_umask(sub {
380 my $amsg = eml_load 't/search-amsg.eml';
381 ok($rw->add_message($amsg), 'added attachment');
384 my $n = $ro->query('n:attached_fart.txt');
385 is(scalar @$n, 1, 'got result for n:');
386 my $res = $ro->query('part_deux.txt');
387 is(scalar @$res, 1, 'got result without n:');
388 is($n->[0]->{mid}, $res->[0]->{mid},
389 'same result with and without') if scalar(@$res);
390 my $txt = $ro->query('"inside another"');
391 is(scalar @$txt, 1, 'found inside another');
392 is($txt->[0]->{mid}, $res->[0]->{mid},
393 'search inside text attachments works') if scalar(@$txt);
396 if (scalar(@$n) >= 1) {
397 my $mid = $n->[0]->{mid};
399 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
400 ok($art, 'article exists in OVER DB');
402 $rw->unindex_blob($amsg);
403 $rw->commit_txn_lazy;
405 skip('$art not defined', 1) unless defined $art;
406 is($ro->{over_ro}->get_art($art->{num}), undef,
407 'gone from OVER DB');
411 my $all_mask = 07777;
412 my $dir_mask = 02770;
414 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
415 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
416 if ($^O =~ /(?:free|open)bsd/i) {
421 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
422 "$git_dir/public-inbox",
423 glob("$git_dir/public-inbox/xapian*/"),
424 glob("$git_dir/public-inbox/xapian*/*")) {
426 my ($bn) = (split(m!/!, $f))[-1];
427 oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
428 "sharedRepository respected for $bn");
431 $ibx->with_umask(sub {
433 my $digits = '10010260936330';
434 my $ua = 'Pine.LNX.4.10';
435 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
436 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
438 my $pine = PublicInbox::Eml->new(<<EOF);
441 From: torvalds\@transmeta
442 To: list\@example.com
445 my $x = $rw->add_message($pine);
446 $rw->commit_txn_lazy;
447 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
448 'searching only digit yielded result');
453 is($ro->query("m:$wild*", { mset => 1})->size, 1,
454 "searching chopped($i) digit yielded result $wild ");
456 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
459 { # List-Id searching
460 my $found = $ro->query('lid:i.m.just.bored');
461 is_deeply([ filter_mids($found) ], [ 'root@s' ],
462 'got expected mid on exact lid: search');
464 $found = $ro->query('lid:just.bored');
465 is_deeply($found, [], 'got nothing on lid: search');
467 $found = $ro->query('lid:*.just.bored');
468 is_deeply($found, [], 'got nothing on lid: search');
470 $found = $ro->query('l:i.m.just.bored');
471 is_deeply([ filter_mids($found) ], [ 'root@s' ],
472 'probabilistic search works on full List-Id contents');
474 $found = $ro->query('l:just.bored');
475 is_deeply([ filter_mids($found) ], [ 'root@s' ],
476 'probabilistic search works on partial List-Id contents');
478 $found = $ro->query('lid:mad');
479 is_deeply($found, [], 'no match on phrase with lid:');
481 $found = $ro->query('lid:bored');
482 is_deeply($found, [], 'no match on partial List-Id with lid:');
484 $found = $ro->query('l:nothing');
485 is_deeply($found, [], 'matched on phrase with l:');
488 $ibx->with_umask(sub {
490 my $doc_id = $rw->add_message(eml_load('t/data/message_embed.eml'));
491 ok($doc_id > 0, 'messages within messages');
492 $rw->commit_txn_lazy;
494 my $n_test_eml = $ro->query('n:test.eml');
495 is(scalar(@$n_test_eml), 1, 'got a result');
496 my $n_embed2x_eml = $ro->query('n:embed2x.eml');
497 is_deeply($n_test_eml, $n_embed2x_eml, '.eml filenames searchable');
498 for my $m (qw(20200418222508.GA13918@dcvr 20200418222020.GA2745@dcvr
499 20200418214114.7575-1-e@yhbt.net)) {
500 is($ro->query("m:$m")->[0]->{mid},
501 '20200418222508.GA13918@dcvr', 'probabilistic m:'.$m);
502 is($ro->query("mid:$m")->[0]->{mid},
503 '20200418222508.GA13918@dcvr', 'boolean mid:'.$m);
505 is($ro->query('dfpost:4dc62c50')->[0]->{mid},
506 '20200418222508.GA13918@dcvr',
507 'diff search reaches inside message/rfc822');
508 is($ro->query('s:"mail header experiments"')->[0]->{mid},
509 '20200418222508.GA13918@dcvr',
510 'Subject search reaches inside message/rfc822');