Debian Codegraph @

cassandane/Cassandane/Cyrus/JMAPTestSuite.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::JMAPTestSuite;
41 use strict;
42 use warnings;
43 use Cwd qw(abs_path);
44 use File::Path qw(mkpath);
45 use DateTime;
46 use JSON::XS qw(encode_json);
47 use File::Find;
48
49 use lib '.';
50 use base qw(Cassandane::Cyrus::TestCase);
51 use Cassandane::Util::Log;
52 use Cassandane::Cassini;
53
54 my $basedir;
55 my $binary;
56 my $testdir;
57 my $authortestdir;
58 my %suppressed;
59
60 # Tests for behaviour that has changed over time, where older releases
61 # contain the older behaviour and can no longer pass the updated test.
62 # The value is the first version number that is supposed to work.
63 # Same as the :min_version annotations on native Cassandane tests, but
64 # dot-separated. See skip_before() below for implementation details.
65 my %notbefore = (
66 't:Email:get:header-header-field-name' => '3.5',
67 't:Email:import:good-imports' => '3.8',
68 't:Email:import:one-fails-another-succeeds' => '3.8',
69 't:core:jmap-session-resource' => '3.13',
70 't:core:jmap-session-resource' => '3.13',
71 );
72
73 # Tests which JMAP-TestSuite will skip anyway when it detects it's
74 # talking to Cyrus.
75 my %notcyrus = map { $_ => 1 } (
76 't:Mailbox:get:no-existing-entities',
77 't:Mailbox:query:no-existing-entities',
78 );
79
80 sub cyrus_version_supports_jmap
81 {
82 my ($maj, $min) = Cassandane::Instance->get_version();
83
84 return 0 if ($maj < 3); # not supported before 3.x
85 return 0 if ($maj == 3 && $min == 0); # not supported in 3.0.x
86
87 # not supported if configured out
88 my $buildinfo = Cassandane::BuildInfo->new();
89 return 0 if not $buildinfo->get('component', 'jmap');
90
91 return 1; # supported in everything newer
92 }
93
94 sub init
95 {
96 my $cassini = Cassandane::Cassini->instance();
97 $basedir = $cassini->val('jmaptestsuite', 'basedir');
98 return unless defined $basedir;
99 $basedir = abs_path($basedir);
100
101 my $supp = $cassini->val('jmaptestsuite', 'suppress', '');
102 map { $suppressed{$_} = 1; } split(/\s+/, $supp);
103
104 $testdir = "$basedir/t";
105 $authortestdir = "$basedir/xt";
106 }
107 init;
108
109 sub new
110 {
111 my $class = shift;
112
113 my $config = Cassandane::Config->default()->clone();
114 $config->set(servername => "127.0.0.1"); # urlauth needs matching servername
115 $config->set(virtdomains => 'userid');
116 $config->set(caldav_realm => 'Cassandane');
117 $config->set(httpallowcompress => 'no');
118 $config->set(conversations => 'yes');
119
120 $config->set(search_engine => 'xapian');
121 $config->set(search_index_headers => 'no');
122 $config->set(search_batchsize => 8192);
123 $config->set(defaultpartition => 'default');
124 $config->set(defaultsearchtier => 't1');
125
126 $config->set('sync_log' => 'on');
127 $config->set('sync_log_channels' => 'squatter');
128
129 if (cyrus_version_supports_jmap()) {
130 $config->set(httpmodules => 'jmap');
131
132 return $class->SUPER::new({
133 config => $config,
134 adminstore => 1,
135 squatter => 1,
136 services => ['imap', 'http'],
137 }, @_);
138 }
139 else {
140 return $class->SUPER::new({}, @_);
141 }
142 }
143
144 sub set_up
145 {
146 my ($self) = @_;
147 $self->SUPER::set_up();
148 }
149
150 sub tear_down
151 {
152 my ($self) = @_;
153 $self->SUPER::tear_down();
154 }
155
156 # n.b. similar to _skip_version() in Cassandane::Unit::TestCase
157 sub skip_before
158 {
159 my ($lim) = @_;
160
161 my ($lim_major, $lim_minor, $lim_revision, $lim_commits)
162 = map { 0 + $_ } split /\./, $lim;
163 return if not defined $lim_major;
164
165 my ($major, $minor, $revision, $commits) =
166 Cassandane::Instance->get_version();
167
168 return 1 if $major < $lim_major; # too old, skip!
169 return if $major > $lim_major; # definitely new enough
170
171 return if not defined $lim_minor; # don't check deeper if caller doesn't care
172 return 1 if $minor < $lim_minor;
173 return if $minor > $lim_minor;
174
175 return if not defined $lim_revision;
176 return 1 if $revision < $lim_revision;
177
178 return if not defined $lim_commits;
179 return 1 if $commits < $lim_commits;
180
181 return;
182 }
183
184 sub find_tests
185 {
186 my ($dir) = @_;
187
188 my @tests;
189
190 find(
191 sub {
192 my $file = $File::Find::name;
193
194 return unless $file =~ s/\.t$//;
195 return unless -f "$file.t";
196 $file =~ s/^$basedir\/?//;
197 $file =~ s{/}{:}g;
198 return if $notcyrus{$file};
199 return if $suppressed{$file};
200 if (exists $notbefore{$file} and skip_before($notbefore{$file})) {
201 return;
202 }
203 push @tests, "test_$file";
204 },
205 $dir,
206 );
207
208 return @tests;
209 }
210
211 sub list_tests
212 {
213 my @tests;
214
215 if (!cyrus_version_supports_jmap())
216 {
217 return ( 'test_jmaptest_disabled' );
218 }
219
220 if (!defined $basedir)
221 {
222 return ( 'test_warning_jmaptestsuite_is_not_installed' );
223 }
224
225 @tests = find_tests($testdir);
226
227 if ($ENV{AUTHOR_TESTING}) {
228 push @tests, find_tests($authortestdir);
229 }
230
231 return @tests;
232 }
233
234 sub run_test
235 {
236 my ($self) = @_;
237
238 if (!defined $basedir)
239 {
240 xlog $self, "JMAP Tests are not enabled. To enabled them, please";
241 xlog $self, "install JMAP-TestSuite from https://github.com/fastmail/JMAP-TestSuite";
242 xlog $self, "and edit [jmaptestsuite]basedir in cassandane.ini";
243 xlog $self, "This is not a failure";
244 return;
245 }
246
247 if (!cyrus_version_supports_jmap())
248 {
249 xlog $self, "The version of Cyrus being tested does not support JMAP";
250 xlog $self, "JMAP-TestSuite tests skipped";
251 return;
252 }
253
254 my $name = $self->name();
255 $name =~ s/^test_//;
256
257 my $configfile = "$self->{instance}->{basedir}/testerconfig.json";
258 my $errfile = $self->{instance}->{basedir} . "/$name.errors";
259 my $outfile = $self->{instance}->{basedir} . "/$name.stdout";
260
261 my $service = $self->{instance}->get_service("http");
262 my $imap = $self->{instance}->get_service("imap");
263
264 local $ENV{JMAP_SERVER_ADAPTER_FILE} = $configfile;
265
266 open(FH, ">$configfile");
267
268 print FH encode_json({
269 adapter => 'Cyrus',
270 base_uri => 'http://' . $service->host() . ':' . $service->port() . '/',
271 cyrus_host => $imap->host(),
272 cyrus_port => $imap->port(),
273 cyrus_admin_user => 'admin',
274 cyrus_admin_pass => 'testpw',
275 no_sasl => 1,
276 credentials => [
277 {
278 username => 'cassandane',
279 password => 'pass',
280 },
281 ],
282 cyrus_hierarchy_separator => '.',
283 cyrus_prefix => $self->{instance}->{cyrus_prefix}
284 });
285 close(FH);
286
287 my $status = 0;
288
289 $name =~ s{:}{/}g;
290
291 local $ENV{JMTS_TEST_OUTPUT_TO_STDERR} = 1 if get_verbose;
292 local $ENV{JMTS_TELEMETRY} = 1 if get_verbose >= 3;
293 local $ENV{JMTS_USE_WEBSOCKETS} = 0;
294
295 $self->{instance}->run_command({
296 redirects => { stderr => $errfile, stdout => $outfile },
297 workingdir => $basedir,
298 handlers => {
299 exited_normally => sub { $status = 1; },
300 exited_abnormally => sub { $status = 0; },
301 },
302 },
303 "perl", '-I' => "$basedir/lib",
304 "$basedir/$name.t",
305 );
306
307 if ((!$status || get_verbose)) {
308 if (-f $errfile) {
309 open FH, '<', $errfile
310 or die "Cannot open $errfile for reading: $!";
311 while (readline FH) {
312 chomp;
313 xlog $_;
314 }
315 close FH;
316 }
317 }
318
319 $self->assert($status);
320 }
321
322 1;