Debian Codegraph @

cassandane/Cassandane/Cyrus/Subscriptions.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::Subscriptions;
41 use strict;
42 use warnings;
43 use DateTime;
44 use Data::Dumper;
45 use File::Basename;
46 use File::Copy;
47 use File::Path qw(mkpath);
48
49 use lib '.';
50 use base qw(Cassandane::Cyrus::TestCase);
51 use Cassandane::Mboxname;
52 use Cassandane::Util::Log;
53 use Cassandane::Util::Words;
54
55 sub new
56 {
57 my $class = shift;
58 return $class->SUPER::new({ adminstore => 1 }, @_);
59 }
60
61 sub set_up
62 {
63 my ($self) = @_;
64 $self->SUPER::set_up();
65
66 my $admintalk = $self->{adminstore}->get_client();
67
68 # Right - let's create ourselves some users and subscriptions
69 # sub folders of the main user
70 $admintalk->create("user.cassandane.asub");
71 $admintalk->create("user.cassandane.asub.deeper");
72
73 # sub folders of another user - one is subscribable
74 $self->{instance}->create_user("other",
75 subdirs => [ 'sub', ['sub', 'folder'] ]);
76 $admintalk->setacl("user.other.sub.folder", "cassandane", "lrs");
77
78 my $usertalk = $self->{store}->get_client();
79 $usertalk->subscribe("INBOX");
80 $usertalk->subscribe("INBOX.asub");
81 $usertalk->subscribe("user.other.sub.folder");
82 }
83
84 sub tear_down
85 {
86 my ($self) = @_;
87 $self->SUPER::tear_down();
88 }
89
90 #
91 # Test LSUB behaviour
92 #
93 sub test_lsub_toplevel
94 :NoAltNameSpace
95 {
96 my ($self) = @_;
97
98 my $imaptalk = $self->{store}->get_client();
99
100 my $alldata = $imaptalk->lsub("", "*");
101 $self->assert_deep_equals($alldata, [
102 [
103 [
104 '\\HasChildren'
105 ],
106 '.',
107 'INBOX'
108 ],
109 [
110 [],
111 '.',
112 'INBOX.asub'
113 ],
114 [
115 [],
116 '.',
117 'user.other.sub.folder'
118 ]
119 ], "LSUB all data mismatch: " . Dumper($alldata));
120
121 my $topdata = $imaptalk->lsub("", "%");
122 $self->assert_deep_equals($topdata, [
123 [
124 [
125 '\\HasChildren'
126 ],
127 '.',
128 'INBOX'
129 ],
130 [
131 [
132 '\\Noselect',
133 '\\HasChildren'
134 ],
135 '.',
136 'user'
137 ],
138 ], "LSUB top data mismatch:" . Dumper($topdata));
139 }
140
141 sub test_lsub_delete
142 {
143 my ($self) = @_;
144
145 my $imaptalk = $self->{store}->get_client();
146
147 $imaptalk->create("INBOX.deltest") || die;
148 $imaptalk->create("INBOX.deltest.sub1") || die;
149 $imaptalk->create("INBOX.deltest.sub2") || die;
150 $imaptalk->subscribe("INBOX.deltest") || die;
151 $imaptalk->subscribe("INBOX.deltest.sub2") || die;
152 my $subdata = $imaptalk->lsub("INBOX.deltest", "*");
153 $self->assert_deep_equals($subdata, [
154 [
155 [
156 '\\HasChildren'
157 ],
158 '.',
159 'INBOX.deltest'
160 ],
161 [
162 [],
163 '.',
164 'INBOX.deltest.sub2'
165 ],
166 ], "LSUB deltest setup mismatch: " . Dumper($subdata));
167
168 $imaptalk->delete("INBOX.deltest.sub2");
169 my $onedata = $imaptalk->lsub("INBOX.deltest", "*");
170 $self->assert_deep_equals($onedata, [
171 [
172 [
173 '\\HasChildren'
174 ],
175 '.',
176 'INBOX.deltest'
177 ],
178 ], "LSUB deltest.sub2 after delete mismatch: " . Dumper($onedata));
179 }
180
181 sub test_lsub_extrachild
182 :NoAltNameSpace
183 {
184 my ($self) = @_;
185
186 my $imaptalk = $self->{store}->get_client();
187
188 $imaptalk->create("INBOX.Test") || die;
189 $imaptalk->create("INBOX.Test.Sub") || die;
190 $imaptalk->create("INBOX.Test Foo") || die;
191 $imaptalk->create("INBOX.Test Bar") || die;
192 $imaptalk->subscribe("INBOX.Test") || die;
193 $imaptalk->subscribe("INBOX.Test.Sub") || die;
194 $imaptalk->subscribe("INBOX.Test Foo") || die;
195 $imaptalk->delete("INBOX.Test.Sub") || die;
196 my $subdata = $imaptalk->lsub("", "*");
197 $self->assert_deep_equals($subdata, [
198 [
199 [
200 '\\HasChildren'
201 ],
202 '.',
203 'INBOX'
204 ],
205 [
206 [
207 '\\HasChildren'
208 ],
209 '.',
210 'INBOX.Test'
211 ],
212 [
213 [],
214 '.',
215 'INBOX.Test Foo'
216 ],
217 [
218 [],
219 '.',
220 'INBOX.asub'
221 ],
222 [
223 [],
224 '.',
225 'user.other.sub.folder'
226 ],
227 ], "LSUB extrachild mismatch: " . Dumper($subdata));
228 }
229
230 sub v2
231 {
232 my ($first, @rest) = @_;
233
234 my $v2 = 'N' . $first;
235 $v2 .= "\x1f" . $_ for @rest;
236 return $v2;
237 }
238
239 sub upgrade_from_2_common
240 {
241 my ($self, $domain) = @_;
242
243 # hardcoded localparts because we need to predict particular badness
244 my $user1 = 'matt';
245 my $user2 = 'matthew';
246
247 # optional domain for testing with/without virtdomains.
248 # we only test with no domain or the same domain, because #5146 didn't
249 # happen if the two users were in different domains, so there wouldn't be
250 # a "bad" db record to "fix"
251 if ($domain) {
252 $user1 .= "\@$domain";
253 $user2 .= "\@$domain";
254 }
255
256 my $config = $self->{instance}->{config};
257 my $sep = $config->get_bool('unixhierarchysep', 'on') ? '/' : '.';
258
259 my $user1_inbox = Cassandane::Mboxname->new(config => $config);
260 $user1_inbox->from_username($user1);
261
262 my @user1_mailboxes = map {
263 $user1_inbox->make_child($_);
264 } random_words(3);
265
266 $self->{instance}->create_user($user1,
267 subdirs => \@user1_mailboxes);
268
269 my $user2_inbox = Cassandane::Mboxname->new(config => $config);
270 $user2_inbox->from_username($user2);
271
272 my @user2_mailboxes = map {
273 $user2_inbox->make_child($_);
274 } random_words(3);
275
276 $self->{instance}->create_user($user2,
277 subdirs => \@user2_mailboxes);
278
279 my $mbpath = $self->{instance}->run_mbpath('-u', $user1);
280 my $subdb = $mbpath->{user}->{sub};
281 my $engine = $self->{instance}->{config}->get('subscription_db') || 'flat';
282
283 # generate user1 subscriptions in the v2 format
284 my @items = (
285 # version key
286 [ 'SET', "\x1fVER\x1f", '2' ],
287 # subscribe to own inbox (user.matt)
288 [ 'SET', v2('INBOX') ],
289 # subscribe to own subdirs (user.matt.foo)
290 (map { [ 'SET', v2('INBOX', $_->box()) ] } @user1_mailboxes),
291 # some bad subscriptions as if we had hit bug #5146
292 [ 'SET', v2('INBOXhew') ], # user.matthew
293 (map { [ 'SET', v2('INBOXhew', $_->box()) ] } @user2_mailboxes),
294 );
295
296 # XXX run_dbcommand needs the file to already exist
297 mkpath(dirname($subdb));
298 open my $fh, '>', $subdb or die "open $subdb: $!";
299 close $fh;
300
301 # create v2 subscriptions file
302 $self->{instance}->run_dbcommand($subdb, $engine, @items);
303
304 # make a copy of sub.db pre-upgrade in case we want to look at it later
305 copy($subdb, "$subdb.orig");
306
307 # discard syslog accumulated so far
308 $self->{instance}->getsyslog();
309
310 # list user1's subscriptions normally (db should be upgraded on open)
311 my $service = $self->{instance}->get_service('imap');
312 my $store = $service->create_store(username => $user1);
313 my $talk = $store->get_client();
314
315 my $subdata = $talk->lsub("", "*");
316 $self->assert_mailbox_structure($subdata, $sep, {
317 $user1_inbox->to_external('owner') => [],
318 (map { $_->to_external('owner') => [] } @user1_mailboxes),
319 $user2_inbox->to_external('other') => [ '\\HasChildren' ],
320 (map { $_->to_external('other') => [] } @user2_mailboxes),
321 });
322 $talk->logout();
323
324 # check that we upgraded
325 $self->assert_syslog_matches($self->{instance},
326 qr{upgrading user subscriptions});
327 my (undef, $version) = $self->{instance}->run_dbcommand($subdb, $engine,
328 [ 'GET', "\x1fVER\x1f" ]
329 );
330 $self->assert_num_equals(3, $version);
331
332 # list user's subscriptions again
333 $talk = $store->get_client();
334
335 $subdata = $talk->lsub("", "*");
336 $self->assert_mailbox_structure($subdata, $sep, {
337 $user1_inbox->to_external('owner') => [],
338 (map { $_->to_external('owner') => [] } @user1_mailboxes),
339 $user2_inbox->to_external('other') => [ '\\HasChildren' ],
340 (map { $_->to_external('other') => [] } @user2_mailboxes),
341 });
342 $talk->logout();
343
344 # better not have upgraded this time
345 $self->assert_syslog_does_not_match($self->{instance},
346 qr{upgrading user subscriptions});
347 }
348
349 sub test_upgrade_from_2
350 {
351 my ($self) = @_;
352
353 $self->upgrade_from_2_common(undef);
354 }
355
356 sub test_upgrade_from_2_vd
357 :VirtDomains :CrossDomains
358 {
359 my ($self) = @_;
360
361 $self->upgrade_from_2_common('example.com');
362 }
363
364 1;