Debian Codegraph @

cassandane/Cassandane/Cyrus/Expunge.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::Expunge;
41 use strict;
42 use warnings;
43 use JSON::XS;
44
45 use lib '.';
46 use base qw(Cassandane::Cyrus::TestCase);
47 use Cassandane::Util::Log;
48
49 sub new
50 {
51 my ($class, @args) = @_;
52 return $class->SUPER::new({ adminstore => 1 }, @args);
53 }
54
55 sub set_up
56 {
57 my ($self) = @_;
58 $self->SUPER::set_up();
59 }
60
61 sub tear_down
62 {
63 my ($self) = @_;
64 $self->SUPER::tear_down();
65 }
66
67 sub test_status_after_expunge
68 {
69 my ($self, $folder, %params) = @_;
70
71 my $store = $self->{store};
72 my $talk = $store->get_client();
73
74 my $subfolder = 'INBOX.foo';
75
76 xlog $self, "First create a sub folder";
77 $talk->create($subfolder)
78 or die "Cannot create folder $subfolder: $@";
79 $self->assert_str_equals('ok', $talk->get_last_completion_response());
80
81 xlog $self, "Generate messages in $subfolder";
82 $store->set_folder($subfolder);
83 $store->_select();
84 for (1..5) {
85 $self->make_message("Message $subfolder $_");
86 }
87 $talk->unselect();
88 $talk->select($subfolder);
89
90 my $stat = $talk->status($subfolder, '(highestmodseq unseen messages)');
91 $self->assert_equals(5, $stat->{unseen});
92 $self->assert_equals(5, $stat->{messages});
93
94 $talk->store('1,3,5', '+flags', '(\\Seen)');
95
96 $stat = $talk->status($subfolder, '(highestmodseq unseen messages)');
97 $self->assert_equals(2, $stat->{unseen});
98 $self->assert_equals(5, $stat->{messages});
99
100 $talk->store('1:*', '+flags', '(\\Deleted \\Seen)');
101 $talk->expunge();
102
103 $stat = $talk->status($subfolder, '(highestmodseq unseen messages)');
104 $self->assert_equals(0, $stat->{unseen});
105 $self->assert_equals(0, $stat->{messages});
106 }
107
108 sub test_auditlog_size
109 :min_version_3_5
110 {
111 my ($self, $folder, %params) = @_;
112
113 my $store = $self->{store};
114 my $talk = $store->get_client();
115
116 my $subfolder = 'INBOX.foo';
117
118 xlog $self, "First create a sub folder";
119 $talk->create($subfolder)
120 or die "Cannot create folder $subfolder: $@";
121 $self->assert_str_equals('ok', $talk->get_last_completion_response());
122
123 xlog $self, "Generate messages in $subfolder";
124 $store->set_folder($subfolder);
125 $store->_select();
126 for (1..5) {
127 $self->make_message("Message $subfolder $_");
128 }
129 $talk->unselect();
130 $talk->select($subfolder);
131
132 # discard syslogs from setup
133 $self->{instance}->getsyslog();
134
135 my $resp = $talk->fetch('1,3,5', 'RFC822.SIZE');
136 $self->assert_str_equals('ok', $talk->get_last_completion_response());
137 $self->assert_not_null($resp);
138 my %expected_sizes = map {
139 $_ => $resp->{$_}->{'rfc822.size'}
140 } keys %{$resp};
141
142 $talk->store('1,3,5', '+flags', '(\\Deleted \\Seen)');
143 $talk->expunge();
144
145 if ($self->{instance}->{have_syslog_replacement}) {
146 my @auditlogs = $self->{instance}->getsyslog(qr/auditlog: expunge/);
147
148 my %actual_sizes = map {
149 m/ uid=<([0-9]+)>.* size=<([0-9]+)>/
150 } @auditlogs;
151
152 $self->assert_deep_equals(\%expected_sizes, \%actual_sizes);
153 }
154 }
155
156 sub test_allowdeleted
157 :AllowDeleted :DelayedExpunge :min_version_3_1
158 {
159 my ($self, $folder, %params) = @_;
160
161 my $store = $self->{store};
162 my $talk = $store->get_client();
163
164 my $subfolder = 'INBOX.foo';
165
166 xlog $self, "First create a sub folder";
167 $talk->create($subfolder)
168 or die "Cannot create folder $subfolder: $@";
169 $self->assert_str_equals('ok', $talk->get_last_completion_response());
170
171 xlog $self, "Generate messages in $subfolder";
172 $store->set_folder($subfolder);
173 $store->_select();
174 for (1..5) {
175 $self->make_message("Message $subfolder $_");
176 }
177 $talk->unselect();
178 $talk->select($subfolder);
179
180 my $stat = $talk->status($subfolder, '(highestmodseq unseen messages)');
181 $self->assert_equals(5, $stat->{unseen});
182 $self->assert_equals(5, $stat->{messages});
183
184 my $oldemailids = $talk->fetch('1:*', 'emailid');
185 my @oldemailids = map { $oldemailids->{$_}{emailid}[0] } sort { $a <=> $b } keys %$oldemailids;
186
187 $talk->store('1,3,5', '+flags', '(\\Deleted)');
188 $talk->expunge();
189
190 $stat = $talk->status($subfolder, '(highestmodseq unseen messages)');
191 $self->assert_equals(2, $stat->{unseen});
192 $self->assert_equals(2, $stat->{messages});
193
194 xlog $self, "regular select finds 2 messages";
195 $talk->unselect();
196 $talk->select($subfolder);
197 $self->assert_str_equals('ok', $talk->get_last_completion_response());
198 $self->assert_num_equals(2, $talk->get_response_code('exists'));
199
200 xlog $self, "include-expunged select finds 5 messages";
201 $talk->unselect();
202 # this API is janky
203 $talk->select($subfolder, '(vendor.cmu-include-expunged)' => 1);
204 $self->assert_str_equals('ok', $talk->get_last_completion_response());
205 $self->assert_num_equals(5, $talk->get_response_code('exists'));
206
207 my $newemailids = $talk->fetch('1:*', 'emailid');
208 my @newemailids = map { $newemailids->{$_}{emailid}[0] } sort { $a <=> $b } keys %$newemailids;
209 $self->assert_deep_equals(\@oldemailids, \@newemailids, Data::Dumper::Dumper(\@oldemailids, \@newemailids));
210
211 xlog $self, "copy of deleted messages recreates them";
212 $talk->copy('1,3,5', $subfolder);
213 $talk->unselect();
214 $talk->select($subfolder);
215 $self->assert_str_equals('ok', $talk->get_last_completion_response());
216 $self->assert_num_equals(5, $talk->get_response_code('exists'));
217
218 xlog $self, "new mailbox contains the same emails";
219 $newemailids = $talk->fetch('1:*', 'emailid');
220 @newemailids = map { $newemailids->{$_}{emailid}[0] } sort { $a <=> $b } keys %$newemailids;
221 $self->assert_deep_equals([sort @oldemailids], [sort @newemailids],
222 Data::Dumper::Dumper([sort @oldemailids], [sort @newemailids]));
223 }
224
225 # XXX this isn't really the right place for this test
226 sub test_ipurge_mboxevent
227 :NoAltNameSpace
228 {
229 my ($self) = @_;
230
231 my $shared_folder = 'shared.folder';
232
233 # set up a shared folder that's easy to write to
234 my $admintalk = $self->{adminstore}->get_client();
235 $admintalk->create($shared_folder);
236 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
237 $admintalk->setacl($shared_folder, 'cassandane' => 'lrswipkxtecd');
238 $self->assert_str_equals('ok', $admintalk->get_last_completion_response());
239
240 # put some test messages in shared.folder
241 my $talk = $self->{store}->get_client();
242 $self->{store}->set_folder($shared_folder);
243 $self->{store}->_select();
244 for (1..5) {
245 $self->make_message("message in $shared_folder $_");
246 }
247 $talk->unselect();
248 $talk->select($shared_folder);
249
250 my $stat = $talk->status($shared_folder, '(highestmodseq unseen messages)');
251 $self->assert_num_equals(5, $stat->{unseen});
252 $self->assert_num_equals(5, $stat->{messages});
253
254 # consume/discard earlier events that we don't care about
255 $self->{instance}->getnotify();
256
257 # run ipurge, and collect any mboxevents it generates
258 $self->{instance}->run_command(
259 { cyrus => 1 },
260 qw( ipurge -v -i -d 2 ), $shared_folder
261 );
262 my $events = $self->{instance}->getnotify();
263
264 # if it stays selected you see the intermittent state
265 $talk->unselect();
266
267 # the messages we just created should've been expunged
268 $stat = $talk->status($shared_folder, '(highestmodseq unseen messages)');
269 $self->assert_num_equals(0, $stat->{unseen});
270 $self->assert_num_equals(0, $stat->{messages});
271
272 # examine the mboxevents
273 foreach (@{$events}) {
274 my $e = decode_json($_->{MESSAGE});
275 # uri must contain the mailbox!
276 $self->assert_matches(qr{^imap://(?:[^/]+)/shared\.folder;UIDVALIDITY=},
277 $e->{uri});
278 }
279 }
280
281 1;