Debian Codegraph @

cassandane/Cassandane/Cyrus/Caldav.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::Caldav;
41 use v5.26.0; # strict + indented here-docs
42 use warnings;
43 use DateTime;
44 use JSON::XS;
45 use Net::CalDAVTalk 0.12;
46 use Net::DAVTalk::XMLParser;
47 use File::Basename;
48 use Data::Dumper;
49 use Text::VCardFast;
50 use Cwd qw(abs_path);
51
52 use lib '.';
53 use base qw(Cassandane::Cyrus::TestCase);
54 use Cassandane::Util::Log;
55 use Cassandane::Util::Slurp;
56 use utf8;
57
58 sub MELBOURNE {
59 return <<~'EOF';
60 BEGIN:VCALENDAR
61 BEGIN:VTIMEZONE
62 TZID:Australia/Melbourne
63 BEGIN:STANDARD
64 TZOFFSETFROM:+1100
65 RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1SU
66 DTSTART:20080406T030000
67 TZNAME:AEST
68 TZOFFSETTO:+1000
69 END:STANDARD
70 BEGIN:DAYLIGHT
71 TZOFFSETFROM:+1000
72 RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=1SU
73 DTSTART:20081005T020000
74 TZNAME:AEDT
75 TZOFFSETTO:+1100
76 END:DAYLIGHT
77 END:VTIMEZONE
78 END:VCALENDAR
79 EOF
80 }
81
82 sub NEW_YORK {
83 return <<~'EOF';
84 BEGIN:VCALENDAR
85 BEGIN:VTIMEZONE
86 TZID:America/New_York
87 BEGIN:DAYLIGHT
88 TZNAME:EDT
89 TZOFFSETFROM:-0500
90 TZOFFSETTO:-0400
91 DTSTART:20070311T020000
92 RRULE:FREQ=YEARLY;BYMONTH=3;BYDAY=2SU
93 END:DAYLIGHT
94 BEGIN:STANDARD
95 TZNAME:EST
96 TZOFFSETFROM:-0400
97 TZOFFSETTO:-0500
98 DTSTART:20071104T020000
99 RRULE:FREQ=YEARLY;BYMONTH=11;BYDAY=1SU
100 END:STANDARD
101 END:VTIMEZONE
102 END:VCALENDAR
103 EOF
104 }
105
106 sub new
107 {
108 my $class = shift;
109
110 my $config = Cassandane::Config->default()->clone();
111 $config->set(caldav_realm => 'Cassandane');
112 $config->set(httpmodules => 'caldav');
113 $config->set(calendar_user_address_set => 'example.com');
114 $config->set(httpallowcompress => 'no');
115 $config->set(caldav_historical_age => -1);
116 $config->set(icalendar_max_size => 100000);
117 $config->set(event_extra_params => 'vnd.cmu.davFilename vnd.cmu.davUid');
118 $config->set(event_groups => 'calendar');
119 $config->set(imipnotifier => 'imip');
120
121 my $self = $class->SUPER::new({
122 config => $config,
123 adminstore => 1,
124 services => ['imap', 'http'],
125 }, @_);
126
127 $self->needs('component', 'httpd');
128 return $self;
129 }
130
131 sub set_up
132 {
133 my ($self) = @_;
134 $self->SUPER::set_up();
135 $ENV{DEBUGDAV} = 1;
136 $ENV{JMAP_ALWAYS_FULL} = 1;
137 }
138
139 sub tear_down
140 {
141 my ($self) = @_;
142 $self->SUPER::tear_down();
143 }
144
145 sub _all_keys_match
146 {
147 my $a = shift;
148 my $b = shift;
149 my $errors = shift;
150
151 my $ref = ref($a);
152 unless ($ref eq ref($b)) {
153 push @$errors, "mismatched refs $ref / " . ref($b);
154 return 0;
155 }
156
157 unless ($ref) {
158 unless (defined $a) {
159 return 1 unless defined $b;
160 return 0;
161 }
162 return 0 unless defined $b;
163 if (lc $a ne lc $b) {
164 push @$errors, "not equal $a / $b";
165 return 0;
166 }
167 return 1;
168 }
169
170 if ($ref eq 'ARRAY') {
171 my @payloads = @$b;
172 my @nomatch;
173 foreach my $item (@$a) {
174 my $match;
175 my @rest;
176 foreach my $payload (@payloads) {
177 if (not $match and _all_keys_match($item, $payload, [])) {
178 $match = $payload;
179 }
180 else {
181 push @rest, $payload;
182 }
183 }
184 push @nomatch, $item unless $match;
185 @payloads = @rest;
186 }
187 if (@payloads or @nomatch) {
188 push @$errors, "failed to match\n" . Dumper(\@nomatch, \@payloads);
189 return 0;
190 }
191 return 1;
192 }
193
194 if ($ref eq 'HASH') {
195 foreach my $key (keys %$a) {
196 unless (exists $b->{$key}) {
197 push @$errors, "no key $key";
198 return 0;
199 }
200 my @err;
201 unless (_all_keys_match($a->{$key}, $b->{$key}, \@err)) {
202 push @$errors, "mismatch for $key: @err";
203 return 0;
204 }
205 }
206 return 1;
207 }
208
209 if ($ref eq 'JSON::PP::Boolean' or $ref eq 'JSON::XS::Boolean') {
210 if ($a != $b) {
211 push @$errors, "mismatched boolean " . (!!$a) . " / " . (!!$b);
212 return 0;
213 }
214 return 1;
215 }
216
217 die "WEIRD REF $ref for $a";
218 }
219
220 sub assert_caldav_notified
221 {
222 my $self = shift;
223 my @expected = @_;
224
225 my $newdata = $self->{instance}->getnotify();
226 my @imip = grep { $_->{METHOD} eq 'imip' } @$newdata;
227 my @payloads = map { decode_json($_->{MESSAGE}) } @imip;
228 foreach my $payload (@payloads) {
229 ($payload->{event}) = $self->{caldav}->vcalendarToEvents($payload->{ical});
230 $payload->{method} = delete $payload->{event}{method};
231 }
232
233 my @err;
234 unless (_all_keys_match(\@expected, \@payloads, \@err)) {
235 $self->fail("@err");
236 }
237 }
238
239 sub _put_event {
240 my $self = shift;
241 my $CalendarId = shift;
242 my %props = @_;
243 my $uuid = delete $props{uuid} || $self->{caldav}->genuuid();
244 my $href = "$CalendarId/$uuid.ics";
245
246 my $card = <<EOF;
247 BEGIN:VCALENDAR
248 VERSION:2.0
249 PRODID:-//Apple Inc.//Mac OS X 10.10.4//EN
250 CALSCALE:GREGORIAN
251 BEGIN:VTIMEZONE
252 TZID:Australia/Melbourne
253 BEGIN:STANDARD
254 TZOFFSETFROM:+1100
255 RRULE:FREQ=YEARLY;BYMONTH=4;BYDAY=1SU
256 DTSTART:20080406T030000
257 TZNAME:AEST
258 TZOFFSETTO:+1000
259 END:STANDARD
260 BEGIN:DAYLIGHT
261 TZOFFSETFROM:+1000
262 RRULE:FREQ=YEARLY;BYMONTH=10;BYDAY=1SU
263 DTSTART:20081005T020000
264 TZNAME:AEDT
265 TZOFFSETTO:+1100
266 END:DAYLIGHT
267 END:VTIMEZONE
268 BEGIN:VEVENT
269 CREATED:20150701T234327Z
270 UID:$uuid
271 DTEND;TZID=Australia/Melbourne:20160601T183000
272 TRANSP:OPAQUE
273 SUMMARY:{summary}
274 DTSTART;TZID=Australia/Melbourne:20160601T153000
275 DTSTAMP:20150806T234327Z
276 SEQUENCE:{sequence}
277 {lines}END:VEVENT
278 {overrides}END:VCALENDAR
279 EOF
280
281 $props{lines} ||= '';
282 $props{overrides} ||= '';
283 $props{sequence} ||= 0;
284 $props{summary} ||= "An Event";
285 foreach my $key (keys %props) {
286 $card =~ s/\{$key\}/$props{$key}/;
287 }
288
289 $self->{caldav}->Request('PUT', $href, $card, 'Content-Type' => 'text/calendar');
290 }
291
292 sub bogus_test_rfc6638_3_2_1_setpartstat_agentserver
293 {
294 my ($self) = @_;
295 my $CalDAV = $self->{caldav};
296
297 my $CalendarId = $CalDAV->NewCalendar({name => 'test'});
298 $self->assert_not_null($CalendarId);
299
300 xlog $self, "attempt to set the partstat to something other than NEEDS-ACTION";
301 # XXX - the server should reject this
302 $self->_put_event($CalendarId, lines => <<EOF);
303 ATTENDEE;CN=Test User;PARTSTAT=ACCEPTED:MAILTO:cassandane\@example.com
304 ATTENDEE;PARTSTAT=ACCEPTED:MAILTO:test1\@example.com
305 ATTENDEE;PARTSTAT=NEEDS-ACTION;RSVP=TRUE:MAILTO:test2\@example.com
306 ORGANIZER;CN=Test User:MAILTO:cassandane\@example.com
307 EOF
308 $self->assert_caldav_notified(
309 { recipient => "test2\@example.com", is_update => JSON::false, method => 'REQUEST' },
310 );
311 }
312
313 ############ REPLIES #############
314
315 sub slurp {
316 my $testdir = shift;
317 my $name = shift;
318 my $ext = shift;
319
320 return slurp_file("$testdir/$name.$ext");
321 }
322
323 sub _safeeq {
324 my ($a, $b) = @_;
325 my $json = JSON::XS->new->canonical;
326 return $json->encode([$a]) eq $json->encode([$b]);
327 }
328
329 use Cassandane::Tiny::Loader 'tiny-tests/Caldav';
330
331 1;