]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
lazy load Xapian and make it optional for v2
[public-inbox.git] / t / search.t
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>
3 use strict;
4 use warnings;
5 use Test::More;
6 eval { require Search::Xapian };
7 plan skip_all => "Search::Xapian missing for search" if $@;
8 require PublicInbox::SearchIdx;
9 use File::Temp qw/tempdir/;
10 use Email::MIME;
11 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
12 my $git_dir = "$tmpdir/a.git";
13 my ($root_id, $last_id);
14
15 is(0, system(qw(git init --shared -q --bare), $git_dir), "git init (main)");
16 eval { PublicInbox::Search->new($git_dir)->xdb };
17 ok($@, "exception raised on non-existent DB");
18
19 my $rw = PublicInbox::SearchIdx->new($git_dir, 1);
20 my $ibx = $rw->{-inbox};
21 $ibx->with_umask(sub {
22         $rw->_xdb_acquire;
23         $rw->_xdb_release;
24 });
25 $rw = undef;
26 my $ro = PublicInbox::Search->new($git_dir);
27 my $rw_commit = sub {
28         $rw->commit_txn_lazy if $rw;
29         $rw = PublicInbox::SearchIdx->new($git_dir, 1);
30         $rw->begin_txn_lazy;
31 };
32
33 {
34         # git repository perms
35         is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
36            "undefined permission is group");
37         is(PublicInbox::InboxWritable::_umask_for(
38              PublicInbox::InboxWritable->_git_config_perm('0644')),
39            0022, "644 => umask(0022)");
40         is(PublicInbox::InboxWritable::_umask_for(
41              PublicInbox::InboxWritable->_git_config_perm('0600')),
42            0077, "600 => umask(0077)");
43         is(PublicInbox::InboxWritable::_umask_for(
44              PublicInbox::InboxWritable->_git_config_perm('0640')),
45            0027, "640 => umask(0027)");
46         is(PublicInbox::InboxWritable::_umask_for(
47              PublicInbox::InboxWritable->_git_config_perm('group')),
48            0007, 'group => umask(0007)');
49         is(PublicInbox::InboxWritable::_umask_for(
50              PublicInbox::InboxWritable->_git_config_perm('everybody')),
51            0002, 'everybody => umask(0002)');
52         is(PublicInbox::InboxWritable::_umask_for(
53              PublicInbox::InboxWritable->_git_config_perm('umask')),
54            umask, 'umask => existing umask');
55 }
56
57 $ibx->with_umask(sub {
58         my $root = Email::MIME->create(
59                 header_str => [
60                         Date => 'Fri, 02 Oct 1993 00:00:00 +0000',
61                         Subject => 'Hello world',
62                         'Message-ID' => '<root@s>',
63                         From => 'John Smith <js@example.com>',
64                         To => 'list@example.com',
65                 ],
66                 body => "\\m/\n");
67         my $last = Email::MIME->create(
68                 header_str => [
69                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
70                         Subject => 'Re: Hello world',
71                         'In-Reply-To' => '<root@s>',
72                         'Message-ID' => '<last@s>',
73                         From => 'John Smith <js@example.com>',
74                         To => 'list@example.com',
75                         Cc => 'foo@example.com',
76                 ],
77                 body => "goodbye forever :<\n");
78
79         my $rv;
80         $rw_commit->();
81         $root_id = $rw->add_message($root);
82         is($root_id, int($root_id), "root_id is an integer: $root_id");
83         $last_id = $rw->add_message($last);
84         is($last_id, int($last_id), "last_id is an integer: $last_id");
85 });
86
87 sub filter_mids {
88         my ($msgs) = @_;
89         sort(map { $_->mid } @$msgs);
90 }
91
92 {
93         $rw_commit->();
94         $ro->reopen;
95         my $found = $ro->query('m:root@s');
96         is(scalar(@$found), 1, "message found");
97         is($found->[0]->mid, 'root@s', 'mid set correctly') if scalar(@$found);
98
99         my ($res, @res);
100         my @exp = sort qw(root@s last@s);
101
102         $res = $ro->query('s:(Hello world)');
103         @res = filter_mids($res);
104         is_deeply(\@res, \@exp, 'got expected results for s:() match');
105
106         $res = $ro->query('s:"Hello world"');
107         @res = filter_mids($res);
108         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
109
110         $res = $ro->query('s:"Hello world"', {limit => 1});
111         is(scalar @$res, 1, "limit works");
112         my $first = $res->[0];
113
114         $res = $ro->query('s:"Hello world"', {offset => 1});
115         is(scalar @$res, 1, "offset works");
116         my $second = $res->[0];
117
118         isnt($first, $second, "offset returned different result from limit");
119 }
120
121 # ghost vivication
122 $ibx->with_umask(sub {
123         $rw_commit->();
124         my $rmid = '<ghost-message@s>';
125         my $reply_to_ghost = Email::MIME->create(
126                 header_str => [
127                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
128                         Subject => 'Re: ghosts',
129                         'Message-ID' => '<ghost-reply@s>',
130                         'In-Reply-To' => $rmid,
131                         From => 'Time Traveler <tt@example.com>',
132                         To => 'list@example.com',
133                 ],
134                 body => "-_-\n");
135
136         my $rv;
137         my $reply_id = $rw->add_message($reply_to_ghost);
138         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
139
140         my $was_ghost = Email::MIME->create(
141                 header_str => [
142                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
143                         Subject => 'ghosts',
144                         'Message-ID' => $rmid,
145                         From => 'Laggy Sender <lag@example.com>',
146                         To => 'list@example.com',
147                 ],
148                 body => "are real\n");
149
150         my $ghost_id = $rw->add_message($was_ghost);
151         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
152         my $msgs = $rw->{over}->get_thread('ghost-message@s');
153         is(scalar(@$msgs), 2, 'got both messages in ghost thread');
154         foreach (qw(sid tid)) {
155                 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
156         }
157         isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
158         ok($_->{num} > 0, 'positive art num') foreach @$msgs
159 });
160
161 # search thread on ghost
162 {
163         $rw_commit->();
164         $ro->reopen;
165
166         # subject
167         my $res = $ro->query('ghost');
168         my @exp = sort qw(ghost-message@s ghost-reply@s);
169         my @res = filter_mids($res);
170         is_deeply(\@res, \@exp, 'got expected results for Subject match');
171
172         # body
173         $res = $ro->query('goodbye');
174         is(scalar(@$res), 1, "goodbye message found");
175         is($res->[0]->mid, 'last@s', 'got goodbye message body') if scalar(@$res);
176
177         # datestamp
178         $res = $ro->query('dt:20101002000001..20101002000001');
179         @res = filter_mids($res);
180         is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
181         $res = $ro->query('dt:20101002000002..20101002000002');
182         is_deeply($res, [], 'exact Date: match down to the second');
183 }
184
185 # long message-id
186 $ibx->with_umask(sub {
187         $rw_commit->();
188         $ro->reopen;
189         my $long_mid = 'last' . ('x' x 60). '@s';
190
191         my $long = Email::MIME->create(
192                 header_str => [
193                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
194                         Subject => 'long message ID',
195                         'References' => '<root@s> <last@s>',
196                         'In-Reply-To' => '<last@s>',
197                         'Message-ID' => "<$long_mid>",
198                         From => '"Long I.D." <long-id@example.com>',
199                         To => 'list@example.com',
200                 ],
201                 body => "wut\n");
202         my $long_id = $rw->add_message($long);
203         is($long_id, int($long_id), "long_id is an integer: $long_id");
204
205         $rw_commit->();
206         $ro->reopen;
207         my $res;
208         my @res;
209
210         my $long_reply_mid = 'reply-to-long@1';
211         my $long_reply = Email::MIME->create(
212                 header_str => [
213                         Subject => 'I break references',
214                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
215                         'Message-ID' => "<$long_reply_mid>",
216                         # No References:
217                         # 'References' => '<root@s> <last@s> <'.$long_mid.'>',
218                         'In-Reply-To' => "<$long_mid>",
219                         From => 'no1 <no1@example.com>',
220                         To => 'list@example.com',
221                 ],
222                 body => "no References\n");
223         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
224
225         $rw_commit->();
226         $ro->reopen;
227         my $t = $ro->{over_ro}->get_thread('root@s');
228         is(scalar(@$t), 4, "got all 4 mesages in thread");
229         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
230         @res = filter_mids($t);
231         is_deeply(\@res, \@exp, "get_thread works");
232 });
233
234 # quote prioritization
235 $ibx->with_umask(sub {
236         $rw_commit->();
237         $rw->add_message(Email::MIME->create(
238                 header_str => [
239                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
240                         Subject => 'Hello',
241                         'Message-ID' => '<quote@a>',
242                         From => 'Quoter <quoter@example.com>',
243                         To => 'list@example.com',
244                 ],
245                 body => "> theatre illusions\nfade\n"));
246
247         $rw->add_message(Email::MIME->create(
248                 header_str => [
249                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
250                         Subject => 'Hello',
251                         'Message-ID' => '<nquote@a>',
252                         From => 'Non-Quoter<non-quoter@example.com>',
253                         To => 'list@example.com',
254                 ],
255                 body => "theatre\nfade\n"));
256         my $res = $rw->query("theatre");
257         is(scalar(@$res), 2, "got both matches");
258         is($res->[0]->mid, 'nquote@a', "non-quoted scores higher") if scalar(@$res);
259         is($res->[1]->mid, 'quote@a', "quoted result still returned") if scalar(@$res);
260
261         $res = $rw->query("illusions");
262         is(scalar(@$res), 1, "got a match for quoted text");
263         is($res->[0]->mid, 'quote@a',
264                 "quoted result returned if nothing else") if scalar(@$res);
265 });
266
267 # circular references
268 $ibx->with_umask(sub {
269         my $s = 'foo://'. ('Circle' x 15).'/foo';
270         my $doc_id = $rw->add_message(Email::MIME->create(
271                 header => [ Subject => $s ],
272                 header_str => [
273                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
274                         'Message-ID' => '<circle@a>',
275                         'References' => '<circle@a>',
276                         'In-Reply-To' => '<circle@a>',
277                         From => 'Circle <circle@example.com>',
278                         To => 'list@example.com',
279                 ],
280                 body => "LOOP!\n"));
281         ok($doc_id > 0, "doc_id defined with circular reference");
282         my $smsg = $rw->query('m:circle@a', {limit=>1})->[0];
283         is(defined($smsg), 1, 'found m:circl@a');
284         is($smsg->references, '', "no references created") if defined($smsg);
285         is($smsg->subject, $s, 'long subject not rewritten') if defined($smsg);
286 });
287
288 $ibx->with_umask(sub {
289         my $str = eval {
290                 my $mbox = 't/utf8.mbox';
291                 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
292                 local $/;
293                 <$fh>
294         };
295         $str =~ s/\AFrom [^\n]+\n//s;
296         my $mime = Email::MIME->new($str);
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);
302 });
303
304 {
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');
311 }
312
313 # names and addresses
314 {
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->lookup_article($m->get_docid);
319                 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
320         }
321
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->lookup_article($m->get_docid);
326                 my $tocc = join("\n", $smsg->to, $smsg->cc);
327                 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
328         }
329
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->lookup_article($m->get_docid);
335                         like($smsg->cc, qr/\bfoo\@example\.com\b/,
336                                 'cc appears');
337                 }
338         }
339
340         foreach my $pfx ('', 'tcf:', 'f:') {
341                 my $res = $ro->query($pfx . 'Laggy');
342                 is(scalar(@$res), 1,
343                         "searched $pfx successfully for From:");
344                 foreach my $smsg (@$res) {
345                         like($smsg->from_name, qr/Laggy Sender/,
346                                 "From appears with $pfx");
347                 }
348         }
349 }
350
351 {
352         $rw_commit->();
353         $ro->reopen;
354         my $res = $ro->query('b:hello');
355         is(scalar(@$res), 0, 'no match on body search only');
356         $res = $ro->query('bs:smith');
357         is(scalar(@$res), 0,
358                 'no match on body+subject search for From');
359
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));
364
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));
369
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);
375         }
376 }
377
378 $ibx->with_umask(sub {
379         my $part1 = Email::MIME->create(
380                  attributes => {
381                      content_type => 'text/plain',
382                      disposition  => 'attachment',
383                      charset => 'US-ASCII',
384                      encoding => 'quoted-printable',
385                      filename => 'attached_fart.txt',
386                  },
387                  body_str => 'inside the attachment',
388         );
389         my $part2 = Email::MIME->create(
390                  attributes => {
391                      content_type => 'text/plain',
392                      disposition  => 'attachment',
393                      charset => 'US-ASCII',
394                      encoding => 'quoted-printable',
395                      filename => 'part_deux.txt',
396                  },
397                  body_str => 'inside another',
398         );
399         my $amsg = Email::MIME->create(
400                 header_str => [
401                         Subject => 'see attachment',
402                         'Message-ID' => '<file@attached>',
403                         From => 'John Smith <js@example.com>',
404                         To => 'list@example.com',
405                 ],
406                 parts => [ $part1, $part2 ],
407         );
408         ok($rw->add_message($amsg), 'added attachment');
409         $rw_commit->();
410         $ro->reopen;
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);
421
422         my $art;
423         if (scalar(@$n) >= 1) {
424                 my $mid = $n->[0]->mid;
425                 my ($id, $prev);
426                 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
427                 ok($art, 'article exists in OVER DB');
428         }
429         $rw->unindex_blob($amsg);
430         $rw->commit_txn_lazy;
431         is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB') if defined($art);
432 });
433
434 my $all_mask = 07777;
435 my $dir_mask = 02770;
436
437 # FreeBSD does not allow non-root users to set S_ISGID, so
438 # git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
439 if ($^O =~ /freebsd/i) {
440         $all_mask = 0777;
441         $dir_mask = 0770;
442 }
443
444 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
445                 "$git_dir/public-inbox",
446                 glob("$git_dir/public-inbox/xapian*/"),
447                 glob("$git_dir/public-inbox/xapian*/*")) {
448         my @st = stat($f);
449         my ($bn) = (split(m!/!, $f))[-1];
450         is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
451                 "sharedRepository respected for $bn");
452 }
453
454 $ibx->with_umask(sub {
455         $rw_commit->();
456         my $digits = '10010260936330';
457         my $ua = 'Pine.LNX.4.10';
458         my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
459         is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
460                 'no results yet');
461         my $pine = Email::MIME->create(
462                 header_str => [
463                         Subject => 'blah',
464                         'Message-ID' => "<$mid>",
465                         From => 'torvalds@transmeta',
466                         To => 'list@example.com',
467                 ],
468                 body => ""
469         );
470         my $x = $rw->add_message($pine);
471         $rw->commit_txn_lazy;
472         is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
473                 'searching only digit yielded result');
474
475         my $wild = $digits;
476         for my $i (1..6) {
477                 chop($wild);
478                 is($ro->query("m:$wild*", { mset => 1})->size, 1,
479                         "searching chopped($i) digit yielded result $wild ");
480         }
481         is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
482 });
483
484 done_testing();
485
486 1;