Debian Codegraph @

cassandane/Cassandane/Cyrus/Simple.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::Simple;
41 use strict;
42 use warnings;
43 use Cwd qw(getcwd realpath);
44 use Data::Dumper;
45 use DateTime;
46
47 use lib '.';
48 use base qw(Cassandane::Cyrus::TestCase);
49 use Cassandane::Util::Log;
50 use Cassandane::Util::Slurp;
51
52 $Data::Dumper::Sortkeys = 1;
53
54 sub new
55 {
56 my $class = shift;
57 return $class->SUPER::new({}, @_);
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 # Test APPEND of messages to IMAP
74 #
75 sub test_append
76 {
77 my ($self) = @_;
78
79 my %exp;
80
81 xlog $self, "generating message A";
82 $exp{A} = $self->make_message("Message A");
83 $self->check_messages(\%exp);
84
85 xlog $self, "generating message B";
86 $exp{B} = $self->make_message("Message B");
87 $self->check_messages(\%exp);
88
89 xlog $self, "generating message C";
90 $exp{C} = $self->make_message("Message C");
91 $self->check_messages(\%exp);
92
93 xlog $self, "generating message D";
94 $exp{D} = $self->make_message("Message D");
95 $self->check_messages(\%exp);
96 }
97
98 sub test_appendlimit_default
99 :min_version_3_6
100 {
101 my ($self) = @_;
102
103 my $imaptalk = $self->{store}->get_client();
104
105 my $capa = $imaptalk->capability();
106 my @appendlimits = grep { m/^appendlimit/ } keys %{$capa};
107
108 # should be only one appendlimit
109 $self->assert_num_equals(1, scalar @appendlimits);
110
111 # we do not support per-mailbox limits, so it must have a value too
112 $self->assert_matches(qr{^appendlimit=\d+$}, $appendlimits[0]);
113
114 # and since we haven't configured it, it ought to be the default
115 # value, which is BYTESIZE_UNLIMITED (2147483647).
116 $self->assert_str_equals("appendlimit=2147483647", $appendlimits[0]);
117 }
118
119 sub test_appendlimit_configured
120 :min_version_3_6 :NoStartInstances
121 {
122 my ($self) = @_;
123
124 my $desired_limit = "52428800"; # based on known failure
125
126 $self->{instance}->{config}->set('maxmessagesize' => $desired_limit);
127 $self->_start_instances();
128
129 my $imaptalk = $self->{store}->get_client();
130
131 my $capa = $imaptalk->capability();
132 my @appendlimits = grep { m/^appendlimit/ } keys %{$capa};
133
134 # should be only one appendlimit
135 $self->assert_num_equals(1, scalar @appendlimits);
136
137 # we do not support per-mailbox limits, so it must have a value too
138 $self->assert_matches(qr{^appendlimit=\d+$}, $appendlimits[0]);
139
140 # and since we've configured it, it'd better be what we asked for!
141 $self->assert_str_equals("appendlimit=$desired_limit", $appendlimits[0]);
142 }
143
144 sub test_select
145 {
146 my ($self) = @_;
147
148 my $imaptalk = $self->{store}->get_client();
149
150 xlog $self, "SELECTing INBOX";
151 $imaptalk->select("INBOX");
152 $self->assert(!$imaptalk->get_last_error());
153
154 xlog $self, "SELECTing inbox";
155 $imaptalk->select("inbox");
156 $self->assert(!$imaptalk->get_last_error());
157
158 xlog $self, "CREATEing sub folders";
159 $imaptalk->create("INBOX.sub");
160 $self->assert(!$imaptalk->get_last_error());
161 $imaptalk->create("inbox.blub");
162 $self->assert(!$imaptalk->get_last_error());
163
164 xlog $self, "SELECTing subfolders";
165 $imaptalk->select("inbox.sub");
166 $self->assert(!$imaptalk->get_last_error());
167 $imaptalk->select("INbOX.blub");
168 $self->assert(!$imaptalk->get_last_error());
169 }
170
171 sub test_cmdtimer_sessionid
172 :min_version_3_5 :NoStartInstances
173 {
174 my ($self) = @_;
175
176 # log the timing for anything that takes longer than zero seconds
177 $self->{instance}->{config}->set('commandmintimer', '0');
178 $self->_start_instances();
179
180 my $imaptalk = $self->{store}->get_client();
181
182 # put a bunch of messages in inbox to make sure fetch isn't instantaneous
183 my %msgs;
184 foreach my $n (1..5) {
185 $msgs{$n} = $self->make_message("message $n");
186 }
187
188 $imaptalk->select("INBOX");
189 $self->assert_str_equals("ok", $imaptalk->get_last_completion_response());
190
191 # discard buffered syslog output from setup
192 $self->{instance}->getsyslog();
193
194 # fetch some things that will take a little while
195 $imaptalk->fetch('1:*', '(uid flags body[])');
196 $self->assert_str_equals("ok", $imaptalk->get_last_completion_response());
197
198 # should have logged some timer output, which should include the sess id
199 if ($self->{instance}->{have_syslog_replacement}) {
200 # make sure that the connection is ended so that imapd reset happens
201 $imaptalk->logout();
202 undef $imaptalk;
203
204 my @lines = $self->{instance}->getsyslog();
205
206 my @timer_lines = grep { m/\bcmdtimer:/ } @lines;
207 $self->assert_num_gte(1, scalar @timer_lines);
208 foreach my $line (@timer_lines) {
209 $self->assert_matches(qr/sessionid=<[^ >]+>/, $line);
210 }
211
212 my (@behavior_lines) = grep { /session ended/ } @lines;
213
214 $self->assert_num_gte(1, scalar @behavior_lines);
215 }
216 }
217
218 sub test_toggleable_debug_logging
219 :min_version_3_9
220 {
221 my ($self) = @_;
222
223 my $config_debug = $self->{instance}->{config}->get_bool('debug', 'no');
224 my $imaptalk = $self->{store}->get_client();
225
226 # can't do anything without captured syslog
227 if (!$self->{instance}->{have_syslog_replacement}) {
228 xlog $self, "can't examine syslog, test is useless";
229 return;
230 }
231
232 # find our imapd pid from syslog
233 my $loginpat = qr{
234 \bimap\[(\d+)\]:\slogin:
235 \s\S+\s\[[\d\.]+\]\scassandane\splaintext
236 \sUser\slogged\sin
237 }x;
238 my @logins = $self->{instance}->getsyslog($loginpat);
239 $self->assert_num_equals(1, scalar @logins);
240 $logins[0] =~ m/$loginpat/;
241 my $imapd_pid = $1;
242
243 for (1..5) {
244 $imaptalk->unselect();
245 my $res = $imaptalk->select('INBOX');
246 $self->assert_str_equals('ok',
247 $imaptalk->get_last_completion_response());
248
249 # this is really looking at cassandane's own injected syslog
250 # output, so it depends on the injected syslog doing the right
251 # thing with masking
252 my $selectpat = qr/open: user cassandane opened INBOX/;
253 my @lines = $self->{instance}->getsyslog($selectpat);
254 if ($config_debug) {
255 $self->assert_num_equals(1, scalar @lines);
256 $self->assert_matches(qr/imap\[$imapd_pid\]:/, $lines[0]);
257 }
258 else {
259 $self->assert_num_equals(0, scalar @lines);
260 }
261
262 $config_debug = !$config_debug;
263
264 # toggle debug logging by sending SIGUSR1
265 my $count = kill 'SIGUSR1', $imapd_pid;
266 $self->assert_num_equals(1, $count);
267
268 # we can also look for the message logged by cyrus at the
269 # time it toggles the value
270 my $statuspat = qr/debug logging turned (on|off)/;
271 @lines = $self->{instance}->getsyslog($statuspat);
272 $self->assert_num_equals(1, scalar @lines);
273 $self->assert_matches(qr/imap\[$imapd_pid\]:/, $lines[0]);
274 $lines[0] =~ $statuspat;
275 my $status = $1;
276 if ($config_debug) {
277 $self->assert_str_equals('on', $status);
278 }
279 else {
280 $self->assert_str_equals('off', $status);
281 }
282 }
283 }
284
285 sub test_append_binary
286 {
287 my ($self) = @_;
288 my $imap = $self->{store}->get_client();
289
290 my $mime = <<'EOF' =~ s/\n/\r\n/gr;
291 To: to@local
292 From: from@local
293 Subject: test
294 Content-Transfer-Encoding:binary
295
296 test
297 EOF
298
299 $imap->append("INBOX", { Binary => $mime });
300 $self->assert_str_equals('ok', $imap->get_last_completion_response());
301
302 $imap->select('INBOX');
303 my $res = $imap->fetch('1', '(BINARY[1])');
304 $self->assert_str_equals("test\r\n", $res->{1}{binary});
305 }
306
307 sub test_fatals_abort_enabled
308 :NoStartInstances
309 {
310 my ($self) = @_;
311
312 $self->{instance}->{config}->set(
313 'fatals_abort' => 'yes',
314 'prometheus_enabled' => 'no',
315 );
316 $self->_start_instances();
317
318 my $basedir = $self->{instance}->get_basedir();
319
320 # run `promstatsd -1` without having set up for prometheus, which should
321 # produce a "Prometheus metrics are not being tracked..." fatal error
322 eval {
323 $self->{instance}->run_command({ cyrus => 1 }, 'promstatsd', '-1');
324 };
325 my $e = $@;
326 $self->assert_not_null($e);
327 $self->assert_matches(qr{promstatsd pid \d+\) terminated by signal 6},
328 $e->{'-text'});
329
330 my @cores = $self->{instance}->find_cores();
331 if (@cores) {
332 # if we dumped core, there'd better only be one core file
333 $self->assert_num_equals(1, scalar @cores);
334
335 # don't barf on it existing during shutdown
336 unlink $cores[0];
337 }
338 }
339
340 sub test_fatals_abort_disabled
341 :NoStartInstances
342 {
343 my ($self) = @_;
344
345 $self->{instance}->{config}->set(
346 'fatals_abort' => 'no',
347 'prometheus_enabled' => 'no',
348 );
349 $self->_start_instances();
350
351 my $basedir = $self->{instance}->get_basedir();
352
353 # run `promstatsd -1` without having set up for prometheus, which should
354 # produce a "Prometheus metrics are not being tracked..." fatal error
355 eval {
356 $self->{instance}->run_command({ cyrus => 1 }, 'promstatsd', '-1');
357 };
358 my $e = $@;
359 $self->assert_not_null($e);
360 $self->assert_matches(qr{promstatsd pid \d+\) exited with code 78},
361 $e->{'-text'});
362
363 # post-test sanity checks will complain for us if a core was left behind
364 }
365
366 sub test_fork_noexec
367 {
368 my ($self) = @_;
369
370 # need some not-executable file to test with, crash.c will do
371 my $noexec_file = realpath('utils/crash.c');
372 # it had better exist, and not be executable
373 $self->assert_file_test($noexec_file, '-e');
374 $self->assert_not_file_test($noexec_file, '-x');
375
376 my $expect_pid = $$;
377 my $expect_cwd = getcwd();
378
379 # try to run it... the exec in the forked child process will fail
380 eval {
381 $self->{instance}->run_command({ cyrus => 0, }, $noexec_file);
382 };
383 my $e = $@;
384 $self->assert_not_null($e);
385 # the child process had better exit!
386 $self->assert_matches(qr{exited with code 71}, $e->get_message());
387
388 # this test had better still be running in the same process!
389 $self->assert_num_equals($expect_pid, $$);
390
391 # cwd better not have changed!
392 $self->assert_str_equals($expect_cwd, getcwd());
393 }
394
395 sub test_sasl_ir
396 {
397 my ($self) = @_;
398
399 my $svc = $self->{instance}->get_service('imap');
400 my $host = $svc->host();
401 my $port = $svc->port();
402
403 my $imtest = $self->{instance}->_find_binary('imtest');
404 my $out = $self->{instance}->get_basedir() . "/imtest.$$.out";
405
406 $self->{instance}->run_command(
407 { redirects => { stdin => \". logout\r\n", stdout => $out } },
408 $imtest, '-a', 'cassandane', '-w', 'password', '-m', 'plain',
409 '-p', $port, $host
410 );
411
412 my $exchange = slurp_file($out);
413
414 # expect to have passed the hashed password in the authenticate line
415 my $re = qr{^C: A01 AUTHENTICATE PLAIN AGNhc3NhbmRhbmUAcGFzc3dvcmQ=\r$}m;
416 $self->assert_matches($re, $exchange);
417 }
418
419 1;