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