| 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::Quota; |
| 41 |
use strict; |
| 42 |
use warnings; |
| 43 |
use Cwd qw(abs_path); |
| 44 |
use File::Path qw(mkpath); |
| 45 |
use DateTime; |
| 46 |
use Data::Dumper; |
| 47 |
|
| 48 |
use lib '.'; |
| 49 |
use base qw(Cassandane::Cyrus::TestCase); |
| 50 |
use Cassandane::Util::Log; |
| 51 |
use Cassandane::Util::NetString; |
| 52 |
use Cassandane::Util::Slurp; |
| 53 |
|
| 54 |
sub res_mailbox { 'MAILBOX' } |
| 55 |
sub res_annot_storage { 'ANNOTATION-STORAGE' } |
| 56 |
|
| 57 |
sub new |
| 58 |
{ |
| 59 |
my $class = shift; |
| 60 |
return $class->SUPER::new({ adminstore => 1, services => ['smmap', 'imap'] }, @_); |
| 61 |
} |
| 62 |
|
| 63 |
sub set_up |
| 64 |
{ |
| 65 |
my ($self) = @_; |
| 66 |
$self->SUPER::set_up(); |
| 67 |
|
| 68 |
my ($maj, $min) = Cassandane::Instance->get_version(); |
| 69 |
if ($maj < 3 || ($maj == 3 && $min < 9)) { |
| 70 |
$self->res_mailbox = 'X-NUM-FOLDERS'; |
| 71 |
$self->res_annot_storage = 'X-ANNOTATION-STORAGE'; |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
sub tear_down |
| 76 |
{ |
| 77 |
my ($self) = @_; |
| 78 |
$self->SUPER::tear_down(); |
| 79 |
} |
| 80 |
|
| 81 |
sub _set_quotaroot |
| 82 |
{ |
| 83 |
my ($self, $quotaroot) = @_; |
| 84 |
$self->{quotaroot} = $quotaroot; |
| 85 |
} |
| 86 |
|
| 87 |
# Utility function to set quota limits and check that it stuck |
| 88 |
sub _set_limits |
| 89 |
{ |
| 90 |
my ($self, %resources) = @_; |
| 91 |
my $admintalk = $self->{adminstore}->get_client(); |
| 92 |
|
| 93 |
my $quotaroot = delete $resources{quotaroot} || $self->{quotaroot}; |
| 94 |
my @quotalist; |
| 95 |
foreach my $resource (keys %resources) |
| 96 |
{ |
| 97 |
my $limit = $resources{$resource} |
| 98 |
or die "No limit specified for $resource"; |
| 99 |
push(@quotalist, uc($resource), $limit); |
| 100 |
} |
| 101 |
$self->{limits}->{$quotaroot} = { @quotalist }; |
| 102 |
$admintalk->setquota($quotaroot, \@quotalist); |
| 103 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 104 |
} |
| 105 |
|
| 106 |
# Utility function to check that quota's usages |
| 107 |
# and limits are where we expect it to be |
| 108 |
sub _check_usages |
| 109 |
{ |
| 110 |
my ($self, %expecteds) = @_; |
| 111 |
my $admintalk = $self->{adminstore}->get_client(); |
| 112 |
|
| 113 |
my $quotaroot = delete $expecteds{quotaroot} || $self->{quotaroot}; |
| 114 |
my $limits = $self->{limits}->{$quotaroot}; |
| 115 |
|
| 116 |
my @result = $admintalk->getquota($quotaroot); |
| 117 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 118 |
|
| 119 |
# check actual and expected number of resources do match |
| 120 |
$self->assert_num_equals(scalar(keys %$limits) * 3, scalar(@result)); |
| 121 |
|
| 122 |
# Convert the IMAP result to a conveniently checkable hash. |
| 123 |
# By checkable, we mean that a failure in assert_deep_equals() |
| 124 |
# will give a human some idea of what went wrong. |
| 125 |
my %act; |
| 126 |
while (scalar(@result)) { |
| 127 |
my ($res, $used, $limit) = splice(@result, 0, 3); |
| 128 |
$res = uc($res); |
| 129 |
die "Resource $res appears twice in result" |
| 130 |
if defined $act{$res}; |
| 131 |
$act{$res} = { |
| 132 |
used => $used, |
| 133 |
limit => $limit, |
| 134 |
}; |
| 135 |
} |
| 136 |
|
| 137 |
# Build a conveniently checkable hash from %expecteds |
| 138 |
# and limits previously by _set_limits(). |
| 139 |
my %exp; |
| 140 |
foreach my $res (keys %expecteds) |
| 141 |
{ |
| 142 |
$exp{uc($res)} = { |
| 143 |
used => $expecteds{$res}, |
| 144 |
limit => $limits->{uc($res)}, |
| 145 |
}; |
| 146 |
} |
| 147 |
|
| 148 |
# Now actually compare |
| 149 |
$self->assert_deep_equals(\%exp, \%act); |
| 150 |
} |
| 151 |
|
| 152 |
# Reset the recorded usage in the database. Used for testing |
| 153 |
# quota -f. Rather hacky. Both _set_quotaroot() and _set_limits() |
| 154 |
# can be used to set default values. |
| 155 |
sub _zap_quota |
| 156 |
{ |
| 157 |
my ($self, %params) = @_; |
| 158 |
|
| 159 |
my $quotaroot = $params{quotaroot} || $self->{quotaroot}; |
| 160 |
my $limits = $params{limits} || $self->{limits}->{$quotaroot}; |
| 161 |
my $useds = $params{useds} || {}; |
| 162 |
$useds = { map { uc($_) => $useds->{$_} } keys %$useds }; |
| 163 |
|
| 164 |
# double check that some other part of Cassandane didn't |
| 165 |
# accidentally futz with the expected quota db backend |
| 166 |
my $backend = $self->{instance}->{config}->get('quota_db'); |
| 167 |
$self->assert_str_equals('quotalegacy', $backend) |
| 168 |
if defined $backend; # the default value is also ok |
| 169 |
|
| 170 |
my ($uc) = ($quotaroot =~ m/^user[\.\/](.)/); |
| 171 |
my ($domain, $dirname); |
| 172 |
($quotaroot, $domain) = split '@', $quotaroot; |
| 173 |
if ($domain) { |
| 174 |
my ($dc) = ($domain =~ m/^(.)/); |
| 175 |
$dirname = $self->{instance}->{basedir} . "/conf/domain/$dc/$domain"; |
| 176 |
} |
| 177 |
else { |
| 178 |
$dirname = $self->{instance}->{basedir} . "/conf"; |
| 179 |
} |
| 180 |
$dirname .= "/quota/$uc"; |
| 181 |
my $qfn = $quotaroot; |
| 182 |
$qfn =~ s/\//\./g; |
| 183 |
my $filename = "$dirname/$qfn"; |
| 184 |
mkpath $dirname; |
| 185 |
|
| 186 |
open QUOTA,'>',$filename |
| 187 |
or die "Failed to open $filename for writing: $!"; |
| 188 |
|
| 189 |
# STORAGE is special and always present, but -1 if unlimited |
| 190 |
my $limit = $limits->{STORAGE} || -1; |
| 191 |
my $used = $useds->{STORAGE} || 0; |
| 192 |
print QUOTA "$used\n$limit"; |
| 193 |
|
| 194 |
# other resources have a leading keyword if present |
| 195 |
my %keywords = ( MESSAGE => 'M', $self->res_annot_storage => 'AS' ); |
| 196 |
foreach my $resource (keys %$limits) |
| 197 |
{ |
| 198 |
my $kw = $keywords{$resource} or next; |
| 199 |
$limit = $limits->{$resource}; |
| 200 |
$used = $useds->{$resource} || 0; |
| 201 |
print QUOTA " $kw $used $limit"; |
| 202 |
} |
| 203 |
|
| 204 |
print QUOTA "\n"; |
| 205 |
close QUOTA; |
| 206 |
|
| 207 |
$self->{instance}->_fix_ownership($self->{instance}{basedir} . "/conf/quota"); |
| 208 |
} |
| 209 |
|
| 210 |
# Utility function to check that there is no quota |
| 211 |
sub _check_no_quota |
| 212 |
{ |
| 213 |
my ($self) = @_; |
| 214 |
my $admintalk = $self->{adminstore}->get_client(); |
| 215 |
|
| 216 |
my @res = $admintalk->getquota($self->{quotaroot}); |
| 217 |
$self->assert_str_equals('no', $admintalk->get_last_completion_response()); |
| 218 |
} |
| 219 |
|
| 220 |
sub _check_smmap |
| 221 |
{ |
| 222 |
my ($self, $name, $expected) = @_; |
| 223 |
my $service = $self->{instance}->get_service('smmap'); |
| 224 |
my $sock = $service->get_socket(); |
| 225 |
|
| 226 |
print_netstring($sock, "0 $name"); |
| 227 |
my $res = get_netstring($sock); |
| 228 |
|
| 229 |
$self->assert($res =~ m/$expected/); |
| 230 |
} |
| 231 |
|
| 232 |
sub bogus_test_upgrade_v2_4 |
| 233 |
{ |
| 234 |
my ($self) = @_; |
| 235 |
|
| 236 |
xlog $self, "test resources usage computing upon upgrading a cyrus v2.4 mailbox"; |
| 237 |
|
| 238 |
$self->_set_quotaroot('user.cassandane'); |
| 239 |
my $talk = $self->{store}->get_client(); |
| 240 |
my $admintalk = $self->{adminstore}->get_client(); |
| 241 |
|
| 242 |
xlog $self, "set ourselves a basic limit"; |
| 243 |
$self->_set_limits($self->res_annot_storage => 100000); |
| 244 |
$self->_check_usages($self->res_annot_storage => 0); |
| 245 |
|
| 246 |
xlog $self, "store annotations"; |
| 247 |
my $data = $self->make_random_data(10); |
| 248 |
my $expected_annotation_storage = length($data); |
| 249 |
$talk->setmetadata($self->{store}->{folder}, '/private/comment', { Quote => $data }); |
| 250 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 251 |
$self->_check_usages($self->res_annot_storage => int($expected_annotation_storage/1024)); |
| 252 |
|
| 253 |
xlog $self, "restore cyrus v2.4 mailbox content and quota file"; |
| 254 |
$self->{instance}->unpackfile(abs_path('data/cyrus/quota_upgrade_v2_4.user.tar.gz'), 'data/user'); |
| 255 |
$self->{instance}->unpackfile(abs_path('data/cyrus/quota_upgrade_v2_4.quota.tar.gz'), 'conf/quota/c'); |
| 256 |
|
| 257 |
xlog $self, "upgrade to version 13 format (v2.5.0)"; |
| 258 |
$self->{instance}->run_command({ cyrus => 1 }, 'reconstruct', '-V' => 13); |
| 259 |
|
| 260 |
# count messages and size from restored mailbox |
| 261 |
my $expected_storage = 0; |
| 262 |
my $expected_message = 0; |
| 263 |
$talk->select($self->{store}->{folder}); |
| 264 |
my $responses = $talk->fetch('1:*', 'RFC822.SIZE'); |
| 265 |
$self->assert_str_equals('ok', $talk->get_last_completion_response()); |
| 266 |
$self->assert_not_null($responses); |
| 267 |
foreach my $response (values(%$responses)) { |
| 268 |
$expected_message++; |
| 269 |
$expected_storage += $response->{'rfc822.size'}; |
| 270 |
} |
| 271 |
$talk->close(); |
| 272 |
|
| 273 |
# check we did restore something |
| 274 |
$self->assert_num_not_equals($expected_storage, 0); |
| 275 |
$self->assert_num_not_equals($expected_message, 0); |
| 276 |
|
| 277 |
# set quota limits on resources which did not exist in previous cyrus versions; |
| 278 |
# when the mailbox was upgraded, new resources quota usage shall have been |
| 279 |
# computed automatically |
| 280 |
$self->_set_limits( |
| 281 |
storage => 100000, |
| 282 |
message => 50000, |
| 283 |
$self->res_annot_storage => 10000, |
| 284 |
); |
| 285 |
$self->_check_usages( |
| 286 |
storage => int($expected_storage/1024), |
| 287 |
message => $expected_message, |
| 288 |
$self->res_annot_storage => int($expected_annotation_storage/1024), |
| 289 |
); |
| 290 |
} |
| 291 |
|
| 292 |
sub XXtest_getset_multiple |
| 293 |
{ |
| 294 |
my ($self) = @_; |
| 295 |
|
| 296 |
xlog $self, "testing getting and setting multiple quota resources"; |
| 297 |
|
| 298 |
my $admintalk = $self->{adminstore}->get_client(); |
| 299 |
my $folder = "user.cassandane"; |
| 300 |
my @res; |
| 301 |
|
| 302 |
xlog $self, "checking there are no initial quotas"; |
| 303 |
@res = $admintalk->getquota($folder); |
| 304 |
$self->assert_str_equals('no', $admintalk->get_last_completion_response()); |
| 305 |
$self->assert($admintalk->get_last_error() =~ m/Quota root does not exist/i); |
| 306 |
|
| 307 |
xlog $self, "set both X-ANNOT-COUNT and X-ANNOT-SIZE quotas"; |
| 308 |
$admintalk->setquota($folder, "(x-annot-count 20 x-annot-size 16384)"); |
| 309 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 310 |
|
| 311 |
xlog $self, "get both resources back, and not STORAGE"; |
| 312 |
@res = $admintalk->getquota($folder); |
| 313 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 314 |
$self->assert_deep_equals(['X-ANNOT-COUNT', 0, 20, 'X-ANNOT-SIZE', 0, 16384], \@res); |
| 315 |
|
| 316 |
xlog $self, "set the X-ANNOT-SIZE resource only"; |
| 317 |
$admintalk->setquota($folder, "(x-annot-size 32768)"); |
| 318 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 319 |
|
| 320 |
xlog $self, "get new -SIZE only and neither STORAGE nor -COUNT"; |
| 321 |
@res = $admintalk->getquota($folder); |
| 322 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 323 |
$self->assert_deep_equals(['X-ANNOT-SIZE', 0, 32768], \@res); |
| 324 |
|
| 325 |
xlog $self, "set all of -COUNT -SIZE and STORAGE"; |
| 326 |
$admintalk->setquota($folder, "(x-annot-count 123 storage 123456 x-annot-size 65536)"); |
| 327 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 328 |
|
| 329 |
xlog $self, "get back all three new values"; |
| 330 |
@res = $admintalk->getquota($folder); |
| 331 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 332 |
$self->assert_deep_equals(['STORAGE', 0, 123456, 'X-ANNOT-COUNT', 0, 123, 'X-ANNOT-SIZE', 0, 65536], \@res); |
| 333 |
|
| 334 |
xlog $self, "clear all quotas"; |
| 335 |
$admintalk->setquota($folder, "()"); |
| 336 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 337 |
|
| 338 |
# Note: the RFC does not define what happens if you remove all the |
| 339 |
# quotas from a quotaroot. Cyrus leaves the quotaroot around until |
| 340 |
# quota -f is run to clean it up. |
| 341 |
xlog $self, "get back an empty set of quotas, but the quota root still exists"; |
| 342 |
@res = $admintalk->getquota($folder); |
| 343 |
$self->assert_str_equals('ok', $admintalk->get_last_completion_response()); |
| 344 |
$self->assert_deep_equals([], \@res); |
| 345 |
} |
| 346 |
|
| 347 |
# Magic: the word 'replication' in the name enables a replica |
| 348 |
sub XXtest_replication_multiple |
| 349 |
:needs_component_replication |
| 350 |
{ |
| 351 |
my ($self) = @_; |
| 352 |
|
| 353 |
xlog $self, "testing replication of multiple quotas"; |
| 354 |
|
| 355 |
my $mastertalk = $self->{master_adminstore}->get_client(); |
| 356 |
my $replicatalk = $self->{replica_adminstore}->get_client(); |
| 357 |
|
| 358 |
my $folder = "user.cassandane"; |
| 359 |
my @res; |
| 360 |
|
| 361 |
xlog $self, "checking there are no initial quotas"; |
| 362 |
@res = $mastertalk->getquota($folder); |
| 363 |
$self->assert_str_equals('no', $mastertalk->get_last_completion_response()); |
| 364 |
$self->assert($mastertalk->get_last_error() =~ m/Quota root does not exist/i); |
| 365 |
@res = $replicatalk->getquota($folder); |
| 366 |
$self->assert_str_equals('no', $replicatalk->get_last_completion_response()); |
| 367 |
$self->assert($replicatalk->get_last_error() =~ m/Quota root does not exist/i); |
| 368 |
|
| 369 |
xlog $self, "set a X-ANNOT-COUNT and X-ANNOT-SIZE quotas on the master"; |
| 370 |
$mastertalk->setquota($folder, "(x-annot-count 20 x-annot-size 16384)"); |
| 371 |
$self->assert_str_equals('ok', $mastertalk->get_last_completion_response()); |
| 372 |
|
| 373 |
xlog $self, "run replication"; |
| 374 |
$self->run_replication(); |
| 375 |
$self->check_replication('cassandane'); |
| 376 |
$mastertalk = $self->{master_adminstore}->get_client(); |
| 377 |
$replicatalk = $self->{replica_adminstore}->get_client(); |
| 378 |
|
| 379 |
xlog $self, "check that the new quota is at both ends"; |
| 380 |
@res = $mastertalk->getquota($folder); |
| 381 |
$self->assert_str_equals('ok', $mastertalk->get_last_completion_response()); |
| 382 |
$self->assert_deep_equals(['X-ANNOT-COUNT', 0, 20, 'X-ANNOT-SIZE', 0, 16384], \@res); |
| 383 |
@res = $replicatalk->getquota($folder); |
| 384 |
$self->assert_str_equals('ok', $replicatalk->get_last_completion_response()); |
| 385 |
$self->assert_deep_equals(['X-ANNOT-COUNT', 0, 20, 'X-ANNOT-SIZE', 0, 16384], \@res); |
| 386 |
|
| 387 |
xlog $self, "set the X-ANNOT-SIZE quota on the master"; |
| 388 |
$mastertalk->setquota($folder, "(x-annot-size 32768)"); |
| 389 |
$self->assert_str_equals('ok', $mastertalk->get_last_completion_response()); |
| 390 |
|
| 391 |
xlog $self, "run replication"; |
| 392 |
$self->run_replication(); |
| 393 |
$self->check_replication('cassandane'); |
| 394 |
$mastertalk = $self->{master_adminstore}->get_client(); |
| 395 |
$replicatalk = $self->{replica_adminstore}->get_client(); |
| 396 |
|
| 397 |
xlog $self, "check that the new quota is at both ends"; |
| 398 |
@res = $mastertalk->getquota($folder); |
| 399 |
$self->assert_str_equals('ok', $mastertalk->get_last_completion_response()); |
| 400 |
$self->assert_deep_equals(['X-ANNOT-SIZE', 0, 32768], \@res); |
| 401 |
@res = $replicatalk->getquota($folder); |
| 402 |
$self->assert_str_equals('ok', $replicatalk->get_last_completion_response()); |
| 403 |
$self->assert_deep_equals(['X-ANNOT-SIZE', 0, 32768], \@res); |
| 404 |
|
| 405 |
xlog $self, "clear all the quotas"; |
| 406 |
$mastertalk->setquota($folder, "()"); |
| 407 |
$self->assert_str_equals('ok', $mastertalk->get_last_completion_response()); |
| 408 |
|
| 409 |
xlog $self, "run replication"; |
| 410 |
$self->run_replication(); |
| 411 |
$self->check_replication('cassandane'); |
| 412 |
$mastertalk = $self->{master_adminstore}->get_client(); |
| 413 |
$replicatalk = $self->{replica_adminstore}->get_client(); |
| 414 |
|
| 415 |
xlog $self, "check that the new quota is at both ends"; |
| 416 |
@res = $mastertalk->getquota($folder); |
| 417 |
$self->assert_str_equals('ok', $mastertalk->get_last_completion_response()); |
| 418 |
$self->assert_deep_equals([], \@res); |
| 419 |
@res = $replicatalk->getquota($folder); |
| 420 |
$self->assert_str_equals('ok', $replicatalk->get_last_completion_response()); |
| 421 |
$self->assert_deep_equals([], \@res); |
| 422 |
} |
| 423 |
|
| 424 |
Cassandane::Cyrus::TestCase::magic(Bug3735 => sub { |
| 425 |
my ($testcase) = @_; |
| 426 |
$testcase->config_set(quota_db => 'quotalegacy'); |
| 427 |
$testcase->config_set(hashimapspool => 1); |
| 428 |
$testcase->config_set(fulldirhash => 1); |
| 429 |
$testcase->config_set(virtdomains => 0); |
| 430 |
}); |
| 431 |
|
| 432 |
use Cassandane::Tiny::Loader 'tiny-tests/Quota'; |
| 433 |
|
| 434 |
1; |