Debian Codegraph @

cassandane/Cassandane/Cyrus/Annotator.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 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
32 # THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
33 # AND FITNESS, IN NO EVENT SHALL OPERA SOFTWARE AUSTRALIA BE LIABLE
34 # FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
35 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
36 # AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
37 # OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
38 #
39
40 package Cassandane::Cyrus::Annotator;
41 use strict;
42 use warnings;
43 use Cwd qw(abs_path);
44
45 use lib '.';
46 use base qw(Cassandane::Cyrus::TestCase);
47 use Cassandane::Util::Log;
48 use Cassandane::Util::Slurp;
49 use Cassandane::Util::Wait;
50
51 sub new
52 {
53 my $class = shift;
54 my $config = Cassandane::Config->default()->clone();
55 $config->set(
56 annotation_callout => '@basedir@/conf/socket/annotator.sock',
57 );
58 return $class->SUPER::new({
59 config => $config,
60 deliver => 1,
61 start_instances => 0,
62 adminstore => 1,
63 }, @_);
64 }
65
66 sub set_up
67 {
68 my ($self) = @_;
69 $self->SUPER::set_up();
70 }
71
72 sub tear_down
73 {
74 my ($self) = @_;
75 $self->SUPER::tear_down();
76 }
77
78 sub start_my_instances
79 {
80 my ($self) = @_;
81
82 $self->{instance}->add_generic_listener(
83 name => 'annotator',
84 port => $self->{instance}->{config}->get('annotation_callout'),
85 argv => sub {
86 my ($listener) = @_;
87 return (
88 abs_path('utils/annotator.pl'),
89 '--port', $listener->port(),
90 '--pidfile', '@basedir@/run/annotator.pid',
91 );
92 });
93
94 $self->_start_instances();
95 }
96
97 sub test_add_annot_deliver
98 {
99 my ($self) = @_;
100
101 $self->start_my_instances();
102
103 my $entry = '/comment';
104 my $attrib = 'value.shared';
105 # Data thanks to http://hipsteripsum.me
106 my $value1 = 'you_probably_havent_heard_of_them';
107
108 my %exp;
109 $exp{A} = $self->{gen}->generate(subject => "Message A");
110 $exp{A}->set_body("set_shared_annotation $entry $value1\r\n");
111 $self->{instance}->deliver($exp{A});
112 $exp{A}->set_annotation($entry, $attrib, $value1);
113
114 # Local delivery adds headers we can't predict or control,
115 # which change the SHA1 of delivered messages, so we can't
116 # be checking the GUIDs here.
117 $self->{store}->set_fetch_attributes('uid', "annotation ($entry $attrib)");
118 $self->check_messages(\%exp, check_guid => 0);
119 }
120
121 sub test_add_annot_deliver_tomailbox
122 :NoAltNamespace
123 {
124 my ($self) = @_;
125
126 $self->start_my_instances();
127
128 xlog $self, "Testing adding an annotation from the Annotator";
129 xlog $self, "when delivering to a non-INBOX mailbox [IRIS-955]";
130
131 my $entry = '/comment';
132 my $attrib = 'value.shared';
133 # Data thanks to http://hipsteripsum.me
134 my $value1 = 'before_they_sold_out';
135
136 my $subfolder = 'target';
137 my $talk = $self->{store}->get_client();
138 $talk->create("INBOX.$subfolder")
139 or die "Failed to create INBOX.$subfolder";
140
141 my %exp;
142 $exp{A} = $self->{gen}->generate(subject => "Message A");
143 $exp{A}->set_body("set_shared_annotation $entry $value1\r\n");
144 $self->{instance}->deliver($exp{A}, folder => $subfolder);
145 $exp{A}->set_annotation($entry, $attrib, $value1);
146
147 # Local delivery adds headers we can't predict or control,
148 # which change the SHA1 of delivered messages, so we can't
149 # be checking the GUIDs here.
150 $self->{store}->set_folder("INBOX.$subfolder");
151 $self->{store}->set_fetch_attributes('uid', "annotation ($entry $attrib)");
152 $self->check_messages(\%exp, check_guid => 0);
153 }
154
155 sub test_set_system_flag_deliver
156 {
157 my ($self) = @_;
158
159 $self->start_my_instances();
160
161 my $flag = '\\Flagged';
162
163 my %exp;
164 $exp{A} = $self->{gen}->generate(subject => "Message A");
165 $exp{A}->set_body("set_flag $flag\r\n");
166 $self->{instance}->deliver($exp{A});
167 $exp{A}->set_attributes(flags => ['\\Recent', $flag]);
168
169 # Local delivery adds headers we can't predict or control,
170 # which change the SHA1 of delivered messages, so we can't
171 # be checking the GUIDs here.
172 $self->{store}->set_fetch_attributes('uid', 'flags');
173 $self->check_messages(\%exp, check_guid => 0);
174 }
175
176 sub test_set_user_flag_deliver
177 {
178 my ($self) = @_;
179
180 $self->start_my_instances();
181
182 # Data thanks to http://hipsteripsum.me
183 my $flag = '$Artisanal';
184
185 my %exp;
186 $exp{A} = $self->{gen}->generate(subject => "Message A");
187 $exp{A}->set_body("set_flag $flag\r\n");
188 $self->{instance}->deliver($exp{A});
189 $exp{A}->set_attributes(flags => ['\\Recent', $flag]);
190
191 # Local delivery adds headers we can't predict or control,
192 # which change the SHA1 of delivered messages, so we can't
193 # be checking the GUIDs here.
194 $self->{store}->set_fetch_attributes('uid', 'flags');
195 $self->check_messages(\%exp, check_guid => 0);
196 }
197
198 sub test_reconstruct_after_delivery
199 {
200 my ($self) = @_;
201
202 $self->start_my_instances();
203
204 xlog $self, "Testing reconstruct after delivery";
205
206 xlog $self, "Create folders";
207 my $imaptalk = $self->{store}->get_client();
208 $self->{store}->set_fetch_attributes('uid');
209
210 xlog $self, "Deliver a message";
211 my %msgs;
212 $msgs{1} = $self->{gen}->generate(subject => "Message 1");
213 $msgs{1}->set_attribute(uid => 1);
214 $msgs{1}->set_body("set_shared_annotation /comment testvalue\r\n");
215 $imaptalk->create("INBOX.subfolder");
216 $self->{instance}->deliver($msgs{1}, user => "cassandane");
217
218 xlog $self, "Check that the message made it";
219 $self->check_messages(\%msgs, check_guid => 0, keyed_on => 'uid');
220
221 # run a fresh reconstruct
222 my $out = "$self->{instance}->{basedir}/$self->{_name}-reconstruct.stdout";
223 $self->{instance}->run_command(
224 { cyrus => 1,
225 redirects => { 'stdout' => $out },
226 }, 'reconstruct', '-u', 'cassandane');
227
228 # check the output
229 $out = slurp_file($out);
230 xlog $self, $out;
231
232 $self->assert_does_not_match(qr/ updating /, $out);
233 }
234
235
236 # Note: remove_annotation can't really be tested with local
237 # delivery, just with the APPEND command.
238
239 sub test_fetch_after_annotate
240 {
241 # This is a test for https://github.com/cyrusimap/cyrus-imapd/issues/2071
242 my ($self) = @_;
243
244 $self->start_my_instances();
245
246 my $flag = '$X-ME-Annot-2';
247 my $imaptalk = $self->{store}->get_client();
248 my $modseq;
249 my %msg;
250
251 $imaptalk->select("INBOX");
252
253 xlog $self, "Create Message A";
254 $msg{A} = $self->{gen}->generate(subject => "Message A");
255 $msg{A}->set_attributes(id => 1,
256 uid => 1,
257 flags => []);
258 $msg{A}->set_body("set_flag $flag\r\n");
259 $self->{instance}->deliver($msg{A});
260
261 $msg{A}->set_attributes(flags => ['\\Recent', $flag]);
262
263 $self->{store}->set_fetch_attributes('uid', 'flags', 'modseq');
264
265 xlog $self, "Fetch message A";
266 my %handlers1;
267 {
268 $handlers1{fetch} = sub {
269 $self->assert_num_equals(scalar @{$_[1]{flags}}, 2);
270 $self->assert_str_equals($_[1]{flags}[0], "\\Recent");
271 $self->assert_str_equals($_[1]{flags}[1], "\$X-ME-Annot-2");
272 };
273 }
274 $imaptalk->_imap_cmd("uid fetch", 1, \%handlers1, '1', '(flags modseq)');
275 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
276
277 xlog $self, "Clear the $flag from the message A.";
278 my %handlers2;
279 {
280 $handlers2{fetch} = sub {
281 $modseq = $_[1]{modseq}[0];
282 $self->assert_num_equals(scalar @{$_[1]{flags}}, 1);
283 $self->assert_str_equals($_[1]{flags}[0], "\\Recent");
284 };
285 }
286 $imaptalk->store('1', '-flags', "($flag)");
287 $imaptalk->_imap_cmd("uid fetch", 1, \%handlers2, '1', '(flags modseq)');
288 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
289
290 xlog $self, "Run xrunannotator";
291 my %handlers3;
292 {
293 $handlers3{fetch} = sub {
294 $self->assert($_[1]{modseq}[0] > $modseq);
295 $self->assert_num_equals(scalar @{$_[1]{flags}}, 2);
296 $self->assert_str_equals($_[1]{flags}[0], "\\Recent");
297 $self->assert_str_equals($_[1]{flags}[1], "\$X-ME-Annot-2");
298 };
299 }
300 $imaptalk->_imap_cmd("uid xrunannotator", 0, {}, '1');
301 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
302
303 $imaptalk->_imap_cmd("uid fetch", 1, \%handlers3, '1', '(flags modseq)');
304 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
305 }
306
307 sub test_annotator_callout_disabled
308 :min_version_3_1
309 {
310 my ($self) = @_;
311 $self->{instance}->{config}->set(annotation_callout_disable_append => 'yes');
312
313 $self->start_my_instances();
314
315 my $flag = '$X-ME-Annot-2';
316 my $imaptalk = $self->{store}->get_client();
317 my $modseq;
318 my %msg;
319
320 $imaptalk->select("INBOX");
321
322 xlog $self, "Create Message A";
323 $msg{A} = $self->{gen}->generate(subject => "Message A");
324 $msg{A}->set_attributes(id => 1,
325 uid => 1,
326 flags => []);
327 $msg{A}->set_body("set_flag $flag\r\n");
328 $self->{instance}->deliver($msg{A});
329
330 $msg{A}->set_attributes(flags => ['\\Recent', $flag]);
331
332 $self->{store}->set_fetch_attributes('uid', 'flags', 'modseq');
333
334 xlog $self, "Fetch message A";
335 my %handlers1;
336 {
337 $handlers1{fetch} = sub {
338 $self->assert_num_equals(scalar @{$_[1]{flags}}, 2);
339 $self->assert_str_equals($_[1]{flags}[0], "\\Recent");
340 $self->assert_str_equals($_[1]{flags}[1], "\$X-ME-Annot-2");
341 };
342 }
343 $imaptalk->_imap_cmd("uid fetch", 1, \%handlers1, '1', '(flags modseq)');
344 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
345
346 xlog $self, "Clear the $flag from the message A.";
347 my %handlers2;
348 {
349 $handlers2{fetch} = sub {
350 $modseq = $_[1]{modseq}[0];
351 $self->assert_num_equals(scalar @{$_[1]{flags}}, 1);
352 $self->assert_str_equals($_[1]{flags}[0], "\\Recent");
353 };
354 }
355 $imaptalk->store('1', '-flags', "($flag)");
356 $imaptalk->_imap_cmd("uid fetch", 1, \%handlers2, '1', '(flags modseq)');
357 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
358
359 xlog $self, "Run xrunannotator";
360 my %handlers3;
361 {
362 $handlers3{fetch} = sub {
363 $self->assert($_[1]{modseq}[0] == $modseq);
364 $self->assert_num_equals(scalar @{$_[1]{flags}}, 1);
365 $self->assert_str_equals($_[1]{flags}[0], "\\Recent");
366 };
367 }
368 $imaptalk->_imap_cmd("uid xrunannotator", 0, {}, '1');
369 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
370
371 xlog $self, "Nothing should have changed from the previous run of uid fetch.";
372 $imaptalk->_imap_cmd("uid fetch", 1, \%handlers3, '1', '(flags modseq)');
373 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
374 }
375
376 sub test_add_annot_splitconv
377 :min_version_3_1 :Conversations
378 {
379 my ($self) = @_;
380 my %exp;
381
382 $self->{instance}->{config}->set(conversations_max_thread => 5);
383
384 $self->start_my_instances();
385
386 my $entry = '/comment';
387 my $attrib = 'value.shared';
388 # Data thanks to http://hipsteripsum.me
389 my $value1 = 'you_probably_havent_heard_of_them';
390
391 $self->{store}->set_fetch_attributes('uid', 'cid', 'basecid', "annotation ($entry $attrib)");
392
393 xlog $self, "generating message A";
394 $exp{A} = $self->make_message("Message A");
395 $exp{A}->set_attributes(uid => 1, cid => $exp{A}->make_cid());
396 $self->check_messages(\%exp);
397
398 xlog $self, "generating replies";
399 for (1..4) {
400 $exp{"A$_"} = $self->make_message("Re: Message A", references => [ $exp{A} ]);
401 $exp{"A$_"}->set_attributes(uid => 1+$_, cid => $exp{A}->make_cid());
402 }
403 $exp{"B"} = $self->make_message("Re: Message A", references => [ $exp{A} ]);
404 $exp{"B"}->set_attributes(uid => 6, cid => $exp{B}->make_cid(), basecid => $exp{A}->make_cid());
405
406 $exp{C} = $self->{gen}->generate(subject => "Re: Message A", references => [ $exp{A} ]);
407 $exp{C}->set_body("set_shared_annotation $entry $value1\r\n");
408 $self->{instance}->deliver($exp{C});
409 $exp{C}->set_annotation($entry, $attrib, $value1);
410 $exp{C}->set_attributes(uid => 7, cid => $exp{B}->make_cid(), basecid => $exp{A}->make_cid());
411
412 # Local delivery adds headers we can't predict or control,
413 # which change the SHA1 of delivered messages, so we can't
414 # be checking the GUIDs here.
415 $self->check_messages(\%exp, keyed_on => 'uid', check_guid => 0);
416 }
417
418 sub test_add_annot_splitconv_rerun
419 :min_version_3_1 :Conversations
420 {
421 my ($self) = @_;
422 my %exp;
423
424 $self->{instance}->{config}->set(conversations_max_thread => 5);
425
426 $self->start_my_instances();
427
428 my $entry = '/comment';
429 my $attrib = 'value.shared';
430 # Data thanks to http://hipsteripsum.me
431 my $value1 = 'you_probably_havent_heard_of_them';
432
433 $self->{store}->set_fetch_attributes('uid', 'cid', 'basecid', 'flags', "annotation ($entry $attrib)");
434
435 xlog $self, "generating message A";
436 $exp{A} = $self->make_message("Message A");
437 $exp{A}->set_attributes(uid => 1, cid => $exp{A}->make_cid());
438 $self->check_messages(\%exp);
439
440 xlog $self, "generating replies";
441 for (1..4) {
442 $exp{"A$_"} = $self->make_message("Re: Message A", references => [ $exp{A} ]);
443 $exp{"A$_"}->set_attributes(uid => 1+$_, cid => $exp{A}->make_cid());
444 }
445 $exp{"B"} = $self->make_message("Re: Message A", references => [ $exp{A} ]);
446 $exp{"B"}->set_attributes(uid => 6, cid => $exp{B}->make_cid(), basecid => $exp{A}->make_cid());
447
448 $exp{C} = $self->{gen}->generate(subject => "Re: Message A", references => [ $exp{A} ]);
449 $exp{C}->set_body("set_shared_annotation $entry $value1\r\nset_flag \$X-FUN");
450 $self->{instance}->deliver($exp{C});
451 $exp{C}->set_attributes(uid => 7, cid => $exp{B}->make_cid(), basecid => $exp{A}->make_cid());
452
453 # Local delivery adds headers we can't predict or control,
454 # which change the SHA1 of delivered messages, so we can't
455 # be checking the GUIDs here.
456 $self->check_messages(\%exp, keyed_on => 'uid', check_guid => 0);
457
458 my $imaptalk = $self->{store}->get_client();
459 $imaptalk->store('7', '-flags', '$X-FUN');
460 $imaptalk->_imap_cmd("uid xrunannotator", 0, {}, '7');
461 $self->assert_str_equals('ok', $imaptalk->get_last_completion_response());
462 $exp{C}->set_annotation($entry, $attrib, $value1);
463
464 $self->check_messages(\%exp, keyed_on => 'uid', check_guid => 0);
465 }
466
467 sub test_log_missing_acl
468 {
469 my ($self) = @_;
470
471 $self->start_my_instances();
472
473 my $imap = $self->{store}->get_client();
474 my $admin = $self->{adminstore}->get_client();
475
476 $self->{instance}->create_user("other");
477
478 my @testCases = ({
479 flag => '$Artisanal',
480 acl => 'lrsitne',
481 need_rights => 'w'
482 }, {
483 flag => '\Seen',
484 acl => 'lritne',
485 need_rights => 's'
486 });
487
488 foreach (@testCases) {
489 $admin->setacl("user.other", "cassandane", $_->{acl}) or die;
490
491 $self->{instance}->getsyslog();
492 $self->{store}->set_folder('Other Users.other');
493 $self->make_message("Email", body => "set_flag $_->{flag}\r\n",
494 store => $self->{store}) or die;
495 my $wantLog = "could not write flag due missing ACL: "
496 . "flag=<\\$_->{flag}> need_rights=<$_->{need_rights}>";
497 $self->assert_syslog_matches($self->{instance}, qr{$wantLog});
498 }
499 }
500
501 1;