]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
run update-copyrights from gnulib for 2019
[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 {
39         # git repository perms
40         is($ibx->_git_config_perm(), &PublicInbox::InboxWritable::PERM_GROUP,
41            "undefined permission is group");
42         is(PublicInbox::InboxWritable::_umask_for(
43              PublicInbox::InboxWritable->_git_config_perm('0644')),
44            0022, "644 => umask(0022)");
45         is(PublicInbox::InboxWritable::_umask_for(
46              PublicInbox::InboxWritable->_git_config_perm('0600')),
47            0077, "600 => umask(0077)");
48         is(PublicInbox::InboxWritable::_umask_for(
49              PublicInbox::InboxWritable->_git_config_perm('0640')),
50            0027, "640 => umask(0027)");
51         is(PublicInbox::InboxWritable::_umask_for(
52              PublicInbox::InboxWritable->_git_config_perm('group')),
53            0007, 'group => umask(0007)');
54         is(PublicInbox::InboxWritable::_umask_for(
55              PublicInbox::InboxWritable->_git_config_perm('everybody')),
56            0002, 'everybody => umask(0002)');
57         is(PublicInbox::InboxWritable::_umask_for(
58              PublicInbox::InboxWritable->_git_config_perm('umask')),
59            umask, 'umask => existing umask');
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 mesages 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 $str = eval {
295                 my $mbox = 't/utf8.mbox';
296                 open(my $fh, '<', $mbox) or die "failed to open mbox: $mbox\n";
297                 local $/;
298                 <$fh>
299         };
300         $str =~ s/\AFrom [^\n]+\n//s;
301         my $mime = Email::MIME->new($str);
302         my $doc_id = $rw->add_message($mime);
303         ok($doc_id > 0, 'message indexed doc_id with UTF-8');
304         my $msg = $rw->query('m:testmessage@example.com', {limit => 1})->[0];
305         is(defined($msg), 1, 'found testmessage@example.com');
306         is($mime->header('Subject'), $msg->subject, 'UTF-8 subject preserved') if defined($msg);
307 });
308
309 {
310         my $msgs = $ro->query('d:19931002..20101002');
311         ok(scalar(@$msgs) > 0, 'got results within range');
312         $msgs = $ro->query('d:20101003..');
313         is(scalar(@$msgs), 0, 'nothing after 20101003');
314         $msgs = $ro->query('d:..19931001');
315         is(scalar(@$msgs), 0, 'nothing before 19931001');
316 }
317
318 # names and addresses
319 {
320         my $mset = $ro->query('t:list@example.com', {mset => 1});
321         is($mset->size, 6, 'searched To: successfully');
322         foreach my $m ($mset->items) {
323                 my $smsg = $ro->lookup_article($m->get_docid);
324                 like($smsg->to, qr/\blist\@example\.com\b/, 'to appears');
325         }
326
327         $mset = $ro->query('tc:list@example.com', {mset => 1});
328         is($mset->size, 6, 'searched To+Cc: successfully');
329         foreach my $m ($mset->items) {
330                 my $smsg = $ro->lookup_article($m->get_docid);
331                 my $tocc = join("\n", $smsg->to, $smsg->cc);
332                 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
333         }
334
335         foreach my $pfx ('tcf:', 'c:') {
336                 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
337                 is($mset->items, 1, "searched $pfx successfully for Cc:");
338                 foreach my $m ($mset->items) {
339                         my $smsg = $ro->lookup_article($m->get_docid);
340                         like($smsg->cc, qr/\bfoo\@example\.com\b/,
341                                 'cc appears');
342                 }
343         }
344
345         foreach my $pfx ('', 'tcf:', 'f:') {
346                 my $res = $ro->query($pfx . 'Laggy');
347                 is(scalar(@$res), 1,
348                         "searched $pfx successfully for From:");
349                 foreach my $smsg (@$res) {
350                         like($smsg->from_name, qr/Laggy Sender/,
351                                 "From appears with $pfx");
352                 }
353         }
354 }
355
356 {
357         $rw_commit->();
358         $ro->reopen;
359         my $res = $ro->query('b:hello');
360         is(scalar(@$res), 0, 'no match on body search only');
361         $res = $ro->query('bs:smith');
362         is(scalar(@$res), 0,
363                 'no match on body+subject search for From');
364
365         $res = $ro->query('q:theatre');
366         is(scalar(@$res), 1, 'only one quoted body');
367         like($res->[0]->from_name, qr/\AQuoter/,
368                 'got quoted body') if (scalar(@$res));
369
370         $res = $ro->query('nq:theatre');
371         is(scalar @$res, 1, 'only one non-quoted body');
372         like($res->[0]->from_name, qr/\ANon-Quoter/,
373                 'got non-quoted body') if (scalar(@$res));
374
375         foreach my $pfx (qw(b: bs:)) {
376                 $res = $ro->query($pfx . 'theatre');
377                 is(scalar @$res, 2, "searched both bodies for $pfx");
378                 like($res->[0]->from_name, qr/\ANon-Quoter/,
379                         "non-quoter first for $pfx") if scalar(@$res);
380         }
381 }
382
383 $ibx->with_umask(sub {
384         my $part1 = Email::MIME->create(
385                  attributes => {
386                      content_type => 'text/plain',
387                      disposition  => 'attachment',
388                      charset => 'US-ASCII',
389                      encoding => 'quoted-printable',
390                      filename => 'attached_fart.txt',
391                  },
392                  body_str => 'inside the attachment',
393         );
394         my $part2 = Email::MIME->create(
395                  attributes => {
396                      content_type => 'text/plain',
397                      disposition  => 'attachment',
398                      charset => 'US-ASCII',
399                      encoding => 'quoted-printable',
400                      filename => 'part_deux.txt',
401                  },
402                  body_str => 'inside another',
403         );
404         my $amsg = Email::MIME->create(
405                 header_str => [
406                         Subject => 'see attachment',
407                         'Message-ID' => '<file@attached>',
408                         From => 'John Smith <js@example.com>',
409                         To => 'list@example.com',
410                 ],
411                 parts => [ $part1, $part2 ],
412         );
413         ok($rw->add_message($amsg), 'added attachment');
414         $rw_commit->();
415         $ro->reopen;
416         my $n = $ro->query('n:attached_fart.txt');
417         is(scalar @$n, 1, 'got result for n:');
418         my $res = $ro->query('part_deux.txt');
419         is(scalar @$res, 1, 'got result without n:');
420         is($n->[0]->mid, $res->[0]->mid,
421                 'same result with and without') if scalar(@$res);
422         my $txt = $ro->query('"inside another"');
423         is(scalar @$txt, 1, 'found inside another');
424         is($txt->[0]->mid, $res->[0]->mid,
425                 'search inside text attachments works') if scalar(@$txt);
426
427         my $art;
428         if (scalar(@$n) >= 1) {
429                 my $mid = $n->[0]->mid;
430                 my ($id, $prev);
431                 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
432                 ok($art, 'article exists in OVER DB');
433         }
434         $rw->unindex_blob($amsg);
435         $rw->commit_txn_lazy;
436         is($ro->lookup_article($art->{num}), undef, 'gone from OVER DB') if defined($art);
437 });
438
439 my $all_mask = 07777;
440 my $dir_mask = 02770;
441
442 # FreeBSD does not allow non-root users to set S_ISGID, so
443 # git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
444 if ($^O =~ /freebsd/i) {
445         $all_mask = 0777;
446         $dir_mask = 0770;
447 }
448
449 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
450                 "$git_dir/public-inbox",
451                 glob("$git_dir/public-inbox/xapian*/"),
452                 glob("$git_dir/public-inbox/xapian*/*")) {
453         my @st = stat($f);
454         my ($bn) = (split(m!/!, $f))[-1];
455         is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
456                 "sharedRepository respected for $bn");
457 }
458
459 $ibx->with_umask(sub {
460         $rw_commit->();
461         my $digits = '10010260936330';
462         my $ua = 'Pine.LNX.4.10';
463         my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
464         is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
465                 'no results yet');
466         my $pine = Email::MIME->create(
467                 header_str => [
468                         Subject => 'blah',
469                         'Message-ID' => "<$mid>",
470                         From => 'torvalds@transmeta',
471                         To => 'list@example.com',
472                 ],
473                 body => ""
474         );
475         my $x = $rw->add_message($pine);
476         $rw->commit_txn_lazy;
477         is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
478                 'searching only digit yielded result');
479
480         my $wild = $digits;
481         for my $i (1..6) {
482                 chop($wild);
483                 is($ro->query("m:$wild*", { mset => 1})->size, 1,
484                         "searching chopped($i) digit yielded result $wild ");
485         }
486         is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
487 });
488
489 done_testing();
490
491 1;