]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
disambiguate OverIdx and Over by field name
[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 PublicInbox::Eml;
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->idx_acquire;
25         $rw->idx_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 {
63         my $crlf_adjust = \&PublicInbox::SearchIdx::crlf_adjust;
64         is($crlf_adjust->("hi\r\nworld\r\n"), 0, 'no adjustment needed');
65         is($crlf_adjust->("hi\nworld\n"), 2, 'LF-only counts two CR');
66         is($crlf_adjust->("hi\r\nworld\n"), 1, 'CRLF/LF-mix 1 counts 1 CR');
67         is($crlf_adjust->("hi\nworld\r\n"), 1, 'CRLF/LF-mix 2 counts 1 CR');
68 }
69
70 $ibx->with_umask(sub {
71         my $root = PublicInbox::Eml->new(<<'EOF');
72 Date: Fri, 02 Oct 1993 00:00:00 +0000
73 Subject: Hello world
74 Message-ID: <root@s>
75 From: John Smith <js@example.com>
76 To: list@example.com
77 List-Id: I'm not mad <i.m.just.bored>
78
79 \m/
80 EOF
81         my $last = PublicInbox::Eml->new(<<'EOF');
82 Date: Sat, 02 Oct 2010 00:00:00 +0000
83 Subject: Re: Hello world
84 In-Reply-To: <root@s>
85 Message-ID: <last@s>
86 From: John Smith <js@example.com>
87 To: list@example.com
88 Cc: foo@example.com
89 List-Id: there's nothing <left.for.me.to.do>
90
91 goodbye forever :<
92 EOF
93         my $rv;
94         $rw_commit->();
95         $root_id = $rw->add_message($root);
96         is($root_id, int($root_id), "root_id is an integer: $root_id");
97         $last_id = $rw->add_message($last);
98         is($last_id, int($last_id), "last_id is an integer: $last_id");
99 });
100
101 sub filter_mids {
102         my ($msgs) = @_;
103         sort(map { $_->{mid} } @$msgs);
104 }
105
106 {
107         $rw_commit->();
108         $ro->reopen;
109         my $found = $ro->query('m:root@s');
110         is(scalar(@$found), 1, "message found");
111         is($found->[0]->{mid}, 'root@s', 'mid set correctly') if @$found;
112
113         my ($res, @res);
114         my @exp = sort qw(root@s last@s);
115
116         $res = $ro->query('s:(Hello world)');
117         @res = filter_mids($res);
118         is_deeply(\@res, \@exp, 'got expected results for s:() match');
119
120         $res = $ro->query('s:"Hello world"');
121         @res = filter_mids($res);
122         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
123
124         $res = $ro->query('s:"Hello world"', {limit => 1});
125         is(scalar @$res, 1, "limit works");
126         my $first = $res->[0];
127
128         $res = $ro->query('s:"Hello world"', {offset => 1});
129         is(scalar @$res, 1, "offset works");
130         my $second = $res->[0];
131
132         isnt($first, $second, "offset returned different result from limit");
133 }
134
135 # ghost vivication
136 $ibx->with_umask(sub {
137         $rw_commit->();
138         my $rmid = '<ghost-message@s>';
139         my $reply_to_ghost = PublicInbox::Eml->new(<<"EOF");
140 Date: Sat, 02 Oct 2010 00:00:00 +0000
141 Subject: Re: ghosts
142 Message-ID: <ghost-reply\@s>
143 In-Reply-To: $rmid
144 From: Time Traveler <tt\@example.com>
145 To: list\@example.com
146
147 -_-
148 EOF
149         my $rv;
150         my $reply_id = $rw->add_message($reply_to_ghost);
151         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
152
153         my $was_ghost = PublicInbox::Eml->new(<<"EOF");
154 Date: Sat, 02 Oct 2010 00:00:01 +0000
155 Subject: ghosts
156 Message-ID: $rmid
157 From: Laggy Sender <lag\@example.com>
158 To: list\@example.com
159
160 are real
161 EOF
162         my $ghost_id = $rw->add_message($was_ghost);
163         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
164         my $msgs = $rw->{oidx}->get_thread('ghost-message@s');
165         is(scalar(@$msgs), 2, 'got both messages in ghost thread');
166         foreach (qw(sid tid)) {
167                 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
168         }
169         isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
170         ok($_->{num} > 0, 'positive art num') foreach @$msgs
171 });
172
173 # search thread on ghost
174 {
175         $rw_commit->();
176         $ro->reopen;
177
178         # subject
179         my $res = $ro->query('ghost');
180         my @exp = sort qw(ghost-message@s ghost-reply@s);
181         my @res = filter_mids($res);
182         is_deeply(\@res, \@exp, 'got expected results for Subject match');
183
184         # body
185         $res = $ro->query('goodbye');
186         is(scalar(@$res), 1, "goodbye message found");
187         is($res->[0]->{mid}, 'last@s', 'got goodbye message body') if @$res;
188
189         # datestamp
190         $res = $ro->query('dt:20101002000001..20101002000001');
191         @res = filter_mids($res);
192         is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
193         $res = $ro->query('dt:20101002000002..20101002000002');
194         is_deeply($res, [], 'exact Date: match down to the second');
195 }
196
197 # long message-id
198 $ibx->with_umask(sub {
199         $rw_commit->();
200         $ro->reopen;
201         my $long_mid = 'last' . ('x' x 60). '@s';
202         my $long = PublicInbox::Eml->new(<<EOF);
203 Date: Sat, 02 Oct 2010 00:00:00 +0000
204 Subject: long message ID
205 References: <root\@s> <last\@s>
206 In-Reply-To: <last\@s>
207 Message-ID: <$long_mid>,
208 From: "Long I.D." <long-id\@example.com>
209 To: list\@example.com
210
211 wut
212 EOF
213         my $long_id = $rw->add_message($long);
214         is($long_id, int($long_id), "long_id is an integer: $long_id");
215
216         $rw_commit->();
217         $ro->reopen;
218         my $res;
219         my @res;
220
221         my $long_reply_mid = 'reply-to-long@1';
222         my $long_reply = PublicInbox::Eml->new(<<EOF);
223 Subject: I break references
224 Date: Sat, 02 Oct 2010 00:00:00 +0000
225 Message-ID: <$long_reply_mid>
226 In-Reply-To: <$long_mid>
227 From: no1 <no1\@example.com>
228 To: list\@example.com
229
230 no References
231 EOF
232         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
233
234         $rw_commit->();
235         $ro->reopen;
236         my $t = $ro->{over_ro}->get_thread('root@s');
237         is(scalar(@$t), 4, "got all 4 messages in thread");
238         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
239         @res = filter_mids($t);
240         is_deeply(\@res, \@exp, "get_thread works");
241 });
242
243 # quote prioritization
244 $ibx->with_umask(sub {
245         $rw_commit->();
246         $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
247 Date: Sat, 02 Oct 2010 00:00:01 +0000
248 Subject: Hello
249 Message-ID: <quote@a>
250 From: Quoter <quoter@example.com>
251 To: list@example.com
252
253 > theatre illusions
254 fade
255 EOF
256         $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
257 Date: Sat, 02 Oct 2010 00:00:02 +0000
258 Subject: Hello
259 Message-ID: <nquote@a>
260 From: Non-Quoter<non-quoter@example.com>
261 To: list@example.com
262
263 theatre
264 fade
265 EOF
266         $rw_commit->();
267         my $res = $ro->reopen->query("theatre");
268         is(scalar(@$res), 2, "got both matches");
269         if (@$res == 2) {
270                 is($res->[0]->{mid}, 'nquote@a', 'non-quoted scores higher');
271                 is($res->[1]->{mid}, 'quote@a', 'quoted result still returned');
272         }
273         $res = $ro->query("illusions");
274         is(scalar(@$res), 1, "got a match for quoted text");
275         is($res->[0]->{mid}, 'quote@a',
276                 "quoted result returned if nothing else") if scalar(@$res);
277 });
278
279 # circular references
280 $ibx->with_umask(sub {
281         my $s = 'foo://'. ('Circle' x 15).'/foo';
282         my $doc_id = $rw->add_message(PublicInbox::Eml->new(<<EOF));
283 Subject: $s
284 Date: Sat, 02 Oct 2010 00:00:01 +0000
285 Message-ID: <circle\@a>
286 References: <circle\@a>
287 In-Reply-To: <circle\@a>
288 From: Circle <circle\@example.com>
289 To: list\@example.com
290
291 LOOP!
292 EOF
293         ok($doc_id > 0, "doc_id defined with circular reference");
294         $rw_commit->();
295         my $smsg = $ro->reopen->query('m:circle@a', {limit=>1})->[0];
296         is(defined($smsg), 1, 'found m:circl@a');
297         if (defined $smsg) {
298                 is($smsg->{references}, '', "no references created");
299                 is($smsg->{subject}, $s, 'long subject not rewritten');
300         }
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 $ibx->with_umask(sub {
313         my $mime = eml_load 't/utf8.eml';
314         my $doc_id = $rw->add_message($mime);
315         ok($doc_id > 0, 'message indexed doc_id with UTF-8');
316         $rw_commit->();
317         my $msg = $ro->reopen->
318                 query('m:testmessage@example.com', {limit => 1})->[0];
319         is(defined($msg), 1, 'found testmessage@example.com');
320         if (defined $msg) {
321                 is($mime->header('Subject'), $msg->{subject},
322                         'UTF-8 subject preserved');
323         }
324 });
325
326 # names and addresses
327 {
328         my $mset = $ro->query('t:list@example.com', {mset => 1});
329         is($mset->size, 9, 'searched To: successfully');
330         foreach my $m ($mset->items) {
331                 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
332                 like($smsg->{to}, qr/\blist\@example\.com\b/, 'to appears');
333                 my $doc = $m->get_document;
334                 my $col = PublicInbox::Search::BYTES();
335                 my $bytes = PublicInbox::Smsg::get_val($doc, $col);
336                 like($bytes, qr/\A[0-9]+\z/, '$bytes stored as digit');
337                 ok($bytes > 0, '$bytes is > 0');
338                 is($bytes, $smsg->{bytes}, 'bytes Xapian value matches Over');
339
340                 $col = PublicInbox::Search::UID();
341                 my $uid = PublicInbox::Smsg::get_val($doc, $col);
342                 is($uid, $smsg->{num}, 'UID column matches {num}');
343                 is($uid, $m->get_docid, 'UID column matches docid');
344         }
345
346         $mset = $ro->query('tc:list@example.com', {mset => 1});
347         is($mset->size, 9, 'searched To+Cc: successfully');
348         foreach my $m ($mset->items) {
349                 my $smsg = $ro->{over_ro}->get_art($m->get_docid);
350                 my $tocc = join("\n", $smsg->{to}, $smsg->{cc});
351                 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
352         }
353
354         foreach my $pfx ('tcf:', 'c:') {
355                 my $mset = $ro->query($pfx . 'foo@example.com', { mset => 1 });
356                 is($mset->items, 1, "searched $pfx successfully for Cc:");
357                 foreach my $m ($mset->items) {
358                         my $smsg = $ro->{over_ro}->get_art($m->get_docid);
359                         like($smsg->{cc}, qr/\bfoo\@example\.com\b/,
360                                 'cc appears');
361                 }
362         }
363
364         foreach my $pfx ('', 'tcf:', 'f:') {
365                 my $res = $ro->query($pfx . 'Laggy');
366                 is(scalar(@$res), 1,
367                         "searched $pfx successfully for From:");
368                 foreach my $smsg (@$res) {
369                         like($smsg->{from_name}, qr/Laggy Sender/,
370                                 "From appears with $pfx");
371                 }
372         }
373 }
374
375 {
376         $rw_commit->();
377         $ro->reopen;
378         my $res = $ro->query('b:hello');
379         is(scalar(@$res), 0, 'no match on body search only');
380         $res = $ro->query('bs:smith');
381         is(scalar(@$res), 0,
382                 'no match on body+subject search for From');
383
384         $res = $ro->query('q:theatre');
385         is(scalar(@$res), 1, 'only one quoted body');
386         like($res->[0]->{from_name}, qr/\AQuoter/,
387                 'got quoted body') if (scalar(@$res));
388
389         $res = $ro->query('nq:theatre');
390         is(scalar @$res, 1, 'only one non-quoted body');
391         like($res->[0]->{from_name}, qr/\ANon-Quoter/,
392                 'got non-quoted body') if (scalar(@$res));
393
394         foreach my $pfx (qw(b: bs:)) {
395                 $res = $ro->query($pfx . 'theatre');
396                 is(scalar @$res, 2, "searched both bodies for $pfx");
397                 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
398                         "non-quoter first for $pfx") if scalar(@$res);
399         }
400 }
401
402 $ibx->with_umask(sub {
403         my $amsg = eml_load 't/search-amsg.eml';
404         my $oid = ('0'x40);
405         my $smsg = bless { blob => $oid }, 'PublicInbox::Smsg';
406         ok($rw->add_message($amsg, $smsg), 'added attachment');
407         $rw_commit->();
408         $ro->reopen;
409         my $n = $ro->query('n:attached_fart.txt');
410         is(scalar @$n, 1, 'got result for n:');
411         my $res = $ro->query('part_deux.txt');
412         is(scalar @$res, 1, 'got result without n:');
413         is($n->[0]->{mid}, $res->[0]->{mid},
414                 'same result with and without') if scalar(@$res);
415         my $txt = $ro->query('"inside another"');
416         is(scalar @$txt, 1, 'found inside another');
417         is($txt->[0]->{mid}, $res->[0]->{mid},
418                 'search inside text attachments works') if scalar(@$txt);
419
420         my $art;
421         if (scalar(@$n) >= 1) {
422                 my $mid = $n->[0]->{mid};
423                 my ($id, $prev);
424                 $art = $ro->{over_ro}->next_by_mid($mid, \$id, \$prev);
425                 ok($art, 'article exists in OVER DB');
426         }
427         $rw->_msgmap_init;
428         $rw->unindex_eml($oid, $amsg);
429         $rw->commit_txn_lazy;
430         SKIP: {
431                 skip('$art not defined', 1) unless defined $art;
432                 is($ro->{over_ro}->get_art($art->{num}), undef,
433                         'gone from OVER DB');
434         };
435 });
436
437 my $all_mask = 07777;
438 my $dir_mask = 02770;
439
440 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
441 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
442 if ($^O =~ /(?:free|open)bsd/i) {
443         $all_mask = 0777;
444         $dir_mask = 0770;
445 }
446
447 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
448                 "$git_dir/public-inbox",
449                 glob("$git_dir/public-inbox/xapian*/"),
450                 glob("$git_dir/public-inbox/xapian*/*")) {
451         my @st = stat($f);
452         my ($bn) = (split(m!/!, $f))[-1];
453         oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
454                 "sharedRepository respected for $bn");
455 }
456
457 $ibx->with_umask(sub {
458         $rw_commit->();
459         my $digits = '10010260936330';
460         my $ua = 'Pine.LNX.4.10';
461         my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
462         is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
463                 'no results yet');
464         my $pine = PublicInbox::Eml->new(<<EOF);
465 Subject: blah
466 Message-ID: <$mid>
467 From: torvalds\@transmeta
468 To: list\@example.com
469
470 EOF
471         my $x = $rw->add_message($pine);
472         $rw->commit_txn_lazy;
473         is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
474                 'searching only digit yielded result');
475
476         my $wild = $digits;
477         for my $i (1..6) {
478                 chop($wild);
479                 is($ro->query("m:$wild*", { mset => 1})->size, 1,
480                         "searching chopped($i) digit yielded result $wild ");
481         }
482         is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
483 });
484
485 { # List-Id searching
486         my $found = $ro->query('lid:i.m.just.bored');
487         is_deeply([ filter_mids($found) ], [ 'root@s' ],
488                 'got expected mid on exact lid: search');
489
490         $found = $ro->query('lid:just.bored');
491         is_deeply($found, [], 'got nothing on lid: search');
492
493         $found = $ro->query('lid:*.just.bored');
494         is_deeply($found, [], 'got nothing on lid: search');
495
496         $found = $ro->query('l:i.m.just.bored');
497         is_deeply([ filter_mids($found) ], [ 'root@s' ],
498                 'probabilistic search works on full List-Id contents');
499
500         $found = $ro->query('l:just.bored');
501         is_deeply([ filter_mids($found) ], [ 'root@s' ],
502                 'probabilistic search works on partial List-Id contents');
503
504         $found = $ro->query('lid:mad');
505         is_deeply($found, [], 'no match on phrase with lid:');
506
507         $found = $ro->query('lid:bored');
508         is_deeply($found, [], 'no match on partial List-Id with lid:');
509
510         $found = $ro->query('l:nothing');
511         is_deeply($found, [], 'matched on phrase with l:');
512 }
513
514 $ibx->with_umask(sub {
515         $rw_commit->();
516         my $doc_id = $rw->add_message(eml_load('t/data/message_embed.eml'));
517         ok($doc_id > 0, 'messages within messages');
518         $rw->commit_txn_lazy;
519         $ro->reopen;
520         my $n_test_eml = $ro->query('n:test.eml');
521         is(scalar(@$n_test_eml), 1, 'got a result');
522         my $n_embed2x_eml = $ro->query('n:embed2x.eml');
523         is_deeply($n_test_eml, $n_embed2x_eml, '.eml filenames searchable');
524         for my $m (qw(20200418222508.GA13918@dcvr 20200418222020.GA2745@dcvr
525                         20200418214114.7575-1-e@yhbt.net)) {
526                 is($ro->query("m:$m")->[0]->{mid},
527                         '20200418222508.GA13918@dcvr', 'probabilistic m:'.$m);
528                 is($ro->query("mid:$m")->[0]->{mid},
529                         '20200418222508.GA13918@dcvr', 'boolean mid:'.$m);
530         }
531         is($ro->query('dfpost:4dc62c50')->[0]->{mid},
532                 '20200418222508.GA13918@dcvr',
533                 'diff search reaches inside message/rfc822');
534         is($ro->query('s:"mail header experiments"')->[0]->{mid},
535                 '20200418222508.GA13918@dcvr',
536                 'Subject search reaches inside message/rfc822');
537 });
538
539 done_testing();
540
541 1;