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