1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 my @mods = qw(DBI DBD::SQLite Search::Xapian);
7 foreach my $mod (@mods) {
9 plan skip_all => "missing $mod for $0" if $@;
11 require PublicInbox::SearchIdx;
12 require PublicInbox::Inbox;
13 use File::Temp qw/tempdir/;
15 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
16 my $git_dir = "$tmpdir/a.git";
17 my $ibx = PublicInbox::Inbox->new({ mainrepo => $git_dir });
18 my ($root_id, $last_id);
20 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)");
21 eval { PublicInbox::Search->new($ibx)->xdb };
22 ok($@, "exception raised on non-existent DB");
24 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
25 $ibx->with_umask(sub {
30 my $ro = PublicInbox::Search->new($ibx);
32 $rw->commit_txn_lazy if $rw;
33 $rw = PublicInbox::SearchIdx->new($ibx, 1);
34 $rw->{qp_flags} = 0; # quiet a warning
39 # git repository perms
40 is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
41 "undefined permission is group");
42 is(PublicInbox::InboxWritable::_umask_for(
43 PublicInbox::InboxWritable->_git_config_perm('0644')),
44 0022, "644 => umask(0022)");
45 is(PublicInbox::InboxWritable::_umask_for(
46 PublicInbox::InboxWritable->_git_config_perm('0600')),
47 0077, "600 => umask(0077)");
48 is(PublicInbox::InboxWritable::_umask_for(
49 PublicInbox::InboxWritable->_git_config_perm('0640')),
50 0027, "640 => umask(0027)");
51 is(PublicInbox::InboxWritable::_umask_for(
52 PublicInbox::InboxWritable->_git_config_perm('group')),
53 0007, 'group => umask(0007)');
54 is(PublicInbox::InboxWritable::_umask_for(
55 PublicInbox::InboxWritable->_git_config_perm('everybody')),
56 0002, 'everybody => umask(0002)');
57 is(PublicInbox::InboxWritable::_umask_for(
58 PublicInbox::InboxWritable->_git_config_perm('umask')),
59 umask, 'umask => existing umask');
62 $ibx->with_umask(sub {
63 my $root = Email::MIME->create(
65 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
66 Subject => 'Hello world',
67 'Message-ID' => '<root@s>',
68 From => 'John Smith <js@example.com>',
69 To => 'list@example.com',
72 my $last = Email::MIME->create(
74 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
75 Subject => 'Re: Hello world',
76 'In-Reply-To' => '<root@s>',
77 'Message-ID' => '<last@s>',
78 From => 'John Smith <js@example.com>',
79 To => 'list@example.com',
80 Cc => 'foo@example.com',
82 body => "goodbye forever :<\n");
86 $root_id = $rw->add_message($root);
87 is($root_id, int($root_id), "root_id is an integer: $root_id");
88 $last_id = $rw->add_message($last);
89 is($last_id, int($last_id), "last_id is an integer: $last_id");
94 sort(map { $_->mid } @$msgs);
100 my $found = $ro->query('m:root@s');
101 is(scalar(@$found), 1, "message found");
102 is($found->[0]->mid, 'root@s', 'mid set correctly') if scalar(@$found);
105 my @exp = sort qw(root@s last@s);
107 $res = $ro->query('s:(Hello world)');
108 @res = filter_mids($res);
109 is_deeply(\@res, \@exp, 'got expected results for s:() match');
111 $res = $ro->query('s:"Hello world"');
112 @res = filter_mids($res);
113 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
115 $res = $ro->query('s:"Hello world"', {limit => 1});
116 is(scalar @$res, 1, "limit works");
117 my $first = $res->[0];
119 $res = $ro->query('s:"Hello world"', {offset => 1});
120 is(scalar @$res, 1, "offset works");
121 my $second = $res->[0];
123 isnt($first, $second, "offset returned different result from limit");
127 $ibx->with_umask(sub {
129 my $rmid = '<ghost-message@s>';
130 my $reply_to_ghost = Email::MIME->create(
132 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
133 Subject => 'Re: ghosts',
134 'Message-ID' => '<ghost-reply@s>',
135 'In-Reply-To' => $rmid,
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 = Email::MIME->create(
147 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
149 'Message-ID' => $rmid,
150 From => 'Laggy Sender <lag@example.com>',
151 To => 'list@example.com',
153 body => "are real\n");
155 my $ghost_id = $rw->add_message($was_ghost);
156 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
157 my $msgs = $rw->{over}->get_thread('ghost-message@s');
158 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
159 foreach (qw(sid tid)) {
160 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
162 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
163 ok($_->{num} > 0, 'positive art num') foreach @$msgs
166 # search thread on ghost
172 my $res = $ro->query('ghost');
173 my @exp = sort qw(ghost-message@s ghost-reply@s);
174 my @res = filter_mids($res);
175 is_deeply(\@res, \@exp, 'got expected results for Subject match');
178 $res = $ro->query('goodbye');
179 is(scalar(@$res), 1, "goodbye message found");
180 is($res->[0]->mid, 'last@s', 'got goodbye message body') if scalar(@$res);
183 $res = $ro->query('dt:20101002000001..20101002000001');
184 @res = filter_mids($res);
185 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
186 $res = $ro->query('dt:20101002000002..20101002000002');
187 is_deeply($res, [], 'exact Date: match down to the second');
191 $ibx->with_umask(sub {
194 my $long_mid = 'last' . ('x' x 60). '@s';
196 my $long = Email::MIME->create(
198 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
199 Subject => 'long message ID',
200 'References' => '<root@s> <last@s>',
201 'In-Reply-To' => '<last@s>',
202 'Message-ID' => "<$long_mid>",
203 From => '"Long I.D." <long-id@example.com>',
204 To => 'list@example.com',
207 my $long_id = $rw->add_message($long);
208 is($long_id, int($long_id), "long_id is an integer: $long_id");
215 my $long_reply_mid = 'reply-to-long@1';
216 my $long_reply = Email::MIME->create(
218 Subject => 'I break references',
219 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
220 'Message-ID' => "<$long_reply_mid>",
222 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
223 'In-Reply-To' => "<$long_mid>",
224 From => 'no1 <no1@example.com>',
225 To => 'list@example.com',
227 body => "no References\n");
228 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
232 my $t = $ro->{over_ro}->get_thread('root@s');
233 is(scalar(@$t), 4, "got all 4 mesages in thread");
234 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
235 @res = filter_mids($t);
236 is_deeply(\@res, \@exp, "get_thread works");
239 # quote prioritization
240 $ibx->with_umask(sub {
242 $rw->add_message(Email::MIME->create(
244 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
246 'Message-ID' => '<quote@a>',
247 From => 'Quoter <quoter@example.com>',
248 To => 'list@example.com',
250 body => "> theatre illusions\nfade\n"));
252 $rw->add_message(Email::MIME->create(
254 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
256 'Message-ID' => '<nquote@a>',
257 From => 'Non-Quoter<non-quoter@example.com>',
258 To => 'list@example.com',
260 body => "theatre\nfade\n"));
261 my $res = $rw->query("theatre");
262 is(scalar(@$res), 2, "got both matches");
263 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
264 is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
266 $res = $rw->query("illusions");
267 is(scalar(@$res), 1, "got a match for quoted text");
268 is($res->[0]->mid, 'quote@a',
269 "quoted result returned if nothing else") if scalar(@$res);
272 # circular references
273 $ibx->with_umask(sub {
274 my $s = 'foo://'. ('Circle' x 15).'/foo';
275 my $doc_id = $rw->add_message(Email::MIME->create(
276 header => [ Subject => $s ],
278 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
279 'Message-ID' => '<circle@a>',
280 'References' => '<circle@a>',
281 'In-Reply-To' => '<circle@a>',
282 From => 'Circle <circle@example.com>',
283 To => 'list@example.com',
286 ok($doc_id > 0, "doc_id defined with circular reference");
287 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
288 is(defined($smsg), 1, 'found m:circl@a');
289 is($smsg->references, '', "no references created") if defined($smsg);
290 is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
293 $ibx->with_umask(sub {
295 my $mbox = 't/utf8.mbox';
296 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
300 $str =~ s/\AFrom [^\n]+\n//s;
301 my $mime = Email::MIME->new($str);
302 my $doc_id = $rw->add_message($mime);
303 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
304 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
305 is(defined($msg), 1, 'found testmessage@example.com');
306 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
310 my $msgs = $ro->query('d:19931002..20101002');
311 ok(scalar(@$msgs) > 0, 'got results within range');
312 $msgs = $ro->query('d:20101003..');
313 is(scalar(@$msgs), 0, 'nothing after 20101003');
314 $msgs = $ro->query('d:..19931001');
315 is(scalar(@$msgs), 0, 'nothing before 19931001');
318 # names and addresses
320 my $mset = $ro->query('t:list@example.com', {mset => 1});
321 is($mset->size, 6, 'searched To: successfully');
322 foreach my $m ($mset->items) {
323 my $smsg = $ro->lookup_article($m->get_docid);
324 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
327 $mset = $ro->query('tc:list@example.com', {mset => 1});
328 is($mset->size, 6, 'searched To+Cc: successfully');
329 foreach my $m ($mset->items) {
330 my $smsg = $ro->lookup_article($m->get_docid);
331 my $tocc = join("\n", $smsg->to, $smsg->cc);
332 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
335 foreach my $pfx ('tcf:', 'c:') {
336 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
337 is($mset->items, 1, "searched $pfx successfully for Cc:");
338 foreach my $m ($mset->items) {
339 my $smsg = $ro->lookup_article($m->get_docid);
340 like($smsg->cc, qr/\bfoo\@example\.com\b/,
345 foreach my $pfx ('', 'tcf:', 'f:') {
346 my $res = $ro->query($pfx . 'Laggy');
348 "searched $pfx successfully for From:");
349 foreach my $smsg (@$res) {
350 like($smsg->from_name, qr/Laggy Sender/,
351 "From appears with $pfx");
359 my $res = $ro->query('b:hello');
360 is(scalar(@$res), 0, 'no match on body search only');
361 $res = $ro->query('bs:smith');
363 'no match on body+subject search for From');
365 $res = $ro->query('q:theatre');
366 is(scalar(@$res), 1, 'only one quoted body');
367 like($res->[0]->from_name, qr/\AQuoter/,
368 'got quoted body') if (scalar(@$res));
370 $res = $ro->query('nq:theatre');
371 is(scalar @$res, 1, 'only one non-quoted body');
372 like($res->[0]->from_name, qr/\ANon-Quoter/,
373 'got non-quoted body') if (scalar(@$res));
375 foreach my $pfx (qw(b: bs:)) {
376 $res = $ro->query($pfx . 'theatre');
377 is(scalar @$res, 2, "searched both bodies for $pfx");
378 like($res->[0]->from_name, qr/\ANon-Quoter/,
379 "non-quoter first for $pfx") if scalar(@$res);
383 $ibx->with_umask(sub {
384 my $part1 = Email::MIME->create(
386 content_type => 'text/plain',
387 disposition => 'attachment',
388 charset => 'US-ASCII',
389 encoding => 'quoted-printable',
390 filename => 'attached_fart.txt',
392 body_str => 'inside the attachment',
394 my $part2 = Email::MIME->create(
396 content_type => 'text/plain',
397 disposition => 'attachment',
398 charset => 'US-ASCII',
399 encoding => 'quoted-printable',
400 filename => 'part_deux.txt',
402 body_str => 'inside another',
404 my $amsg = Email::MIME->create(
406 Subject => 'see attachment',
407 'Message-ID' => '<file@attached>',
408 From => 'John Smith <js@example.com>',
409 To => 'list@example.com',
411 parts => [ $part1, $part2 ],
413 ok($rw->add_message($amsg), 'added attachment');
416 my $n = $ro->query('n:attached_fart.txt');
417 is(scalar @$n, 1, 'got result for n:');
418 my $res = $ro->query('part_deux.txt');
419 is(scalar @$res, 1, 'got result without n:');
420 is($n->[0]->mid, $res->[0]->mid,
421 'same result with and without') if scalar(@$res);
422 my $txt = $ro->query('"inside another"');
423 is(scalar @$txt, 1, 'found inside another');
424 is($txt->[0]->mid, $res->[0]->mid,
425 'search inside text attachments works') if scalar(@$txt);
428 if (scalar(@$n) >= 1) {
429 my $mid = $n->[0]->mid;
431 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
432 ok($art, 'article exists in OVER DB');
434 $rw->unindex_blob($amsg);
435 $rw->commit_txn_lazy;
436 is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB') if defined($art);
439 my $all_mask = 07777;
440 my $dir_mask = 02770;
442 # FreeBSD does not allow non-root users to set S_ISGID, so
443 # git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
444 if ($^O =~ /freebsd/i) {
449 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
450 "$git_dir/public-inbox",
451 glob("$git_dir/public-inbox/xapian*/"),
452 glob("$git_dir/public-inbox/xapian*/*")) {
454 my ($bn) = (split(m!/!, $f))[-1];
455 is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
456 "sharedRepository respected for $bn");
459 $ibx->with_umask(sub {
461 my $digits = '10010260936330';
462 my $ua = 'Pine.LNX.4.10';
463 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
464 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
466 my $pine = Email::MIME->create(
469 'Message-ID' => "<$mid>",
470 From => 'torvalds@transmeta',
471 To => 'list@example.com',
475 my $x = $rw->add_message($pine);
476 $rw->commit_txn_lazy;
477 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
478 'searching only digit yielded result');
483 is($ro->query("m:$wild*", { mset => 1})->size, 1,
484 "searching chopped($i) digit yielded result $wild ");
486 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);