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