| 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::TesterCardDAV; |
| 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; |
| 47 |
|
| 48 |
use lib '.'; |
| 49 |
use base qw(Cassandane::Cyrus::TestCase); |
| 50 |
use Cassandane::Util::Log; |
| 51 |
use Cassandane::Util::Slurp; |
| 52 |
use Cassandane::Cassini; |
| 53 |
|
| 54 |
my $basedir; |
| 55 |
my $binary; |
| 56 |
my $testdir; |
| 57 |
my %suppressed; |
| 58 |
my %expected; |
| 59 |
|
| 60 |
my $KNOWN_ERRORS = <<EOF; |
| 61 |
default-addressbook/Change property/2 | 1 |
| 62 |
default-addressbook/Change property/4 | 1 |
| 63 |
default-addressbook/Existing property/1 | 1 |
| 64 |
default-addressbook/No default delete/4 | 1 |
| 65 |
directory/GET on collections/4 | 1 |
| 66 |
directory/GET on collections/5 | 1 |
| 67 |
directory/GET on collections/8 | 1 |
| 68 |
errorcondition/PUT/2 | 1 |
| 69 |
errorcondition/PUT/5 | 1 |
| 70 |
errorcondition/PUT/8 | 1 |
| 71 |
errors/ERROR CONDITIONS/25 | 1 |
| 72 |
errors/ERROR CONDITIONS/26 | 1 |
| 73 |
errors/ERROR CONDITIONS/8 | 1 |
| 74 |
get/GET on resource/2 | 1 |
| 75 |
get/GET on resource/3 | 1 |
| 76 |
mkcol/MKCOL with body/1 | 1 |
| 77 |
mkcol/OPTIONS header/2 | 1 |
| 78 |
nonascii/high-ascii addressbook data/3 | 1 |
| 79 |
nonascii/high-ascii addressbook data/4 | 1 |
| 80 |
nonascii/Non-utf-8 address data/1 | 1 |
| 81 |
nonascii/Non-utf-8 address data/2 | 1 |
| 82 |
nonascii/Non-utf-8 address data/3 | 1 |
| 83 |
proppatch/prop patches/4 | 1 |
| 84 |
proppatch/prop patch property attributes/1 | 1 |
| 85 |
proppatch/prop patch property attributes/2 | 1 |
| 86 |
put/PUT groups/1 | 1 |
| 87 |
put/PUT VCARD/6 | 1 |
| 88 |
put/PUT VCARD/7 | 1 |
| 89 |
put/PUT with X- using VALUE != TEXT/1 | 1 |
| 90 |
put/Test \\ escapes/1 | 1 |
| 91 |
reports/basic query reports/10 | 1 |
| 92 |
reports/basic query reports/1 | 1 |
| 93 |
reports/basic query reports/11 | 1 |
| 94 |
reports/basic query reports/12 | 1 |
| 95 |
reports/basic query reports/13 | 1 |
| 96 |
reports/basic query reports/14 | 1 |
| 97 |
reports/basic query reports/15 | 1 |
| 98 |
reports/basic query reports/16 | 1 |
| 99 |
reports/basic query reports/17 | 1 |
| 100 |
reports/basic query reports/18 | 1 |
| 101 |
reports/basic query reports/19 | 1 |
| 102 |
reports/basic query reports/20 | 1 |
| 103 |
reports/basic query reports/2 | 1 |
| 104 |
reports/basic query reports/21 | 1 |
| 105 |
reports/basic query reports/22 | 1 |
| 106 |
reports/basic query reports/23 | 1 |
| 107 |
reports/basic query reports/24 | 1 |
| 108 |
reports/basic query reports/25 | 1 |
| 109 |
reports/basic query reports/26 | 1 |
| 110 |
reports/basic query reports/27 | 1 |
| 111 |
reports/basic query reports/28 | 1 |
| 112 |
reports/basic query reports/29 | 1 |
| 113 |
reports/basic query reports/30 | 1 |
| 114 |
reports/basic query reports/3 | 1 |
| 115 |
reports/basic query reports/31 | 1 |
| 116 |
reports/basic query reports/32 | 1 |
| 117 |
reports/basic query reports/33 | 1 |
| 118 |
reports/basic query reports/4 | 1 |
| 119 |
reports/basic query reports/5 | 1 |
| 120 |
reports/basic query reports/6 | 1 |
| 121 |
reports/basic query reports/7 | 1 |
| 122 |
reports/basic query reports/8 | 1 |
| 123 |
reports/basic query reports/9 | 1 |
| 124 |
sync-report/simple reports - diff token - no props/1 | 1 |
| 125 |
sync-report/simple reports - diff token - props/1 | 1 |
| 126 |
sync-report/simple reports - empty token - no props/1 | 1 |
| 127 |
sync-report/simple reports - empty token - no props/13 | 1 |
| 128 |
sync-report/simple reports - empty token - no props/5 | 1 |
| 129 |
sync-report/simple reports - empty token - no props/9 | 1 |
| 130 |
sync-report/simple reports - empty token - props/1 | 1 |
| 131 |
sync-report/simple reports - empty token - props/2 | 1 |
| 132 |
sync-report/simple reports - empty token - props/3 | 1 |
| 133 |
sync-report/simple reports - empty token - props/4 | 1 |
| 134 |
sync-report/support-report-set/1 | 1 |
| 135 |
sync-report/support-report-set/2 | 1 |
| 136 |
well-known/Simple GET tests/3 | 1 |
| 137 |
well-known/Simple GET tests/4 | 1 |
| 138 |
well-known/Simple PROPFIND tests/1 | 1 |
| 139 |
well-known/Simple PROPFIND tests/2 | 1 |
| 140 |
well-known/Simple PROPFIND tests/3 | 1 |
| 141 |
well-known/Simple PROPFIND tests/4 | 1 |
| 142 |
well-known/Simple PROPFIND tests/5 | 1 |
| 143 |
well-known/Simple PROPFIND tests/6 | 1 |
| 144 |
EOF |
| 145 |
|
| 146 |
sub init |
| 147 |
{ |
| 148 |
my $cassini = Cassandane::Cassini->instance(); |
| 149 |
$basedir = $cassini->val('caldavtester', 'basedir'); |
| 150 |
return unless defined $basedir; |
| 151 |
$basedir = abs_path($basedir); |
| 152 |
|
| 153 |
my $supp = $cassini->val('caldavtester', 'suppress-carddav', |
| 154 |
''); |
| 155 |
map { $suppressed{$_} = 1; } split(/\s+/, $supp); |
| 156 |
|
| 157 |
foreach my $row (split /\n/, $KNOWN_ERRORS) { |
| 158 |
next if $row =~ m/\s*\#/; |
| 159 |
next unless $row =~ m/\S/; |
| 160 |
my ($key, @items) = split /\s*\|\s*/, $row; |
| 161 |
$expected{$key} = \@items; |
| 162 |
} |
| 163 |
|
| 164 |
$binary = "$basedir/testcaldav.py"; |
| 165 |
$testdir = "$basedir/scripts/tests/CardDAV"; |
| 166 |
} |
| 167 |
init; |
| 168 |
|
| 169 |
sub new |
| 170 |
{ |
| 171 |
my $class = shift; |
| 172 |
|
| 173 |
my $buildinfo = Cassandane::BuildInfo->new(); |
| 174 |
|
| 175 |
if (not defined $basedir or not $buildinfo->get('component', 'httpd')) { |
| 176 |
# don't bother setting up, we're not running tests anyway |
| 177 |
return $class->SUPER::new({}, @_); |
| 178 |
} |
| 179 |
|
| 180 |
my $config = Cassandane::Config->default()->clone(); |
| 181 |
$config->set(servername => "127.0.0.1"); # urlauth needs matching servername |
| 182 |
$config->set(caldav_realm => 'Cassandane'); |
| 183 |
$config->set(httpmodules => 'carddav'); |
| 184 |
$config->set(httpallowcompress => 'no'); |
| 185 |
|
| 186 |
return $class->SUPER::new({ |
| 187 |
config => $config, |
| 188 |
adminstore => 1, |
| 189 |
services => ['imap', 'http'], |
| 190 |
}, @_); |
| 191 |
} |
| 192 |
|
| 193 |
sub set_up |
| 194 |
{ |
| 195 |
my ($self) = @_; |
| 196 |
$self->SUPER::set_up(); |
| 197 |
|
| 198 |
if (not defined $basedir |
| 199 |
or not $self->{instance}->{buildinfo}->get('component', 'httpd')) |
| 200 |
{ |
| 201 |
# don't bother setting up further, we're not running tests anyway |
| 202 |
return; |
| 203 |
} |
| 204 |
|
| 205 |
my $admintalk = $self->{adminstore}->get_client(); |
| 206 |
|
| 207 |
for (1..40) { |
| 208 |
my $name = sprintf("user%02d", $_); |
| 209 |
$admintalk->create("user.$name"); |
| 210 |
$admintalk->setacl("user.$name", admin => 'lrswipkxtecda'); |
| 211 |
$admintalk->setacl("user.$name", $name => 'lrswipkxtecd'); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
sub tear_down |
| 216 |
{ |
| 217 |
my ($self) = @_; |
| 218 |
$self->SUPER::tear_down(); |
| 219 |
} |
| 220 |
|
| 221 |
sub list_tests |
| 222 |
{ |
| 223 |
my @tests; |
| 224 |
|
| 225 |
if (!defined $basedir) |
| 226 |
{ |
| 227 |
return ( 'test_warning_caldavtester_is_not_installed' ); |
| 228 |
} |
| 229 |
|
| 230 |
open(FH, "-|", 'find', $testdir, '-name' => '*.xml'); |
| 231 |
while (<FH>) |
| 232 |
{ |
| 233 |
chomp; |
| 234 |
next unless s{^$testdir/}{}; |
| 235 |
next unless s{\.xml$}{}; |
| 236 |
next if $suppressed{$_}; |
| 237 |
push(@tests, "test_$_"); |
| 238 |
} |
| 239 |
close(FH); |
| 240 |
|
| 241 |
return @tests; |
| 242 |
} |
| 243 |
|
| 244 |
sub run_test |
| 245 |
{ |
| 246 |
my ($self) = @_; |
| 247 |
|
| 248 |
if (!defined $basedir) |
| 249 |
{ |
| 250 |
xlog "CalDAVTester tests are not enabled. To enabled them, please"; |
| 251 |
xlog "install CalDAVTester from http://calendarserver.org/wiki/CalDAVTester"; |
| 252 |
xlog "and edit [caldavtester]basedir in cassandane.ini"; |
| 253 |
xlog "This is not a failure"; |
| 254 |
return; |
| 255 |
} |
| 256 |
|
| 257 |
my $name = $self->name(); |
| 258 |
$name =~ s/^test_//; |
| 259 |
my $testname = $name; |
| 260 |
$testname .= ".xml"; |
| 261 |
|
| 262 |
my $logdir = "$self->{instance}->{basedir}/rawlog/"; |
| 263 |
mkdir($logdir); |
| 264 |
|
| 265 |
my $svc = $self->{instance}->get_service('http'); |
| 266 |
my $params = $svc->store_params(); |
| 267 |
|
| 268 |
my $rundir = "$self->{instance}->{basedir}/run"; |
| 269 |
mkdir($rundir); |
| 270 |
|
| 271 |
system('ln', '-s', "$testdir", "$rundir/tests"); |
| 272 |
system('ln', '-s', "$basedir", "$rundir/data"); |
| 273 |
|
| 274 |
# XXX - make the config file! |
| 275 |
my $configfile = "$rundir/serverinfo.xml"; |
| 276 |
{ |
| 277 |
my $config = slurp_file(abs_path("data/caldavtester-serverinfo-template.xml")); |
| 278 |
$config =~ s/SERVICE_HOST/$params->{host}/g; |
| 279 |
$config =~ s/SERVICE_PORT/$params->{port}/g; |
| 280 |
|
| 281 |
open(FH, ">", $configfile); |
| 282 |
print FH $config; |
| 283 |
close(FH); |
| 284 |
} |
| 285 |
|
| 286 |
my $errfile = $self->{instance}->{basedir} . "/$name.errors"; |
| 287 |
my $outfile = $self->{instance}->{basedir} . "/$name.stdout"; |
| 288 |
my $status; |
| 289 |
my @verbose; |
| 290 |
if (get_verbose) { |
| 291 |
push @verbose, "--always-print-request", "--always-print-response"; |
| 292 |
} |
| 293 |
$self->{instance}->run_command({ |
| 294 |
redirects => { stderr => $errfile, stdout => $outfile }, |
| 295 |
workingdir => $logdir, |
| 296 |
handlers => { |
| 297 |
exited_normally => sub { $status = 1; }, |
| 298 |
exited_abnormally => sub { $status = 0; }, |
| 299 |
}, |
| 300 |
}, |
| 301 |
$binary, |
| 302 |
"--basedir" => $rundir, |
| 303 |
"--observer=jsondump", |
| 304 |
@verbose, |
| 305 |
$testname); |
| 306 |
|
| 307 |
my $json = decode_json(slurp_file($outfile)); |
| 308 |
|
| 309 |
if (0 && (!$status || get_verbose)) { |
| 310 |
foreach my $file ($errfile) { |
| 311 |
next unless -f $file; |
| 312 |
xlog $self, slurp_file($file); |
| 313 |
} |
| 314 |
} |
| 315 |
|
| 316 |
$json->[0]{name} = $name; # short name at top level |
| 317 |
$self->assert(_check_result($name, $json->[0])); |
| 318 |
} |
| 319 |
|
| 320 |
sub _check_result { |
| 321 |
my $name = shift; |
| 322 |
my $json = shift; |
| 323 |
my $res = 1; |
| 324 |
|
| 325 |
if (defined $json->{result}) { |
| 326 |
if ($json->{result} == 0) { |
| 327 |
xlog "$name [OK]"; |
| 328 |
} |
| 329 |
elsif ($json->{result} == 1) { |
| 330 |
xlog "$name [FAILED]"; |
| 331 |
$res = 0; |
| 332 |
} |
| 333 |
elsif ($json->{result} == 3) { |
| 334 |
xlog "$name [SKIPPED]"; |
| 335 |
} |
| 336 |
if (exists $expected{$name}) { |
| 337 |
if ($json->{result} == $expected{$name}[0]) { |
| 338 |
xlog "EXPECTED RESULT FOR $name"; |
| 339 |
$res = 1; |
| 340 |
} |
| 341 |
else { |
| 342 |
xlog "UNEXPECTED RESULT FOR $name: " . $expected{$name}[1] if $expected{$name}[1]; |
| 343 |
$res = 0; # yep, even if we succeeded |
| 344 |
} |
| 345 |
} |
| 346 |
xlog $json->{details} if $json->{result}; |
| 347 |
} |
| 348 |
|
| 349 |
xlog "FAILED WHEN NOT EXPECTED $name" unless $res; |
| 350 |
|
| 351 |
if ($json->{tests}) { |
| 352 |
foreach my $test (@{$json->{tests}}) { |
| 353 |
$res = 0 unless _check_result("$name/$test->{name}", $test); |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
return $res; |
| 358 |
} |
| 359 |
|
| 360 |
1; |