Debian Codegraph @

cassandane/Cassandane/Cyrus/SearchSquat.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2020 FastMail Pty Ltd. All rights reserved.
4 #
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions
7 # are met:
8 #
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 #
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in
14 # the documentation and/or other materials provided with the
15 # distribution.
16 #
17 # 3. The name "Fastmail Pty Ltd" must not be used to
18 # endorse or promote products derived from this software without
19 # prior written permission. For permission or any legal
20 # details, please contact
21 # FastMail Pty Ltd
22 # PO Box 234
23 # Collins St West 8007
24 # Victoria
25 # Australia
26 #
27 # 4. Redistributions of any form whatsoever must retain the following
28 # acknowledgment:
29 # "This product includes software developed by Fastmail Pty. Ltd."
30 #
31 # FASTMAIL PTY LTD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
32 # INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
33 # EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE FOR ANY SPECIAL, INDIRECT
34 # OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
35 # USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
36 # TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
37 # OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::SearchSquat;
41 use strict;
42 use warnings;
43 use Cwd qw(abs_path);
44 use DateTime;
45 use Data::Dumper;
46
47 use lib '.';
48 use base qw(Cassandane::Cyrus::TestCase);
49 use Cassandane::Util::Log;
50 use Cassandane::Util::Slurp;
51
52 sub new
53 {
54 my ($class, @args) = @_;
55 my $config = Cassandane::Config->default()->clone();
56 $config->set(conversations => 'on');
57 return $class->SUPER::new({ config => $config }, @args);
58 }
59
60 sub set_up
61 {
62 my ($self) = @_;
63 $self->SUPER::set_up();
64 }
65
66 sub tear_down
67 {
68 my ($self) = @_;
69 $self->SUPER::tear_down();
70 }
71
72 sub run_squatter
73 {
74 my ($self, @args) = @_;
75
76 my $outfname = $self->{instance}->{basedir} . "/squatter.out";
77 my $errfname = $self->{instance}->{basedir} . "/squatter.err";
78
79 $self->{instance}->run_command({
80 cyrus => 1,
81 redirects => {
82 stdout => $outfname,
83 stderr => $errfname,
84 },
85 },
86 'squatter',
87 @args
88 );
89
90 return (slurp_file($outfname), slurp_file($errfname));
91 }
92
93 # XXX version gated to 3.4+ for now to keep travis happy, but if we
94 # XXX backport the fix we should change or remove the gate...
95 sub test_simple
96 :SearchEngineSquat :min_version_3_4
97 {
98 my ($self) = @_;
99 my $imap = $self->{store}->get_client();
100
101 $self->make_message("term2", body => "term1") || die;
102 $self->make_message("term2", body => "term1") || die;
103 $self->make_message("term1", body => "term2") || die;
104 $self->make_message("term3", body => "term4") || die;
105
106 $self->{instance}->run_command({cyrus => 1}, 'squatter');
107
108 my @tests = ({
109 search => ['body', 'term1'],
110 wantUids => [1,2],
111 }, {
112 search => ['text', 'term1'],
113 wantUids => [1,2,3],
114 }, {
115 search => ['subject', 'term2'],
116 wantUids => [1,2],
117 }, {
118 search => ['subject', 'term3'],
119 wantUids => [4],
120 }, {
121 search => ['body', 'term4'],
122 wantUids => [4],
123 }, {
124 search => ['fuzzy', 'body', 'term4'],
125 wantUids => [4],
126 }, {
127 # we don't index content-type, make sure we actually didn't
128 search => ['from', 'text/plain'],
129 wantUids => [],
130 }, {
131 # we don't index content-type, make sure we actually didn't
132 search => ['to', 'text/plain'],
133 wantUids => [],
134 }, {
135 # we don't index content-type, make sure we actually didn't
136 search => ['subject', 'text/plain'],
137 wantUids => [],
138 });
139
140 foreach (@tests) {
141 $self->{instance}->getsyslog();
142
143 my $uids = $imap->search(@{$_->{search}}) || die;
144 $self->assert_deep_equals($_->{wantUids}, $uids);
145
146 $self->assert_syslog_matches($self->{instance}, qr{Squat run});
147 }
148 }
149
150 sub test_one_doc_per_message
151 :SearchEngineSquat :min_version_3_4
152 {
153 my ($self) = @_;
154 my $imap = $self->{store}->get_client();
155
156 # make some messages where the only indexed field is the body
157 foreach my $body (qw(term1 term1 term2 term4)) {
158 $self->make_message(undef,
159 from => undef,
160 to => undef,
161 body => $body) || die;
162 }
163
164 # make enough other messages such that an incremental reindex
165 # will need to realloc doc_ID_map
166 for (1..50) {
167 $self->make_message() || die;
168 }
169
170 $self->run_squatter();
171
172 my @tests = ({
173 search => ['body', 'term1'],
174 wantUids => [1,2],
175 }, {
176 search => ['body', 'term2'],
177 wantUids => [3],
178 }, {
179 search => ['body', 'term3'],
180 wantUids => [],
181 }, {
182 search => ['body', 'term4'],
183 wantUids => [4],
184 });
185
186 foreach (@tests) {
187 $self->{instance}->getsyslog();
188
189 my $uids = $imap->search(@{$_->{search}}) || die;
190 $self->assert_deep_equals($_->{wantUids}, $uids);
191
192 $self->assert_syslog_matches($self->{instance}, qr/Squat run/);
193 }
194
195 # make some more messages
196 foreach my $body (qw(term5 term6 term6 term8)) {
197 $self->make_message(undef,
198 from => undef,
199 to => undef,
200 body => $body) || die;
201 }
202
203 # incremental reindex
204 my (undef, $err) = $self->run_squatter('-i', '-v');
205 $self->assert_matches(qr{indexed 4 messages}, $err);
206
207 push @tests, {
208 search => ['body', 'term5'],
209 wantUids => [55],
210 }, {
211 search => ['body', 'term6'],
212 wantUids => [56, 57],
213 }, {
214 search => ['body', 'term7'],
215 wantUids => [],
216 }, {
217 search => ['body', 'term8'],
218 wantUids => [58],
219 };
220
221 # better not be any off-by-one errors in search results!
222 foreach (@tests) {
223 $self->{instance}->getsyslog();
224
225 my $uids = $imap->search(@{$_->{search}}) || die;
226 $self->assert_deep_equals($_->{wantUids}, $uids);
227
228 $self->assert_syslog_matches($self->{instance}, qr/Squat run/);
229 }
230 }
231
232 # XXX version gated to 3.4+ for now to keep travis happy, but if we
233 # XXX backport the fix we should change or remove the gate...
234 sub test_skip_unmodified
235 :SearchEngineSquat :min_version_3_4
236 {
237 my ($self) = @_;
238 my $imap = $self->{store}->get_client();
239
240 $self->make_message() || die;
241
242 sleep(1);
243
244 $self->{instance}->getsyslog();
245 $self->{instance}->run_command({cyrus => 1}, 'squatter');
246 $self->assert_syslog_does_not_match($self->{instance},
247 qr{Squat skipping mailbox});
248
249 $self->{instance}->getsyslog();
250 $self->{instance}->run_command({cyrus => 1}, 'squatter', '-v', '-s', '0');
251 $self->assert_syslog_matches($self->{instance},
252 qr{Squat skipping mailbox});
253 }
254
255 sub test_nonincremental
256 :SearchEngineSquat
257 {
258 my ($self) = @_;
259 my $imap = $self->{store}->get_client();
260 my $n = 0;
261
262 for (1..5) {
263 # make a new message
264 $self->make_message();
265 $n++;
266
267 # do a full reindex
268 my (undef, $err) = $self->run_squatter('-vv');
269
270 # better have indexed them all, not just the new one!
271 $self->assert_matches(qr{indexed $n messages}, $err);
272 }
273
274 # make a message with no subject, to, or from
275 $self->make_message(undef, to => undef, from => undef);
276 $n++;
277
278 # do a full reindex
279 my (undef, $err) = $self->run_squatter('-vv');
280
281 # better have indexed them all, not just the new one!
282 $self->assert_matches(qr{indexed $n messages}, $err);
283 }
284
285 sub test_incremental
286 :SearchEngineSquat
287 {
288 my ($self) = @_;
289 my $imap = $self->{store}->get_client();
290 my $err;
291
292 # some initial messages - enough to definitely force a doc_ID_map realloc
293 # when incrementally reindexing later
294 for (1..50) {
295 $self->make_message();
296 }
297
298 # make a message with no subject, to, or from
299 # this used to trigger an indexing bug and produce a corrupt index,
300 # which would lead to a crash during incremental reindex
301 my $weird = $self->make_message(undef, to => undef, from => undef);
302 xlog "weird message:\n" . $weird->as_string();
303
304 sleep(1);
305
306 # initial non-incremental index
307 (undef, $err) = $self->run_squatter('-vv');
308 $self->assert_matches(qr{indexed 51 messages}, $err);
309
310 # incremental reindex with no changes to mailbox
311 (undef, $err) = $self->run_squatter('-i', '-vv');
312 $self->assert_matches(qr{indexed 0 messages}, $err);
313
314 # delete, expunge, and cyr_expire some messages
315 # n.b. this does not unindex the message in any way
316 $imap->store('5', '+flags', '(\\Deleted)');
317 $self->assert_str_equals('ok', $imap->get_last_completion_response());
318 $imap->expunge();
319 $self->assert_str_equals('ok', $imap->get_last_completion_response());
320 $self->{instance}->run_command({cyrus => 1}, 'cyr_expire', '-X', '0');
321
322 # incremental reindex after one message expunged
323 (undef, $err) = $self->run_squatter('-i', '-vv');
324 $self->assert_matches(qr{indexed 0 messages}, $err);
325
326 # make one new message
327 for (1) {
328 $self->make_message();
329 }
330 sleep(1);
331
332 # incremental reindex after one new message
333 (undef, $err) = $self->run_squatter('-i', '-vv');
334 $self->assert_matches(qr{indexed 1 messages}, $err);
335
336 # incremental reindex with no changes to mailbox
337 (undef, $err) = $self->run_squatter('-i', '-vv');
338 $self->assert_matches(qr{indexed 0 messages}, $err);
339
340 # make some new messages
341 for (1..10) {
342 $self->make_message();
343 }
344 sleep(1);
345
346 # incremental reindex after new messages
347 (undef, $err) = $self->run_squatter('-i', '-vv');
348 $self->assert_matches(qr{indexed 10 messages}, $err);
349
350 # incremental reindex with no changes to mailbox
351 (undef, $err) = $self->run_squatter('-i', '-vv');
352 $self->assert_matches(qr{indexed 0 messages}, $err);
353 }
354
355 sub test_relocate_legacy_searchdb
356 :DelayedDelete :min_version_3_6 :MailboxLegacyDirs
357 :Admin :SearchEngineSquat :NoAltNamespace :VirtDomains
358 {
359 my ($self) = @_;
360
361 my $adminstore = $self->{adminstore};
362 my $admintalk = $adminstore->get_client();
363
364 my $inbox = "user.magicuser\@example.com";
365 my $subfolder = "user.magicuser.foo\@example.com";
366
367 $admintalk->create($inbox);
368 $admintalk->setacl($inbox, admin => 'lrswipkxtecdan');
369 $admintalk->create($subfolder);
370 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
371
372 $adminstore->set_folder($subfolder);
373 $self->make_message("Email", store => $adminstore) or die;
374
375 # Create the search database.
376 xlog $self, "Run squatter";
377 $self->{instance}->run_command({cyrus => 1}, 'squatter');
378
379 my $basedir = $self->{instance}{basedir};
380 open(FH, "-|", "find", $basedir);
381 my @files = grep { m{/magicuser/} and not m{/conf/lock/} } <FH>;
382 close(FH);
383
384 xlog $self, "files exist";
385 $self->assert_not_equals(0, scalar @files);
386
387 $self->{instance}->run_command({ cyrus => 1 }, 'relocate_by_id', '-u' => "magicuser\@example.com" );
388
389 open(FH, "-|", "find", $basedir);
390 @files = grep { m{/magicuser/} and not m{/conf/lock/} } <FH>;
391 close(FH);
392
393 xlog $self, "no files left for this user";
394 $self->assert_equals(0, scalar @files);
395 }
396
397 sub test_relocate_legacy_nosearchdb
398 :DelayedDelete :min_version_3_6 :MailboxLegacyDirs
399 :Admin :SearchEngineSquat :NoAltNamespace :VirtDomains
400 {
401 my ($self) = @_;
402
403 my $adminstore = $self->{adminstore};
404 my $admintalk = $adminstore->get_client();
405
406 my $inbox = "user.magicuser\@example.com";
407 my $subfolder = "user.magicuser.foo\@example.com";
408
409 $admintalk->create($inbox);
410 $admintalk->setacl($inbox, admin => 'lrswipkxtecdan');
411 $admintalk->create($subfolder);
412 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
413
414 $adminstore->set_folder($subfolder);
415 $self->make_message("Email", store => $adminstore) or die;
416
417 # Don't create the search database!
418 # A user who's never been indexed should still relocate cleanly
419
420 my $basedir = $self->{instance}{basedir};
421 open(FH, "-|", "find", $basedir);
422 my @files = grep { m{/magicuser/} and not m{/conf/lock/} } <FH>;
423 close(FH);
424
425 xlog $self, "files exist";
426 $self->assert_not_equals(0, scalar @files);
427
428 $self->{instance}->run_command({ cyrus => 1 }, 'relocate_by_id', '-u' => "magicuser\@example.com" );
429
430 open(FH, "-|", "find", $basedir);
431 @files = grep { m{/magicuser/} and not m{/conf/lock/} } <FH>;
432 close(FH);
433
434 xlog $self, "no files left for this user";
435 $self->assert_equals(0, scalar @files);
436 }
437
438 sub test_unindexed
439 :SearchEngineSquat :min_version_3_4
440 {
441 my ($self) = @_;
442 my $imap = $self->{store}->get_client();
443
444 $self->make_message("needle 1", body => "needle") || die;
445 $self->make_message("xxxxxx 2", body => "xxxxxx") || die;
446
447 $self->run_squatter;
448
449 my $uids = $imap->search('text', 'needle');
450 $self->assert_deep_equals([1], $uids);
451
452 $self->make_message("needle 3", body => "needle") || die;
453 $self->make_message("xxxxxx 4", body => "xxxxxx") || die;
454
455 # Do not rerun squatter. Make sure search only returns
456 # a matching unindexed message.
457
458 $uids = $imap->search('text', 'needle');
459 $self->assert_deep_equals([1,3], $uids);
460 }
461
462 sub test_unindexed_fuzzy
463 :SearchEngineSquat :min_version_3_4
464 {
465 my ($self) = @_;
466 my $imap = $self->{store}->get_client();
467
468 $self->make_message("needle 1", body => "needle") || die;
469 $self->make_message("xxxxxx 2", body => "xxxxxx") || die;
470
471 $self->run_squatter;
472
473 my $uids = $imap->search('fuzzy', 'body', 'needle');
474 $self->assert_deep_equals([1], $uids);
475
476 $self->make_message("needle 3", body => "needle") || die;
477 $self->make_message("xxxxxx 4", body => "xxxxxx") || die;
478
479 # Do not rerun squatter. Make sure search only returns
480 # a matching unindexed message.
481
482 $uids = $imap->search('fuzzy', 'body', 'needle');
483 $self->assert_deep_equals([1,3], $uids);
484 }
485
486 sub test_unindexed_since
487 :SearchEngineSquat :min_version_3_4
488 {
489 my ($self) = @_;
490 my $imap = $self->{store}->get_client();
491
492 my $past_dt = DateTime->last_day_of_month(year => 2023, month => 12);
493
494 $self->make_message("needle 1", body => "needle") || die;
495 $self->make_message("xxxxxx 2", body => "xxxxxx") || die;
496 $self->make_message("old 3", date => $past_dt, body => "needle") || die;
497
498 $self->run_squatter;
499
500 my $uids = $imap->search('text', 'needle', 'since', '1-Feb-2024');
501 $self->assert_deep_equals([1], $uids);
502
503 $uids = $imap->search('text', 'needle', 'not', 'since', '1-Feb-2024');
504 $self->assert_deep_equals([3], $uids);
505
506 $self->make_message("needle 4", body => "needle") || die;
507 $self->make_message("xxxxxx 5", body => "xxxxxx") || die;
508 $self->make_message("old 6", date => $past_dt, body => "needle") || die;
509
510 # Do not rerun squatter. Make sure search only returns
511 # a matching unindexed message.
512
513 $uids = $imap->search('text', 'needle', 'since', '1-Feb-2024');
514 $self->assert_deep_equals([1,4], $uids);
515
516 $uids = $imap->search('text', 'needle', 'not', 'since', '1-Feb-2024');
517 $self->assert_deep_equals([3, 6], $uids);
518 }
519
520 sub test_since
521 :SearchEngineSquat :min_version_3_4
522 {
523 my ($self) = @_;
524 my $imap = $self->{store}->get_client();
525
526 my $past_dt = DateTime->last_day_of_month(year => 2023, month => 12);
527
528 $self->make_message("needle 1", body => "needle") || die;
529 $self->make_message("xxxxxx 2", body => "xxxxxx") || die;
530 $self->make_message("old 3", date => $past_dt, body => "needle") || die;
531
532 $self->run_squatter;
533
534 my $uids = $imap->search('since', '1-Feb-2024');
535 $self->assert_deep_equals([1,2], $uids);
536
537 $uids = $imap->search('not', 'since', '1-Feb-2024');
538 $self->assert_deep_equals([3], $uids);
539
540 $self->make_message("needle 4", body => "needle") || die;
541 $self->make_message("xxxxxx 5", body => "xxxxxx") || die;
542 $self->make_message("old 6", date => $past_dt, body => "needle") || die;
543
544 # Do not rerun squatter. Make sure search only returns
545 # a matching unindexed message.
546
547 $uids = $imap->search('since', '1-Feb-2024');
548 $self->assert_deep_equals([1,2,4,5], $uids);
549
550 $uids = $imap->search('not', 'since', '1-Feb-2024');
551 $self->assert_deep_equals([3,6], $uids);
552 }
553
554 1;