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