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 PublicInbox::SearchIdx; };
7 plan skip_all => "Xapian missing for search" if $@;
8 use File::Temp qw/tempdir/;
10 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
11 my $git_dir = "$tmpdir/a.git";
12 my ($root_id, $last_id);
14 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)");
15 eval { PublicInbox::Search->new($git_dir) };
16 ok($@, "exception raised on non-existent DB");
18 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
19 my $ibx = $rw->{-inbox};
20 $ibx->with_umask(sub {
25 my $ro = PublicInbox::Search->new($git_dir);
27 $rw->commit_txn_lazy if $rw;
28 $rw = PublicInbox::SearchIdx->new($git_dir, 1);
33 # git repository perms
34 is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
35 "undefined permission is group");
36 is(PublicInbox::InboxWritable::_umask_for(
37 PublicInbox::InboxWritable->_git_config_perm('0644')),
38 0022, "644 => umask(0022)");
39 is(PublicInbox::InboxWritable::_umask_for(
40 PublicInbox::InboxWritable->_git_config_perm('0600')),
41 0077, "600 => umask(0077)");
42 is(PublicInbox::InboxWritable::_umask_for(
43 PublicInbox::InboxWritable->_git_config_perm('0640')),
44 0027, "640 => umask(0027)");
45 is(PublicInbox::InboxWritable::_umask_for(
46 PublicInbox::InboxWritable->_git_config_perm('group')),
47 0007, 'group => umask(0007)');
48 is(PublicInbox::InboxWritable::_umask_for(
49 PublicInbox::InboxWritable->_git_config_perm('everybody')),
50 0002, 'everybody => umask(0002)');
51 is(PublicInbox::InboxWritable::_umask_for(
52 PublicInbox::InboxWritable->_git_config_perm('umask')),
53 umask, 'umask => existing umask');
56 $ibx->with_umask(sub {
57 my $root = Email::MIME->create(
59 Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
60 Subject => 'Hello world',
61 'Message-ID' => '<root@s>',
62 From => 'John Smith <js@example.com>',
63 To => 'list@example.com',
66 my $last = Email::MIME->create(
68 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
69 Subject => 'Re: Hello world',
70 'In-Reply-To' => '<root@s>',
71 'Message-ID' => '<last@s>',
72 From => 'John Smith <js@example.com>',
73 To => 'list@example.com',
74 Cc => 'foo@example.com',
76 body => "goodbye forever :<\n");
80 $root_id = $rw->add_message($root);
81 is($root_id, int($root_id), "root_id is an integer: $root_id");
82 $last_id = $rw->add_message($last);
83 is($last_id, int($last_id), "last_id is an integer: $last_id");
88 sort(map { $_->mid } @$msgs);
94 my $found = $ro->query('m:root@s');
95 is(scalar(@$found), 1, "message found");
96 is($found->[0]->mid, 'root@s', 'mid set correctly');
99 my @exp = sort qw(root@s last@s);
101 $res = $ro->query('s:(Hello world)');
102 @res = filter_mids($res);
103 is_deeply(\@res, \@exp, 'got expected results for s:() match');
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"', {limit => 1});
110 is(scalar @$res, 1, "limit works");
111 my $first = $res->[0];
113 $res = $ro->query('s:"Hello world"', {offset => 1});
114 is(scalar @$res, 1, "offset works");
115 my $second = $res->[0];
117 isnt($first, $second, "offset returned different result from limit");
121 $ibx->with_umask(sub {
123 my $rmid = '<ghost-message@s>';
124 my $reply_to_ghost = Email::MIME->create(
126 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
127 Subject => 'Re: ghosts',
128 'Message-ID' => '<ghost-reply@s>',
129 'In-Reply-To' => $rmid,
130 From => 'Time Traveler <tt@example.com>',
131 To => 'list@example.com',
136 my $reply_id = $rw->add_message($reply_to_ghost);
137 is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
139 my $was_ghost = Email::MIME->create(
141 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
143 'Message-ID' => $rmid,
144 From => 'Laggy Sender <lag@example.com>',
145 To => 'list@example.com',
147 body => "are real\n");
149 my $ghost_id = $rw->add_message($was_ghost);
150 is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
151 my $msgs = $rw->{over}->get_thread('ghost-message@s');
152 is(scalar(@$msgs), 2, 'got both messages in ghost thread');
153 foreach (qw(sid tid)) {
154 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
156 isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
157 ok($_->{num} > 0, 'positive art num') foreach @$msgs
160 # search thread on ghost
166 my $res = $ro->query('ghost');
167 my @exp = sort qw(ghost-message@s ghost-reply@s);
168 my @res = filter_mids($res);
169 is_deeply(\@res, \@exp, 'got expected results for Subject match');
172 $res = $ro->query('goodbye');
173 is($res->[0]->mid, 'last@s', 'got goodbye message body');
176 $res = $ro->query('dt:20101002000001..20101002000001');
177 @res = filter_mids($res);
178 is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
179 $res = $ro->query('dt:20101002000002..20101002000002');
180 is_deeply($res, [], 'exact Date: match down to the second');
184 $ibx->with_umask(sub {
187 my $long_mid = 'last' . ('x' x 60). '@s';
189 my $long = Email::MIME->create(
191 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
192 Subject => 'long message ID',
193 'References' => '<root@s> <last@s>',
194 'In-Reply-To' => '<last@s>',
195 'Message-ID' => "<$long_mid>",
196 From => '"Long I.D." <long-id@example.com>',
197 To => 'list@example.com',
200 my $long_id = $rw->add_message($long);
201 is($long_id, int($long_id), "long_id is an integer: $long_id");
208 my $long_reply_mid = 'reply-to-long@1';
209 my $long_reply = Email::MIME->create(
211 Subject => 'I break references',
212 Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
213 'Message-ID' => "<$long_reply_mid>",
215 # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
216 'In-Reply-To' => "<$long_mid>",
217 From => 'no1 <no1@example.com>',
218 To => 'list@example.com',
220 body => "no References\n");
221 ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
225 my $t = $ro->get_thread('root@s');
226 is(scalar(@$t), 4, "got all 4 mesages in thread");
227 my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
228 @res = filter_mids($t);
229 is_deeply(\@res, \@exp, "get_thread works");
232 # quote prioritization
233 $ibx->with_umask(sub {
235 $rw->add_message(Email::MIME->create(
237 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
239 'Message-ID' => '<quote@a>',
240 From => 'Quoter <quoter@example.com>',
241 To => 'list@example.com',
243 body => "> theatre illusions\nfade\n"));
245 $rw->add_message(Email::MIME->create(
247 Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
249 'Message-ID' => '<nquote@a>',
250 From => 'Non-Quoter<non-quoter@example.com>',
251 To => 'list@example.com',
253 body => "theatre\nfade\n"));
254 my $res = $rw->query("theatre");
255 is(scalar(@$res), 2, "got both matches");
256 is($res->[0]->mid, 'nquote@a', "non-quoted scores higher");
257 is($res->[1]->mid, 'quote@a', "quoted result still returned");
259 $res = $rw->query("illusions");
260 is(scalar(@$res), 1, "got a match for quoted text");
261 is($res->[0]->mid, 'quote@a',
262 "quoted result returned if nothing else");
265 # circular references
266 $ibx->with_umask(sub {
267 my $s = 'foo://'. ('Circle' x 15).'/foo';
268 my $doc_id = $rw->add_message(Email::MIME->create(
269 header => [ Subject => $s ],
271 Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
272 'Message-ID' => '<circle@a>',
273 'References' => '<circle@a>',
274 'In-Reply-To' => '<circle@a>',
275 From => 'Circle <circle@example.com>',
276 To => 'list@example.com',
279 ok($doc_id > 0, "doc_id defined with circular reference");
280 my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
281 is($smsg->references, '', "no references created");
282 is($s, $smsg->subject, 'long subject not rewritten');
285 $ibx->with_umask(sub {
287 my $mbox = 't/utf8.mbox';
288 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
292 $str =~ s/\AFrom [^\n]+\n//s;
293 my $mime = Email::MIME->new($str);
294 my $doc_id = $rw->add_message($mime);
295 ok($doc_id > 0, 'message indexed doc_id with UTF-8');
296 my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
297 is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved');
301 my $msgs = $ro->query('d:19931002..20101002');
302 ok(scalar(@$msgs) > 0, 'got results within range');
303 $msgs = $ro->query('d:20101003..');
304 is(scalar(@$msgs), 0, 'nothing after 20101003');
305 $msgs = $ro->query('d:..19931001');
306 is(scalar(@$msgs), 0, 'nothing before 19931001');
309 # names and addresses
311 my $mset = $ro->query('t:list@example.com', {mset => 1});
312 is($mset->size, 6, 'searched To: successfully');
313 foreach my $m ($mset->items) {
314 my $smsg = $ro->lookup_article($m->get_docid);
315 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
318 $mset = $ro->query('tc:list@example.com', {mset => 1});
319 is($mset->size, 6, 'searched To+Cc: successfully');
320 foreach my $m ($mset->items) {
321 my $smsg = $ro->lookup_article($m->get_docid);
322 my $tocc = join("\n", $smsg->to, $smsg->cc);
323 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
326 foreach my $pfx ('tcf:', 'c:') {
327 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
328 is($mset->items, 1, "searched $pfx successfully for Cc:");
329 foreach my $m ($mset->items) {
330 my $smsg = $ro->lookup_article($m->get_docid);
331 like($smsg->cc, qr/\bfoo\@example\.com\b/,
336 foreach my $pfx ('', 'tcf:', 'f:') {
337 my $res = $ro->query($pfx . 'Laggy');
339 "searched $pfx successfully for From:");
340 foreach my $smsg (@$res) {
341 like($smsg->from, qr/Laggy Sender/,
342 "From appears with $pfx");
350 my $res = $ro->query('b:hello');
351 is(scalar(@$res), 0, 'no match on body search only');
352 $res = $ro->query('bs:smith');
354 'no match on body+subject search for From');
356 $res = $ro->query('q:theatre');
357 is(scalar(@$res), 1, 'only one quoted body');
358 like($res->[0]->from, qr/\AQuoter/, 'got quoted body');
360 $res = $ro->query('nq:theatre');
361 is(scalar @$res, 1, 'only one non-quoted body');
362 like($res->[0]->from, qr/\ANon-Quoter/, 'got non-quoted body');
364 foreach my $pfx (qw(b: bs:)) {
365 $res = $ro->query($pfx . 'theatre');
366 is(scalar @$res, 2, "searched both bodies for $pfx");
367 like($res->[0]->from, qr/\ANon-Quoter/,
368 "non-quoter first for $pfx");
372 $ibx->with_umask(sub {
373 my $part1 = Email::MIME->create(
375 content_type => 'text/plain',
376 disposition => 'attachment',
377 charset => 'US-ASCII',
378 encoding => 'quoted-printable',
379 filename => 'attached_fart.txt',
381 body_str => 'inside the attachment',
383 my $part2 = Email::MIME->create(
385 content_type => 'text/plain',
386 disposition => 'attachment',
387 charset => 'US-ASCII',
388 encoding => 'quoted-printable',
389 filename => 'part_deux.txt',
391 body_str => 'inside another',
393 my $amsg = Email::MIME->create(
395 Subject => 'see attachment',
396 'Message-ID' => '<file@attached>',
397 From => 'John Smith <js@example.com>',
398 To => 'list@example.com',
400 parts => [ $part1, $part2 ],
402 ok($rw->add_message($amsg), 'added attachment');
405 my $n = $ro->query('n:attached_fart.txt');
406 is(scalar @$n, 1, 'got result for n:');
407 my $res = $ro->query('part_deux.txt');
408 is(scalar @$res, 1, 'got result without n:');
409 is($n->[0]->mid, $res->[0]->mid,
410 'same result with and without');
411 my $txt = $ro->query('"inside another"');
412 is($txt->[0]->mid, $res->[0]->mid,
413 'search inside text attachments works');
415 my $mid = $n->[0]->mid;
417 my $art = $ro->next_by_mid($mid, \$id, \$prev);
418 ok($art, 'article exists in OVER DB');
419 $rw->unindex_blob($amsg);
420 $rw->commit_txn_lazy;
421 is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB');
424 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
425 "$git_dir/public-inbox",
426 glob("$git_dir/public-inbox/xapian*/"),
427 glob("$git_dir/public-inbox/xapian*/*")) {
429 my ($bn) = (split(m!/!, $f))[-1];
430 is($st[2] & 07777, -f _ ? 0660 : 02770,
431 "sharedRepository respected for $bn");
434 $ibx->with_umask(sub {
436 my $digits = '10010260936330';
437 my $ua = 'Pine.LNX.4.10';
438 my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
439 is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
441 my $pine = Email::MIME->create(
444 'Message-ID' => "<$mid>",
445 From => 'torvalds@transmeta',
446 To => 'list@example.com',
450 my $x = $rw->add_message($pine);
451 $rw->commit_txn_lazy;
452 is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
453 'searching only digit yielded result');
458 is($ro->query("m:$wild*", { mset => 1})->size, 1,
459 "searching chopped($i) digit yielded result $wild ");
461 is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);