Debian Codegraph @

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