]> Sergey Matveev's repositories - public-inbox.git/blob - t/search.t
testcommon: introduce mime_load sub
[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 Email::MIME));
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 $eml = 't/utf8.eml';
290         my $mime = PublicInbox::InboxWritable::mime_from_path($eml) or
291                 die "open $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', sub {
375         my $part1 = Email::MIME->create(
376                  attributes => {
377                      content_type => 'text/plain',
378                      disposition  => 'attachment',
379                      charset => 'US-ASCII',
380                      encoding => 'quoted-printable',
381                      filename => 'attached_fart.txt',
382                  },
383                  body_str => 'inside the attachment',
384         );
385         my $part2 = Email::MIME->create(
386                  attributes => {
387                      content_type => 'text/plain',
388                      disposition  => 'attachment',
389                      charset => 'US-ASCII',
390                      encoding => 'quoted-printable',
391                      filename => 'part_deux.txt',
392                  },
393                  body_str => 'inside another',
394         );
395         Email::MIME->create(
396                 header_str => [
397                         Subject => 'see attachment',
398                         'Message-ID' => '<file@attached>',
399                         From => 'John Smith <js@example.com>',
400                         To => 'list@example.com',
401                 ],
402                 parts => [ $part1, $part2 ],
403         )}; # mime_load sub
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->{over_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         SKIP: {
429                 skip('$art not defined', 1) unless defined $art;
430                 is($ro->{over_ro}->get_art($art->{num}), undef,
431                         'gone from OVER DB');
432         };
433 });
434
435 my $all_mask = 07777;
436 my $dir_mask = 02770;
437
438 # FreeBSD and apparently OpenBSD does not allow non-root users to set S_ISGID,
439 # so git doesn't set it, either (see DIR_HAS_BSD_GROUP_SEMANTICS in git.git)
440 if ($^O =~ /(?:free|open)bsd/i) {
441         $all_mask = 0777;
442         $dir_mask = 0770;
443 }
444
445 foreach my $f ("$git_dir/public-inbox/msgmap.sqlite3",
446                 "$git_dir/public-inbox",
447                 glob("$git_dir/public-inbox/xapian*/"),
448                 glob("$git_dir/public-inbox/xapian*/*")) {
449         my @st = stat($f);
450         my ($bn) = (split(m!/!, $f))[-1];
451         oct_is($st[2] & $all_mask, -f _ ? 0660 : $dir_mask,
452                 "sharedRepository respected for $bn");
453 }
454
455 $ibx->with_umask(sub {
456         $rw_commit->();
457         my $digits = '10010260936330';
458         my $ua = 'Pine.LNX.4.10';
459         my $mid = "$ua.$digits.2460-100000\@penguin.transmeta.com";
460         is($ro->reopen->query("m:$digits", { mset => 1})->size, 0,
461                 'no results yet');
462         my $pine = PublicInbox::MIME->new(<<EOF);
463 Subject: blah
464 Message-ID: <$mid>
465 From: torvalds\@transmeta
466 To: list\@example.com
467
468 EOF
469         my $x = $rw->add_message($pine);
470         $rw->commit_txn_lazy;
471         is($ro->reopen->query("m:$digits", { mset => 1})->size, 1,
472                 'searching only digit yielded result');
473
474         my $wild = $digits;
475         for my $i (1..6) {
476                 chop($wild);
477                 is($ro->query("m:$wild*", { mset => 1})->size, 1,
478                         "searching chopped($i) digit yielded result $wild ");
479         }
480         is($ro->query("m:Pine m:LNX m:10010260936330", {mset=>1})->size, 1);
481 });
482
483 done_testing();
484
485 1;