1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
6 use PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite Search::Xapian));
8 require PublicInbox::SearchIdx;
9 require PublicInbox::Inbox;
10 require PublicInbox::InboxWritable;
12 my ($tmpdir, $for_destroy) = tmpdir();
13 my $git_dir = "$tmpdir/a.git";
14 my $ibx = PublicInbox::Inbox->new({ inboxdir => $git_dir });
15 my ($root_id, $last_id);
17 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)")
18 or BAIL_OUT("`git init --shared' failed, weird FS or seccomp?");
19 eval { PublicInbox::Search->new($ibx)->xdb };
20 ok($@, "exception raised on non-existent DB");
22 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
23 $ibx->with_umask(sub {
28 my $ro = PublicInbox::Search->new($ibx);
30 $rw->commit_txn_lazy if $rw;
31 $rw = PublicInbox::SearchIdx->new($ibx, 1);
32 $rw->{qp_flags} = 0; # quiet a warning
37 my ($got, $exp, $msg) = @_;
38 is(sprintf('0%03o', $got), sprintf('0%03o', $exp), $msg);
42 # git repository perms
43 oct_is($ibx->_git_config_perm(),
44 &PublicInbox::InboxWritable::PERM_GROUP,
45 'undefined permission is group');
47 [ '0644', 0022, '644 => umask(0022)' ],
48 [ '0600', 0077, '600 => umask(0077)' ],
49 [ '0640', 0027, '640 => umask(0027)' ],
50 [ 'group', 0007, 'group => umask(0007)' ],
51 [ 'everybody', 0002, 'everybody => umask(0002)' ],
52 [ 'umask', umask, 'umask => existing umask' ],
55 my ($perm, $exp, $msg) = @$_;
56 my $got = PublicInbox::InboxWritable::_umask_for(
57 PublicInbox::InboxWritable->_git_config_perm($perm));
58 oct_is($got, $exp, $msg);
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 messages 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 {
294 my $eml = 't/utf8.eml';
295 my $mime = PublicInbox::InboxWritable::mime_from_path($eml) or
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->{over_ro}->get_art($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->{over_ro}->get_art($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->{over_ro}->get_art($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;
432 skip('$art not defined', 1) unless defined $art;
433 is($ro->{over_ro}->get_art($art->{num}), undef,
434 'gone from OVER DB');
438 my $all_mask = 07777;
439 my $dir_mask = 02770;
441 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
442 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
443 if ($^O =~ /(?:free|open)bsd/i) {
448 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
449 "$git_dir/public-inbox",
450 glob("$git_dir/public-inbox/xapian*/"),
451 glob("$git_dir/public-inbox/xapian*/*")) {
453 my ($bn) = (split(m!/!, $f))[-1];
454 oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
455 "sharedRepository respected for $bn");
458 $ibx->with_umask(sub {
460 my $digits = '10010260936330';
461 my $ua = 'Pine.LNX.4.10';
462 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
463 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
465 my $pine = Email::MIME->create(
468 'Message-ID' => "<$mid>",
469 From => 'torvalds@transmeta',
470 To => 'list@example.com',
474 my $x = $rw->add_message($pine);
475 $rw->commit_txn_lazy;
476 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
477 'searching only digit yielded result');
482 is($ro->query("m:$wild*", { mset => 1})->size, 1,
483 "searching chopped($i) digit yielded result $wild ");
485 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);