1 # Copyright (C) 2015-2019 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 my ($got, $exp, $msg) = @_;
40 is(sprintf('0%03o', $got), sprintf('0%03o', $exp), $msg);
44 # git repository perms
45 oct_is($ibx->_git_config_perm(),
46 &PublicInbox::InboxWritable::PERM_GROUP,
47 'undefined permission is group');
49 [ '0644', 0022, '644 => umask(0022)' ],
50 [ '0600', 0077, '600 => umask(0077)' ],
51 [ '0640', 0027, '640 => umask(0027)' ],
52 [ 'group', 0007, 'group => umask(0007)' ],
53 [ 'everybody', 0002, 'everybody => umask(0002)' ],
54 [ 'umask', umask, 'umask => existing umask' ],
57 my ($perm, $exp, $msg) = @$_;
58 my $got = PublicInbox::InboxWritable::_umask_for(
59 PublicInbox::InboxWritable->_git_config_perm($perm));
60 oct_is($got, $exp, $msg);
64 $ibx->with_umask(sub {
65 my $root = Email::MIME->create(
67 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
68 Subject => 'Hello world',
69 'Message-ID' => '<root@s>',
70 From => 'John Smith <js@example.com>',
71 To => 'list@example.com',
74 my $last = Email::MIME->create(
76 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
77 Subject => 'Re: Hello world',
78 'In-Reply-To' => '<root@s>',
79 'Message-ID' => '<last@s>',
80 From => 'John Smith <js@example.com>',
81 To => 'list@example.com',
82 Cc => 'foo@example.com',
84 body => "goodbye forever :<\n");
88 $root_id = $rw->add_message($root);
89 is($root_id, int($root_id), "root_id is an integer: $root_id");
90 $last_id = $rw->add_message($last);
91 is($last_id, int($last_id), "last_id is an integer: $last_id");
96 sort(map { $_->mid } @$msgs);
102 my $found = $ro->query('m:root@s');
103 is(scalar(@$found), 1, "message found");
104 is($found->[0]->mid, 'root@s', 'mid set correctly') if scalar(@$found);
107 my @exp = sort qw(root@s last@s);
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"');
114 @res = filter_mids($res);
115 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
117 $res = $ro->query('s:"Hello world"', {limit => 1});
118 is(scalar @$res, 1, "limit works");
119 my $first = $res->[0];
121 $res = $ro->query('s:"Hello world"', {offset => 1});
122 is(scalar @$res, 1, "offset works");
123 my $second = $res->[0];
125 isnt($first, $second, "offset returned different result from limit");
129 $ibx->with_umask(sub {
131 my $rmid = '<ghost-message@s>';
132 my $reply_to_ghost = Email::MIME->create(
134 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
135 Subject => 'Re: ghosts',
136 'Message-ID' => '<ghost-reply@s>',
137 'In-Reply-To' => $rmid,
138 From => 'Time Traveler <tt@example.com>',
139 To => 'list@example.com',
144 my $reply_id = $rw->add_message($reply_to_ghost);
145 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
147 my $was_ghost = Email::MIME->create(
149 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
151 'Message-ID' => $rmid,
152 From => 'Laggy Sender <lag@example.com>',
153 To => 'list@example.com',
155 body => "are real\n");
157 my $ghost_id = $rw->add_message($was_ghost);
158 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
159 my $msgs = $rw->{over}->get_thread('ghost-message@s');
160 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
161 foreach (qw(sid tid)) {
162 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
164 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
165 ok($_->{num} > 0, 'positive art num') foreach @$msgs
168 # search thread on ghost
174 my $res = $ro->query('ghost');
175 my @exp = sort qw(ghost-message@s ghost-reply@s);
176 my @res = filter_mids($res);
177 is_deeply(\@res, \@exp, 'got expected results for Subject match');
180 $res = $ro->query('goodbye');
181 is(scalar(@$res), 1, "goodbye message found");
182 is($res->[0]->mid, 'last@s', 'got goodbye message body') if scalar(@$res);
185 $res = $ro->query('dt:20101002000001..20101002000001');
186 @res = filter_mids($res);
187 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
188 $res = $ro->query('dt:20101002000002..20101002000002');
189 is_deeply($res, [], 'exact Date: match down to the second');
193 $ibx->with_umask(sub {
196 my $long_mid = 'last' . ('x' x 60). '@s';
198 my $long = Email::MIME->create(
200 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
201 Subject => 'long message ID',
202 'References' => '<root@s> <last@s>',
203 'In-Reply-To' => '<last@s>',
204 'Message-ID' => "<$long_mid>",
205 From => '"Long I.D." <long-id@example.com>',
206 To => 'list@example.com',
209 my $long_id = $rw->add_message($long);
210 is($long_id, int($long_id), "long_id is an integer: $long_id");
217 my $long_reply_mid = 'reply-to-long@1';
218 my $long_reply = Email::MIME->create(
220 Subject => 'I break references',
221 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
222 'Message-ID' => "<$long_reply_mid>",
224 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
225 'In-Reply-To' => "<$long_mid>",
226 From => 'no1 <no1@example.com>',
227 To => 'list@example.com',
229 body => "no References\n");
230 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
234 my $t = $ro->{over_ro}->get_thread('root@s');
235 is(scalar(@$t), 4, "got all 4 mesages in thread");
236 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
237 @res = filter_mids($t);
238 is_deeply(\@res, \@exp, "get_thread works");
241 # quote prioritization
242 $ibx->with_umask(sub {
244 $rw->add_message(Email::MIME->create(
246 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
248 'Message-ID' => '<quote@a>',
249 From => 'Quoter <quoter@example.com>',
250 To => 'list@example.com',
252 body => "> theatre illusions\nfade\n"));
254 $rw->add_message(Email::MIME->create(
256 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
258 'Message-ID' => '<nquote@a>',
259 From => 'Non-Quoter<non-quoter@example.com>',
260 To => 'list@example.com',
262 body => "theatre\nfade\n"));
263 my $res = $rw->query("theatre");
264 is(scalar(@$res), 2, "got both matches");
265 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
266 is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
268 $res = $rw->query("illusions");
269 is(scalar(@$res), 1, "got a match for quoted text");
270 is($res->[0]->mid, 'quote@a',
271 "quoted result returned if nothing else") if scalar(@$res);
274 # circular references
275 $ibx->with_umask(sub {
276 my $s = 'foo://'. ('Circle' x 15).'/foo';
277 my $doc_id = $rw->add_message(Email::MIME->create(
278 header => [ Subject => $s ],
280 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
281 'Message-ID' => '<circle@a>',
282 'References' => '<circle@a>',
283 'In-Reply-To' => '<circle@a>',
284 From => 'Circle <circle@example.com>',
285 To => 'list@example.com',
288 ok($doc_id > 0, "doc_id defined with circular reference");
289 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
290 is(defined($smsg), 1, 'found m:circl@a');
291 is($smsg->references, '', "no references created") if defined($smsg);
292 is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
295 $ibx->with_umask(sub {
297 my $mbox = 't/utf8.mbox';
298 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
302 $str =~ s/\AFrom [^\n]+\n//s;
303 my $mime = Email::MIME->new($str);
304 my $doc_id = $rw->add_message($mime);
305 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
306 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
307 is(defined($msg), 1, 'found testmessage@example.com');
308 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
312 my $msgs = $ro->query('d:19931002..20101002');
313 ok(scalar(@$msgs) > 0, 'got results within range');
314 $msgs = $ro->query('d:20101003..');
315 is(scalar(@$msgs), 0, 'nothing after 20101003');
316 $msgs = $ro->query('d:..19931001');
317 is(scalar(@$msgs), 0, 'nothing before 19931001');
320 # names and addresses
322 my $mset = $ro->query('t:list@example.com', {mset => 1});
323 is($mset->size, 6, 'searched To: successfully');
324 foreach my $m ($mset->items) {
325 my $smsg = $ro->lookup_article($m->get_docid);
326 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
329 $mset = $ro->query('tc:list@example.com', {mset => 1});
330 is($mset->size, 6, 'searched To+Cc: successfully');
331 foreach my $m ($mset->items) {
332 my $smsg = $ro->lookup_article($m->get_docid);
333 my $tocc = join("\n", $smsg->to, $smsg->cc);
334 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
337 foreach my $pfx ('tcf:', 'c:') {
338 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
339 is($mset->items, 1, "searched $pfx successfully for Cc:");
340 foreach my $m ($mset->items) {
341 my $smsg = $ro->lookup_article($m->get_docid);
342 like($smsg->cc, qr/\bfoo\@example\.com\b/,
347 foreach my $pfx ('', 'tcf:', 'f:') {
348 my $res = $ro->query($pfx . 'Laggy');
350 "searched $pfx successfully for From:");
351 foreach my $smsg (@$res) {
352 like($smsg->from_name, qr/Laggy Sender/,
353 "From appears with $pfx");
361 my $res = $ro->query('b:hello');
362 is(scalar(@$res), 0, 'no match on body search only');
363 $res = $ro->query('bs:smith');
365 'no match on body+subject search for From');
367 $res = $ro->query('q:theatre');
368 is(scalar(@$res), 1, 'only one quoted body');
369 like($res->[0]->from_name, qr/\AQuoter/,
370 'got quoted body') if (scalar(@$res));
372 $res = $ro->query('nq:theatre');
373 is(scalar @$res, 1, 'only one non-quoted body');
374 like($res->[0]->from_name, qr/\ANon-Quoter/,
375 'got non-quoted body') if (scalar(@$res));
377 foreach my $pfx (qw(b: bs:)) {
378 $res = $ro->query($pfx . 'theatre');
379 is(scalar @$res, 2, "searched both bodies for $pfx");
380 like($res->[0]->from_name, qr/\ANon-Quoter/,
381 "non-quoter first for $pfx") if scalar(@$res);
385 $ibx->with_umask(sub {
386 my $part1 = Email::MIME->create(
388 content_type => 'text/plain',
389 disposition => 'attachment',
390 charset => 'US-ASCII',
391 encoding => 'quoted-printable',
392 filename => 'attached_fart.txt',
394 body_str => 'inside the attachment',
396 my $part2 = Email::MIME->create(
398 content_type => 'text/plain',
399 disposition => 'attachment',
400 charset => 'US-ASCII',
401 encoding => 'quoted-printable',
402 filename => 'part_deux.txt',
404 body_str => 'inside another',
406 my $amsg = Email::MIME->create(
408 Subject => 'see attachment',
409 'Message-ID' => '<file@attached>',
410 From => 'John Smith <js@example.com>',
411 To => 'list@example.com',
413 parts => [ $part1, $part2 ],
415 ok($rw->add_message($amsg), 'added attachment');
418 my $n = $ro->query('n:attached_fart.txt');
419 is(scalar @$n, 1, 'got result for n:');
420 my $res = $ro->query('part_deux.txt');
421 is(scalar @$res, 1, 'got result without n:');
422 is($n->[0]->mid, $res->[0]->mid,
423 'same result with and without') if scalar(@$res);
424 my $txt = $ro->query('"inside another"');
425 is(scalar @$txt, 1, 'found inside another');
426 is($txt->[0]->mid, $res->[0]->mid,
427 'search inside text attachments works') if scalar(@$txt);
430 if (scalar(@$n) >= 1) {
431 my $mid = $n->[0]->mid;
433 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
434 ok($art, 'article exists in OVER DB');
436 $rw->unindex_blob($amsg);
437 $rw->commit_txn_lazy;
438 is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB') if defined($art);
441 my $all_mask = 07777;
442 my $dir_mask = 02770;
444 # FreeBSD does not allow non-root users to set S_ISGID, so
445 # git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
446 if ($^O =~ /freebsd/i) {
451 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
452 "$git_dir/public-inbox",
453 glob("$git_dir/public-inbox/xapian*/"),
454 glob("$git_dir/public-inbox/xapian*/*")) {
456 my ($bn) = (split(m!/!, $f))[-1];
457 oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
458 "sharedRepository respected for $bn");
461 $ibx->with_umask(sub {
463 my $digits = '10010260936330';
464 my $ua = 'Pine.LNX.4.10';
465 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
466 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
468 my $pine = Email::MIME->create(
471 'Message-ID' => "<$mid>",
472 From => 'torvalds@transmeta',
473 To => 'list@example.com',
477 my $x = $rw->add_message($pine);
478 $rw->commit_txn_lazy;
479 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
480 'searching only digit yielded result');
485 is($ro->query("m:$wild*", { mset => 1})->size, 1,
486 "searching chopped($i) digit yielded result $wild ");
488 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);