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 require './t/common.perl';
15 my ($tmpdir, $for_destroy) = tmpdir();
16 my $git_dir = "$tmpdir/a.git";
17 my $ibx = PublicInbox::Inbox->new({ inboxdir => $git_dir });
18 my ($root_id, $last_id);
20 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)")
21 or BAIL_OUT("`git init --shared' failed, weird FS or seccomp?");
22 eval { PublicInbox::Search->new($ibx)->xdb };
23 ok($@, "exception raised on non-existent DB");
25 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
26 $ibx->with_umask(sub {
31 my $ro = PublicInbox::Search->new($ibx);
33 $rw->commit_txn_lazy if $rw;
34 $rw = PublicInbox::SearchIdx->new($ibx, 1);
35 $rw->{qp_flags} = 0; # quiet a warning
40 my ($got, $exp, $msg) = @_;
41 is(sprintf('0%03o', $got), sprintf('0%03o', $exp), $msg);
45 # git repository perms
46 oct_is($ibx->_git_config_perm(),
47 &PublicInbox::InboxWritable::PERM_GROUP,
48 'undefined permission is group');
50 [ '0644', 0022, '644 => umask(0022)' ],
51 [ '0600', 0077, '600 => umask(0077)' ],
52 [ '0640', 0027, '640 => umask(0027)' ],
53 [ 'group', 0007, 'group => umask(0007)' ],
54 [ 'everybody', 0002, 'everybody => umask(0002)' ],
55 [ 'umask', umask, 'umask => existing umask' ],
58 my ($perm, $exp, $msg) = @$_;
59 my $got = PublicInbox::InboxWritable::_umask_for(
60 PublicInbox::InboxWritable->_git_config_perm($perm));
61 oct_is($got, $exp, $msg);
65 $ibx->with_umask(sub {
66 my $root = Email::MIME->create(
68 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
69 Subject => 'Hello world',
70 'Message-ID' => '<root@s>',
71 From => 'John Smith <js@example.com>',
72 To => 'list@example.com',
75 my $last = Email::MIME->create(
77 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
78 Subject => 'Re: Hello world',
79 'In-Reply-To' => '<root@s>',
80 'Message-ID' => '<last@s>',
81 From => 'John Smith <js@example.com>',
82 To => 'list@example.com',
83 Cc => 'foo@example.com',
85 body => "goodbye forever :<\n");
89 $root_id = $rw->add_message($root);
90 is($root_id, int($root_id), "root_id is an integer: $root_id");
91 $last_id = $rw->add_message($last);
92 is($last_id, int($last_id), "last_id is an integer: $last_id");
97 sort(map { $_->mid } @$msgs);
103 my $found = $ro->query('m:root@s');
104 is(scalar(@$found), 1, "message found");
105 is($found->[0]->mid, 'root@s', 'mid set correctly') if scalar(@$found);
108 my @exp = sort qw(root@s last@s);
110 $res = $ro->query('s:(Hello world)');
111 @res = filter_mids($res);
112 is_deeply(\@res, \@exp, 'got expected results for s:() match');
114 $res = $ro->query('s:"Hello world"');
115 @res = filter_mids($res);
116 is_deeply(\@res, \@exp, 'got expected results for s:"" match');
118 $res = $ro->query('s:"Hello world"', {limit => 1});
119 is(scalar @$res, 1, "limit works");
120 my $first = $res->[0];
122 $res = $ro->query('s:"Hello world"', {offset => 1});
123 is(scalar @$res, 1, "offset works");
124 my $second = $res->[0];
126 isnt($first, $second, "offset returned different result from limit");
130 $ibx->with_umask(sub {
132 my $rmid = '<ghost-message@s>';
133 my $reply_to_ghost = Email::MIME->create(
135 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
136 Subject => 'Re: ghosts',
137 'Message-ID' => '<ghost-reply@s>',
138 'In-Reply-To' => $rmid,
139 From => 'Time Traveler <tt@example.com>',
140 To => 'list@example.com',
145 my $reply_id = $rw->add_message($reply_to_ghost);
146 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
148 my $was_ghost = Email::MIME->create(
150 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
152 'Message-ID' => $rmid,
153 From => 'Laggy Sender <lag@example.com>',
154 To => 'list@example.com',
156 body => "are real\n");
158 my $ghost_id = $rw->add_message($was_ghost);
159 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
160 my $msgs = $rw->{over}->get_thread('ghost-message@s');
161 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
162 foreach (qw(sid tid)) {
163 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
165 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
166 ok($_->{num} > 0, 'positive art num') foreach @$msgs
169 # search thread on ghost
175 my $res = $ro->query('ghost');
176 my @exp = sort qw(ghost-message@s ghost-reply@s);
177 my @res = filter_mids($res);
178 is_deeply(\@res, \@exp, 'got expected results for Subject match');
181 $res = $ro->query('goodbye');
182 is(scalar(@$res), 1, "goodbye message found");
183 is($res->[0]->mid, 'last@s', 'got goodbye message body') if scalar(@$res);
186 $res = $ro->query('dt:20101002000001..20101002000001');
187 @res = filter_mids($res);
188 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
189 $res = $ro->query('dt:20101002000002..20101002000002');
190 is_deeply($res, [], 'exact Date: match down to the second');
194 $ibx->with_umask(sub {
197 my $long_mid = 'last' . ('x' x 60). '@s';
199 my $long = Email::MIME->create(
201 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
202 Subject => 'long message ID',
203 'References' => '<root@s> <last@s>',
204 'In-Reply-To' => '<last@s>',
205 'Message-ID' => "<$long_mid>",
206 From => '"Long I.D." <long-id@example.com>',
207 To => 'list@example.com',
210 my $long_id = $rw->add_message($long);
211 is($long_id, int($long_id), "long_id is an integer: $long_id");
218 my $long_reply_mid = 'reply-to-long@1';
219 my $long_reply = Email::MIME->create(
221 Subject => 'I break references',
222 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
223 'Message-ID' => "<$long_reply_mid>",
225 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
226 'In-Reply-To' => "<$long_mid>",
227 From => 'no1 <no1@example.com>',
228 To => 'list@example.com',
230 body => "no References\n");
231 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
235 my $t = $ro->{over_ro}->get_thread('root@s');
236 is(scalar(@$t), 4, "got all 4 mesages in thread");
237 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
238 @res = filter_mids($t);
239 is_deeply(\@res, \@exp, "get_thread works");
242 # quote prioritization
243 $ibx->with_umask(sub {
245 $rw->add_message(Email::MIME->create(
247 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
249 'Message-ID' => '<quote@a>',
250 From => 'Quoter <quoter@example.com>',
251 To => 'list@example.com',
253 body => "> theatre illusions\nfade\n"));
255 $rw->add_message(Email::MIME->create(
257 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
259 'Message-ID' => '<nquote@a>',
260 From => 'Non-Quoter<non-quoter@example.com>',
261 To => 'list@example.com',
263 body => "theatre\nfade\n"));
264 my $res = $rw->query("theatre");
265 is(scalar(@$res), 2, "got both matches");
266 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
267 is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
269 $res = $rw->query("illusions");
270 is(scalar(@$res), 1, "got a match for quoted text");
271 is($res->[0]->mid, 'quote@a',
272 "quoted result returned if nothing else") if scalar(@$res);
275 # circular references
276 $ibx->with_umask(sub {
277 my $s = 'foo://'. ('Circle' x 15).'/foo';
278 my $doc_id = $rw->add_message(Email::MIME->create(
279 header => [ Subject => $s ],
281 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
282 'Message-ID' => '<circle@a>',
283 'References' => '<circle@a>',
284 'In-Reply-To' => '<circle@a>',
285 From => 'Circle <circle@example.com>',
286 To => 'list@example.com',
289 ok($doc_id > 0, "doc_id defined with circular reference");
290 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
291 is(defined($smsg), 1, 'found m:circl@a');
292 is($smsg->references, '', "no references created") if defined($smsg);
293 is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
296 $ibx->with_umask(sub {
298 my $mbox = 't/utf8.mbox';
299 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
303 $str =~ s/\AFrom [^\n]+\n//s;
304 my $mime = Email::MIME->new($str);
305 my $doc_id = $rw->add_message($mime);
306 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
307 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
308 is(defined($msg), 1, 'found testmessage@example.com');
309 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
313 my $msgs = $ro->query('d:19931002..20101002');
314 ok(scalar(@$msgs) > 0, 'got results within range');
315 $msgs = $ro->query('d:20101003..');
316 is(scalar(@$msgs), 0, 'nothing after 20101003');
317 $msgs = $ro->query('d:..19931001');
318 is(scalar(@$msgs), 0, 'nothing before 19931001');
321 # names and addresses
323 my $mset = $ro->query('t:list@example.com', {mset => 1});
324 is($mset->size, 6, 'searched To: successfully');
325 foreach my $m ($mset->items) {
326 my $smsg = $ro->lookup_article($m->get_docid);
327 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
330 $mset = $ro->query('tc:list@example.com', {mset => 1});
331 is($mset->size, 6, 'searched To+Cc: successfully');
332 foreach my $m ($mset->items) {
333 my $smsg = $ro->lookup_article($m->get_docid);
334 my $tocc = join("\n", $smsg->to, $smsg->cc);
335 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
338 foreach my $pfx ('tcf:', 'c:') {
339 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
340 is($mset->items, 1, "searched $pfx successfully for Cc:");
341 foreach my $m ($mset->items) {
342 my $smsg = $ro->lookup_article($m->get_docid);
343 like($smsg->cc, qr/\bfoo\@example\.com\b/,
348 foreach my $pfx ('', 'tcf:', 'f:') {
349 my $res = $ro->query($pfx . 'Laggy');
351 "searched $pfx successfully for From:");
352 foreach my $smsg (@$res) {
353 like($smsg->from_name, qr/Laggy Sender/,
354 "From appears with $pfx");
362 my $res = $ro->query('b:hello');
363 is(scalar(@$res), 0, 'no match on body search only');
364 $res = $ro->query('bs:smith');
366 'no match on body+subject search for From');
368 $res = $ro->query('q:theatre');
369 is(scalar(@$res), 1, 'only one quoted body');
370 like($res->[0]->from_name, qr/\AQuoter/,
371 'got quoted body') if (scalar(@$res));
373 $res = $ro->query('nq:theatre');
374 is(scalar @$res, 1, 'only one non-quoted body');
375 like($res->[0]->from_name, qr/\ANon-Quoter/,
376 'got non-quoted body') if (scalar(@$res));
378 foreach my $pfx (qw(b: bs:)) {
379 $res = $ro->query($pfx . 'theatre');
380 is(scalar @$res, 2, "searched both bodies for $pfx");
381 like($res->[0]->from_name, qr/\ANon-Quoter/,
382 "non-quoter first for $pfx") if scalar(@$res);
386 $ibx->with_umask(sub {
387 my $part1 = Email::MIME->create(
389 content_type => 'text/plain',
390 disposition => 'attachment',
391 charset => 'US-ASCII',
392 encoding => 'quoted-printable',
393 filename => 'attached_fart.txt',
395 body_str => 'inside the attachment',
397 my $part2 = Email::MIME->create(
399 content_type => 'text/plain',
400 disposition => 'attachment',
401 charset => 'US-ASCII',
402 encoding => 'quoted-printable',
403 filename => 'part_deux.txt',
405 body_str => 'inside another',
407 my $amsg = Email::MIME->create(
409 Subject => 'see attachment',
410 'Message-ID' => '<file@attached>',
411 From => 'John Smith <js@example.com>',
412 To => 'list@example.com',
414 parts => [ $part1, $part2 ],
416 ok($rw->add_message($amsg), 'added attachment');
419 my $n = $ro->query('n:attached_fart.txt');
420 is(scalar @$n, 1, 'got result for n:');
421 my $res = $ro->query('part_deux.txt');
422 is(scalar @$res, 1, 'got result without n:');
423 is($n->[0]->mid, $res->[0]->mid,
424 'same result with and without') if scalar(@$res);
425 my $txt = $ro->query('"inside another"');
426 is(scalar @$txt, 1, 'found inside another');
427 is($txt->[0]->mid, $res->[0]->mid,
428 'search inside text attachments works') if scalar(@$txt);
431 if (scalar(@$n) >= 1) {
432 my $mid = $n->[0]->mid;
434 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
435 ok($art, 'article exists in OVER DB');
437 $rw->unindex_blob($amsg);
438 $rw->commit_txn_lazy;
439 is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB') if defined($art);
442 my $all_mask = 07777;
443 my $dir_mask = 02770;
445 # FreeBSD does not allow non-root users to set S_ISGID, so
446 # git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
447 if ($^O =~ /freebsd/i) {
452 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
453 "$git_dir/public-inbox",
454 glob("$git_dir/public-inbox/xapian*/"),
455 glob("$git_dir/public-inbox/xapian*/*")) {
457 my ($bn) = (split(m!/!, $f))[-1];
458 oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
459 "sharedRepository respected for $bn");
462 $ibx->with_umask(sub {
464 my $digits = '10010260936330';
465 my $ua = 'Pine.LNX.4.10';
466 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
467 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
469 my $pine = Email::MIME->create(
472 'Message-ID' => "<$mid>",
473 From => 'torvalds@transmeta',
474 To => 'list@example.com',
478 my $x = $rw->add_message($pine);
479 $rw->commit_txn_lazy;
480 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
481 'searching only digit yielded result');
486 is($ro->query("m:$wild*", { mset => 1})->size, 1,
487 "searching chopped($i) digit yielded result $wild ");
489 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);