Debian Codegraph @

cassandane/Cassandane/Cyrus/Idle.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::Idle;
41 use strict;
42 use warnings;
43 use DateTime;
44
45 use lib '.';
46 use base qw(Cassandane::Cyrus::TestCase);
47 use Cassandane::Util::Log;
48
49 sub new
50 {
51 my $class = shift;
52 my $config = Cassandane::Config->default()->clone();
53 $config->set(imapidlepoll => 2);
54 return $class->SUPER::new({
55 config => $config,
56 deliver => 1,
57 start_instances => 0,
58 }, @_);
59 }
60
61 sub set_up
62 {
63 my ($self) = @_;
64 $self->SUPER::set_up();
65 }
66
67 sub tear_down
68 {
69 my ($self) = @_;
70 $self->SUPER::tear_down();
71 }
72
73 sub start_and_abort_idled
74 {
75 my ($self) = @_;
76
77 # We don't start idled via the START section in cyrus.conf,
78 # because master would restart it when we kill it, and we
79 # want to test that the fallback to polling mode works even
80 # when it's not restarted.
81 #
82 # Also note, one of the effects of the -d option is to prevent
83 # idled forking, which lets us predict which pid to kill.
84
85 my $pid = $self->{instance}->run_command({
86 cyrus => 1,
87 background => 1
88 }, 'idled', '-d');
89 xlog $self, "pid of idled should be $pid";
90
91 xlog $self, "giving idled some time to start up";
92 my $tries = 60;
93 my $idle_sock = $self->{instance}->{basedir} . "/conf/socket/idle";
94 while ($tries--) {
95 last if -S $idle_sock;
96 sleep 1;
97 }
98 $self->assert($tries > 0, "idled started successfully");
99
100 xlog $self, "bring idled's reign to an abrupt and brutal end";
101 kill('KILL', $pid)
102 or die "Failed to kill idled $pid: $!";
103
104 # reap_command will 'die' because the process terminated
105 # on SIGKILL. We need to avoid that stopping the test.
106 # But we still need to waitpid() to avoid zombies.
107 xlog $self, "reaping pid $pid";
108 eval { $self->{instance}->reap_command($pid); };
109
110 # Now, no idled is running, but any state created by idled in the
111 # filesystem is still present. In particular, the idle socket.
112 # Let's check that our assumption is correct.
113 xlog $self, "check that idle left a socket lying around";
114 $self->assert( -S $idle_sock, "$idle_sock exists and is a socket");
115 }
116
117 sub test_disabled
118 {
119 my ($self) = @_;
120
121 xlog $self, "Test that the IDLE command can be disabled in";
122 xlog $self, "imapd.conf by setting imapidlepoll = 0";
123
124 xlog $self, "Starting up the instance";
125 $self->{instance}->{config}->set(imapidlepoll => '0');
126 $self->{instance}->start();
127 my $svc = $self->{instance}->get_service('imap');
128
129 my $store = $svc->create_store(folder => 'INBOX');
130 my $talk = $store->get_client();
131
132 xlog $self, "The server should not report the IDLE capability";
133 $self->assert(!$talk->capability()->{idle});
134
135 xlog $self, "The IDLE command should not be recognised";
136 # Note that we don't use idle_begin() because that will get
137 # upset if we get "tag BAD ..." back instead of "+ something".
138 my $r = $talk->_imap_cmd('idle', 0, '');
139 $self->assert_null($r);
140 $self->assert_str_equals('bad', $talk->get_last_completion_response());
141 $self->assert_matches(qr/Unrecognized command/, $talk->get_last_error());
142 }
143
144 sub common_basic
145 {
146 my ($self) = @_;
147
148 my $svc = $self->{instance}->get_service('imap');
149
150 my $store = $svc->create_store(folder => 'INBOX');
151 my $talk = $store->get_client();
152 $store->_select();
153
154 xlog $self, "The server should report the IDLE capability";
155 $self->assert($talk->capability()->{idle});
156
157 xlog $self, "Sending the IDLE command";
158 $store->idle_begin()
159 or die "IDLE failed: $@";
160
161 xlog $self, "Poll for any unsolicited response - should be none";
162 my $r = $store->idle_response({}, 0);
163 $self->assert(!$r, "No unsolicted response");
164
165 xlog $self, "Sending DONE continuation";
166 $store->idle_end({});
167 $self->assert_str_equals('ok', $talk->get_last_completion_response());
168
169 xlog $self, "Testing that normal IMAP commands still work";
170 my $res = $talk->status('INBOX', '(messages unseen)');
171 $self->assert_deep_equals({ messages => 0, unseen => 0 }, $res);
172 }
173
174 sub test_basic_idled
175 :needs_component_idled
176 {
177 my ($self) = @_;
178
179 xlog $self, "Basic test of the IDLE command, idled started";
180
181 $self->{instance}->{config}->set(imapidlepoll => '2');
182 $self->{instance}->add_start(name => 'idled',
183 argv => [ 'idled' ]);
184 $self->{instance}->start();
185 $self->common_basic();
186 }
187
188 sub test_basic_noidled
189 {
190 my ($self) = @_;
191
192 xlog $self, "Basic test of the IDLE command, no idled started";
193
194 $self->{instance}->{config}->set(imapidlepoll => '2');
195 $self->{instance}->start();
196 $self->common_basic();
197 }
198
199 sub test_basic_abortedidled
200 :needs_component_idled
201 {
202 my ($self) = @_;
203
204 xlog $self, "Basic test of the IDLE command, idled started but aborted";
205
206 $self->{instance}->{config}->set(imapidlepoll => '2');
207 $self->{instance}->start();
208 $self->start_and_abort_idled();
209
210 $self->common_basic();
211 }
212
213 sub common_delivery
214 {
215 my ($self) = @_;
216
217 xlog $self, "Starting up the instance";
218 my $svc = $self->{instance}->get_service('imap');
219
220 my $store = $svc->create_store(folder => 'INBOX');
221 my $talk = $store->get_client();
222 $store->_select();
223
224 xlog $self, "Sending the IDLE command";
225 $store->idle_begin()
226 or die "IDLE failed: $@";
227
228 xlog $self, "Poll for any unsolicited response - should be none";
229 my $r = $store->idle_response({}, 0);
230 $self->assert(!$r, "No unsolicted response");
231
232 xlog $self, "sleeping for 3 seconds";
233 sleep(3);
234
235 xlog $self, "Poll for any unsolicited response - should be none";
236 $r = $store->idle_response({}, 0);
237 $self->assert(!$r, "No unsolicted response");
238
239 xlog $self, "Deliver a message";
240 my $msg = $self->{gen}->generate(subject => "Message 1");
241 $self->{instance}->deliver($msg);
242
243 $r = $store->idle_response({}, 5);
244 $self->assert($r, "received an unsolicited response");
245 $r = $store->idle_response({}, 5);
246 $self->assert($r, "received an unsolicited response");
247 $r = $store->idle_response({}, 1);
248 $self->assert(!$r, "no more unsolicited responses");
249 $self->assert_num_equals(1, $talk->get_response_code('exists'));
250 $self->assert_num_equals(1, $talk->get_response_code('recent'));
251
252 xlog $self, "Sending DONE continuation";
253 $store->idle_end({});
254 $self->assert_str_equals('ok', $talk->get_last_completion_response());
255 }
256
257 sub test_delivery_idled
258 :needs_component_idled
259 {
260 my ($self) = @_;
261
262 xlog $self, "Test the IDLE command vs local delivery, idled started";
263
264 $self->{instance}->{config}->set(imapidlepoll => '2');
265 $self->{instance}->add_start(name => 'idled',
266 argv => [ 'idled' ]);
267 $self->{instance}->start();
268 $self->common_delivery();
269 }
270
271 sub test_delivery_noidled
272 {
273 my ($self) = @_;
274
275 xlog $self, "Test the IDLE command vs local delivery, no idled started";
276
277 $self->{instance}->{config}->set(imapidlepoll => '2');
278 $self->{instance}->start();
279 $self->common_delivery();
280 }
281
282 sub test_delivery_abortedidled
283 :needs_component_idled
284 {
285 my ($self) = @_;
286
287 xlog $self, "Test the IDLE command vs local delivery, idled started but aborted";
288
289 $self->{instance}->{config}->set(imapidlepoll => '2');
290 $self->{instance}->start();
291 $self->start_and_abort_idled();
292
293 $self->common_delivery();
294 }
295
296 sub common_shutdownfile
297 {
298 my ($self) = @_;
299
300 xlog $self, "Starting up the instance";
301 my $svc = $self->{instance}->get_service('imap');
302
303 my $store = $svc->create_store(folder => 'INBOX');
304 my $talk = $store->get_client();
305 $store->_select();
306
307 xlog $self, "Sending the IDLE command";
308 $store->idle_begin()
309 or die "IDLE failed: $@";
310
311 xlog $self, "Poll for any unsolicited response - should be none";
312 my $r = $store->idle_response({}, 0);
313 $self->assert(!$r, "No unsolicted response");
314
315 xlog $self, "sleeping for 3 seconds";
316 sleep(3);
317
318 xlog $self, "Poll for any unsolicited response - should be none";
319 $r = $store->idle_response({}, 0);
320 $self->assert(!$r, "No unsolicted response");
321
322 $self->assert_null($talk->get_response_code('alert'));
323
324 xlog $self, "Write some text to the shutdown file";
325 my $admin_store = $svc->create_store(folder => 'user.cassandane',
326 username => 'admin');
327 my $shut_message = "The Mayans were right";
328 $admin_store->get_client()->setmetadata("",
329 "/shared/vendor/cmu/cyrus-imapd/shutdown", $shut_message);
330 $admin_store->disconnect();
331 $admin_store = undef;
332
333 # We want to override Mail::IMAPTalk's builtin handling of the BYE
334 # untagged response, as it will 'die' immediately without parsing
335 # the remainder of the line and especially without picking out the
336 # [ALERT] message that we want to see.
337 my $got_bye_alert;
338 my $handlers =
339 {
340 bye => sub
341 {
342 my ($response, $rr) = @_;
343 if (lc($rr->[0]) eq '[alert]')
344 {
345 # Arguments to [ALERT] is the rest of the line
346 # Sadly we've already split on whitespace but lets
347 # hope the original message only had single spaces
348 $got_bye_alert = join(' ', splice(@$rr, 1));
349 }
350 }
351 };
352
353 xlog $self, "Check that we got a BYE [ALERT] response with the message";
354 $r = $store->idle_response($handlers, 5);
355 $self->assert($r, "Got an unsolicited response");
356 $self->assert_not_null($got_bye_alert);
357 $self->assert_str_equals($shut_message, $got_bye_alert);
358
359 xlog $self, "Check that the server disconnected";
360 eval
361 {
362 # We use _send_cmd() and _next_atom() rather the normal path
363 # through _imap_cmd() because the latter will warn() to stderr
364 # about the exception we're about to generate, which is
365 # downright untidy.
366 $talk->_send_cmd('status', 'INBOX', '(messages unseen)');
367 $talk->_parse_response({});
368 };
369 my $mm = $@; # this doesn't survive unless we save it
370 $self->assert_matches(qr/IMAP Connection closed by other end/, $mm);
371 }
372
373 sub test_shutdownfile_idled
374 :needs_component_idled
375 {
376 my ($self) = @_;
377
378 xlog $self, "Test the IDLE command vs the shutdownfile, idled started";
379
380 $self->{instance}->{config}->set(imapidlepoll => '2');
381 $self->{instance}->add_start(name => 'idled',
382 argv => [ 'idled' ]);
383 $self->{instance}->start();
384 $self->common_shutdownfile();
385 }
386
387 sub test_shutdownfile_noidled
388 {
389 my ($self) = @_;
390
391 xlog $self, "Test the IDLE command vs the shutdownfile";
392
393 $self->{instance}->{config}->set(imapidlepoll => '2');
394 $self->{instance}->start();
395 $self->common_shutdownfile();
396 }
397
398 sub test_shutdownfile_abortedidled
399 :needs_component_idled
400 {
401 my ($self) = @_;
402
403 xlog $self, "Test the IDLE command vs the shutdownfile, idled started but aborted";
404
405 $self->{instance}->{config}->set(imapidlepoll => '2');
406 $self->{instance}->start();
407 $self->start_and_abort_idled();
408
409 $self->common_shutdownfile();
410 }
411
412 sub test_sigterm
413 :needs_component_idled
414 {
415 my ($self) = @_;
416
417 xlog $self, "Test that an imapd can be killed with SIGTERM";
418 xlog $self, "while executing an IDLE command";
419
420 $self->{instance}->{config}->set(imapidlepoll => '2');
421 $self->{instance}->add_start(name => 'idled',
422 argv => [ 'idled' ]);
423 xlog $self, "Starting up the instance";
424 $self->{instance}->start();
425
426 my $svc = $self->{instance}->get_service('imap');
427
428 my $store = $svc->create_store(folder => 'INBOX');
429 my $talk = $store->get_client();
430
431 $store->_select();
432
433 xlog $self, "Sending the IDLE command";
434 $store->idle_begin()
435 or die "IDLE failed: $@";
436
437 # procinfo: pid SP servicename SP host [SP user] [SP mailbox] [SP cmdname]
438 my $procinfo = join '', $self->{instance}->run_cyr_info('proc');
439 my ($imapd_pid, undef) =
440 ($procinfo =~ m/^(\d+) imap (.+) cassandane user.cassandane Idle$/);
441 $self->assert_not_null($imapd_pid);
442 $imapd_pid = 0 + $imapd_pid;
443 $self->assert($imapd_pid > 1);
444 xlog $self, "PID of imapd process is $imapd_pid";
445
446 xlog $self, "Poll for any unsolicited response - should be none";
447 my $r = $store->idle_response({}, 0);
448 $self->assert(!$r, "No unsolicted response");
449
450 xlog $self, "sleeping for 3 seconds";
451 sleep(3);
452
453 xlog $self, "Poll for any unsolicited response - should be none";
454 $r = $store->idle_response({}, 0);
455 $self->assert(!$r, "No unsolicted response");
456
457 $self->assert_null($talk->get_response_code('alert'));
458
459 xlog $self, "Send SIGQUIT (or worse) to the imapd";
460 $r = Cassandane::Instance::_stop_pid($imapd_pid);
461 $self->assert($r == 1, "shutdown required brute force");
462
463 xlog $self, "Check that the server disconnected";
464 eval
465 {
466 # We use _send_cmd() and _next_atom() rather the normal path
467 # through _imap_cmd() because the latter will warn() to stderr
468 # about the exception we're about to generate, which is
469 # downright untidy.
470 $talk->_send_cmd('status', 'INBOX', '(messages unseen)');
471 $talk->_parse_response({});
472 };
473 my $mm = $@; # this doesn't survive unless we save it
474 $self->assert_matches(qr/IMAP Connection closed by other end/, $mm);
475 }
476
477 sub test_sigterm_many
478 :needs_component_idled
479 {
480 my ($self) = @_;
481
482 xlog $self, "Test that the Cyrus instance can be cleanly shut";
483 xlog $self, "down with SIGTERM while many imapds execute an";
484 xlog $self, "IDLE command";
485
486 $self->{instance}->{config}->set(imapidlepoll => '2');
487 $self->{instance}->add_start(name => 'idled',
488 argv => [ 'idled' ]);
489 xlog $self, "Starting up the instance";
490 $self->{instance}->start();
491
492 my $svc = $self->{instance}->get_service('imap');
493
494 my $N = 16;
495 my @stores;
496 my $r;
497
498 for (my $i = 0 ; $i < $N ; $i++)
499 {
500 my $store = $svc->create_store(folder => 'INBOX');
501 push(@stores, $store);
502 my $talk = $store->get_client();
503
504 $store->_select();
505
506 xlog $self, "Sending the IDLE command";
507 $store->idle_begin()
508 or die "IDLE failed: $@";
509
510 xlog $self, "Poll for any unsolicited response - should be none";
511 $r = $store->idle_response({}, 0);
512 $self->assert(!$r, "No unsolicted response");
513 }
514
515 xlog $self, "sleeping for 3 seconds";
516 sleep(3);
517
518 foreach my $store (@stores)
519 {
520 xlog $self, "Poll for any unsolicited response - should be none";
521 $r = $store->idle_response({}, 0);
522 $self->assert(!$r, "No unsolicted response");
523
524 $self->assert_null($store->get_client()->get_response_code('alert'));
525 }
526
527 xlog $self, "Shut down the instance";
528 $self->{instance}->stop();
529 # $self->assert($r == 1, "shutdown required brute force");
530
531 xlog $self, "Check that the server disconnected";
532
533 foreach my $store (@stores)
534 {
535 eval
536 {
537 # We use _send_cmd() and _next_atom() rather the normal path
538 # through _imap_cmd() because the latter will warn() to stderr
539 # about the exception we're about to generate, which is
540 # downright untidy.
541 my $talk = $store->get_client();
542 $talk->_send_cmd('status', 'INBOX', '(messages unseen)');
543 $talk->_parse_response({});
544 };
545 my $mm = $@; # this doesn't survive unless we save it
546 $self->assert_matches(qr/IMAP Connection closed by other end/, $mm);
547 }
548 }
549
550 sub test_idled_default_timeout
551 :needs_component_idled
552 {
553 my ($self) = @_;
554
555 # The default timeout if `imapidlepoll` isn't set in imapd.conf
556 # is set to 60 seconds. If idled is not broken, then we should
557 # return immediately(pretty much), instead of having to wait all
558 # of 60 seconds.
559 xlog $self, "Set idle poll timeout 60 seconds";
560 $self->{instance}->{config}->set(imapidlepoll => '60');
561 $self->{instance}->add_start(name => 'idled',
562 argv => [ 'idled' ]);
563 $self->{instance}->start();
564
565 xlog $self, "Starting up the instance";
566 my $svc = $self->{instance}->get_service('imap');
567
568 my $store = $svc->create_store(folder => 'INBOX');
569 my $talk = $store->get_client();
570 $store->_select();
571
572 xlog $self, "Sending the IDLE command";
573 $store->idle_begin()
574 or die "IDLE failed: $@";
575
576 my $date1 = DateTime->from_epoch(epoch => time());
577
578 xlog $self, "Poll for any unsolicited response - should be none";
579 my $r = $store->idle_response({}, 0);
580 $self->assert(!$r, "No unsolicted response");
581
582 xlog $self, "Poll for any unsolicited response - should be none";
583 $r = $store->idle_response({}, 0);
584 $self->assert(!$r, "No unsolicted response");
585
586 xlog $self, "Deliver a message";
587 my $msg = $self->{gen}->generate(subject => "Message 1");
588 $self->{instance}->deliver($msg);
589
590 $r = $store->idle_response({}, 5);
591 $self->assert($r, "received an unsolicited response");
592 $r = $store->idle_response({}, 5);
593 $self->assert($r, "received an unsolicited response");
594 $r = $store->idle_response({}, 1);
595 $self->assert(!$r, "no more unsolicited responses");
596 $self->assert_num_equals(1, $talk->get_response_code('exists'));
597 $self->assert_num_equals(1, $talk->get_response_code('recent'));
598
599 xlog $self, "Sending DONE continuation";
600 $store->idle_end({});
601 $self->assert_str_equals('ok', $talk->get_last_completion_response());
602
603 my $date2 = DateTime->from_epoch(epoch => time());
604
605 my $dur = $date2->epoch - $date1->epoch;
606 $self->assert($dur < 15, "IDLE took longer than expected");
607 }
608
609
610 1;