Debian Codegraph @

cassandane/Cassandane/Cyrus/CyrusDB.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2017 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::CyrusDB;
41 use strict;
42 use warnings;
43 use Data::Dumper;
44 use File::Copy;
45 use IO::File;
46
47 use lib '.';
48 use base qw(Cassandane::Cyrus::TestCase);
49 use Cassandane::Util::Log;
50 use Cassandane::Instance;
51
52 use lib '../perl/imap';
53 use Cyrus::DList;
54 use Cyrus::HeaderFile;
55
56 sub new
57 {
58 my $class = shift;
59 return $class->SUPER::new({ start_instances => 0 }, @_);
60 }
61
62 sub set_up
63 {
64 my ($self) = @_;
65 $self->SUPER::set_up();
66 }
67
68 sub tear_down
69 {
70 my ($self) = @_;
71 $self->SUPER::tear_down();
72 }
73
74 # Some databases aren't created automatically during a minimal
75 # startup on a new install, so run some commands such that they
76 # become extant.
77 sub _force_db_creations
78 {
79 my ($self) = @_;
80
81 # nothing currently required here!
82 }
83
84 sub test_alternate_quotadb_path
85 {
86 my ($self) = @_;
87
88 my $quota_db_path = $self->{instance}->get_basedir()
89 . '/conf/non-default-quotas.db';
90
91 $self->{instance}->{config}->set(quota_db => 'twoskip');
92 $self->{instance}->{config}->set(quota_db_path => $quota_db_path);
93 $self->{instance}->start();
94
95 $self->_force_db_creations();
96
97 # Check that ctl_cyrusdb -c (checkpoint) uses correct db filename.
98 # If it mistakenly tries to use the default filename, it will error
99 # out due to it not existing.
100 eval {
101 $self->{instance}->run_command({
102 cyrus => 1,
103 }, 'ctl_cyrusdb', '-c');
104 };
105 $self->assert(not $@);
106
107 # TODO more/better checks
108 }
109
110 sub test_mboxlistdb_skiplist
111 {
112 my ($self) = @_;
113
114 $self->{instance}->{config}->set(mboxlist_db => 'skiplist');
115 $self->{instance}->start();
116
117 # 'ctl_cyrusdb -r' will run on startup, and it should not crash!
118 }
119
120 sub test_recover_uniqueid_from_header_legacymb
121 :min_version_3_6 :MailboxLegacyDirs
122 {
123 my ($self) = @_;
124 my $entry = '/shared/vendor/cmu/cyrus-imapd/uniqueid';
125
126 # first start will set up cassandane user
127 $self->_start_instances();
128 my $basedir = $self->{instance}->get_basedir();
129 my $mailboxes_db = "$basedir/conf/mailboxes.db";
130 $self->assert(-f $mailboxes_db, "$mailboxes_db not present");
131
132 # find out the uniqueid of the inbox
133 my $imaptalk = $self->{store}->get_client();
134 my $res = $imaptalk->getmetadata("INBOX", $entry);
135 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
136 $self->assert_not_null($res);
137 my $uniqueid = $res->{INBOX}{$entry};
138 xlog "XXX got uniqueid: " . Dumper \$uniqueid;
139 $self->assert_not_null($uniqueid);
140 $imaptalk->logout();
141 undef $imaptalk;
142
143 # stop service while tinkering
144 $self->{instance}->stop();
145 $self->{instance}->{re_use_dir} = 1;
146
147 # lose that uniqueid from mailboxes.db
148 my $I = "I$uniqueid";
149 my $N = "Nuser\x1fcassandane";
150 $self->{instance}->run_dbcommand($mailboxes_db, "twoskip",
151 [ 'DELETE', $I ]);
152 my (undef, $mbentry) = $self->{instance}->run_dbcommand(
153 $mailboxes_db, "twoskip",
154 ['SHOW', $N]);
155 my $dlist = Cyrus::DList->parse_string($mbentry);
156 my $hash = $dlist->as_perl();
157 $self->assert_str_equals($uniqueid, $hash->{I});
158 $hash->{I} = undef;
159 $dlist = Cyrus::DList->new_perl('', $hash);
160 $self->{instance}->run_dbcommand(
161 $mailboxes_db, "twoskip",
162 [ 'SET', $N, $dlist->as_string() ]);
163
164 my %updated = $self->{instance}->run_dbcommand(
165 $mailboxes_db, "twoskip", ['SHOW']);
166 xlog "updated mailboxes.db: " . Dumper \%updated;
167
168 # bring service back up
169 # ctl_cyrusdb -r should find and fix the missing uniqueid
170 $self->{instance}->getsyslog();
171 $self->{instance}->start();
172 if ($self->{instance}->{have_syslog_replacement}) {
173 my $syslog = join(q{}, $self->{instance}->getsyslog());
174
175 # should have still existed in cyrus.header
176 $self->assert_does_not_match(
177 qr{mailbox header had no uniqueid, creating one}, $syslog);
178
179 # expect to find the log line
180 $self->assert_matches(qr{mbentry had no uniqueid, setting from header},
181 $syslog);
182 }
183
184 # header should have the same uniqueid as before
185 my $cyrus_header = $self->{instance}->folder_to_directory('INBOX')
186 . '/cyrus.header';
187 $self->assert(-f $cyrus_header, "couldn't find cyrus.header file");
188 my $hf = Cyrus::HeaderFile->new_file($cyrus_header);
189 $self->assert_str_equals($uniqueid, $hf->{header}->{UniqueId});
190
191 # mbentry should have the same uniqueid as before
192 (undef, $mbentry) = $self->{instance}->run_dbcommand(
193 $mailboxes_db, "twoskip",
194 ['SHOW', $N]);
195 $dlist = Cyrus::DList->parse_string($mbentry);
196 $hash = $dlist->as_perl();
197 $self->assert_str_equals($uniqueid, $hash->{I});
198
199 # $I entry should be back
200 my ($key, $value) = $self->{instance}->run_dbcommand(
201 $mailboxes_db, "twoskip",
202 ['SHOW', $I]);
203 $self->assert_str_equals($I, $key);
204 $dlist = Cyrus::DList->parse_string($value);
205 $hash = $dlist->as_perl();
206 $self->assert_str_equals("user\x1fcassandane", $hash->{N});
207 }
208
209 sub test_recover_create_missing_uniqueid_legacymb
210 :min_version_3_6 :MailboxLegacyDirs
211 {
212 my ($self) = @_;
213 my $entry = '/shared/vendor/cmu/cyrus-imapd/uniqueid';
214
215 # first start will set up cassandane user
216 $self->_start_instances();
217 my $basedir = $self->{instance}->get_basedir();
218 my $mailboxes_db = "$basedir/conf/mailboxes.db";
219 $self->assert(-f $mailboxes_db, "$mailboxes_db not present");
220
221 # find out the uniqueid of the inbox
222 my $imaptalk = $self->{store}->get_client();
223 my $res = $imaptalk->getmetadata("INBOX", $entry);
224 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
225 $self->assert_not_null($res);
226 my $uniqueid = $res->{INBOX}{$entry};
227 $self->assert_not_null($uniqueid);
228 $imaptalk->logout();
229 undef $imaptalk;
230
231 # stop service while tinkering
232 $self->{instance}->stop();
233 $self->{instance}->{re_use_dir} = 1;
234
235 # lose that uniqueid from mailboxes.db
236 my $I = "I$uniqueid";
237 my $N = "Nuser\x1fcassandane";
238 $self->{instance}->run_dbcommand($mailboxes_db, "twoskip",
239 [ 'DELETE', $I ]);
240 my (undef, $mbentry) = $self->{instance}->run_dbcommand(
241 $mailboxes_db, "twoskip",
242 ['SHOW', $N]);
243 my $dlist = Cyrus::DList->parse_string($mbentry);
244 my $hash = $dlist->as_perl();
245 $self->assert_str_equals($uniqueid, $hash->{I});
246 $hash->{I} = undef;
247 $dlist = Cyrus::DList->new_perl('', $hash);
248 $self->{instance}->run_dbcommand(
249 $mailboxes_db, "twoskip",
250 [ 'SET', $N, $dlist->as_string() ]);
251
252 my %updated = $self->{instance}->run_dbcommand(
253 $mailboxes_db, "twoskip", ['SHOW']);
254 xlog "updated mailboxes.db: " . Dumper \%updated;
255
256 # lose it from cyrus.header too
257 my $cyrus_header = $self->{instance}->folder_to_directory('INBOX')
258 . '/cyrus.header';
259 $self->assert(-f $cyrus_header, "couldn't find cyrus.header file");
260 copy($cyrus_header, "$cyrus_header.OLD");
261 my $hf = Cyrus::HeaderFile->new_file("$cyrus_header.OLD");
262 $self->assert_str_equals($uniqueid, $hf->{header}->{UniqueId});
263 $hf->{header}->{UniqueId} = undef;
264 my $out = IO::File->new($cyrus_header, 'w');
265 $hf->write_header($out, $hf->{header});
266
267 # bring service back up
268 # ctl_cyrusdb -r should find and fix the missing uniqueid
269 $self->{instance}->getsyslog();
270 $self->{instance}->start();
271 if ($self->{instance}->{have_syslog_replacement}) {
272 my $syslog = join(q{}, $self->{instance}->getsyslog());
273
274 # expect to find it was missing in the header
275 $self->assert_matches(qr{mailbox header had no uniqueid, creating one},
276 $syslog);
277
278 # expect to find it was missing from mbentry
279 $self->assert_matches(qr{mbentry had no uniqueid, setting from header},
280 $syslog);
281 }
282
283 # should not be the same uniqueid as before
284 $imaptalk = $self->{store}->get_client();
285 $res = $imaptalk->getmetadata("INBOX", $entry);
286 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
287 $self->assert_not_null($res);
288 $self->assert_not_null($res->{INBOX}{$entry});
289 my $newuniqueid = $res->{INBOX}{$entry};
290 $self->assert_str_not_equals($uniqueid, $newuniqueid);
291
292 # header file should have the new uniqueid
293 $cyrus_header = $self->{instance}->folder_to_directory('INBOX')
294 . '/cyrus.header';
295 $self->assert(-f $cyrus_header, "couldn't find cyrus.header file");
296 $hf = Cyrus::HeaderFile->new_file($cyrus_header);
297 $self->assert_str_equals($newuniqueid, $hf->{header}->{UniqueId});
298
299 # mbentry should have the new uniqueid
300 (undef, $mbentry) = $self->{instance}->run_dbcommand(
301 $mailboxes_db, "twoskip",
302 ['SHOW', $N]);
303 $dlist = Cyrus::DList->parse_string($mbentry);
304 $hash = $dlist->as_perl();
305 $self->assert_str_equals($newuniqueid, $hash->{I});
306
307 # new runq entry should exist
308 my $newI = "I$newuniqueid";
309 my ($key, $value) = $self->{instance}->run_dbcommand(
310 $mailboxes_db, "twoskip",
311 ['SHOW', $newI]);
312 $self->assert_str_equals($newI, $key);
313 $dlist = Cyrus::DList->parse_string($value);
314 $hash = $dlist->as_perl();
315 $self->assert_str_equals("user\x1fcassandane", $hash->{N});
316 }
317
318 sub test_recover_skipstamp
319 {
320 my ($self) = @_;
321
322 my $dbdir = $self->{instance}->get_basedir() . "/conf/db";
323
324 # no 'ctl_cyrusdb -r' on startup
325 $self->{instance}->remove_start('recover');
326 $self->{instance}->start();
327
328 # expect skipstamp file to be missing
329 $self->assert_not_file_test("$dbdir/skipstamp", '-e');
330
331 # cyrus processes will whinge about missing skipstamp file
332 if ($self->{instance}->{have_syslog_replacement}) {
333 my $syslog = join "\n", $self->{instance}->getsyslog();
334 $self->assert_matches(qr/skipstamp is missing/, $syslog);
335 $self->assert_matches(qr/DBERROR: skipstamp/, $syslog);
336 }
337
338 # shut down, enable recover, and restart
339 $self->{instance}->stop();
340 # n.b. no "re_use_dir" here, because we need cyrus.conf regenerated
341 $self->{instance}->add_recover();
342 $self->{instance}->start();
343
344 # skipstamp file should be present now
345 $self->assert_file_test("$dbdir/skipstamp", '-e');
346
347 if ($self->{instance}->{have_syslog_replacement}) {
348 my $syslog = join "\n", $self->{instance}->getsyslog();
349
350 # recover should have logged itself updating skipstamp
351 $self->assert_matches(qr/updating recovery stamp/, $syslog);
352
353 # cyrus processes should not whinge about missing skipstamp file
354 $self->assert_does_not_match(qr/skipstamp is missing/, $syslog);
355 $self->assert_does_not_match(qr/DBERROR: skipstamp/, $syslog);
356 }
357 }
358
359 sub create_empty_file
360 {
361 my ($fname) = @_;
362
363 open my $fh, '>', $fname
364 or die "create_empty_file($fname): $!";
365 close $fh;
366 }
367
368 sub test_recover_skipcleanshutdown
369 {
370 my ($self) = @_;
371
372 my $dbdir = $self->{instance}->get_basedir() . "/conf/db";
373
374 # need to start up once to create a reusable basedir
375 $self->{instance}->start();
376 $self->{instance}->stop();
377 $self->{instance}->{re_use_dir} = 1;
378
379 # act like we were previously shut down cleanly by some rc script,
380 # but without a skipstamp somehow
381 create_empty_file("$dbdir/skipcleanshutdown");
382 unlink "$dbdir/skipstamp";
383 $self->assert_not_file_test("$dbdir/skipstamp", '-e');
384
385 # start 'er up
386 $self->{instance}->start();
387
388 # recover should have created a skipstamp file, despite skipcleanshutdown
389 $self->assert_file_test("$dbdir/skipstamp", '-e');
390 my $prev_skipstamp_mtime = (stat "$dbdir/skipstamp")[9];
391
392 # and skipcleanshutdown should have been removed
393 $self->assert_not_file_test("$dbdir/skipcleanshutdown", '-e');
394
395 if ($self->{instance}->{have_syslog_replacement}) {
396 my $syslog = join "\n", $self->{instance}->getsyslog();
397
398 # recover should not claim this was a normal start
399 $self->assert_does_not_match(qr/starting normally/, $syslog);
400
401 # recover should have logged itself updating skipstamp
402 $self->assert_matches(qr/updating recovery stamp/, $syslog);
403
404 # cyrus processes should not whinge about missing skipstamp file
405 $self->assert_does_not_match(qr/skipstamp is missing/, $syslog);
406 $self->assert_does_not_match(qr/DBERROR: skipstamp/, $syslog);
407 }
408
409 # shut down "cleanly" again, but this time leaving skipstamp alone
410 $self->{instance}->stop();
411 $self->{instance}->{re_use_dir} = 1;
412 create_empty_file("$dbdir/skipcleanshutdown");
413
414 # restart
415 $self->{instance}->start();
416
417 # skipstamp file should be present and unmodified since previous run
418 $self->assert_file_test("$dbdir/skipstamp", '-e');
419 my $skipstamp_mtime = (stat "$dbdir/skipstamp")[9];
420 $self->assert_num_equals($prev_skipstamp_mtime, $skipstamp_mtime);
421
422 # and skipcleanshutdown should have been removed
423 $self->assert_not_file_test("$dbdir/skipcleanshutdown", '-e');
424
425 if ($self->{instance}->{have_syslog_replacement}) {
426 my $syslog = join "\n", $self->{instance}->getsyslog();
427
428 # recover should claim this was a normal start
429 $self->assert_matches(qr/starting normally/, $syslog);
430
431 # recover should not have updated skipstamp
432 $self->assert_does_not_match(qr/updating recovery stamp/, $syslog);
433
434 # cyrus processes should not whinge about missing skipstamp file
435 $self->assert_does_not_match(qr/skipstamp is missing/, $syslog);
436 $self->assert_does_not_match(qr/DBERROR: skipstamp/, $syslog);
437 }
438 }
439
440 1;