]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
testcommon: spawn-aware system() and qx[] workalikes
[public-inbox.git] / t / search.t
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>
3 use strict;
4 use warnings;
5 use Test::More;
6 use PublicInbox::TestCommon;
7 require_mods(qw(DBD::SQLite Search::Xapian));
8 require PublicInbox::SearchIdx;
9 require PublicInbox::Inbox;
10 require PublicInbox::InboxWritable;
11 use Email::MIME;
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);
16
17 is(0, xsys(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");
21
22 my $rw = PublicInbox::SearchIdx->new($ibx, 1);
23 $ibx->with_umask(sub {
24         $rw->_xdb_acquire;
25         $rw->_xdb_release;
26 });
27 $rw = undef;
28 my $ro = PublicInbox::Search->new($ibx);
29 my $rw_commit = sub {
30         $rw->commit_txn_lazy if $rw;
31         $rw = PublicInbox::SearchIdx->new($ibx, 1);
32         $rw->{qp_flags} = 0; # quiet a warning
33         $rw->begin_txn_lazy;
34 };
35
36 sub oct_is ($$$) {
37         my ($got, $exp, $msg) = @_;
38         is(sprintf('0%03o', $got), sprintf('0%03o', $exp), $msg);
39 }
40
41 {
42         # git repository perms
43         oct_is($ibx->_git_config_perm(),
44                 &PublicInbox::InboxWritable::PERM_GROUP,
45                 'undefined permission is group');
46         my @t = (
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' ],
53         );
54         for (@t) {
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);
59         }
60 }
61
62 $ibx->with_umask(sub {
63         my $root = Email::MIME->create(
64                 header_str => [
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',
70                 ],
71                 body => "\\m/\n");
72         my $last = Email::MIME->create(
73                 header_str => [
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',
81                 ],
82                 body => "goodbye forever :<\n");
83
84         my $rv;
85         $rw_commit->();
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");
90 });
91
92 sub filter_mids {
93         my ($msgs) = @_;
94         sort(map { $_->mid } @$msgs);
95 }
96
97 {
98         $rw_commit->();
99         $ro->reopen;
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);
103
104         my ($res, @res);
105         my @exp = sort qw(root@s last@s);
106
107         $res = $ro->query('s:(Hello world)');
108         @res = filter_mids($res);
109         is_deeply(\@res, \@exp, 'got expected results for s:() match');
110
111         $res = $ro->query('s:"Hello world"');
112         @res = filter_mids($res);
113         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
114
115         $res = $ro->query('s:"Hello world"', {limit => 1});
116         is(scalar @$res, 1, "limit works");
117         my $first = $res->[0];
118
119         $res = $ro->query('s:"Hello world"', {offset => 1});
120         is(scalar @$res, 1, "offset works");
121         my $second = $res->[0];
122
123         isnt($first, $second, "offset returned different result from limit");
124 }
125
126 # ghost vivication
127 $ibx->with_umask(sub {
128         $rw_commit->();
129         my $rmid = '<ghost-message@s>';
130         my $reply_to_ghost = Email::MIME->create(
131                 header_str => [
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',
138                 ],
139                 body => "-_-\n");
140
141         my $rv;
142         my $reply_id = $rw->add_message($reply_to_ghost);
143         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
144
145         my $was_ghost = Email::MIME->create(
146                 header_str => [
147                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
148                         Subject => 'ghosts',
149                         'Message-ID' => $rmid,
150                         From => 'Laggy Sender <lag@example.com>',
151                         To => 'list@example.com',
152                 ],
153                 body => "are real\n");
154
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");
161         }
162         isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
163         ok($_->{num} > 0, 'positive art num') foreach @$msgs
164 });
165
166 # search thread on ghost
167 {
168         $rw_commit->();
169         $ro->reopen;
170
171         # subject
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');
176
177         # body
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);
181
182         # datestamp
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');
188 }
189
190 # long message-id
191 $ibx->with_umask(sub {
192         $rw_commit->();
193         $ro->reopen;
194         my $long_mid = 'last' . ('x' x 60). '@s';
195
196         my $long = Email::MIME->create(
197                 header_str => [
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',
205                 ],
206                 body => "wut\n");
207         my $long_id = $rw->add_message($long);
208         is($long_id, int($long_id), "long_id is an integer: $long_id");
209
210         $rw_commit->();
211         $ro->reopen;
212         my $res;
213         my @res;
214
215         my $long_reply_mid = 'reply-to-long@1';
216         my $long_reply = Email::MIME->create(
217                 header_str => [
218                         Subject => 'I break references',
219                         Date => 'Sat, 02 Oct 2010 00:00:00 +0000',
220                         'Message-ID' => "<$long_reply_mid>",
221                         # No References:
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',
226                 ],
227                 body => "no References\n");
228         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
229
230         $rw_commit->();
231         $ro->reopen;
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");
237 });
238
239 # quote prioritization
240 $ibx->with_umask(sub {
241         $rw_commit->();
242         $rw->add_message(Email::MIME->create(
243                 header_str => [
244                         Date => 'Sat, 02 Oct 2010 00:00:01 +0000',
245                         Subject => 'Hello',
246                         'Message-ID' => '<quote@a>',
247                         From => 'Quoter <quoter@example.com>',
248                         To => 'list@example.com',
249                 ],
250                 body => "> theatre illusions\nfade\n"));
251
252         $rw->add_message(Email::MIME->create(
253                 header_str => [
254                         Date => 'Sat, 02 Oct 2010 00:00:02 +0000',
255                         Subject => 'Hello',
256                         'Message-ID' => '<nquote@a>',
257                         From => 'Non-Quoter<non-quoter@example.com>',
258                         To => 'list@example.com',
259                 ],
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);
265
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);
270 });
271
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 ],
277                 header_str => [
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',
284                 ],
285                 body => "LOOP!\n"));
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);
291 });
292
293 $ibx->with_umask(sub {
294         my $eml = 't/utf8.eml';
295         my $mime = PublicInbox::InboxWritable::mime_from_path($eml) or
296                 die "open $eml: $!";
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->{over_ro}->get_art($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->{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');
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->{over_ro}->get_art($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         SKIP: {
432                 skip('$art not defined', 1) unless defined $art;
433                 is($ro->{over_ro}->get_art($art->{num}), undef,
434                         'gone from OVER DB');
435         };
436 });
437
438 my $all_mask = 07777;
439 my $dir_mask = 02770;
440
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) {
444         $all_mask = 0777;
445         $dir_mask = 0770;
446 }
447
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*/*")) {
452         my @st = stat($f);
453         my ($bn) = (split(m!/!, $f))[-1];
454         oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
455                 "sharedRepository respected for $bn");
456 }
457
458 $ibx->with_umask(sub {
459         $rw_commit->();
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,
464                 'no results yet');
465         my $pine = Email::MIME->create(
466                 header_str => [
467                         Subject => 'blah',
468                         'Message-ID' => "<$mid>",
469                         From => 'torvalds@transmeta',
470                         To => 'list@example.com',
471                 ],
472                 body => ""
473         );
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');
478
479         my $wild = $digits;
480         for my $i (1..6) {
481                 chop($wild);
482                 is($ro->query("m:$wild*", { mset => 1})->size, 1,
483                         "searching chopped($i) digit yielded result $wild ");
484         }
485         is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
486 });
487
488 done_testing();
489
490 1;