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