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 eval { require Search::Xapian };
7 plan skip_all => "Search::Xapian missing for search" if $@;
8 require PublicInbox::SearchIdx;
9 use File::Temp qw/tempdir/;
11 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
12 my $git_dir = "$tmpdir/a.git";
13 my ($root_id, $last_id);
15 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)");
16 eval { PublicInbox::Search->new($git_dir)->xdb };
17 ok($@, "exception raised on non-existent DB");
19 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
20 my $ibx = $rw->{-inbox};
21 $ibx->with_umask(sub {
26 my $ro = PublicInbox::Search->new($git_dir);
28 $rw->commit_txn_lazy if $rw;
29 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
34 # git repository perms
35 is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
36 "undefined permission is group");
37 is(PublicInbox::InboxWritable::_umask_for(
38 PublicInbox::InboxWritable->_git_config_perm('0644')),
39 0022, "644 => umask(0022)");
40 is(PublicInbox::InboxWritable::_umask_for(
41 PublicInbox::InboxWritable->_git_config_perm('0600')),
42 0077, "600 => umask(0077)");
43 is(PublicInbox::InboxWritable::_umask_for(
44 PublicInbox::InboxWritable->_git_config_perm('0640')),
45 0027, "640 => umask(0027)");
46 is(PublicInbox::InboxWritable::_umask_for(
47 PublicInbox::InboxWritable->_git_config_perm('group')),
48 0007, 'group => umask(0007)');
49 is(PublicInbox::InboxWritable::_umask_for(
50 PublicInbox::InboxWritable->_git_config_perm('everybody')),
51 0002, 'everybody => umask(0002)');
52 is(PublicInbox::InboxWritable::_umask_for(
53 PublicInbox::InboxWritable->_git_config_perm('umask')),
54 umask, 'umask => existing umask');
57 $ibx->with_umask(sub {
58 my $root = Email::MIME->create(
60 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
61 Subject => 'Hello world',
62 'Message-ID' => '<root@s>',
63 From => 'John Smith <js@example.com>',
64 To => 'list@example.com',
67 my $last = Email::MIME->create(
69 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
70 Subject => 'Re: Hello world',
71 'In-Reply-To' => '<root@s>',
72 'Message-ID' => '<last@s>',
73 From => 'John Smith <js@example.com>',
74 To => 'list@example.com',
75 Cc => 'foo@example.com',
77 body => "goodbye forever :<\n");
81 $root_id = $rw->add_message($root);
82 is($root_id, int($root_id), "root_id is an integer: $root_id");
83 $last_id = $rw->add_message($last);
84 is($last_id, int($last_id), "last_id is an integer: $last_id");
89 sort(map { $_->mid } @$msgs);
95 my $found = $ro->query('m:root@s');
96 is(scalar(@$found), 1, "message found");
97 is($found->[0]->mid, 'root@s', 'mid set correctly') if scalar(@$found);
100 my @exp = sort qw(root@s last@s);
102 $res = $ro->query('s:(Hello world)');
103 @res = filter_mids($res);
104 is_deeply(\@res, \@exp, 'got expected results for s:() match');
106 $res = $ro->query('s:"Hello world"');
107 @res = filter_mids($res);
108 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
110 $res = $ro->query('s:"Hello world"', {limit => 1});
111 is(scalar @$res, 1, "limit works");
112 my $first = $res->[0];
114 $res = $ro->query('s:"Hello world"', {offset => 1});
115 is(scalar @$res, 1, "offset works");
116 my $second = $res->[0];
118 isnt($first, $second, "offset returned different result from limit");
122 $ibx->with_umask(sub {
124 my $rmid = '<ghost-message@s>';
125 my $reply_to_ghost = Email::MIME->create(
127 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
128 Subject => 'Re: ghosts',
129 'Message-ID' => '<ghost-reply@s>',
130 'In-Reply-To' => $rmid,
131 From => 'Time Traveler <tt@example.com>',
132 To => 'list@example.com',
137 my $reply_id = $rw->add_message($reply_to_ghost);
138 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
140 my $was_ghost = Email::MIME->create(
142 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
144 'Message-ID' => $rmid,
145 From => 'Laggy Sender <lag@example.com>',
146 To => 'list@example.com',
148 body => "are real\n");
150 my $ghost_id = $rw->add_message($was_ghost);
151 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
152 my $msgs = $rw->{over}->get_thread('ghost-message@s');
153 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
154 foreach (qw(sid tid)) {
155 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
157 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
158 ok($_->{num} > 0, 'positive art num') foreach @$msgs
161 # search thread on ghost
167 my $res = $ro->query('ghost');
168 my @exp = sort qw(ghost-message@s ghost-reply@s);
169 my @res = filter_mids($res);
170 is_deeply(\@res, \@exp, 'got expected results for Subject match');
173 $res = $ro->query('goodbye');
174 is(scalar(@$res), 1, "goodbye message found");
175 is($res->[0]->mid, 'last@s', 'got goodbye message body') if scalar(@$res);
178 $res = $ro->query('dt:20101002000001..20101002000001');
179 @res = filter_mids($res);
180 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
181 $res = $ro->query('dt:20101002000002..20101002000002');
182 is_deeply($res, [], 'exact Date: match down to the second');
186 $ibx->with_umask(sub {
189 my $long_mid = 'last' . ('x' x 60). '@s';
191 my $long = Email::MIME->create(
193 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
194 Subject => 'long message ID',
195 'References' => '<root@s> <last@s>',
196 'In-Reply-To' => '<last@s>',
197 'Message-ID' => "<$long_mid>",
198 From => '"Long I.D." <long-id@example.com>',
199 To => 'list@example.com',
202 my $long_id = $rw->add_message($long);
203 is($long_id, int($long_id), "long_id is an integer: $long_id");
210 my $long_reply_mid = 'reply-to-long@1';
211 my $long_reply = Email::MIME->create(
213 Subject => 'I break references',
214 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
215 'Message-ID' => "<$long_reply_mid>",
217 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
218 'In-Reply-To' => "<$long_mid>",
219 From => 'no1 <no1@example.com>',
220 To => 'list@example.com',
222 body => "no References\n");
223 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
227 my $t = $ro->{over_ro}->get_thread('root@s');
228 is(scalar(@$t), 4, "got all 4 mesages in thread");
229 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
230 @res = filter_mids($t);
231 is_deeply(\@res, \@exp, "get_thread works");
234 # quote prioritization
235 $ibx->with_umask(sub {
237 $rw->add_message(Email::MIME->create(
239 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
241 'Message-ID' => '<quote@a>',
242 From => 'Quoter <quoter@example.com>',
243 To => 'list@example.com',
245 body => "> theatre illusions\nfade\n"));
247 $rw->add_message(Email::MIME->create(
249 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
251 'Message-ID' => '<nquote@a>',
252 From => 'Non-Quoter<non-quoter@example.com>',
253 To => 'list@example.com',
255 body => "theatre\nfade\n"));
256 my $res = $rw->query("theatre");
257 is(scalar(@$res), 2, "got both matches");
258 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
259 is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
261 $res = $rw->query("illusions");
262 is(scalar(@$res), 1, "got a match for quoted text");
263 is($res->[0]->mid, 'quote@a',
264 "quoted result returned if nothing else") if scalar(@$res);
267 # circular references
268 $ibx->with_umask(sub {
269 my $s = 'foo://'. ('Circle' x 15).'/foo';
270 my $doc_id = $rw->add_message(Email::MIME->create(
271 header => [ Subject => $s ],
273 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
274 'Message-ID' => '<circle@a>',
275 'References' => '<circle@a>',
276 'In-Reply-To' => '<circle@a>',
277 From => 'Circle <circle@example.com>',
278 To => 'list@example.com',
281 ok($doc_id > 0, "doc_id defined with circular reference");
282 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
283 is(defined($smsg), 1, 'found m:circl@a');
284 is($smsg->references, '', "no references created") if defined($smsg);
285 is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
288 $ibx->with_umask(sub {
290 my $mbox = 't/utf8.mbox';
291 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
295 $str =~ s/\AFrom [^\n]+\n//s;
296 my $mime = Email::MIME->new($str);
297 my $doc_id = $rw->add_message($mime);
298 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
299 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
300 is(defined($msg), 1, 'found testmessage@example.com');
301 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
305 my $msgs = $ro->query('d:19931002..20101002');
306 ok(scalar(@$msgs) > 0, 'got results within range');
307 $msgs = $ro->query('d:20101003..');
308 is(scalar(@$msgs), 0, 'nothing after 20101003');
309 $msgs = $ro->query('d:..19931001');
310 is(scalar(@$msgs), 0, 'nothing before 19931001');
313 # names and addresses
315 my $mset = $ro->query('t:list@example.com', {mset => 1});
316 is($mset->size, 6, 'searched To: successfully');
317 foreach my $m ($mset->items) {
318 my $smsg = $ro->lookup_article($m->get_docid);
319 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
322 $mset = $ro->query('tc:list@example.com', {mset => 1});
323 is($mset->size, 6, 'searched To+Cc: successfully');
324 foreach my $m ($mset->items) {
325 my $smsg = $ro->lookup_article($m->get_docid);
326 my $tocc = join("\n", $smsg->to, $smsg->cc);
327 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
330 foreach my $pfx ('tcf:', 'c:') {
331 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
332 is($mset->items, 1, "searched $pfx successfully for Cc:");
333 foreach my $m ($mset->items) {
334 my $smsg = $ro->lookup_article($m->get_docid);
335 like($smsg->cc, qr/\bfoo\@example\.com\b/,
340 foreach my $pfx ('', 'tcf:', 'f:') {
341 my $res = $ro->query($pfx . 'Laggy');
343 "searched $pfx successfully for From:");
344 foreach my $smsg (@$res) {
345 like($smsg->from_name, qr/Laggy Sender/,
346 "From appears with $pfx");
354 my $res = $ro->query('b:hello');
355 is(scalar(@$res), 0, 'no match on body search only');
356 $res = $ro->query('bs:smith');
358 'no match on body+subject search for From');
360 $res = $ro->query('q:theatre');
361 is(scalar(@$res), 1, 'only one quoted body');
362 like($res->[0]->from_name, qr/\AQuoter/,
363 'got quoted body') if (scalar(@$res));
365 $res = $ro->query('nq:theatre');
366 is(scalar @$res, 1, 'only one non-quoted body');
367 like($res->[0]->from_name, qr/\ANon-Quoter/,
368 'got non-quoted body') if (scalar(@$res));
370 foreach my $pfx (qw(b: bs:)) {
371 $res = $ro->query($pfx . 'theatre');
372 is(scalar @$res, 2, "searched both bodies for $pfx");
373 like($res->[0]->from_name, qr/\ANon-Quoter/,
374 "non-quoter first for $pfx") if scalar(@$res);
378 $ibx->with_umask(sub {
379 my $part1 = Email::MIME->create(
381 content_type => 'text/plain',
382 disposition => 'attachment',
383 charset => 'US-ASCII',
384 encoding => 'quoted-printable',
385 filename => 'attached_fart.txt',
387 body_str => 'inside the attachment',
389 my $part2 = Email::MIME->create(
391 content_type => 'text/plain',
392 disposition => 'attachment',
393 charset => 'US-ASCII',
394 encoding => 'quoted-printable',
395 filename => 'part_deux.txt',
397 body_str => 'inside another',
399 my $amsg = Email::MIME->create(
401 Subject => 'see attachment',
402 'Message-ID' => '<file@attached>',
403 From => 'John Smith <js@example.com>',
404 To => 'list@example.com',
406 parts => [ $part1, $part2 ],
408 ok($rw->add_message($amsg), 'added attachment');
411 my $n = $ro->query('n:attached_fart.txt');
412 is(scalar @$n, 1, 'got result for n:');
413 my $res = $ro->query('part_deux.txt');
414 is(scalar @$res, 1, 'got result without n:');
415 is($n->[0]->mid, $res->[0]->mid,
416 'same result with and without') if scalar(@$res);
417 my $txt = $ro->query('"inside another"');
418 is(scalar @$txt, 1, 'found inside another');
419 is($txt->[0]->mid, $res->[0]->mid,
420 'search inside text attachments works') if scalar(@$txt);
423 if (scalar(@$n) >= 1) {
424 my $mid = $n->[0]->mid;
426 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
427 ok($art, 'article exists in OVER DB');
429 $rw->unindex_blob($amsg);
430 $rw->commit_txn_lazy;
431 is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB') if defined($art);
434 my $all_mask = 07777;
435 my $dir_mask = 02770;
437 # FreeBSD does not allow non-root users to set S_ISGID, so
438 # git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
439 if ($^O =~ /freebsd/i) {
444 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
445 "$git_dir/public-inbox",
446 glob("$git_dir/public-inbox/xapian*/"),
447 glob("$git_dir/public-inbox/xapian*/*")) {
449 my ($bn) = (split(m!/!, $f))[-1];
450 is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
451 "sharedRepository respected for $bn");
454 $ibx->with_umask(sub {
456 my $digits = '10010260936330';
457 my $ua = 'Pine.LNX.4.10';
458 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
459 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
461 my $pine = Email::MIME->create(
464 'Message-ID' => "<$mid>",
465 From => 'torvalds@transmeta',
466 To => 'list@example.com',
470 my $x = $rw->add_message($pine);
471 $rw->commit_txn_lazy;
472 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
473 'searching only digit yielded result');
478 is($ro->query("m:$wild*", { mset => 1})->size, 1,
479 "searching chopped($i) digit yielded result $wild ");
481 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);