| 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::Reconstruct; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use Data::Dumper; |
| 44 |
use File::Copy; |
| 45 |
use IO::File; |
| 46 |
use JSON; |
| 47 |
|
| 48 |
use lib '.'; |
| 49 |
use base qw(Cassandane::Cyrus::TestCase); |
| 50 |
use Cassandane::Util::Log; |
| 51 |
use Cassandane::Instance; |
| 52 |
|
| 53 |
use lib '../perl/imap'; |
| 54 |
use Cyrus::DList; |
| 55 |
use Cyrus::HeaderFile; |
| 56 |
use Cyrus::IndexFile; |
| 57 |
|
| 58 |
sub new |
| 59 |
{ |
| 60 |
my $class = shift; |
| 61 |
return $class->SUPER::new({ adminstore => 1 }, @_); |
| 62 |
} |
| 63 |
|
| 64 |
sub set_up |
| 65 |
{ |
| 66 |
my ($self) = @_; |
| 67 |
$self->SUPER::set_up(); |
| 68 |
} |
| 69 |
|
| 70 |
sub tear_down |
| 71 |
{ |
| 72 |
my ($self) = @_; |
| 73 |
$self->SUPER::tear_down(); |
| 74 |
} |
| 75 |
|
| 76 |
# |
| 77 |
# Test zeroed out data across the UID |
| 78 |
# |
| 79 |
sub test_reconstruct_zerouid |
| 80 |
{ |
| 81 |
my ($self) = @_; |
| 82 |
|
| 83 |
my $imaptalk = $self->{store}->get_client(); |
| 84 |
|
| 85 |
for (1..10) { |
| 86 |
my $msg = $self->{gen}->generate(subject => "subject $_"); |
| 87 |
$self->{store}->write_message($msg, flags => ["\\Seen", "\$NotJunk"]); |
| 88 |
} |
| 89 |
$self->{store}->write_end(); |
| 90 |
$imaptalk->select("INBOX") || die; |
| 91 |
|
| 92 |
my @records = $imaptalk->search("all"); |
| 93 |
$self->assert_num_equals(10, scalar @records); |
| 94 |
$self->assert(grep { $_ == 6 } @records); |
| 95 |
|
| 96 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct'); |
| 97 |
|
| 98 |
@records = $imaptalk->search("all"); |
| 99 |
$self->assert_num_equals(10, scalar @records); |
| 100 |
$self->assert(grep { $_ == 6 } @records); |
| 101 |
|
| 102 |
# this needs a bit of magic to know where to write... so |
| 103 |
# we do some hard-coded cyrus.index handling |
| 104 |
my $dir = $self->{instance}->folder_to_directory('user.cassandane'); |
| 105 |
my $file = "$dir/cyrus.index"; |
| 106 |
my $fh = IO::File->new($file, "+<"); |
| 107 |
die "NO SUCH FILE $file" unless $fh; |
| 108 |
my $index = Cyrus::IndexFile->new($fh); |
| 109 |
|
| 110 |
my $offset = $index->header('StartOffset') + (5 * $index->header('RecordSize')); |
| 111 |
warn "seeking to offset $offset"; |
| 112 |
$fh->seek($offset, 0); |
| 113 |
$fh->syswrite("\0\0\0\0\0\0\0\0", 8); |
| 114 |
$fh->close(); |
| 115 |
|
| 116 |
# this time, the reconstruct will fix up the broken record and re-insert later |
| 117 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct', 'user.cassandane'); |
| 118 |
|
| 119 |
@records = $imaptalk->search("all"); |
| 120 |
$self->assert_num_equals(10, scalar @records); |
| 121 |
$self->assert(not grep { $_ == 6 } @records); |
| 122 |
$self->assert(grep { $_ == 11 } @records); |
| 123 |
} |
| 124 |
|
| 125 |
# |
| 126 |
# Test truncated file |
| 127 |
# |
| 128 |
sub test_reconstruct_truncated |
| 129 |
{ |
| 130 |
my ($self) = @_; |
| 131 |
|
| 132 |
my $imaptalk = $self->{store}->get_client(); |
| 133 |
|
| 134 |
for (1..10) { |
| 135 |
my $msg = $self->{gen}->generate(subject => "subject $_"); |
| 136 |
$self->{store}->write_message($msg, flags => ["\\Seen", "\$NotJunk"]); |
| 137 |
} |
| 138 |
$self->{store}->write_end(); |
| 139 |
$imaptalk->select("INBOX") || die; |
| 140 |
|
| 141 |
my @records = $imaptalk->search("all"); |
| 142 |
$self->assert_num_equals(10, scalar @records); |
| 143 |
$self->assert(grep { $_ == 6 } @records); |
| 144 |
|
| 145 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct'); |
| 146 |
|
| 147 |
@records = $imaptalk->search("all"); |
| 148 |
$self->assert_num_equals(10, scalar @records); |
| 149 |
$self->assert(grep { $_ == 6 } @records); |
| 150 |
|
| 151 |
# this needs a bit of magic to know where to write... so |
| 152 |
# we do some hard-coded cyrus.index handling |
| 153 |
my $dir = $self->{instance}->folder_to_directory('user.cassandane'); |
| 154 |
my $file = "$dir/cyrus.index"; |
| 155 |
my $fh = IO::File->new($file, "+<"); |
| 156 |
die "NO SUCH FILE $file" unless $fh; |
| 157 |
my $index = Cyrus::IndexFile->new($fh); |
| 158 |
|
| 159 |
my $offset = $index->header('StartOffset') + (5 * $index->header('RecordSize')); |
| 160 |
$fh->truncate($offset); |
| 161 |
$fh->close(); |
| 162 |
|
| 163 |
# this time, the reconstruct will create the records again |
| 164 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct', 'user.cassandane'); |
| 165 |
|
| 166 |
# XXX - this actually deletes everything, so we unselect and reselect. A |
| 167 |
# too-short cyrus.index is a fatal error, so we don't even try to read it. |
| 168 |
$imaptalk->unselect(); |
| 169 |
$imaptalk->select("INBOX") || die; |
| 170 |
|
| 171 |
@records = $imaptalk->search("all"); |
| 172 |
$self->assert_num_equals(10, scalar @records); |
| 173 |
$self->assert(grep { $_ == 6 } @records); |
| 174 |
$self->assert(not grep { $_ == 11 } @records); |
| 175 |
|
| 176 |
# We should have generated a SYNCERROR or two |
| 177 |
$self->assert_syslog_matches($self->{instance}, |
| 178 |
qr/IOERROR: refreshing index/); |
| 179 |
} |
| 180 |
# |
| 181 |
# Test removed file |
| 182 |
# |
| 183 |
sub test_reconstruct_removedfile |
| 184 |
{ |
| 185 |
my ($self) = @_; |
| 186 |
|
| 187 |
my $imaptalk = $self->{store}->get_client(); |
| 188 |
|
| 189 |
for (1..10) { |
| 190 |
my $msg = $self->{gen}->generate(subject => "subject $_"); |
| 191 |
$self->{store}->write_message($msg, flags => ["\\Seen", "\$NotJunk"]); |
| 192 |
} |
| 193 |
$self->{store}->write_end(); |
| 194 |
$imaptalk->select("INBOX") || die; |
| 195 |
|
| 196 |
my @records = $imaptalk->search("all"); |
| 197 |
$self->assert_num_equals(10, scalar @records); |
| 198 |
$self->assert(grep { $_ == 6 } @records); |
| 199 |
|
| 200 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct'); |
| 201 |
|
| 202 |
@records = $imaptalk->search("all"); |
| 203 |
$self->assert_num_equals(10, scalar @records); |
| 204 |
$self->assert(grep { $_ == 6 } @records); |
| 205 |
|
| 206 |
# this needs a bit of magic to know where to write... so |
| 207 |
# we do some hard-coded cyrus.index handling |
| 208 |
my $dir = $self->{instance}->folder_to_directory('user.cassandane'); |
| 209 |
unlink("$dir/6."); |
| 210 |
|
| 211 |
# this time, the reconstruct will fix up the broken record and re-insert later |
| 212 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct', 'user.cassandane'); |
| 213 |
|
| 214 |
@records = $imaptalk->search("all"); |
| 215 |
$self->assert_num_equals(9, scalar @records); |
| 216 |
$self->assert(not grep { $_ == 6 } @records); |
| 217 |
} |
| 218 |
|
| 219 |
# |
| 220 |
# Test snoozed annotation fixup |
| 221 |
# |
| 222 |
# XXX need to downgrade min version if this is backported to 3.2 |
| 223 |
sub test_reconstruct_snoozed |
| 224 |
:min_version_3_3 |
| 225 |
{ |
| 226 |
my ($self) = @_; |
| 227 |
|
| 228 |
my $imaptalk = $self->{store}->get_client(); |
| 229 |
|
| 230 |
for (1..10) { |
| 231 |
my $msg = $self->{gen}->generate(subject => "subject $_"); |
| 232 |
$self->{store}->write_message($msg, flags => ["\\Seen", "\$NotJunk"]); |
| 233 |
} |
| 234 |
$self->{store}->write_end(); |
| 235 |
$imaptalk->select("INBOX") || die; |
| 236 |
|
| 237 |
my @records = $imaptalk->search("all"); |
| 238 |
$self->assert_num_equals(10, scalar @records); |
| 239 |
$self->assert(grep { $_ == 6 } @records); |
| 240 |
|
| 241 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct'); |
| 242 |
|
| 243 |
@records = $imaptalk->search("all"); |
| 244 |
$self->assert_num_equals(10, scalar @records); |
| 245 |
$self->assert(grep { $_ == 6 } @records); |
| 246 |
|
| 247 |
$imaptalk->store('5', 'annotation', ["/vendor/cmu/cyrus-imapd/snoozed", |
| 248 |
['value.shared', { Quote => encode_json({until => '2020-01-01T00:00:00'}) }], |
| 249 |
]); |
| 250 |
|
| 251 |
# this needs a bit of magic to know where to write... so |
| 252 |
# we do some hard-coded cyrus.index handling |
| 253 |
my $dir = $self->{instance}->folder_to_directory('user.cassandane'); |
| 254 |
my $file = "$dir/cyrus.index"; |
| 255 |
my $fh = IO::File->new($file, "+<"); |
| 256 |
die "NO SUCH FILE $file" unless $fh; |
| 257 |
my $index = Cyrus::IndexFile->new($fh); |
| 258 |
|
| 259 |
while (my $record = $index->next_record_hash()) { |
| 260 |
if ($record->{Uid} == 5) { |
| 261 |
$self->assert_str_equals(substr($record->{SystemFlags}, 5, 1), '1'); |
| 262 |
} |
| 263 |
else { |
| 264 |
$self->assert_str_equals(substr($record->{SystemFlags}, 5, 1), '0'); |
| 265 |
} |
| 266 |
} |
| 267 |
close($fh); |
| 268 |
|
| 269 |
# the reconstruct shouldn't change anything |
| 270 |
$self->{instance}->getsyslog(); |
| 271 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct', 'user.cassandane'); |
| 272 |
$self->assert_syslog_does_not_match($self->{instance}, qr/mismatch/); |
| 273 |
|
| 274 |
xlog $self, "update some \\Snoozed flags"; |
| 275 |
$fh = IO::File->new($file, "+<"); |
| 276 |
die "NO SUCH FILE $file" unless $fh; |
| 277 |
$index = Cyrus::IndexFile->new($fh); |
| 278 |
|
| 279 |
while (my $record = $index->next_record_hash()) { |
| 280 |
if ($record->{Uid} == 5) { |
| 281 |
# nuke the Snoozed flag |
| 282 |
$self->assert_str_equals(substr($record->{SystemFlags}, 5, 1), '1'); |
| 283 |
substr($record->{SystemFlags}, 5, 1) = '0'; |
| 284 |
$index->rewrite_record($record); |
| 285 |
} |
| 286 |
elsif ($record->{Uid} == 6) { |
| 287 |
# add the Snoozed flag |
| 288 |
$self->assert_str_equals(substr($record->{SystemFlags}, 5, 1), '0'); |
| 289 |
substr($record->{SystemFlags}, 5, 1) = '1'; |
| 290 |
$index->rewrite_record($record); |
| 291 |
} |
| 292 |
else { |
| 293 |
$self->assert_str_equals(substr($record->{SystemFlags}, 5, 1), '0'); |
| 294 |
} |
| 295 |
} |
| 296 |
close($fh); |
| 297 |
|
| 298 |
# this reconstruct should change things back! |
| 299 |
$self->{instance}->getsyslog(); |
| 300 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct', 'user.cassandane'); |
| 301 |
if ($self->{instance}->{have_syslog_replacement}) { |
| 302 |
my @lines = $self->{instance}->getsyslog(); |
| 303 |
$self->assert_matches(qr/uid 5 snoozed mismatch/, "@lines"); |
| 304 |
$self->assert_matches(qr/uid 6 snoozed mismatch/, "@lines"); |
| 305 |
} |
| 306 |
|
| 307 |
xlog $self, "check that the values are changed back"; |
| 308 |
$fh = IO::File->new($file, "+<"); |
| 309 |
die "NO SUCH FILE $file" unless $fh; |
| 310 |
$index = Cyrus::IndexFile->new($fh); |
| 311 |
|
| 312 |
while (my $record = $index->next_record_hash()) { |
| 313 |
if ($record->{Uid} == 5) { |
| 314 |
$self->assert_str_equals(substr($record->{SystemFlags}, 5, 1), '1'); |
| 315 |
} |
| 316 |
else { |
| 317 |
$self->assert_str_equals(substr($record->{SystemFlags}, 5, 1), '0'); |
| 318 |
} |
| 319 |
} |
| 320 |
close($fh); |
| 321 |
} |
| 322 |
|
| 323 |
sub test_reconstruct_uniqueid_from_header_path_legacymb |
| 324 |
:min_version_3_7 :MailboxLegacyDirs :NoStartInstances |
| 325 |
{ |
| 326 |
my ($self) = @_; |
| 327 |
my $entry = '/shared/vendor/cmu/cyrus-imapd/uniqueid'; |
| 328 |
|
| 329 |
# first start will set up cassandane user |
| 330 |
$self->_start_instances(); |
| 331 |
my $basedir = $self->{instance}->get_basedir(); |
| 332 |
my $mailboxes_db = "$basedir/conf/mailboxes.db"; |
| 333 |
$self->assert(-f $mailboxes_db, "$mailboxes_db not present"); |
| 334 |
|
| 335 |
# find out the uniqueid of the inbox |
| 336 |
my $imaptalk = $self->{store}->get_client(); |
| 337 |
my $res = $imaptalk->getmetadata("INBOX", $entry); |
| 338 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 339 |
$self->assert_not_null($res); |
| 340 |
my $uniqueid = $res->{INBOX}{$entry}; |
| 341 |
$self->assert_not_null($uniqueid); |
| 342 |
$imaptalk->logout(); |
| 343 |
undef $imaptalk; |
| 344 |
|
| 345 |
# stop service while tinkering |
| 346 |
$self->{instance}->stop(); |
| 347 |
$self->{instance}->{re_use_dir} = 1; |
| 348 |
|
| 349 |
# get header path |
| 350 |
my $cyrus_header = $self->{instance}->folder_to_directory('INBOX') |
| 351 |
. '/cyrus.header'; |
| 352 |
$self->assert(-f $cyrus_header, "couldn't find cyrus.header file"); |
| 353 |
|
| 354 |
# lose uniqueid from cyrus.header |
| 355 |
$self->assert(-f $cyrus_header, "couldn't find cyrus.header file"); |
| 356 |
copy($cyrus_header, "$cyrus_header.OLD"); |
| 357 |
my $hf = Cyrus::HeaderFile->new_file("$cyrus_header.OLD"); |
| 358 |
my $dlist = Cyrus::DList->parse_string($hf->{dlistheader}); |
| 359 |
my $hash = $dlist->as_perl(); |
| 360 |
$self->assert_str_equals($uniqueid, $hash->{I}); |
| 361 |
$hash->{I} = undef; |
| 362 |
$dlist = Cyrus::DList->new_perl('', $hash); |
| 363 |
my $out = IO::File->new($cyrus_header, 'w'); |
| 364 |
$hf->write_newheader($out, $dlist->as_string()); |
| 365 |
|
| 366 |
# reconstruct -P should find and fix the missing uniqueid |
| 367 |
$self->{instance}->getsyslog(); |
| 368 |
$self->{instance}->run_command({ cyrus => 1 }, |
| 369 |
'reconstruct', '-P', $cyrus_header); |
| 370 |
|
| 371 |
# should not have existed in cyrus.header, get from mbentry |
| 372 |
$self->assert_syslog_matches( |
| 373 |
$self->{instance}, |
| 374 |
qr{mailbox header had no uniqueid, setting from mbentry} |
| 375 |
); |
| 376 |
|
| 377 |
# bring service back up |
| 378 |
$self->{instance}->start(); |
| 379 |
|
| 380 |
# header should have the same uniqueid as before |
| 381 |
$self->assert(-f $cyrus_header, "couldn't find cyrus.header file"); |
| 382 |
$hf = Cyrus::HeaderFile->new_file($cyrus_header); |
| 383 |
$self->assert_str_equals($uniqueid, $hf->{header}->{UniqueId}); |
| 384 |
} |
| 385 |
|
| 386 |
sub test_reconstruct_uniqueid_from_header_path_uuidmb |
| 387 |
:min_version_3_7 :NoStartInstances |
| 388 |
{ |
| 389 |
my ($self) = @_; |
| 390 |
my $entry = '/shared/vendor/cmu/cyrus-imapd/uniqueid'; |
| 391 |
|
| 392 |
# first start will set up cassandane user |
| 393 |
$self->_start_instances(); |
| 394 |
my $basedir = $self->{instance}->get_basedir(); |
| 395 |
my $mailboxes_db = "$basedir/conf/mailboxes.db"; |
| 396 |
$self->assert(-f $mailboxes_db, "$mailboxes_db not present"); |
| 397 |
|
| 398 |
# find out the uniqueid of the inbox |
| 399 |
my $imaptalk = $self->{store}->get_client(); |
| 400 |
my $res = $imaptalk->getmetadata("INBOX", $entry); |
| 401 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 402 |
$self->assert_not_null($res); |
| 403 |
my $uniqueid = $res->{INBOX}{$entry}; |
| 404 |
$self->assert_not_null($uniqueid); |
| 405 |
$imaptalk->logout(); |
| 406 |
undef $imaptalk; |
| 407 |
|
| 408 |
# stop service while tinkering |
| 409 |
$self->{instance}->stop(); |
| 410 |
$self->{instance}->{re_use_dir} = 1; |
| 411 |
|
| 412 |
# get header path |
| 413 |
my $cyrus_header = $self->{instance}->folder_to_directory('INBOX') |
| 414 |
. '/cyrus.header'; |
| 415 |
$self->assert(-f $cyrus_header, "couldn't find cyrus.header file"); |
| 416 |
|
| 417 |
# lose that uniqueid from mailboxes.db |
| 418 |
my $I = "I$uniqueid"; |
| 419 |
my $N = "Nuser\x1fcassandane"; |
| 420 |
$self->{instance}->run_dbcommand($mailboxes_db, "twoskip", |
| 421 |
[ 'DELETE', $I ]); |
| 422 |
my (undef, $mbentry) = $self->{instance}->run_dbcommand( |
| 423 |
$mailboxes_db, "twoskip", |
| 424 |
['SHOW', $N]); |
| 425 |
my $dlist = Cyrus::DList->parse_string($mbentry); |
| 426 |
my $hash = $dlist->as_perl(); |
| 427 |
$self->assert_str_equals($uniqueid, $hash->{I}); |
| 428 |
$hash->{I} = undef; |
| 429 |
$dlist = Cyrus::DList->new_perl('', $hash); |
| 430 |
$self->{instance}->run_dbcommand( |
| 431 |
$mailboxes_db, "twoskip", |
| 432 |
[ 'SET', $N, $dlist->as_string() ]); |
| 433 |
|
| 434 |
my %updated = $self->{instance}->run_dbcommand( |
| 435 |
$mailboxes_db, "twoskip", ['SHOW']); |
| 436 |
xlog "updated mailboxes.db: " . Dumper \%updated; |
| 437 |
|
| 438 |
# lose it from cyrus.header too |
| 439 |
$self->assert(-f $cyrus_header, "couldn't find cyrus.header file"); |
| 440 |
copy($cyrus_header, "$cyrus_header.OLD"); |
| 441 |
my $hf = Cyrus::HeaderFile->new_file("$cyrus_header.OLD"); |
| 442 |
$dlist = Cyrus::DList->parse_string($hf->{dlistheader}); |
| 443 |
$hash = $dlist->as_perl(); |
| 444 |
$self->assert_str_equals($uniqueid, $hash->{I}); |
| 445 |
$hash->{I} = undef; |
| 446 |
$dlist = Cyrus::DList->new_perl('', $hash); |
| 447 |
my $out = IO::File->new($cyrus_header, 'w'); |
| 448 |
$hf->write_newheader($out, $dlist->as_string()); |
| 449 |
|
| 450 |
# reconstruct -P should find and fix the missing uniqueid |
| 451 |
$self->{instance}->getsyslog(); |
| 452 |
$self->{instance}->run_command({ cyrus => 1 }, |
| 453 |
'reconstruct', '-P', $cyrus_header); |
| 454 |
|
| 455 |
# should not have existed in cyrus.header, get from path |
| 456 |
$self->assert_syslog_matches( |
| 457 |
$self->{instance}, |
| 458 |
qr{mailbox header had no uniqueid, setting from path} |
| 459 |
); |
| 460 |
|
| 461 |
# bring service back up |
| 462 |
$self->{instance}->start(); |
| 463 |
|
| 464 |
# header should have the same uniqueid as before |
| 465 |
$self->assert(-f $cyrus_header, "couldn't find cyrus.header file"); |
| 466 |
$hf = Cyrus::HeaderFile->new_file($cyrus_header); |
| 467 |
$self->assert_str_equals($uniqueid, $hf->{header}->{UniqueId}); |
| 468 |
|
| 469 |
# mbentry should have the same uniqueid as before |
| 470 |
(undef, $mbentry) = $self->{instance}->run_dbcommand( |
| 471 |
$mailboxes_db, "twoskip", |
| 472 |
['SHOW', $N]); |
| 473 |
$dlist = Cyrus::DList->parse_string($mbentry); |
| 474 |
$hash = $dlist->as_perl(); |
| 475 |
$self->assert_str_equals($uniqueid, $hash->{I}); |
| 476 |
|
| 477 |
# $I entry should be back |
| 478 |
my ($key, $value) = $self->{instance}->run_dbcommand( |
| 479 |
$mailboxes_db, "twoskip", |
| 480 |
['SHOW', $I]); |
| 481 |
$self->assert_str_equals($I, $key); |
| 482 |
$dlist = Cyrus::DList->parse_string($value); |
| 483 |
$hash = $dlist->as_perl(); |
| 484 |
$self->assert_str_equals("user\x1fcassandane", $hash->{N}); |
| 485 |
} |
| 486 |
|
| 487 |
sub test_reconstruct_uniqueid_from_header_uuidmb |
| 488 |
:min_version_3_7 :NoStartInstances |
| 489 |
{ |
| 490 |
my ($self) = @_; |
| 491 |
my $entry = '/shared/vendor/cmu/cyrus-imapd/uniqueid'; |
| 492 |
|
| 493 |
# first start will set up cassandane user |
| 494 |
$self->_start_instances(); |
| 495 |
my $basedir = $self->{instance}->get_basedir(); |
| 496 |
my $mailboxes_db = "$basedir/conf/mailboxes.db"; |
| 497 |
$self->assert(-f $mailboxes_db, "$mailboxes_db not present"); |
| 498 |
|
| 499 |
# find out the uniqueid of the inbox |
| 500 |
my $imaptalk = $self->{store}->get_client(); |
| 501 |
my $res = $imaptalk->getmetadata("INBOX", $entry); |
| 502 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 503 |
$self->assert_not_null($res); |
| 504 |
my $uniqueid = $res->{INBOX}{$entry}; |
| 505 |
$self->assert_not_null($uniqueid); |
| 506 |
$imaptalk->logout(); |
| 507 |
undef $imaptalk; |
| 508 |
|
| 509 |
# stop service while tinkering |
| 510 |
$self->{instance}->stop(); |
| 511 |
$self->{instance}->{re_use_dir} = 1; |
| 512 |
|
| 513 |
# get header path |
| 514 |
my $cyrus_header = $self->{instance}->folder_to_directory('INBOX') |
| 515 |
. '/cyrus.header'; |
| 516 |
$self->assert(-f $cyrus_header, "couldn't find cyrus.header file"); |
| 517 |
|
| 518 |
# lose that uniqueid from mailboxes.db |
| 519 |
my $I = "I$uniqueid"; |
| 520 |
my $N = "Nuser\x1fcassandane"; |
| 521 |
$self->{instance}->run_dbcommand($mailboxes_db, "twoskip", |
| 522 |
[ 'DELETE', $I ]); |
| 523 |
my (undef, $mbentry) = $self->{instance}->run_dbcommand( |
| 524 |
$mailboxes_db, "twoskip", |
| 525 |
['SHOW', $N]); |
| 526 |
my $dlist = Cyrus::DList->parse_string($mbentry); |
| 527 |
my $hash = $dlist->as_perl(); |
| 528 |
$self->assert_str_equals($uniqueid, $hash->{I}); |
| 529 |
$hash->{I} = undef; |
| 530 |
$dlist = Cyrus::DList->new_perl('', $hash); |
| 531 |
$self->{instance}->run_dbcommand( |
| 532 |
$mailboxes_db, "twoskip", |
| 533 |
[ 'SET', $N, $dlist->as_string() ]); |
| 534 |
|
| 535 |
my %updated = $self->{instance}->run_dbcommand( |
| 536 |
$mailboxes_db, "twoskip", ['SHOW']); |
| 537 |
xlog "updated mailboxes.db: " . Dumper \%updated; |
| 538 |
|
| 539 |
# reconstruct -P should find and fix the missing uniqueid |
| 540 |
$self->{instance}->getsyslog(); |
| 541 |
$self->{instance}->run_command({ cyrus => 1 }, |
| 542 |
'reconstruct', '-P', $cyrus_header); |
| 543 |
if ($self->{instance}->{have_syslog_replacement}) { |
| 544 |
my $syslog = join(q{}, $self->{instance}->getsyslog()); |
| 545 |
|
| 546 |
# should have still existed in cyrus.header |
| 547 |
$self->assert_does_not_match(qr{mailbox header had no uniqueid}, |
| 548 |
$syslog); |
| 549 |
|
| 550 |
# expect to find the log line |
| 551 |
$self->assert_matches(qr{setting mbentry uniqueid from header}, |
| 552 |
$syslog); |
| 553 |
} |
| 554 |
|
| 555 |
# bring service back up |
| 556 |
$self->{instance}->start(); |
| 557 |
|
| 558 |
# header should have the same uniqueid as before |
| 559 |
$self->assert(-f $cyrus_header, "couldn't find cyrus.header file"); |
| 560 |
my $hf = Cyrus::HeaderFile->new_file($cyrus_header); |
| 561 |
$self->assert_str_equals($uniqueid, $hf->{header}->{UniqueId}); |
| 562 |
|
| 563 |
# mbentry should have the same uniqueid as before |
| 564 |
(undef, $mbentry) = $self->{instance}->run_dbcommand( |
| 565 |
$mailboxes_db, "twoskip", |
| 566 |
['SHOW', $N]); |
| 567 |
$dlist = Cyrus::DList->parse_string($mbentry); |
| 568 |
$hash = $dlist->as_perl(); |
| 569 |
$self->assert_str_equals($uniqueid, $hash->{I}); |
| 570 |
|
| 571 |
# $I entry should be back |
| 572 |
my ($key, $value) = $self->{instance}->run_dbcommand( |
| 573 |
$mailboxes_db, "twoskip", |
| 574 |
['SHOW', $I]); |
| 575 |
$self->assert_str_equals($I, $key); |
| 576 |
$dlist = Cyrus::DList->parse_string($value); |
| 577 |
$hash = $dlist->as_perl(); |
| 578 |
$self->assert_str_equals("user\x1fcassandane", $hash->{N}); |
| 579 |
} |
| 580 |
|
| 581 |
sub test_downgrade_upgrade |
| 582 |
{ |
| 583 |
my ($self) = @_; |
| 584 |
|
| 585 |
my $talk = $self->{store}->get_client(); |
| 586 |
$self->{store}->_select(); |
| 587 |
$self->assert_num_equals(1, $talk->uid()); |
| 588 |
$self->{store}->set_fetch_attributes(qw(uid flags)); |
| 589 |
|
| 590 |
xlog $self, "Add two messages"; |
| 591 |
my %msg; |
| 592 |
$msg{A} = $self->make_message('Message A'); |
| 593 |
$msg{A}->set_attributes(id => 1, |
| 594 |
uid => 1, |
| 595 |
flags => []); |
| 596 |
$msg{B} = $self->make_message('Message B'); |
| 597 |
$msg{B}->set_attributes(id => 2, |
| 598 |
uid => 2, |
| 599 |
flags => []); |
| 600 |
$self->check_messages(\%msg); |
| 601 |
|
| 602 |
xlog $self, "Set \\Seen on message A"; |
| 603 |
my $res = $talk->store('1', '+flags', '(\\Seen)'); |
| 604 |
$self->assert_deep_equals({ '1' => { 'flags' => [ '\\Seen' ] }}, $res); |
| 605 |
$msg{A}->set_attribute(flags => ['\\Seen']); |
| 606 |
$self->check_messages(\%msg); |
| 607 |
|
| 608 |
xlog $self, "Clear \\Seen on message A"; |
| 609 |
$res = $talk->store('1', '-flags', '(\\Seen)'); |
| 610 |
$self->assert_deep_equals({ '1' => { 'flags' => [] }}, $res); |
| 611 |
$msg{A}->set_attribute(flags => []); |
| 612 |
$self->check_messages(\%msg); |
| 613 |
|
| 614 |
xlog $self, "Set \\Seen on message A again"; |
| 615 |
$res = $talk->store('1', '+flags', '(\\Seen)'); |
| 616 |
$self->assert_deep_equals({ '1' => { 'flags' => [ '\\Seen' ] }}, $res); |
| 617 |
$msg{A}->set_attribute(flags => ['\\Seen']); |
| 618 |
$self->check_messages(\%msg); |
| 619 |
|
| 620 |
for my $version (12, 14, 16, 'max') { |
| 621 |
xlog $self, "Set to version $version"; |
| 622 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct', '-V', $version); |
| 623 |
|
| 624 |
xlog $self, "Reconnect, \\Seen should still be on message A"; |
| 625 |
$self->{store}->disconnect(); |
| 626 |
$self->{store}->connect(); |
| 627 |
$self->{store}->_select(); |
| 628 |
$self->check_messages(\%msg); |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
1; |