| 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::Metadata; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use DateTime; |
| 44 |
use File::Temp qw(:POSIX); |
| 45 |
use Config; |
| 46 |
|
| 47 |
use lib '.'; |
| 48 |
use base qw(Cassandane::Cyrus::TestCase); |
| 49 |
use Cassandane::Util::Log; |
| 50 |
|
| 51 |
use lib '../perl/imap'; |
| 52 |
use Cyrus::DList; |
| 53 |
|
| 54 |
sub new |
| 55 |
{ |
| 56 |
my ($class, @args) = @_; |
| 57 |
return $class->SUPER::new({ adminstore => 1 }, @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 |
# |
| 73 |
# Create and save two messages to two stores, according to GUID |
| 74 |
# on the messages, so that the first store gets the message with |
| 75 |
# the lower GUID and the second store the message with the higher |
| 76 |
# GUID. Both cases need to be done in a controlled manner in order |
| 77 |
# to exercise some of the more obscure code paths in message |
| 78 |
# replication. |
| 79 |
# |
| 80 |
# Returns: Message, Message in the order they went to Stores |
| 81 |
# |
| 82 |
sub make_message_pair |
| 83 |
{ |
| 84 |
my ($self, $store0, $store1) = @_; |
| 85 |
|
| 86 |
# Generate two messages and detect their resulting GUIDs |
| 87 |
my $msg0 = $self->{gen}->generate(subject => 'Message Zero'); |
| 88 |
my $msg1 = $self->{gen}->generate(subject => 'Message One'); |
| 89 |
my $guid0 = $msg0->get_guid(); |
| 90 |
my $guid1 = $msg1->get_guid(); |
| 91 |
xlog $self, "Message 'Message Zero' has GUID $guid0"; |
| 92 |
xlog $self, "Message 'Message One' has GUID $guid1"; |
| 93 |
|
| 94 |
# choose ordering of messages |
| 95 |
$self->assert_str_not_equals($guid0, $guid1); |
| 96 |
if ($guid0 gt $guid1) |
| 97 |
{ |
| 98 |
# swap |
| 99 |
my $t = $msg0; |
| 100 |
$msg0 = $msg1; |
| 101 |
$msg1 = $t; |
| 102 |
} |
| 103 |
|
| 104 |
# Save and return the messages |
| 105 |
$self->_save_message($msg0, $store0); |
| 106 |
$self->_save_message($msg1, $store1); |
| 107 |
return ($msg0, $msg1); |
| 108 |
} |
| 109 |
|
| 110 |
# List annotations actually stored in the database. |
| 111 |
sub list_annotations |
| 112 |
{ |
| 113 |
my ($self, %params) = @_; |
| 114 |
|
| 115 |
my $scope = delete $params{scope} || 'global'; |
| 116 |
my $mailbox = delete $params{mailbox} || 'user.cassandane'; |
| 117 |
my $tombstones = delete $params{tombstones}; |
| 118 |
my $withmdata = delete $params{withmdata}; |
| 119 |
my $instance = delete $params{instance} || $self->{instance}; |
| 120 |
my $uids = delete $params{uids}; |
| 121 |
die "Unknown parameters: " . join(' ', map { $_ . '=' . $params{$_}; } keys %params) |
| 122 |
if scalar %params; |
| 123 |
|
| 124 |
my $basedir = $instance->{basedir}; |
| 125 |
|
| 126 |
my $mailbox_db; |
| 127 |
if ($scope eq 'global' || $scope eq 'mailbox') |
| 128 |
{ |
| 129 |
$mailbox_db = "$basedir/conf/annotations.db"; |
| 130 |
} |
| 131 |
elsif ($scope eq 'message') |
| 132 |
{ |
| 133 |
my $mb = $mailbox; |
| 134 |
my $datadir = $self->{instance}->folder_to_directory($mailbox); |
| 135 |
$mailbox_db = "$datadir/cyrus.annotations"; |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
die "Unknown scope: $scope"; |
| 140 |
} |
| 141 |
|
| 142 |
my $format = $instance->{config}->get('annotation_db'); |
| 143 |
$format = $format // 'twoskip'; |
| 144 |
|
| 145 |
my @annots; |
| 146 |
|
| 147 |
my $res = $instance->run_dbcommand_cb(sub { |
| 148 |
my ($key, $value) = @_; |
| 149 |
my ($uid, $item, $userid, @rest) = split '\0', $key; |
| 150 |
my ($data, $modseq, $flags); |
| 151 |
if (substr($value, 0, 1) eq '%') { |
| 152 |
my $dlist = Cyrus::DList->parse_string($value, 0); |
| 153 |
my $hash = $dlist->as_perl; |
| 154 |
$data = $hash->{V}; |
| 155 |
$modseq = $hash->{M}; |
| 156 |
$flags = $hash->{F} ? 1 : 0; # XXX - parse more options later |
| 157 |
} |
| 158 |
else { |
| 159 |
my $offset = 0; |
| 160 |
my $vallen = unpack('N', substr($value, $offset, 4)); |
| 161 |
$offset += 8; # 4 more bytes of rubbish |
| 162 |
$data = substr($value, $offset, $vallen); |
| 163 |
$offset += $vallen + 1; # trailing null |
| 164 |
my $strend = index($value, "\0", $offset); |
| 165 |
my $type = substr($value, $offset, ($strend - $offset)); |
| 166 |
$offset = $strend + 1; |
| 167 |
my $modtime = unpack('N', substr($value, $offset, 4)); |
| 168 |
$offset += 8; # 4 more bytes of rubbish again |
| 169 |
$modseq = unpack('x[N]N', substr($value, $offset, 8)); |
| 170 |
$offset += 8; |
| 171 |
$flags = unpack('C', substr($value, $offset, 1)); |
| 172 |
} |
| 173 |
|
| 174 |
if ($flags and not $tombstones) { |
| 175 |
return; |
| 176 |
} |
| 177 |
my $annot = { |
| 178 |
uid => ($scope eq 'message' ? $uid : 0), |
| 179 |
mboxname => ($scope eq 'message' ? $mailbox : $uid), |
| 180 |
entry => $item, |
| 181 |
userid => $userid, |
| 182 |
data => $data, |
| 183 |
}; |
| 184 |
|
| 185 |
if ($withmdata) { |
| 186 |
$annot->{modseq} = $modseq; |
| 187 |
$annot->{flags} = $flags; |
| 188 |
} |
| 189 |
|
| 190 |
if ($uids) { |
| 191 |
my %wantuids = map { $_ => 1 } $uids; |
| 192 |
if ($uids and not exists($wantuids{$annot->{uid}})) { |
| 193 |
return; |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
if ($annot->{userid} eq '[.OwNeR.]') { |
| 198 |
$annot->{userid} = 'cassandane'; # XXX - strip owner from $mailbox? |
| 199 |
} |
| 200 |
|
| 201 |
push(@annots, $annot); |
| 202 |
}, $mailbox_db, $format, ['SHOW']); |
| 203 |
|
| 204 |
# enforce a stable order so we have some chance of |
| 205 |
# comparing the results |
| 206 |
@annots = sort { |
| 207 |
$a->{mboxname} cmp $b->{mboxname} || |
| 208 |
$a->{uid} <=> $b->{uid} || |
| 209 |
$a->{userid} cmp $b->{userid} || |
| 210 |
$a->{entry} cmp $b->{entry}; |
| 211 |
} @annots; |
| 212 |
|
| 213 |
return \@annots; |
| 214 |
} |
| 215 |
|
| 216 |
sub list_uids |
| 217 |
{ |
| 218 |
my ($self, $store) = @_; |
| 219 |
my @uids; |
| 220 |
|
| 221 |
$store->read_begin(); |
| 222 |
while (my $msg = $store->read_message()) |
| 223 |
{ |
| 224 |
push(@uids, $msg->uid); |
| 225 |
} |
| 226 |
$store->read_end(); |
| 227 |
|
| 228 |
return \@uids; |
| 229 |
} |
| 230 |
|
| 231 |
sub check_msg_annotation_replication |
| 232 |
{ |
| 233 |
my ($self, $master_store, $replica_store, %params) = @_; |
| 234 |
|
| 235 |
my $master_annots = $self->list_annotations((%params, |
| 236 |
scope => 'message', |
| 237 |
instance => $self->{instance}, |
| 238 |
withmdata => 1, |
| 239 |
tombstones => 1, |
| 240 |
uids => $self->list_uids($master_store), |
| 241 |
)); |
| 242 |
my $replica_annots = $self->list_annotations((%params, |
| 243 |
scope => 'message', |
| 244 |
instance => $self->{replica}, |
| 245 |
withmdata => 1, |
| 246 |
tombstones => 1, |
| 247 |
uids => $self->list_uids($replica_store), |
| 248 |
)); |
| 249 |
|
| 250 |
$self->assert_deep_equals($master_annots, $replica_annots); |
| 251 |
} |
| 252 |
|
| 253 |
sub set_msg_annotation |
| 254 |
{ |
| 255 |
my ($self, $store, $uid, $entry, $attrib, $value) = @_; |
| 256 |
|
| 257 |
$store ||= $self->{store}; |
| 258 |
$store->connect(); |
| 259 |
$store->_select(); |
| 260 |
my $talk = $store->get_client(); |
| 261 |
# Note $value might have no whitespace so we have to |
| 262 |
# convince Mail::IMAPTalk to quote it anyway |
| 263 |
$talk->store('' . $uid, 'annotation', [$entry, [$attrib, { Quote => $value }]]); |
| 264 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 265 |
} |
| 266 |
|
| 267 |
# Not sure if this cases can even work... |
| 268 |
# sub test_msg_replication_mod_bot_mse |
| 269 |
|
| 270 |
# Get the highestmodseq of the folder |
| 271 |
sub get_highestmodseq |
| 272 |
{ |
| 273 |
my ($self) = @_; |
| 274 |
|
| 275 |
my $store = $self->{store}; |
| 276 |
my $talk = $store->get_client(); |
| 277 |
my $stat = $talk->status($store->{folder}, '(highestmodseq)'); |
| 278 |
return undef unless defined $stat; |
| 279 |
return undef unless ref $stat eq 'HASH'; |
| 280 |
return undef unless defined $stat->{highestmodseq}; |
| 281 |
return 0 + $stat->{highestmodseq}; |
| 282 |
} |
| 283 |
|
| 284 |
# sub test_mbox_replication_new_rep |
| 285 |
# sub test_mbox_replication_new_bot |
| 286 |
# sub test_mbox_replication_mod_mas |
| 287 |
# sub test_mbox_replication_mod_rep |
| 288 |
# sub test_mbox_replication_mod_bot |
| 289 |
# sub test_mbox_replication_del_mas |
| 290 |
# sub test_mbox_replication_del_rep |
| 291 |
# sub test_mbox_replication_del_bot |
| 292 |
|
| 293 |
sub folder_delete_mboxa_common |
| 294 |
{ |
| 295 |
my ($self) = @_; |
| 296 |
|
| 297 |
my $imaptalk = $self->{store}->get_client(); |
| 298 |
# data thanks to hipsteripsum.me |
| 299 |
my $folder = 'INBOX.williamsburg'; |
| 300 |
my $fentry = '/private/comment'; |
| 301 |
my $data = $self->make_random_data(0.3, maxreps => 15); |
| 302 |
|
| 303 |
xlog $self, "create a mailbox"; |
| 304 |
$imaptalk->create($folder) |
| 305 |
or die "Cannot create mailbox $folder: $@"; |
| 306 |
|
| 307 |
xlog $self, "set and then get the same back again"; |
| 308 |
$imaptalk->setmetadata($folder, $fentry, $data) |
| 309 |
or die "Cannot setmetadata: $@"; |
| 310 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 311 |
|
| 312 |
my $res = $imaptalk->getmetadata($folder, $fentry) |
| 313 |
or die "Cannot getmetadata: $@"; |
| 314 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 315 |
$self->assert_deep_equals({ |
| 316 |
$folder => { $fentry => $data } |
| 317 |
}, $res); |
| 318 |
|
| 319 |
xlog $self, "delete the mailbox"; |
| 320 |
$imaptalk->delete($folder) |
| 321 |
or die "Cannot delete mailbox $folder: $@"; |
| 322 |
|
| 323 |
xlog $self, "create a new mailbox with the same name"; |
| 324 |
$imaptalk->create($folder) |
| 325 |
or die "Cannot create mailbox $folder: $@"; |
| 326 |
|
| 327 |
xlog $self, "new mailbox reports NIL for the per-mailbox metadata"; |
| 328 |
$res = $imaptalk->getmetadata($folder, $fentry) |
| 329 |
or die "Cannot getmetadata: $@"; |
| 330 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 331 |
$self->assert_deep_equals({ |
| 332 |
$folder => { $fentry => undef } |
| 333 |
}, $res); |
| 334 |
} |
| 335 |
|
| 336 |
sub folder_delete_mboxm_common |
| 337 |
{ |
| 338 |
my ($self) = @_; |
| 339 |
|
| 340 |
my $imaptalk = $self->{store}->get_client(); |
| 341 |
# data thanks to hipsteripsum.me |
| 342 |
my $folder = 'INBOX.williamsburg'; |
| 343 |
my $fentry = '/private/comment'; |
| 344 |
my $data = $self->make_random_data(0.3, maxreps => 15); |
| 345 |
|
| 346 |
xlog $self, "create a mailbox"; |
| 347 |
$imaptalk->create($folder) |
| 348 |
or die "Cannot create mailbox $folder: $@"; |
| 349 |
|
| 350 |
xlog $self, "set and then get the same back again"; |
| 351 |
$imaptalk->setmetadata($folder, $fentry, $data) |
| 352 |
or die "Cannot setmetadata: $@"; |
| 353 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 354 |
|
| 355 |
my $res = $imaptalk->getmetadata($folder, $fentry) |
| 356 |
or die "Cannot getmetadata: $@"; |
| 357 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 358 |
$self->assert_deep_equals({ |
| 359 |
$folder => { $fentry => $data } |
| 360 |
}, $res); |
| 361 |
|
| 362 |
xlog $self, "delete the mailbox"; |
| 363 |
$imaptalk->delete($folder) |
| 364 |
or die "Cannot delete mailbox $folder: $@"; |
| 365 |
|
| 366 |
xlog $self, "cannot get metadata for deleted mailbox"; |
| 367 |
$res = $imaptalk->getmetadata($folder, $fentry); |
| 368 |
$self->assert_str_equals('no', $imaptalk->get_last_completion_response()); |
| 369 |
$self->assert($imaptalk->get_last_error() =~ m/does not exist/i); |
| 370 |
|
| 371 |
xlog $self, "create a new mailbox with the same name"; |
| 372 |
$imaptalk->create($folder) |
| 373 |
or die "Cannot create mailbox $folder: $@"; |
| 374 |
|
| 375 |
xlog $self, "new mailbox reports NIL for the per-mailbox metadata"; |
| 376 |
$res = $imaptalk->getmetadata($folder, $fentry) |
| 377 |
or die "Cannot getmetadata: $@"; |
| 378 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 379 |
$self->assert_deep_equals({ |
| 380 |
$folder => { $fentry => undef } |
| 381 |
}, $res); |
| 382 |
} |
| 383 |
|
| 384 |
sub folder_delete_msg_common |
| 385 |
{ |
| 386 |
my ($self) = @_; |
| 387 |
|
| 388 |
my $imaptalk = $self->{store}->get_client(); |
| 389 |
# data thanks to hipsteripsum.me |
| 390 |
my $folder = 'INBOX.williamsburg'; |
| 391 |
my $mentry = '/comment'; |
| 392 |
my $mattrib = 'value.priv'; |
| 393 |
$self->{store}->set_fetch_attributes('uid', "annotation ($mentry $mattrib)"); |
| 394 |
$self->{store}->set_folder($folder); |
| 395 |
|
| 396 |
xlog $self, "create a mailbox"; |
| 397 |
$imaptalk->create($folder) |
| 398 |
or die "Cannot create mailbox $folder: $@"; |
| 399 |
|
| 400 |
xlog $self, "add some messages"; |
| 401 |
my $uid = 1; |
| 402 |
my %exp; |
| 403 |
for (1..10) |
| 404 |
{ |
| 405 |
my $msg = $self->make_message("Message $_"); |
| 406 |
$exp{$uid} = $msg; |
| 407 |
$msg->set_attribute('uid', $uid); |
| 408 |
my $data = $self->make_random_data(0.3, maxreps => 15); |
| 409 |
$msg->set_annotation($mentry, $mattrib, $data); |
| 410 |
$imaptalk->store('' . $uid, 'annotation', |
| 411 |
[$mentry, [$mattrib, $data]]); |
| 412 |
$self->assert_str_equals('ok', $imaptalk->get_last_completion_response()); |
| 413 |
$uid++; |
| 414 |
} |
| 415 |
|
| 416 |
xlog $self, "Check the messages are all there"; |
| 417 |
$self->check_messages(\%exp); |
| 418 |
|
| 419 |
xlog $self, "delete the mailbox"; |
| 420 |
$imaptalk->unselect(); |
| 421 |
$imaptalk->delete($folder) |
| 422 |
or die "Cannot delete mailbox $folder: $@"; |
| 423 |
|
| 424 |
xlog $self, "create a new mailbox with the same name"; |
| 425 |
$imaptalk->create($folder) |
| 426 |
or die "Cannot create mailbox $folder: $@"; |
| 427 |
|
| 428 |
xlog $self, "create some new messages"; |
| 429 |
%exp = (); |
| 430 |
$uid = 1; |
| 431 |
for (1..10) |
| 432 |
{ |
| 433 |
my $msg = $self->make_message("Message NEW $_"); |
| 434 |
$exp{$uid} = $msg; |
| 435 |
$msg->set_attribute('uid', $uid); |
| 436 |
# Note: no annotation on the new message |
| 437 |
$uid++; |
| 438 |
} |
| 439 |
|
| 440 |
xlog $self, "new mailbox reports NIL for the per-message metadata"; |
| 441 |
$self->check_messages(\%exp); |
| 442 |
} |
| 443 |
|
| 444 |
# This is like Mail::IMAPTalk::getmetadata, but |
| 445 |
# a) doesn't assume incorrect placement of the options, and |
| 446 |
# b) handles the METADATA LONGENTRIES response code |
| 447 |
sub getmetadata |
| 448 |
{ |
| 449 |
my ($talk, @args) = @_; |
| 450 |
|
| 451 |
my $res = {}; |
| 452 |
|
| 453 |
my %handlers = |
| 454 |
( |
| 455 |
metadata => sub |
| 456 |
{ |
| 457 |
my ($response, $rr, $id) = @_; |
| 458 |
if ($rr->[0] =~ m/^longentries/i) |
| 459 |
{ |
| 460 |
$res->{longentries} = 0 + $rr->[1]; |
| 461 |
} |
| 462 |
else |
| 463 |
{ |
| 464 |
my $f = $talk->_unfix_folder_name($rr->[0]); |
| 465 |
my %kv = ( @{$rr->[1]} ); |
| 466 |
map { $res->{$f}->{$_} = $kv{$_}; } keys %kv; |
| 467 |
} |
| 468 |
} |
| 469 |
); |
| 470 |
|
| 471 |
my $r = $talk->_imap_cmd('getmetadata', 0, \%handlers, @args); |
| 472 |
return if !defined $r; |
| 473 |
return $res; |
| 474 |
} |
| 475 |
|
| 476 |
use Cassandane::Tiny::Loader 'tiny-tests/Metadata'; |
| 477 |
|
| 478 |
1; |