Debian Codegraph @

cassandane/Cassandane/Cyrus/MaxMessages.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2024 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::MaxMessages;
41 use strict;
42 use warnings;
43 use v5.10;
44 use Data::Dumper;
45 use Net::DAVTalk 0.14;
46 use Net::CardDAVTalk 0.05;
47 use Net::CardDAVTalk::VCard;
48
49 use lib '.';
50 use base qw(Cassandane::Cyrus::TestCase);
51 use Cassandane::Generator;
52 use Cassandane::Util::Log;
53
54 my $LOTS = 20;
55 my $LIMITED = 5;
56
57 sub new
58 {
59 my $class = shift;
60
61 my $config = Cassandane::Config->default()->clone();
62 $config->set(
63 caldav_realm => 'Cassandane',
64 calendar_user_address_set => 'example.com',
65 caldav_historical_age => -1,
66 conversations => 1,
67 httpmodules => 'caldav carddav jmap',
68 httpallowcompress => 'no',
69 icalendar_max_size => 100000,
70 jmap_nonstandard_extensions => 'yes',
71 sieve_maxscripts => $LOTS,
72 vcard_max_size => 100000,
73 );
74
75 my $self = $class->SUPER::new({
76 config => $config,
77 jmap => 1,
78 services => ['imap', 'http', 'sieve'],
79 smtpdaemon => 1,
80 }, @_);
81
82 $self->needs('component', 'jmap');
83 $self->needs('component', 'sieve');
84 return $self;
85 }
86
87 sub set_up
88 {
89 my ($self) = @_;
90 $self->SUPER::set_up();
91 $ENV{DEBUGDAV} = 1;
92 $ENV{JMAP_ALWAYS_FULL} = 1;
93 }
94
95 sub tear_down
96 {
97 my ($self) = @_;
98 $self->SUPER::tear_down();
99 }
100
101 sub _random_vevent
102 {
103 my ($self) = @_;
104 state $counter = 1;
105
106 my $uuid = $self->{caldav}->genuuid();
107
108 my $ics = <<"EOF";
109 BEGIN:VCALENDAR
110 VERSION:2.0
111 PRODID:-//Apple Inc.//Mac OS X 10.10.4//EN
112 CALSCALE:GREGORIAN
113 BEGIN:VEVENT
114 CREATED:20150701T234327Z
115 UID:$uuid
116 DTEND;TZID=Australia/Melbourne:20160601T183000
117 TRANSP:OPAQUE
118 DTSTART;TZID=Australia/Melbourne:20160601T153000
119 DTSTAMP:20150806T234327Z
120 DESCRIPTION:event $counter
121 END:VEVENT
122 END:VCALENDAR
123 EOF
124
125 $counter ++;
126
127 return $ics, $uuid;
128 }
129
130 sub _random_vcard
131 {
132 my $fn = Cassandane::Generator::make_random_address()->name();
133 my ($first, $middle, $last) = split /[\s\.]+/, $fn;
134 my $n = "$last;$first;$middle;;";
135 my $str = <<"EOF";
136 BEGIN:VCARD
137 VERSION:3.0
138 N:$n
139 FN:$fn
140 REV:2008-04-24T19:52:43Z
141 END:VCARD
142 EOF
143 return $str;
144 }
145
146 sub put_vevent
147 {
148 my ($self, $calendarid) = @_;
149
150 my ($ics, $uuid) = $self->_random_vevent();
151 my $href = "$calendarid/$uuid.ics";
152
153 $self->{caldav}->Request('PUT', $href, $ics,
154 'Content-Type' => 'text/calendar');
155 }
156
157 sub put_vcard
158 {
159 my ($self, $addrbookid) = @_;
160
161 my $vcard = Net::CardDAVTalk::VCard->new_fromstring(_random_vcard());
162
163 $self->{carddav}->NewContact($addrbookid, $vcard);
164 }
165
166 sub put_script
167 {
168 my ($self) = @_;
169 state $counter = 1;
170
171 my $name = "script $counter";
172 my $script = "# $name\r\nkeep;\r\n";
173 $counter ++;
174
175 $self->{jmap}->DefaultUsing([
176 'urn:ietf:params:jmap:core',
177 'urn:ietf:params:jmap:mail',
178 'https://cyrusimap.org/ns/jmap/sieve',
179 'https://cyrusimap.org/ns/jmap/blob',
180 ]);
181
182 my $res = $self->{jmap}->CallMethods([
183 ['Blob/upload', {
184 create => {
185 "A" => { data => [{'data:asText' => $script}] }
186 }
187 }, "R0"],
188 ['SieveScript/set', {
189 create => {
190 "1" => {
191 name => $name,
192 blobId => "#A"
193 },
194 },
195 }, "R1"],
196 ]);
197
198 $self->assert_not_null($res);
199 $self->assert_not_null($res->[1][1]{created}{"1"}{id});
200 }
201
202 # XXX lots of copies of getinbox -- dedup them!
203 sub getinbox
204 {
205 my ($self, $args) = @_;
206
207 $args = {} unless $args;
208
209 my $jmap = $self->{jmap};
210
211 xlog $self, "get existing mailboxes";
212 my $res = $jmap->CallMethods([['Mailbox/get', $args, "R1"]]);
213 $self->assert_not_null($res);
214
215 my %m = map { $_->{name} => $_ } @{$res->[0][1]{list}};
216 return $m{"Inbox"};
217 }
218
219 sub _submissions_mailbox
220 {
221 my ($self, $counter) = @_;
222
223 my $jmap = $self->{jmap};
224 my $folder = "submission $counter";
225
226 my $inboxId = $self->getinbox()->{id};
227 $self->assert_not_null($inboxId);
228
229 my $res = $jmap->CallMethods([['Mailbox/set', {
230 create => {
231 "m$counter" => {
232 parentId => $inboxId,
233 name => $folder,
234 },
235 },
236 }, "R1"]]);
237
238 $self->assert_not_null($res);
239 $self->assert_not_null($res->[0][1]{created}{"m$counter"}{id});
240
241 return $res->[0][1]{created}{"m$counter"}{id};
242 }
243
244 sub put_submission
245 {
246 my ($self) = @_;
247 state $counter = 0;
248
249 $counter ++;
250
251 my $jmap = $self->{jmap};
252 $jmap->DefaultUsing([
253 'urn:ietf:params:jmap:core',
254 'urn:ietf:params:jmap:mail',
255 'urn:ietf:params:jmap:submission',
256 ]);
257
258 my $res = $jmap->CallMethods( [ [ 'Identity/get', {}, "R1" ] ] );
259 my $identityId = $res->[0][1]->{list}[0]->{id};
260 $self->assert_not_null($identityId);
261
262 # upload each draft to its own mailbox so that we don't accidentally
263 # exceed mailbox_maxmessages_email
264 my $mailboxId = $self->_submissions_mailbox($counter);
265 $self->assert_not_null($mailboxId);
266
267 my $rcpt = Cassandane::Generator::make_random_address();
268
269 $res = $jmap->CallMethods([
270 ['Email/set', {
271 create => {
272 "m$counter" => {
273 mailboxIds => {
274 $mailboxId => JSON::true,
275 },
276 from => [{
277 name => 'cassandane',
278 email => 'cassandane@local',
279 }],
280 to => [{
281 name => $rcpt->name(),
282 email => $rcpt->address(),
283 }],
284 subject => "message $counter",
285 bodyStructure => {
286 type => 'text/plain',
287 partId => 'part1',
288 },
289 bodyValues => {
290 part1 => {
291 value => 'hello world',
292 }
293 },
294 },
295 },
296 }, 'R1'],
297 [ 'EmailSubmission/set', {
298 create => {
299 "s$counter" => {
300 identityId => $identityId,
301 emailId => "#m$counter",
302 envelope => {
303 mailFrom => {
304 email => 'cassandane@localhost',
305 parameters => {
306 "holdfor" => "30",
307 }
308 },
309 rcptTo => [{
310 email => $rcpt->address(),
311 }],
312 },
313 }
314 },
315 }, 'R2' ],
316 ]);
317
318 $self->assert_not_null($res);
319 $self->assert_num_equals(1, scalar keys %{$res->[0][1]{created}});
320 $self->assert_num_equals(0, scalar keys %{$res->[0][1]{notCreated}});
321 $self->assert_num_equals(1, scalar keys %{$res->[1][1]{created}});
322 $self->assert_num_equals(0, scalar keys %{$res->[1][1]{notCreated}});
323 }
324
325 sub put_email
326 {
327 my ($self) = @_;
328 state $counter = 0;
329
330 $counter ++;
331
332 $self->make_message("message $counter");
333 }
334
335 sub test_maxmsg_addressbook_limited
336 :JMAPExtensions :NoStartInstances
337 {
338 my ($self) = @_;
339
340 my $mailbox_maxmessages_addressbook = $LIMITED;
341 $self->{instance}->{config}->set(
342 mailbox_maxmessages_addressbook => $mailbox_maxmessages_addressbook,
343 );
344 $self->_start_instances();
345 $self->_setup_http_service_objects();
346
347 my $carddav = $self->{carddav};
348 my $addrbookid = $carddav->NewAddressBook('foo');
349 $self->assert_not_null($addrbookid);
350
351 # should be able to upload 5
352 foreach my $i (1..$mailbox_maxmessages_addressbook) {
353 $self->put_vcard($addrbookid);
354 }
355
356 # but any more should be rejected
357 eval {
358 $self->put_vcard($addrbookid);
359 };
360 my $e = $@;
361 $self->assert_not_null($e);
362 $self->assert_matches(qr{quota-not-exceeded}, $e);
363
364 # should have syslogged about it too
365 $self->assert_syslog_matches($self->{instance},
366 qr{client hit per-addressbook exists limit});
367
368 # should be able to upload lots of calendar events
369 my $caldav = $self->{caldav};
370 my $calendarid = $caldav->NewCalendar({name => 'mycalendar'});
371 $self->assert_not_null($calendarid);
372 foreach my $i (1..$LOTS) {
373 $self->put_vevent($calendarid);
374 }
375
376 # should be able to upload lots of sieve scripts
377 foreach my $i (1..$LOTS) {
378 $self->put_script();
379 }
380
381 # should be able to upload lots of jmap submissions
382 foreach my $i (1..$LOTS) {
383 $self->put_submission();
384 }
385
386 # should be able to upload lots of regular emails
387 foreach my $i (1..$LOTS) {
388 $self->put_email();
389 }
390 }
391
392 sub test_maxmsg_calendar_limited
393 :JMAPExtensions :NoStartInstances
394 {
395 my ($self) = @_;
396
397 my $mailbox_maxmessages_calendar = $LIMITED;
398 $self->{instance}->{config}->set(
399 mailbox_maxmessages_calendar => $mailbox_maxmessages_calendar,
400 );
401 $self->_start_instances();
402 $self->_setup_http_service_objects();
403
404 my $caldav = $self->{caldav};
405 my $calendarid = $caldav->NewCalendar({name => 'mycalendar'});
406 $self->assert_not_null($calendarid);
407
408 # should be able to upload 5
409 foreach my $i (1..$mailbox_maxmessages_calendar) {
410 $self->put_vevent($calendarid);
411 }
412
413 # but any more should be rejected
414 eval {
415 $self->put_vevent($calendarid);
416 };
417 my $e = $@;
418 $self->assert_not_null($e);
419 $self->assert_matches(qr{quota-not-exceeded}, $e);
420
421 # should have syslogged about it too
422 $self->assert_syslog_matches($self->{instance},
423 qr{client hit per-calendar exists limit});
424
425 # should be able to upload lots of contacts
426 my $carddav = $self->{carddav};
427 my $addrbookid = $carddav->NewAddressBook('foo');
428 $self->assert_not_null($addrbookid);
429 foreach my $i (1..$LOTS) {
430 $self->put_vcard($addrbookid);
431 }
432
433 # should be able to upload lots of sieve scripts
434 foreach my $i (1..$LOTS) {
435 $self->put_script();
436 }
437
438 # should be able to upload lots of jmap submissions
439 foreach my $i (1..$LOTS) {
440 $self->put_submission();
441 }
442
443 # should be able to upload lots of regular emails
444 foreach my $i (1..$LOTS) {
445 $self->put_email();
446 }
447 }
448
449 sub test_maxmsg_email_limited
450 :JMAPExtensions :NoStartInstances
451 {
452 my ($self) = @_;
453
454 my $mailbox_maxmessages_email = $LIMITED;
455 $self->{instance}->{config}->set(
456 mailbox_maxmessages_email => $mailbox_maxmessages_email,
457 );
458 $self->_start_instances();
459 $self->_setup_http_service_objects();
460
461 # should be able to upload 5
462 foreach my $i (1..$mailbox_maxmessages_email) {
463 $self->put_email();
464 }
465
466 # but any more should be rejected
467 eval {
468 $self->put_email();
469 };
470 my $e = $@;
471 $self->assert_not_null($e);
472 $self->assert_matches(qr{Over quota}, $e);
473
474 # should have syslogged about it too
475 $self->assert_syslog_matches($self->{instance},
476 qr{client hit per-mailbox exists limit});
477
478 # should be able to upload lots of contacts
479 my $carddav = $self->{carddav};
480 my $addrbookid = $carddav->NewAddressBook('foo');
481 $self->assert_not_null($addrbookid);
482 foreach my $i (1..$LOTS) {
483 $self->put_vcard($addrbookid);
484 }
485
486 # should be able to upload lots of calendar events
487 my $caldav = $self->{caldav};
488 my $calendarid = $caldav->NewCalendar({name => 'mycalendar'});
489 $self->assert_not_null($calendarid);
490 foreach my $i (1..$LOTS) {
491 $self->put_vevent($calendarid);
492 }
493
494 # should be able to upload lots of sieve scripts
495 foreach my $i (1..$LOTS) {
496 $self->put_script();
497 }
498
499 # should be able to upload lots of jmap submissions
500 foreach my $i (1..$LOTS) {
501 $self->put_submission();
502 }
503 }
504
505 sub test_maxmsg_unlimited
506 :JMAPExtensions
507 {
508 my ($self) = @_;
509
510 # should be able to upload lots of contacts
511 my $carddav = $self->{carddav};
512 my $addrbookid = $carddav->NewAddressBook('foo');
513 $self->assert_not_null($addrbookid);
514 foreach my $i (1..$LOTS) {
515 $self->put_vcard($addrbookid);
516 }
517
518 # should be able to upload lots of calendar events
519 my $caldav = $self->{caldav};
520 my $calendarid = $caldav->NewCalendar({name => 'mycalendar'});
521 $self->assert_not_null($calendarid);
522 foreach my $i (1..$LOTS) {
523 $self->put_vevent($calendarid);
524 }
525
526 # should be able to upload lots of sieve scripts
527 foreach my $i (1..$LOTS) {
528 $self->put_script();
529 }
530
531 # should be able to upload lots of jmap submissions
532 foreach my $i (1..$LOTS) {
533 $self->put_submission();
534 }
535
536 # should be able to upload lots of regular emails
537 foreach my $i (1..$LOTS) {
538 $self->put_email();
539 }
540 }
541
542 1;