]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
searchidx: introduce "xref3" concept
[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 $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         $ibx->search->reopen;
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 my $query = sub {
107         my ($query_string, $opt) = @_;
108         my $mset = $ibx->search->mset($query_string, $opt);
109         $ibx->search->mset_to_smsg($ibx, $mset);
110 };
111
112 {
113         $rw_commit->();
114         my $found = $query->('m:root@s');
115         is(scalar(@$found), 1, "message found");
116         is($found->[0]->{mid}, 'root@s', 'mid set correctly') if @$found;
117
118         my ($res, @res);
119         my @exp = sort qw(root@s last@s);
120
121         $res = $query->('s:(Hello world)');
122         @res = filter_mids($res);
123         is_deeply(\@res, \@exp, 'got expected results for s:() match');
124
125         $res = $query->('s:"Hello world"');
126         @res = filter_mids($res);
127         is_deeply(\@res, \@exp, 'got expected results for s:"" match');
128
129         $res = $query->('s:"Hello world"', {limit => 1});
130         is(scalar @$res, 1, "limit works");
131         my $first = $res->[0];
132
133         $res = $query->('s:"Hello world"', {offset => 1});
134         is(scalar @$res, 1, "offset works");
135         my $second = $res->[0];
136
137         isnt($first, $second, "offset returned different result from limit");
138 }
139
140 # ghost vivication
141 $ibx->with_umask(sub {
142         $rw_commit->();
143         my $rmid = '<ghost-message@s>';
144         my $reply_to_ghost = PublicInbox::Eml->new(<<"EOF");
145 Date: Sat, 02 Oct 2010 00:00:00 +0000
146 Subject: Re: ghosts
147 Message-ID: <ghost-reply\@s>
148 In-Reply-To: $rmid
149 From: Time Traveler <tt\@example.com>
150 To: list\@example.com
151
152 -_-
153 EOF
154         my $rv;
155         my $reply_id = $rw->add_message($reply_to_ghost);
156         is($reply_id, int($reply_id), "reply_id is an integer: $reply_id");
157
158         my $was_ghost = PublicInbox::Eml->new(<<"EOF");
159 Date: Sat, 02 Oct 2010 00:00:01 +0000
160 Subject: ghosts
161 Message-ID: $rmid
162 From: Laggy Sender <lag\@example.com>
163 To: list\@example.com
164
165 are real
166 EOF
167         my $ghost_id = $rw->add_message($was_ghost);
168         is($ghost_id, int($ghost_id), "ghost_id is an integer: $ghost_id");
169         my $msgs = $rw->{oidx}->get_thread('ghost-message@s');
170         is(scalar(@$msgs), 2, 'got both messages in ghost thread');
171         foreach (qw(sid tid)) {
172                 is($msgs->[0]->{$_}, $msgs->[1]->{$_}, "{$_} match");
173         }
174         isnt($msgs->[0]->{num}, $msgs->[1]->{num}, "num do not match");
175         ok($_->{num} > 0, 'positive art num') foreach @$msgs
176 });
177
178 # search thread on ghost
179 {
180         $rw_commit->();
181
182         # subject
183         my $res = $query->('ghost');
184         my @exp = sort qw(ghost-message@s ghost-reply@s);
185         my @res = filter_mids($res);
186         is_deeply(\@res, \@exp, 'got expected results for Subject match');
187
188         # body
189         $res = $query->('goodbye');
190         is(scalar(@$res), 1, "goodbye message found");
191         is($res->[0]->{mid}, 'last@s', 'got goodbye message body') if @$res;
192
193         # datestamp
194         $res = $query->('dt:20101002000001..20101002000001');
195         @res = filter_mids($res);
196         is_deeply(\@res, ['ghost-message@s'], 'exact Date: match works');
197         $res = $query->('dt:20101002000002..20101002000002');
198         is_deeply($res, [], 'exact Date: match down to the second');
199 }
200
201 # long message-id
202 $ibx->with_umask(sub {
203         $rw_commit->();
204         my $long_mid = 'last' . ('x' x 60). '@s';
205         my $long = PublicInbox::Eml->new(<<EOF);
206 Date: Sat, 02 Oct 2010 00:00:00 +0000
207 Subject: long message ID
208 References: <root\@s> <last\@s>
209 In-Reply-To: <last\@s>
210 Message-ID: <$long_mid>,
211 From: "Long I.D." <long-id\@example.com>
212 To: list\@example.com
213
214 wut
215 EOF
216         my $long_id = $rw->add_message($long);
217         is($long_id, int($long_id), "long_id is an integer: $long_id");
218
219         $rw_commit->();
220         my $res;
221         my @res;
222
223         my $long_reply_mid = 'reply-to-long@1';
224         my $long_reply = PublicInbox::Eml->new(<<EOF);
225 Subject: I break references
226 Date: Sat, 02 Oct 2010 00:00:00 +0000
227 Message-ID: <$long_reply_mid>
228 In-Reply-To: <$long_mid>
229 From: no1 <no1\@example.com>
230 To: list\@example.com
231
232 no References
233 EOF
234         ok($rw->add_message($long_reply) > $long_id, "inserted long reply");
235
236         $rw_commit->();
237         my $t = $ibx->over->get_thread('root@s');
238         is(scalar(@$t), 4, "got all 4 messages in thread");
239         my @exp = sort($long_reply_mid, 'root@s', 'last@s', $long_mid);
240         @res = filter_mids($t);
241         is_deeply(\@res, \@exp, "get_thread works");
242 });
243
244 # quote prioritization
245 $ibx->with_umask(sub {
246         $rw_commit->();
247         $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
248 Date: Sat, 02 Oct 2010 00:00:01 +0000
249 Subject: Hello
250 Message-ID: <quote@a>
251 From: Quoter <quoter@example.com>
252 To: list@example.com
253
254 > theatre illusions
255 fade
256 EOF
257         $rw->add_message(PublicInbox::Eml->new(<<'EOF'));
258 Date: Sat, 02 Oct 2010 00:00:02 +0000
259 Subject: Hello
260 Message-ID: <nquote@a>
261 From: Non-Quoter<non-quoter@example.com>
262 To: list@example.com
263
264 theatre
265 fade
266 EOF
267         $rw_commit->();
268         my $res = $query->("theatre");
269         is(scalar(@$res), 2, "got both matches");
270         if (@$res == 2) {
271                 is($res->[0]->{mid}, 'nquote@a', 'non-quoted scores higher');
272                 is($res->[1]->{mid}, 'quote@a', 'quoted result still returned');
273         }
274         $res = $query->("illusions");
275         is(scalar(@$res), 1, "got a match for quoted text");
276         is($res->[0]->{mid}, 'quote@a',
277                 "quoted result returned if nothing else") if scalar(@$res);
278 });
279
280 # circular references
281 $ibx->with_umask(sub {
282         my $s = 'foo://'. ('Circle' x 15).'/foo';
283         my $doc_id = $rw->add_message(PublicInbox::Eml->new(<<EOF));
284 Subject: $s
285 Date: Sat, 02 Oct 2010 00:00:01 +0000
286 Message-ID: <circle\@a>
287 References: <circle\@a>
288 In-Reply-To: <circle\@a>
289 From: Circle <circle\@example.com>
290 To: list\@example.com
291
292 LOOP!
293 EOF
294         ok($doc_id > 0, "doc_id defined with circular reference");
295         $rw_commit->();
296         my $smsg = $query->('m:circle@a', {limit=>1})->[0];
297         is(defined($smsg), 1, 'found m:circl@a');
298         if (defined $smsg) {
299                 is($smsg->{references}, '', "no references created");
300                 is($smsg->{subject}, $s, 'long subject not rewritten');
301         }
302 });
303
304 {
305         my $msgs = $query->('d:19931002..20101002');
306         ok(scalar(@$msgs) > 0, 'got results within range');
307         $msgs = $query->('d:20101003..');
308         is(scalar(@$msgs), 0, 'nothing after 20101003');
309         $msgs = $query->('d:..19931001');
310         is(scalar(@$msgs), 0, 'nothing before 19931001');
311 }
312
313 $ibx->with_umask(sub {
314         my $mime = eml_load 't/utf8.eml';
315         my $doc_id = $rw->add_message($mime);
316         ok($doc_id > 0, 'message indexed doc_id with UTF-8');
317         $rw_commit->();
318         my $msg = $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 = $ibx->search->mset('t:list@example.com');
329         is($mset->size, 9, 'searched To: successfully');
330         foreach my $m ($mset->items) {
331                 my $smsg = $ibx->over->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::SearchIdx::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::SearchIdx::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                 # check ->xref3 for external index:
346                 is_deeply($smsg->xref3($doc), [], 'xref3 empty by default');
347                 my $exp = "inbox.com.example:$uid:deadbeef";
348                 $doc->add_boolean_term('P'.$exp);
349                 is_deeply($smsg->xref3($doc), [ $exp ], 'xref3 can be set');
350                 $doc->remove_term('P'.$exp);
351                 is_deeply($smsg->xref3($doc), [], 'xref3 can be unset');
352         }
353
354         $mset = $ibx->search->mset('tc:list@example.com');
355         is($mset->size, 9, 'searched To+Cc: successfully');
356         foreach my $m ($mset->items) {
357                 my $smsg = $ibx->over->get_art($m->get_docid);
358                 my $tocc = join("\n", $smsg->{to}, $smsg->{cc});
359                 like($tocc, qr/\blist\@example\.com\b/, 'tocc appears');
360         }
361
362         foreach my $pfx ('tcf:', 'c:') {
363                 my $mset = $ibx->search->mset($pfx . 'foo@example.com');
364                 is($mset->items, 1, "searched $pfx successfully for Cc:");
365                 foreach my $m ($mset->items) {
366                         my $smsg = $ibx->over->get_art($m->get_docid);
367                         like($smsg->{cc}, qr/\bfoo\@example\.com\b/,
368                                 'cc appears');
369                 }
370         }
371
372         foreach my $pfx ('', 'tcf:', 'f:') {
373                 my $res = $query->($pfx . 'Laggy');
374                 is(scalar(@$res), 1,
375                         "searched $pfx successfully for From:");
376                 foreach my $smsg (@$res) {
377                         like($smsg->{from_name}, qr/Laggy Sender/,
378                                 "From appears with $pfx");
379                 }
380         }
381 }
382
383 {
384         $rw_commit->();
385         my $res = $query->('b:hello');
386         is(scalar(@$res), 0, 'no match on body search only');
387         $res = $query->('bs:smith');
388         is(scalar(@$res), 0,
389                 'no match on body+subject search for From');
390
391         $res = $query->('q:theatre');
392         is(scalar(@$res), 1, 'only one quoted body');
393         like($res->[0]->{from_name}, qr/\AQuoter/,
394                 'got quoted body') if (scalar(@$res));
395
396         $res = $query->('nq:theatre');
397         is(scalar @$res, 1, 'only one non-quoted body');
398         like($res->[0]->{from_name}, qr/\ANon-Quoter/,
399                 'got non-quoted body') if (scalar(@$res));
400
401         foreach my $pfx (qw(b: bs:)) {
402                 $res = $query->($pfx . 'theatre');
403                 is(scalar @$res, 2, "searched both bodies for $pfx");
404                 like($res->[0]->{from_name}, qr/\ANon-Quoter/,
405                         "non-quoter first for $pfx") if scalar(@$res);
406         }
407 }
408
409 $ibx->with_umask(sub {
410         my $amsg = eml_load 't/search-amsg.eml';
411         my $oid = ('0'x40);
412         my $smsg = bless { blob => $oid }, 'PublicInbox::Smsg';
413         ok($rw->add_message($amsg, $smsg), 'added attachment');
414         $rw_commit->();
415         my $n = $query->('n:attached_fart.txt');
416         is(scalar @$n, 1, 'got result for n:');
417         my $res = $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 = $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 = $ibx->over->next_by_mid($mid, \$id, \$prev);
431                 ok($art, 'article exists in OVER DB');
432         }
433         $rw->_msgmap_init;
434         $rw->unindex_eml($oid, $amsg);
435         $rw->commit_txn_lazy;
436         SKIP: {
437                 skip('$art not defined', 1) unless defined $art;
438                 is($ibx->over->get_art($art->{num}), undef,
439                         'gone from OVER DB');
440         };
441 });
442
443 my $all_mask = 07777;
444 my $dir_mask = 02770;
445
446 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
447 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
448 if ($^O =~ /(?:free|open)bsd/i) {
449         $all_mask = 0777;
450         $dir_mask = 0770;
451 }
452
453 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
454                 "$git_dir/public-inbox",
455                 glob("$git_dir/public-inbox/xapian*/"),
456                 glob("$git_dir/public-inbox/xapian*/*")) {
457         my @st = stat($f);
458         my ($bn) = (split(m!/!, $f))[-1];
459         oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
460                 "sharedRepository respected for $bn");
461 }
462
463 $ibx->with_umask(sub {
464         $rw_commit->();
465         my $digits = '10010260936330';
466         my $ua = 'Pine.LNX.4.10';
467         my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
468         is($ibx->search->mset("m:$digits")->size, 0, 'no results yet');
469         my $pine = PublicInbox::Eml->new(<<EOF);
470 Subject: blah
471 Message-ID: <$mid>
472 From: torvalds\@transmeta
473 To: list\@example.com
474
475 EOF
476         my $x = $rw->add_message($pine);
477         $rw->commit_txn_lazy;
478         $ibx->search->reopen;
479         is($ibx->search->mset("m:$digits")->size, 1,
480                 'searching only digit yielded result');
481
482         my $wild = $digits;
483         for my $i (1..6) {
484                 chop($wild);
485                 is($ibx->search->mset("m:$wild*")->size, 1,
486                         "searching chopped($i) digit yielded result $wild ");
487         }
488         is($ibx->search->mset('m:Pine m:LNX m:10010260936330')->size, 1);
489 });
490
491 { # List-Id searching
492         my $found = $query->('lid:i.m.just.bored');
493         is_deeply([ filter_mids($found) ], [ 'root@s' ],
494                 'got expected mid on exact lid: search');
495
496         $found = $query->('lid:just.bored');
497         is_deeply($found, [], 'got nothing on lid: search');
498
499         $found = $query->('lid:*.just.bored');
500         is_deeply($found, [], 'got nothing on lid: search');
501
502         $found = $query->('l:i.m.just.bored');
503         is_deeply([ filter_mids($found) ], [ 'root@s' ],
504                 'probabilistic search works on full List-Id contents');
505
506         $found = $query->('l:just.bored');
507         is_deeply([ filter_mids($found) ], [ 'root@s' ],
508                 'probabilistic search works on partial List-Id contents');
509
510         $found = $query->('lid:mad');
511         is_deeply($found, [], 'no match on phrase with lid:');
512
513         $found = $query->('lid:bored');
514         is_deeply($found, [], 'no match on partial List-Id with lid:');
515
516         $found = $query->('l:nothing');
517         is_deeply($found, [], 'matched on phrase with l:');
518 }
519
520 $ibx->with_umask(sub {
521         $rw_commit->();
522         my $doc_id = $rw->add_message(eml_load('t/data/message_embed.eml'));
523         ok($doc_id > 0, 'messages within messages');
524
525         my $eml = PublicInbox::Eml->new(<<EOF);
526 List-Id: <blahblah.example.com>
527
528 EOF
529         $rw->add_xref3($doc_id, 1, 'deadbeef', 'newsgroup1.example', $eml);
530         $rw_commit->();
531         my $n_test_eml = $query->('n:test.eml');
532         is(scalar(@$n_test_eml), 1, 'got a result');
533         my $n_embed2x_eml = $query->('n:embed2x.eml');
534         is_deeply($n_test_eml, $n_embed2x_eml, '.eml filenames searchable');
535         for my $m (qw(20200418222508.GA13918@dcvr 20200418222020.GA2745@dcvr
536                         20200418214114.7575-1-e@yhbt.net)) {
537                 is($query->("m:$m")->[0]->{mid},
538                         '20200418222508.GA13918@dcvr', 'probabilistic m:'.$m);
539                 is($query->("mid:$m")->[0]->{mid},
540                         '20200418222508.GA13918@dcvr', 'boolean mid:'.$m);
541         }
542         is($query->('dfpost:4dc62c50')->[0]->{mid},
543                 '20200418222508.GA13918@dcvr',
544                 'diff search reaches inside message/rfc822');
545         is($query->('s:"mail header experiments"')->[0]->{mid},
546                 '20200418222508.GA13918@dcvr',
547                 'Subject search reaches inside message/rfc822');
548         is($query->('l:blahblah.example.com')->[0]->{num}, $doc_id,
549                 'xref3 List-Id probabilistic works');
550         is($query->('lid:blahblah.example.com')->[0]->{num}, $doc_id,
551                 'xref3 List-Id boolean term works');
552         $rw->remove_xref3($doc_id, 'deadbeef', 'newsgroup1.example', $eml);
553         $rw->commit_txn_lazy;
554         $ibx->search->reopen;
555         my $res = $query->('lid:blahblah.example.com');
556         is_deeply($res, [], '->remove_xref3 dropped boolean term');
557 });
558
559 done_testing();