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 use File::Temp qw/tempdir/;
14 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
15 my $git_dir = "$tmpdir/a.git";
16 my ($root_id, $last_id);
18 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)");
19 eval { PublicInbox::Search->new($git_dir)->xdb };
20 ok($@, "exception raised on non-existent DB");
22 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
23 my $ibx = $rw->{-inbox};
24 $ibx->with_umask(sub {
29 my $ro = PublicInbox::Search->new($git_dir);
31 $rw->commit_txn_lazy if $rw;
32 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
37 # git repository perms
38 is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
39 "undefined permission is group");
40 is(PublicInbox::InboxWritable::_umask_for(
41 PublicInbox::InboxWritable->_git_config_perm('0644')),
42 0022, "644 => umask(0022)");
43 is(PublicInbox::InboxWritable::_umask_for(
44 PublicInbox::InboxWritable->_git_config_perm('0600')),
45 0077, "600 => umask(0077)");
46 is(PublicInbox::InboxWritable::_umask_for(
47 PublicInbox::InboxWritable->_git_config_perm('0640')),
48 0027, "640 => umask(0027)");
49 is(PublicInbox::InboxWritable::_umask_for(
50 PublicInbox::InboxWritable->_git_config_perm('group')),
51 0007, 'group => umask(0007)');
52 is(PublicInbox::InboxWritable::_umask_for(
53 PublicInbox::InboxWritable->_git_config_perm('everybody')),
54 0002, 'everybody => umask(0002)');
55 is(PublicInbox::InboxWritable::_umask_for(
56 PublicInbox::InboxWritable->_git_config_perm('umask')),
57 umask, 'umask => existing umask');
60 $ibx->with_umask(sub {
61 my $root = Email::MIME->create(
63 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
64 Subject => 'Hello world',
65 'Message-ID' => '<root@s>',
66 From => 'John Smith <js@example.com>',
67 To => 'list@example.com',
70 my $last = Email::MIME->create(
72 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
73 Subject => 'Re: Hello world',
74 'In-Reply-To' => '<root@s>',
75 'Message-ID' => '<last@s>',
76 From => 'John Smith <js@example.com>',
77 To => 'list@example.com',
78 Cc => 'foo@example.com',
80 body => "goodbye forever :<\n");
84 $root_id = $rw->add_message($root);
85 is($root_id, int($root_id), "root_id is an integer: $root_id");
86 $last_id = $rw->add_message($last);
87 is($last_id, int($last_id), "last_id is an integer: $last_id");
92 sort(map { $_->mid } @$msgs);
98 my $found = $ro->query('m:root@s');
99 is(scalar(@$found), 1, "message found");
100 is($found->[0]->mid, 'root@s', 'mid set correctly') if scalar(@$found);
103 my @exp = sort qw(root@s last@s);
105 $res = $ro->query('s:(Hello world)');
106 @res = filter_mids($res);
107 is_deeply(\@res, \@exp, 'got expected results for s:() match');
109 $res = $ro->query('s:"Hello world"');
110 @res = filter_mids($res);
111 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
113 $res = $ro->query('s:"Hello world"', {limit => 1});
114 is(scalar @$res, 1, "limit works");
115 my $first = $res->[0];
117 $res = $ro->query('s:"Hello world"', {offset => 1});
118 is(scalar @$res, 1, "offset works");
119 my $second = $res->[0];
121 isnt($first, $second, "offset returned different result from limit");
125 $ibx->with_umask(sub {
127 my $rmid = '<ghost-message@s>';
128 my $reply_to_ghost = Email::MIME->create(
130 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
131 Subject => 'Re: ghosts',
132 'Message-ID' => '<ghost-reply@s>',
133 'In-Reply-To' => $rmid,
134 From => 'Time Traveler <tt@example.com>',
135 To => 'list@example.com',
140 my $reply_id = $rw->add_message($reply_to_ghost);
141 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
143 my $was_ghost = Email::MIME->create(
145 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
147 'Message-ID' => $rmid,
148 From => 'Laggy Sender <lag@example.com>',
149 To => 'list@example.com',
151 body => "are real\n");
153 my $ghost_id = $rw->add_message($was_ghost);
154 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
155 my $msgs = $rw->{over}->get_thread('ghost-message@s');
156 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
157 foreach (qw(sid tid)) {
158 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
160 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
161 ok($_->{num} > 0, 'positive art num') foreach @$msgs
164 # search thread on ghost
170 my $res = $ro->query('ghost');
171 my @exp = sort qw(ghost-message@s ghost-reply@s);
172 my @res = filter_mids($res);
173 is_deeply(\@res, \@exp, 'got expected results for Subject match');
176 $res = $ro->query('goodbye');
177 is(scalar(@$res), 1, "goodbye message found");
178 is($res->[0]->mid, 'last@s', 'got goodbye message body') if scalar(@$res);
181 $res = $ro->query('dt:20101002000001..20101002000001');
182 @res = filter_mids($res);
183 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
184 $res = $ro->query('dt:20101002000002..20101002000002');
185 is_deeply($res, [], 'exact Date: match down to the second');
189 $ibx->with_umask(sub {
192 my $long_mid = 'last' . ('x' x 60). '@s';
194 my $long = Email::MIME->create(
196 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
197 Subject => 'long message ID',
198 'References' => '<root@s> <last@s>',
199 'In-Reply-To' => '<last@s>',
200 'Message-ID' => "<$long_mid>",
201 From => '"Long I.D." <long-id@example.com>',
202 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 = Email::MIME->create(
216 Subject => 'I break references',
217 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
218 'Message-ID' => "<$long_reply_mid>",
220 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
221 'In-Reply-To' => "<$long_mid>",
222 From => 'no1 <no1@example.com>',
223 To => 'list@example.com',
225 body => "no References\n");
226 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
230 my $t = $ro->{over_ro}->get_thread('root@s');
231 is(scalar(@$t), 4, "got all 4 mesages in thread");
232 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
233 @res = filter_mids($t);
234 is_deeply(\@res, \@exp, "get_thread works");
237 # quote prioritization
238 $ibx->with_umask(sub {
240 $rw->add_message(Email::MIME->create(
242 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
244 'Message-ID' => '<quote@a>',
245 From => 'Quoter <quoter@example.com>',
246 To => 'list@example.com',
248 body => "> theatre illusions\nfade\n"));
250 $rw->add_message(Email::MIME->create(
252 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
254 'Message-ID' => '<nquote@a>',
255 From => 'Non-Quoter<non-quoter@example.com>',
256 To => 'list@example.com',
258 body => "theatre\nfade\n"));
259 my $res = $rw->query("theatre");
260 is(scalar(@$res), 2, "got both matches");
261 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
262 is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
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(Email::MIME->create(
274 header => [ Subject => $s ],
276 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
277 'Message-ID' => '<circle@a>',
278 'References' => '<circle@a>',
279 'In-Reply-To' => '<circle@a>',
280 From => 'Circle <circle@example.com>',
281 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');
287 is($smsg->references, '', "no references created") if defined($smsg);
288 is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
291 $ibx->with_umask(sub {
293 my $mbox = 't/utf8.mbox';
294 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
298 $str =~ s/\AFrom [^\n]+\n//s;
299 my $mime = Email::MIME->new($str);
300 my $doc_id = $rw->add_message($mime);
301 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
302 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
303 is(defined($msg), 1, 'found testmessage@example.com');
304 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
308 my $msgs = $ro->query('d:19931002..20101002');
309 ok(scalar(@$msgs) > 0, 'got results within range');
310 $msgs = $ro->query('d:20101003..');
311 is(scalar(@$msgs), 0, 'nothing after 20101003');
312 $msgs = $ro->query('d:..19931001');
313 is(scalar(@$msgs), 0, 'nothing before 19931001');
316 # names and addresses
318 my $mset = $ro->query('t:list@example.com', {mset => 1});
319 is($mset->size, 6, 'searched To: successfully');
320 foreach my $m ($mset->items) {
321 my $smsg = $ro->lookup_article($m->get_docid);
322 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
325 $mset = $ro->query('tc:list@example.com', {mset => 1});
326 is($mset->size, 6, 'searched To+Cc: successfully');
327 foreach my $m ($mset->items) {
328 my $smsg = $ro->lookup_article($m->get_docid);
329 my $tocc = join("\n", $smsg->to, $smsg->cc);
330 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
333 foreach my $pfx ('tcf:', 'c:') {
334 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
335 is($mset->items, 1, "searched $pfx successfully for Cc:");
336 foreach my $m ($mset->items) {
337 my $smsg = $ro->lookup_article($m->get_docid);
338 like($smsg->cc, qr/\bfoo\@example\.com\b/,
343 foreach my $pfx ('', 'tcf:', 'f:') {
344 my $res = $ro->query($pfx . 'Laggy');
346 "searched $pfx successfully for From:");
347 foreach my $smsg (@$res) {
348 like($smsg->from_name, qr/Laggy Sender/,
349 "From appears with $pfx");
357 my $res = $ro->query('b:hello');
358 is(scalar(@$res), 0, 'no match on body search only');
359 $res = $ro->query('bs:smith');
361 'no match on body+subject search for From');
363 $res = $ro->query('q:theatre');
364 is(scalar(@$res), 1, 'only one quoted body');
365 like($res->[0]->from_name, qr/\AQuoter/,
366 'got quoted body') if (scalar(@$res));
368 $res = $ro->query('nq:theatre');
369 is(scalar @$res, 1, 'only one non-quoted body');
370 like($res->[0]->from_name, qr/\ANon-Quoter/,
371 'got non-quoted body') if (scalar(@$res));
373 foreach my $pfx (qw(b: bs:)) {
374 $res = $ro->query($pfx . 'theatre');
375 is(scalar @$res, 2, "searched both bodies for $pfx");
376 like($res->[0]->from_name, qr/\ANon-Quoter/,
377 "non-quoter first for $pfx") if scalar(@$res);
381 $ibx->with_umask(sub {
382 my $part1 = Email::MIME->create(
384 content_type => 'text/plain',
385 disposition => 'attachment',
386 charset => 'US-ASCII',
387 encoding => 'quoted-printable',
388 filename => 'attached_fart.txt',
390 body_str => 'inside the attachment',
392 my $part2 = Email::MIME->create(
394 content_type => 'text/plain',
395 disposition => 'attachment',
396 charset => 'US-ASCII',
397 encoding => 'quoted-printable',
398 filename => 'part_deux.txt',
400 body_str => 'inside another',
402 my $amsg = Email::MIME->create(
404 Subject => 'see attachment',
405 'Message-ID' => '<file@attached>',
406 From => 'John Smith <js@example.com>',
407 To => 'list@example.com',
409 parts => [ $part1, $part2 ],
411 ok($rw->add_message($amsg), 'added attachment');
414 my $n = $ro->query('n:attached_fart.txt');
415 is(scalar @$n, 1, 'got result for n:');
416 my $res = $ro->query('part_deux.txt');
417 is(scalar @$res, 1, 'got result without n:');
418 is($n->[0]->mid, $res->[0]->mid,
419 'same result with and without') if scalar(@$res);
420 my $txt = $ro->query('"inside another"');
421 is(scalar @$txt, 1, 'found inside another');
422 is($txt->[0]->mid, $res->[0]->mid,
423 'search inside text attachments works') if scalar(@$txt);
426 if (scalar(@$n) >= 1) {
427 my $mid = $n->[0]->mid;
429 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
430 ok($art, 'article exists in OVER DB');
432 $rw->unindex_blob($amsg);
433 $rw->commit_txn_lazy;
434 is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB') if defined($art);
437 my $all_mask = 07777;
438 my $dir_mask = 02770;
440 # FreeBSD does not allow non-root users to set S_ISGID, so
441 # git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
442 if ($^O =~ /freebsd/i) {
447 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
448 "$git_dir/public-inbox",
449 glob("$git_dir/public-inbox/xapian*/"),
450 glob("$git_dir/public-inbox/xapian*/*")) {
452 my ($bn) = (split(m!/!, $f))[-1];
453 is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
454 "sharedRepository respected for $bn");
457 $ibx->with_umask(sub {
459 my $digits = '10010260936330';
460 my $ua = 'Pine.LNX.4.10';
461 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
462 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
464 my $pine = Email::MIME->create(
467 'Message-ID' => "<$mid>",
468 From => 'torvalds@transmeta',
469 To => 'list@example.com',
473 my $x = $rw->add_message($pine);
474 $rw->commit_txn_lazy;
475 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
476 'searching only digit yielded result');
481 is($ro->query("m:$wild*", { mset => 1})->size, 1,
482 "searching chopped($i) digit yielded result $wild ");
484 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);