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 scalar(@$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 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';
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");
260 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
261 is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
263 $res = $rw->query("illusions");
264 is(scalar(@$res), 1, "got a match for quoted text");
265 is($res->[0]->mid, 'quote@a',
266 "quoted result returned if nothing else") if scalar(@$res);
269 # circular references
270 $ibx->with_umask(sub {
271 my $s = 'foo://'. ('Circle' x 15).'/foo';
272 my $doc_id = $rw->add_message(PublicInbox::Eml->new(<<EOF));
274 Date: Sat, 02 Oct 2010 00:00:01 +0000
275 Message-ID: <circle\@a>
276 References: <circle\@a>
277 In-Reply-To: <circle\@a>
278 From: Circle <circle\@example.com>
279 To: list\@example.com
283 ok($doc_id > 0, "doc_id defined with circular reference");
284 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
285 is(defined($smsg), 1, 'found m:circl@a');
286 is($smsg->references, '', "no references created") if defined($smsg);
287 is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
290 $ibx->with_umask(sub {
291 my $mime = eml_load 't/utf8.eml';
292 my $doc_id = $rw->add_message($mime);
293 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
294 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
295 is(defined($msg), 1, 'found testmessage@example.com');
296 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
300 my $msgs = $ro->query('d:19931002..20101002');
301 ok(scalar(@$msgs) > 0, 'got results within range');
302 $msgs = $ro->query('d:20101003..');
303 is(scalar(@$msgs), 0, 'nothing after 20101003');
304 $msgs = $ro->query('d:..19931001');
305 is(scalar(@$msgs), 0, 'nothing before 19931001');
308 # names and addresses
310 my $mset = $ro->query('t:list@example.com', {mset => 1});
311 is($mset->size, 6, 'searched To: successfully');
312 foreach my $m ($mset->items) {
313 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
314 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
317 $mset = $ro->query('tc:list@example.com', {mset => 1});
318 is($mset->size, 6, 'searched To+Cc: successfully');
319 foreach my $m ($mset->items) {
320 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
321 my $tocc = join("\n", $smsg->to, $smsg->cc);
322 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
325 foreach my $pfx ('tcf:', 'c:') {
326 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
327 is($mset->items, 1, "searched $pfx successfully for Cc:");
328 foreach my $m ($mset->items) {
329 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
330 like($smsg->cc, qr/\bfoo\@example\.com\b/,
335 foreach my $pfx ('', 'tcf:', 'f:') {
336 my $res = $ro->query($pfx . 'Laggy');
338 "searched $pfx successfully for From:");
339 foreach my $smsg (@$res) {
340 like($smsg->from_name, qr/Laggy Sender/,
341 "From appears with $pfx");
349 my $res = $ro->query('b:hello');
350 is(scalar(@$res), 0, 'no match on body search only');
351 $res = $ro->query('bs:smith');
353 'no match on body+subject search for From');
355 $res = $ro->query('q:theatre');
356 is(scalar(@$res), 1, 'only one quoted body');
357 like($res->[0]->from_name, qr/\AQuoter/,
358 'got quoted body') if (scalar(@$res));
360 $res = $ro->query('nq:theatre');
361 is(scalar @$res, 1, 'only one non-quoted body');
362 like($res->[0]->from_name, qr/\ANon-Quoter/,
363 'got non-quoted body') if (scalar(@$res));
365 foreach my $pfx (qw(b: bs:)) {
366 $res = $ro->query($pfx . 'theatre');
367 is(scalar @$res, 2, "searched both bodies for $pfx");
368 like($res->[0]->from_name, qr/\ANon-Quoter/,
369 "non-quoter first for $pfx") if scalar(@$res);
373 $ibx->with_umask(sub {
374 my $amsg = eml_load 't/search-amsg.eml';
375 ok($rw->add_message($amsg), 'added attachment');
378 my $n = $ro->query('n:attached_fart.txt');
379 is(scalar @$n, 1, 'got result for n:');
380 my $res = $ro->query('part_deux.txt');
381 is(scalar @$res, 1, 'got result without n:');
382 is($n->[0]->mid, $res->[0]->mid,
383 'same result with and without') if scalar(@$res);
384 my $txt = $ro->query('"inside another"');
385 is(scalar @$txt, 1, 'found inside another');
386 is($txt->[0]->mid, $res->[0]->mid,
387 'search inside text attachments works') if scalar(@$txt);
390 if (scalar(@$n) >= 1) {
391 my $mid = $n->[0]->mid;
393 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
394 ok($art, 'article exists in OVER DB');
396 $rw->unindex_blob($amsg);
397 $rw->commit_txn_lazy;
399 skip('$art not defined', 1) unless defined $art;
400 is($ro->{over_ro}->get_art($art->{num}), undef,
401 'gone from OVER DB');
405 my $all_mask = 07777;
406 my $dir_mask = 02770;
408 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
409 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
410 if ($^O =~ /(?:free|open)bsd/i) {
415 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
416 "$git_dir/public-inbox",
417 glob("$git_dir/public-inbox/xapian*/"),
418 glob("$git_dir/public-inbox/xapian*/*")) {
420 my ($bn) = (split(m!/!, $f))[-1];
421 oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
422 "sharedRepository respected for $bn");
425 $ibx->with_umask(sub {
427 my $digits = '10010260936330';
428 my $ua = 'Pine.LNX.4.10';
429 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
430 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
432 my $pine = PublicInbox::Eml->new(<<EOF);
435 From: torvalds\@transmeta
436 To: list\@example.com
439 my $x = $rw->add_message($pine);
440 $rw->commit_txn_lazy;
441 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
442 'searching only digit yielded result');
447 is($ro->query("m:$wild*", { mset => 1})->size, 1,
448 "searching chopped($i) digit yielded result $wild ");
450 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
453 { # List-Id searching
454 my $found = $ro->query('lid:i.m.just.bored');
455 is_deeply([ filter_mids($found) ], [ 'root@s' ],
456 'got expected mid on exact lid: search');
458 $found = $ro->query('lid:just.bored');
459 is_deeply($found, [], 'got nothing on lid: search');
461 $found = $ro->query('lid:*.just.bored');
462 is_deeply($found, [], 'got nothing on lid: search');
464 $found = $ro->query('l:i.m.just.bored');
465 is_deeply([ filter_mids($found) ], [ 'root@s' ],
466 'probabilistic search works on full List-Id contents');
468 $found = $ro->query('l:just.bored');
469 is_deeply([ filter_mids($found) ], [ 'root@s' ],
470 'probabilistic search works on partial List-Id contents');
472 $found = $ro->query('lid:mad');
473 is_deeply($found, [], 'no match on phrase with lid:');
475 $found = $ro->query('lid:bored');
476 is_deeply($found, [], 'no match on partial List-Id with lid:');
478 $found = $ro->query('l:nothing');
479 is_deeply($found, [], 'matched on phrase with l:');