Debian Codegraph @

cassandane/Cassandane/Cyrus/IMAP4rev2.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::IMAP4rev2;
41 use strict;
42 use warnings;
43 use Cwd qw(abs_path);
44 use File::Path qw(mkpath);
45 use DateTime;
46 use Data::Dumper;
47
48 use lib '.';
49 use base qw(Cassandane::Cyrus::TestCase);
50 use Cassandane::Util::Log;
51 use Cassandane::Util::NetString;
52
53
54 sub new
55 {
56 my $class = shift;
57 return $class->SUPER::new({ adminstore => 1, services => ['imap'] }, @_);
58 }
59
60 sub set_up
61 {
62 my ($self) = @_;
63 $self->SUPER::set_up();
64 }
65
66 sub tear_down
67 {
68 my ($self) = @_;
69 $self->SUPER::tear_down();
70 }
71
72 sub test_basic
73 :NoAltNameSpace :min_version_3_7
74 {
75 my ($self) = @_;
76
77 xlog $self, "Make some messages";
78 my $uid = 1;
79 my %msgs;
80 for (1..20)
81 {
82 $msgs{$uid} = $self->make_message("Message $uid");
83 $msgs{$uid}->set_attribute('uid', $uid);
84 $uid++;
85 }
86
87 my $talk = $self->{store}->get_client();
88 $talk->unselect();
89
90 xlog $self, "Create mailbox with mUTF7 encoded name";
91 my $res = $talk->_imap_cmd('CREATE', 0, "", "INBOX.&JgA-");
92 $self->assert_str_equals('ok', $talk->get_last_completion_response());
93
94 xlog $self, "ENABLE IMAP4rev2";
95 $res = $talk->_imap_cmd('ENABLE', 0, "enabled", "IMAP4rev2");
96 $self->assert_num_equals(1, $res->{imap4rev2});
97
98 xlog $self, "Verify that LIST responses use UTF8 mailbox names";
99 $res = $talk->list("", "*");
100 $self->assert_mailbox_structure($res, '.', {
101 'INBOX' => [qw( \\HasChildren )],
102 "INBOX.☀" => [qw( \\HasNoChildren )],
103 });
104
105 xlog $self, "EXAMINE mailbox with UTF8 mailbox name";
106 $res = $talk->_imap_cmd('EXAMINE', 0, "", "INBOX.☀");
107 $self->assert_str_equals('ok', $talk->get_last_completion_response());
108
109 xlog $self, "Verify that LIST response is returned with UTF8 mailbox name";
110 my @list = $talk->get_response_code('list');
111 $self->assert_str_equals("INBOX.☀", $list[0][0][2]);
112
113 xlog $self, "Mark some messages \\Deleted";
114 $talk->select('INBOX');
115 $res = $talk->store('5:9', '+flags', '(\\Deleted)');
116
117 xlog $self, "Verify that FETCH responses include UID";
118 $self->assert_str_equals("5", $res->{5}->{uid});
119 $self->assert_str_equals("6", $res->{6}->{uid});
120 $self->assert_str_equals("7", $res->{7}->{uid});
121 $self->assert_str_equals("8", $res->{8}->{uid});
122 $self->assert_str_equals("9", $res->{9}->{uid});
123
124 xlog $self, "Check STATUS (DELETED)";
125 $res = $talk->status('INBOX', [ 'deleted' ]);
126 $self->assert_str_equals('ok', $talk->get_last_completion_response());
127 $self->assert_str_equals("5", $res->{deleted});
128
129 xlog $self, "SEARCH DELETED";
130 my @results = ();
131 my %handlers =
132 (
133 esearch => sub
134 {
135 my (undef, $esearch) = @_;
136 push(@results, $esearch);
137 },
138 );
139 $res = $talk->_imap_cmd('SEARCH', 0, \%handlers, 'DELETED');
140 $self->assert_str_equals('ok', $talk->get_last_completion_response());
141
142 xlog $self, "Verify that ESEARCH response is returned";
143 $self->assert_num_equals(1, scalar @results);
144 $self->assert_str_equals('5:9', $results[0][2]);
145
146 xlog $self, "COPY a deleted message to mailbox with UTF8 name";
147 $res = $talk->_imap_cmd('COPY', 0, "", '5', "INBOX.☀");
148 $self->assert_str_equals('ok', $talk->get_last_completion_response());
149
150 xlog $self, "ESEARCH IN (PERSONAL) DELETED";
151 @results = ();
152 $res = $talk->_imap_cmd('ESEARCH', 0, \%handlers,
153 'IN', '(PERSONAL)', 'DELETED');
154
155 xlog $self, "Verify that ESEARCH response uses UTF8 mailbox name";
156 $self->assert_str_equals('ok', $talk->get_last_completion_response());
157 $self->assert_num_equals(2, scalar @results);
158 $self->assert_str_equals("INBOX", $results[0][0][3]);
159 $self->assert_str_equals('5:9', $results[0][3]);
160 $self->assert_str_equals("INBOX.☀", $results[1][0][3]);
161 $self->assert_str_equals('1', $results[1][3]);
162 }
163
164 sub test_oldname
165 :NoAltNameSpace :min_version_3_7
166 {
167 my ($self) = @_;
168
169 xlog $self, "Make some messages";
170 my $uid = 1;
171 my %msgs;
172 for (1..20)
173 {
174 $msgs{$uid} = $self->make_message("Message $uid");
175 $msgs{$uid}->set_attribute('uid', $uid);
176 $uid++;
177 }
178
179 my $talk = $self->{store}->get_client();
180 $talk->unselect();
181
182 xlog $self, "ENABLE IMAP4rev2";
183 my $res = $talk->_imap_cmd('ENABLE', 0, "enabled", "IMAP4rev2");
184 $self->assert_num_equals(1, $res->{imap4rev2});
185
186 my @results = ();
187 my %handlers =
188 (
189 list => sub
190 {
191 my (undef, $list) = @_;
192 push(@results, $list);
193 },
194 );
195
196 xlog $self, "Create a mailbox with denormalized mailbox name";
197 $res = $talk->_imap_cmd('CREATE', 0, \%handlers, "INBOX.Å");
198 $self->assert_str_equals('ok', $talk->get_last_completion_response());
199
200 xlog $self, "Verify that LIST response is returned with OLDNAME";
201 $self->assert_str_equals("INBOX.Å", $results[0][2]);
202 $self->assert_str_equals('OLDNAME', $results[0][3][0]);
203 $self->assert_str_equals('INBOX.Å', $results[0][3][1][0]);
204
205 xlog $self, "Create a child mailbox with normalized mailbox name";
206 @results = ();
207 $res = $talk->_imap_cmd('CREATE', 0, \%handlers, "INBOX.Å.B");
208 $self->assert_str_equals('ok', $talk->get_last_completion_response());
209
210 xlog $self, "Verify that there is no LIST response";
211 $self->assert_null($results[0]);
212
213 xlog $self, "Append to mailbox with denormalized mailbox name";
214 my $MsgTxt = <<EOF;
215 From: blah\@xyz.com
216 To: whoever\@whereever.com
217
218 Hello
219 EOF
220 $MsgTxt =~ s/\n/\015\012/g;
221 @results = ();
222 $res = $talk->_imap_cmd('APPEND', 0, \%handlers, "INBOX.Å", { Literal => $MsgTxt });
223 $self->assert_str_equals('ok', $talk->get_last_completion_response());
224
225 xlog $self, "Verify that LIST response is returned with OLDNAME";
226 $self->assert_str_equals("INBOX.Å", $results[0][2]);
227 $self->assert_str_equals('OLDNAME', $results[0][3][0]);
228 $self->assert_str_equals('INBOX.Å', $results[0][3][1][0]);
229
230 xlog $self, "EXAMINE mailbox with denormalized mailbox name";
231 @results = ();
232 $res = $talk->_imap_cmd('EXAMINE', 0, \%handlers, "INBOX.Å");
233 $self->assert_str_equals('ok', $talk->get_last_completion_response());
234
235 xlog $self, "Verify that LIST response is returned with OLDNAME";
236 $self->assert_str_equals("INBOX.Å", $results[0][2]);
237 $self->assert_str_equals('OLDNAME', $results[0][3][0]);
238 $self->assert_str_equals('INBOX.Å', $results[0][3][1][0]);
239
240 $talk->unselect();
241
242 xlog $self, "RENAME mailbox with denormalized mailbox names";
243 @results = ();
244 $res = $talk->_imap_cmd('RENAME', 0, \%handlers, "INBOX.Å", "INBOX.Ω");
245 $self->assert_str_equals('ok', $talk->get_last_completion_response());
246
247 xlog $self, "Verify that LIST responses are returned with OLDNAMEs";
248 $self->assert_str_equals("\\NonExistent", $results[0][0][0]);
249 $self->assert_str_equals("INBOX.Å", $results[0][2]);
250 $self->assert_str_equals('OLDNAME', $results[0][3][0]);
251 $self->assert_str_equals('INBOX.Å', $results[0][3][1][0]);
252 $self->assert_str_equals("\\HasChildren", $results[1][0][0]);
253 $self->assert_str_equals("INBOX.Ω", $results[1][2]);
254 $self->assert_str_equals('OLDNAME', $results[1][3][0]);
255 $self->assert_str_equals('INBOX.Ω', $results[1][3][1][0]);
256
257 xlog $self, "LIST renamed mailbox";
258 @results = ();
259 $res = $talk->_imap_cmd('LIST', 0, \%handlers, "", "INBOX.Ω");
260 $self->assert_str_equals('ok', $talk->get_last_completion_response());
261
262 xlog $self, "Verify that OLDNAME appears in LIST response";
263 $self->assert_str_equals('OLDNAME', $results[0][3][0]);
264 $self->assert_str_equals('INBOX.Å', $results[0][3][1][0]);
265
266 xlog $self, "DELETE a child mailbox with normalized mailbox name";
267 @results = ();
268 $res = $talk->_imap_cmd('DELETE', 0, \%handlers, "INBOX.Ω.B");
269 $self->assert_str_equals('ok', $talk->get_last_completion_response());
270
271 xlog $self, "Verify that there is no LIST response";
272 $self->assert_null($results[0]);
273
274 xlog $self, "DELETE mailbox with denormalized mailbox name";
275 @results = ();
276 $res = $talk->_imap_cmd('DELETE', 0, \%handlers, "INBOX.Ω");
277 $self->assert_str_equals('ok', $talk->get_last_completion_response());
278
279 xlog $self, "Verify that LIST response is returned with OLDNAME";
280 $self->assert_str_equals("\\NonExistent", $results[0][0][0]);
281 $self->assert_str_equals("INBOX.Ω", $results[0][2]);
282 $self->assert_str_equals('OLDNAME', $results[0][3][0]);
283 $self->assert_str_equals('INBOX.Ω', $results[0][3][1][0]);
284 }
285
286 1;