]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
t/search: bail out on `git init --shared' failures
[public-inbox.git] / t / search.t
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>
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 require PublicInbox::Inbox;
13 use File::Temp qw/tempdir/;
14 use Email::MIME;
15 my $tmpdir = tempdir('pi-search-XXXXXX', TMPDIR => 1, CLEANUP => 1);
16 my $git_dir = "$tmpdir/a.git";
17 my $ibx = PublicInbox::Inbox->new({ mainrepo => $git_dir });
18 my ($root_id, $last_id);
19
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");
24
25 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
26 $ibx->with_umask(sub {
27         $rw->_xdb_acquire;
28         $rw->_xdb_release;
29 });
30 $rw = undef;
31 my $ro = PublicInbox::Search->new($ibx);
32 my $rw_commit = sub {
33         $rw->commit_txn_lazy if $rw;
34         $rw = PublicInbox::SearchIdx->new($ibx, 1);
35         $rw->{qp_flags} = 0; # quiet a warning
36         $rw->begin_txn_lazy;
37 };
38
39 sub oct_is ($$$) {
40         my ($got, $exp, $msg) = @_;
41         is(sprintf('0%03o', $got), sprintf('0%03o', $exp), $msg);
42 }
43
44 {
45         # git repository perms
46         oct_is($ibx->_git_config_perm(),
47                 &PublicInbox::InboxWritable::PERM_GROUP,
48                 'undefined permission is group');
49         my @t = (
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' ],
56         );
57         for (@t) {
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);
62         }
63 }
64
65 $ibx->with_umask(sub {
66         my $root = Email::MIME->create(
67                 header_str => [
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',
73                 ],
74                 body => "\\m/\n");
75         my $last = Email::MIME->create(
76                 header_str => [
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',
84                 ],
85                 body => "goodbye forever :<\n");
86
87         my $rv;
88         $rw_commit->();
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");
93 });
94
95 sub filter_mids {
96         my ($msgs) = @_;
97         sort(map { $_->mid } @$msgs);
98 }
99
100 {
101         $rw_commit->();
102         $ro->reopen;
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);
106
107         my ($res, @res);
108         my @exp = sort qw(root@s last@s);
109
110         $res = $ro->query('s:(Hello world)');
111         @res = filter_mids($res);
112         is_deeply(\@res, \@exp, 'got expected results for s:() match');
113
114         $res = $ro->query('s:"Hello world"');
115         @res = filter_mids($res);
116         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
117
118         $res = $ro->query('s:"Hello world"', {limit => 1});
119         is(scalar @$res, 1, "limit works");
120         my $first = $res->[0];
121
122         $res = $ro->query('s:"Hello world"', {offset => 1});
123         is(scalar @$res, 1, "offset works");
124         my $second = $res->[0];
125
126         isnt($first, $second, "offset returned different result from limit");
127 }
128
129 # ghost vivication
130 $ibx->with_umask(sub {
131         $rw_commit->();
132         my $rmid = '<ghost-message@s>';
133         my $reply_to_ghost = Email::MIME->create(
134                 header_str => [
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',
141                 ],
142                 body => "-_-\n");
143
144         my $rv;
145         my $reply_id = $rw->add_message($reply_to_ghost);
146         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
147
148         my $was_ghost = Email::MIME->create(
149                 header_str => [
150                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
151                         Subject => 'ghosts',
152                         'Message-ID' => $rmid,
153                         From => 'Laggy Sender <lag@example.com>',
154                         To => 'list@example.com',
155                 ],
156                 body => "are real\n");
157
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");
164         }
165         isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
166         ok($_->{num} > 0, 'positive art num') foreach @$msgs
167 });
168
169 # search thread on ghost
170 {
171         $rw_commit->();
172         $ro->reopen;
173
174         # subject
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');
179
180         # body
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);
184
185         # datestamp
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');
191 }
192
193 # long message-id
194 $ibx->with_umask(sub {
195         $rw_commit->();
196         $ro->reopen;
197         my $long_mid = 'last' . ('x' x 60). '@s';
198
199         my $long = Email::MIME->create(
200                 header_str => [
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',
208                 ],
209                 body => "wut\n");
210         my $long_id = $rw->add_message($long);
211         is($long_id, int($long_id), "long_id is an integer: $long_id");
212
213         $rw_commit->();
214         $ro->reopen;
215         my $res;
216         my @res;
217
218         my $long_reply_mid = 'reply-to-long@1';
219         my $long_reply = Email::MIME->create(
220                 header_str => [
221                         Subject => 'I break references',
222                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
223                         'Message-ID' => "<$long_reply_mid>",
224                         # No References:
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',
229                 ],
230                 body => "no References\n");
231         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
232
233         $rw_commit->();
234         $ro->reopen;
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");
240 });
241
242 # quote prioritization
243 $ibx->with_umask(sub {
244         $rw_commit->();
245         $rw->add_message(Email::MIME->create(
246                 header_str => [
247                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
248                         Subject => 'Hello',
249                         'Message-ID' => '<quote@a>',
250                         From => 'Quoter <quoter@example.com>',
251                         To => 'list@example.com',
252                 ],
253                 body => "> theatre illusions\nfade\n"));
254
255         $rw->add_message(Email::MIME->create(
256                 header_str => [
257                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
258                         Subject => 'Hello',
259                         'Message-ID' => '<nquote@a>',
260                         From => 'Non-Quoter<non-quoter@example.com>',
261                         To => 'list@example.com',
262                 ],
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);
268
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);
273 });
274
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 ],
280                 header_str => [
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',
287                 ],
288                 body => "LOOP!\n"));
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);
294 });
295
296 $ibx->with_umask(sub {
297         my $str = eval {
298                 my $mbox = 't/utf8.mbox';
299                 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
300                 local $/;
301                 <$fh>
302         };
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);
310 });
311
312 {
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');
319 }
320
321 # names and addresses
322 {
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');
328         }
329
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');
336         }
337
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/,
344                                 'cc appears');
345                 }
346         }
347
348         foreach my $pfx ('', 'tcf:', 'f:') {
349                 my $res = $ro->query($pfx . 'Laggy');
350                 is(scalar(@$res), 1,
351                         "searched $pfx successfully for From:");
352                 foreach my $smsg (@$res) {
353                         like($smsg->from_name, qr/Laggy Sender/,
354                                 "From appears with $pfx");
355                 }
356         }
357 }
358
359 {
360         $rw_commit->();
361         $ro->reopen;
362         my $res = $ro->query('b:hello');
363         is(scalar(@$res), 0, 'no match on body search only');
364         $res = $ro->query('bs:smith');
365         is(scalar(@$res), 0,
366                 'no match on body+subject search for From');
367
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));
372
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));
377
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);
383         }
384 }
385
386 $ibx->with_umask(sub {
387         my $part1 = Email::MIME->create(
388                  attributes => {
389                      content_type => 'text/plain',
390                      disposition  => 'attachment',
391                      charset => 'US-ASCII',
392                      encoding => 'quoted-printable',
393                      filename => 'attached_fart.txt',
394                  },
395                  body_str => 'inside the attachment',
396         );
397         my $part2 = Email::MIME->create(
398                  attributes => {
399                      content_type => 'text/plain',
400                      disposition  => 'attachment',
401                      charset => 'US-ASCII',
402                      encoding => 'quoted-printable',
403                      filename => 'part_deux.txt',
404                  },
405                  body_str => 'inside another',
406         );
407         my $amsg = Email::MIME->create(
408                 header_str => [
409                         Subject => 'see attachment',
410                         'Message-ID' => '<file@attached>',
411                         From => 'John Smith <js@example.com>',
412                         To => 'list@example.com',
413                 ],
414                 parts => [ $part1, $part2 ],
415         );
416         ok($rw->add_message($amsg), 'added attachment');
417         $rw_commit->();
418         $ro->reopen;
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);
429
430         my $art;
431         if (scalar(@$n) >= 1) {
432                 my $mid = $n->[0]->mid;
433                 my ($id, $prev);
434                 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
435                 ok($art, 'article exists in OVER DB');
436         }
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);
440 });
441
442 my $all_mask = 07777;
443 my $dir_mask = 02770;
444
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) {
448         $all_mask = 0777;
449         $dir_mask = 0770;
450 }
451
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*/*")) {
456         my @st = stat($f);
457         my ($bn) = (split(m!/!, $f))[-1];
458         oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
459                 "sharedRepository respected for $bn");
460 }
461
462 $ibx->with_umask(sub {
463         $rw_commit->();
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,
468                 'no results yet');
469         my $pine = Email::MIME->create(
470                 header_str => [
471                         Subject => 'blah',
472                         'Message-ID' => "<$mid>",
473                         From => 'torvalds@transmeta',
474                         To => 'list@example.com',
475                 ],
476                 body => ""
477         );
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');
482
483         my $wild = $digits;
484         for my $i (1..6) {
485                 chop($wild);
486                 is($ro->query("m:$wild*", { mset => 1})->size, 1,
487                         "searching chopped($i) digit yielded result $wild ");
488         }
489         is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
490 });
491
492 done_testing();
493
494 1;