Debian Codegraph @

cassandane/Cassandane/Cyrus/HTTPPTS.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2022 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::HTTPPTS;
41 use strict;
42 use warnings;
43 use Cwd qw(realpath);
44 use JSON;
45 use Data::Dumper;
46
47 use lib '.';
48 use base qw(Cassandane::Cyrus::TestCase);
49 use base qw(Cassandane::Unit::TestCase);
50 use Cassandane::Util::Log;
51
52 sub new
53 {
54 my ($class, @args) = @_;
55
56 my $config = Cassandane::Config->default()->clone();
57 $config->set(
58 auth_mech => 'pts',
59 pts_module => 'http',
60 ptloader_sock => '@basedir@/conf/ptsock',
61 );
62
63 my $self = $class->SUPER::new({
64 config => $config,
65 adminstore => 1,
66 services => [qw( imap ptloader )],
67 start_instances => 0,
68 }, @args);
69
70 return $self;
71 }
72
73 sub set_up
74 {
75 my ($self) = @_;
76
77 $self->SUPER::set_up();
78
79 $self->{server} = $self->new_test_url(sub {
80 my $env = shift;
81 my $req = Plack::Request->new($env);
82
83 my $res;
84
85 if ($req->method eq 'GET') {
86 if ($req->query_parameters->{id} eq 'cassandane') {
87 $res = Plack::Response->new(200);
88 $res->content_type('application/json');
89 $res->body(encode_json({ cassandane => [ "group:group co",
90 "group:group c" ] }));
91 } elsif ($req->query_parameters->{id} eq 'otheruser') {
92 $res = Plack::Response->new(200);
93 $res->content_type('application/json');
94 $res->body(encode_json({ otheruser => [ "group:group co",
95 "group:group o" ] }));
96 } elsif ($req->query_parameters->{id} eq 'group:group c') {
97 $res = Plack::Response->new(200);
98 $res->content_type('application/json');
99 $res->body(encode_json({ 'group:group c' => [ "cassandane" ] }));
100 } elsif ($req->query_parameters->{id} eq 'group:group co') {
101 $res = Plack::Response->new(200);
102 $res->content_type('application/json');
103 $res->body(encode_json({ 'group:group co' => [ "cassandane",
104 "otheruser" ] }));
105 } elsif ($req->query_parameters->{id} eq 'group:group o') {
106 $res = Plack::Response->new(200);
107 $res->content_type('application/json');
108 $res->body(encode_json({ 'group:group o' => [ "otheruser" ] }));
109 } elsif ($req->query_parameters->{id} eq 'group:foo') {
110 $res = Plack::Response->new(200);
111 $res->content_type('application/json');
112 $res->body(encode_json({ 'group:foo' => [ ] }));
113 } elsif ($req->query_parameters->{id} eq 'group:this group name has spaces') {
114 $res = Plack::Response->new(200);
115 $res->content_type('application/json');
116 $res->body(encode_json({ 'group:this group name has spaces' => [ ] }));
117 } else {
118 $res = Plack::Response->new(404);
119 }
120 }
121 elsif ($req->method eq 'OPTIONS') {
122 $res = Plack::Response->new(200);
123 }
124 else {
125 $res = Plack::Response->new(501);
126 }
127
128 return $res->finalize;
129 });
130
131 my $uri = $self->{server}->url . "?id={groupId}";
132
133 $self->{instance}->{config}->set(
134 httppts_uri => $uri
135 );
136
137 $self->_start_instances();
138
139 $self->{instance}->create_user("otheruser");
140 }
141
142 sub tear_down
143 {
144 my ($self) = @_;
145
146 # clean this up as soon as we're done with it, cause it's holding a
147 # port open!
148 delete $self->{server};
149
150 $self->SUPER::tear_down();
151 }
152
153 sub test_alternate_ptscache_db_path
154 :min_version_3_7 :AltPTSDBPath
155 {
156 my ($self) = @_;
157
158 # just interact with the store, and it should work
159 my $admintalk = $self->{adminstore}->get_client();
160
161 $admintalk->list('user.cassandane', '*');
162 $self->assert_str_equals('ok',
163 $admintalk->get_last_completion_response());
164
165 my $confdir = $self->{instance}->{basedir} . "/conf";
166 $self->assert_file_test($confdir . "/non-default-ptscache.db");
167 $self->assert_not_file_test($confdir . "/ptclient/ptscache.db");
168 }
169
170 sub test_setacl_groupid
171 :min_version_3_7
172 {
173 my ($self) = @_;
174
175 my $admintalk = $self->{adminstore}->get_client();
176
177 $admintalk->create("user.cassandane.groupid");
178 $self->assert_str_equals('ok',
179 $admintalk->get_last_completion_response());
180
181 $admintalk->setacl("user.cassandane.groupid",
182 "group:foo",
183 "lrswipkxtecdan");
184 $self->assert_str_equals('ok',
185 $admintalk->get_last_completion_response());
186 }
187
188 sub test_setacl_groupid_spaces
189 :min_version_3_7
190 {
191 my ($self) = @_;
192
193 my $admintalk = $self->{adminstore}->get_client();
194
195 $admintalk->create("user.cassandane.groupid_spaces");
196 $self->assert_str_equals('ok',
197 $admintalk->get_last_completion_response());
198
199 $admintalk->setacl("user.cassandane.groupid_spaces",
200 "group:this group name has spaces",
201 "lrswipkxtecdan");
202 $self->assert_str_equals('ok',
203 $admintalk->get_last_completion_response());
204
205 my $data = $admintalk->getacl("user.cassandane.groupid_spaces");
206 $self->assert_str_equals('ok',
207 $admintalk->get_last_completion_response());
208
209 $self->assert(scalar @{$data} % 2 == 0);
210 my %acl = @{$data};
211 $self->assert_str_equals($acl{"group:this group name has spaces"},
212 "lrswipkxtecdan");
213
214 $admintalk->select("user.cassandane.groupid_spaces");
215 $self->assert_str_equals('ok',
216 $admintalk->get_last_completion_response());
217 }
218
219 sub test_list_groupaccess_noracl
220 :min_version_3_7 :NoAltNamespace
221 {
222 my ($self) = @_;
223
224 my $admintalk = $self->{adminstore}->get_client();
225 my $imaptalk = $self->{store}->get_client();
226
227 $admintalk->create("user.otheruser.groupaccess");
228 $self->assert_str_equals('ok',
229 $admintalk->get_last_completion_response());
230
231 $admintalk->setacl("user.otheruser.groupaccess",
232 "group:group co", "lrswipkxtecdan");
233 $self->assert_str_equals('ok',
234 $admintalk->get_last_completion_response());
235
236 my $list = $imaptalk->list("", "*");
237 my @boxes = sort map { $_->[2] } @{$list};
238
239 $self->assert_deep_equals(\@boxes,
240 ['INBOX', 'user.otheruser.groupaccess']);
241 }
242
243 sub test_list_groupaccess_racl
244 :ReverseACLs :min_version_3_7 :NoAltNamespace :Conversations
245 {
246 my ($self) = @_;
247
248 my $admintalk = $self->{adminstore}->get_client();
249 my $imaptalk = $self->{store}->get_client();
250
251 $admintalk->create("user.otheruser.groupaccess");
252 $self->assert_str_equals('ok',
253 $admintalk->get_last_completion_response());
254
255 my $precounters = $self->{store}->get_counters();
256
257 $admintalk->setacl("user.otheruser.groupaccess",
258 "group:group co", "lrswipkxtecdn");
259 $self->assert_str_equals('ok',
260 $admintalk->get_last_completion_response());
261
262 my $postcounters = $self->{store}->get_counters();
263 $self->assert_num_not_equals($precounters->{raclmodseq}, $postcounters->{raclmodseq}, "RACL modseq changed");
264
265 if (get_verbose()) {
266 $self->{instance}->run_command(
267 { cyrus => 1, },
268 'cyr_dbtool',
269 "$self->{instance}->{basedir}/conf/mailboxes.db",
270 'twoskip',
271 'show'
272 );
273 }
274
275 my $list = $imaptalk->list("", "*");
276 my @boxes = sort map { $_->[2] } @{$list};
277
278 $self->assert_deep_equals(\@boxes,
279 ['INBOX', 'user.otheruser.groupaccess']);
280 }
281
282 sub do_test_list_order
283 :min_version_3_7
284 {
285 my ($self) = @_;
286
287 my $admintalk = $self->{adminstore}->get_client();
288 my $imaptalk = $self->{store}->get_client();
289
290 $imaptalk->create("INBOX.zzz");
291 $self->assert_str_equals('ok',
292 $imaptalk->get_last_completion_response());
293
294 $imaptalk->create("INBOX.aaa");
295 $self->assert_str_equals('ok',
296 $imaptalk->get_last_completion_response());
297
298 my %adminfolders = (
299 'user.otheruser.order-user' => 'cassandane',
300 'user.otheruser.order-co' => 'group:group co',
301 'user.otheruser.order-c' => 'group:group c',
302 'user.otheruser.order-o' => 'group:group o',
303 'shared.order-co' => 'group:group co',
304 'shared.order-c' => 'group:group c',
305 'shared.order-o' => 'group:group o',
306 );
307
308 while (my ($folder, $identifier) = each %adminfolders) {
309 $admintalk->create($folder);
310 $self->assert_str_equals('ok',
311 $admintalk->get_last_completion_response(),
312 "created folder $folder successfully");
313
314 $admintalk->setacl($folder, $identifier, "lrswipkxtecdn");
315 $self->assert_str_equals('ok',
316 $admintalk->get_last_completion_response(),
317 "setacl folder $folder for $identifier successfully");
318
319 if ($folder =~ m/^shared/) {
320 # subvert default permissions on shared namespace for
321 # purpose of testing ordering
322 $admintalk->setacl($folder, "anyone", "p");
323 $self->assert_str_equals('ok',
324 $admintalk->get_last_completion_response(),
325 "setacl folder $folder for anyone successfully");
326 }
327 }
328
329 if (get_verbose()) {
330 $self->{instance}->run_command(
331 { cyrus => 1, },
332 'cyr_dbtool',
333 "$self->{instance}->{basedir}/conf/mailboxes.db",
334 'twoskip',
335 'show'
336 );
337 }
338
339 my $list = $imaptalk->list("", "*");
340 my @boxes = map { $_->[2] } @{$list};
341
342 # Note: order is
343 # * mine, alphabetically,
344 # * other users', alphabetically,
345 # * shared, alphabetically
346 # ... which is not the order we created them ;)
347 # Also, the "order-o" folders are not returned, because cassandane
348 # is not a member of that group
349 my @expect = qw(
350 INBOX
351 INBOX.aaa
352 INBOX.zzz
353 user.otheruser.order-c
354 user.otheruser.order-co
355 user.otheruser.order-user
356 );
357 my ($maj, $min) = Cassandane::Instance->get_version();
358 if ($maj > 3 || ($maj == 3 && $min > 4)) {
359 push @expect, qw(shared);
360 }
361 push @expect, qw( shared.order-c shared.order-co );
362 $self->assert_deep_equals(\@boxes, \@expect);
363 }
364
365 sub test_list_order_noracl
366 :min_version_3_7 :NoAltNamespace
367 {
368 my $self = shift;
369 return $self->do_test_list_order(@_);
370 }
371
372 sub test_list_order_racl
373 :ReverseACLs :min_version_3_7 :NoAltNamespace
374 {
375 my $self = shift;
376 return $self->do_test_list_order(@_);
377 }
378
379 1;