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