Debian Codegraph @

cassandane/Cassandane/Cyrus/UTF8Accept.pm

raw
1 #!/usr/bin/perl
2 #
3 # Copyright (c) 2011-2024 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::UTF8Accept;
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 my $config = Cassandane::Config->default()->clone();
58
59 # Make sure the server will advertise support for UTF8=ACCEPT
60 $config->set(reject8bit => 'off');
61 $config->set(munge8bit => 'off');
62
63 return $class->SUPER::new({ services => ['imap'], config => $config }, @_);
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 test_mboxname
79 :NoAltNameSpace :min_version_3_9
80 {
81 my ($self) = @_;
82
83 my $talk = $self->{store}->get_client();
84
85 xlog $self, "Create mailbox with mUTF7 encoded name";
86 my $res = $talk->_imap_cmd('CREATE', 0, "", "INBOX.&JgA-");
87 $self->assert_str_equals('ok', $talk->get_last_completion_response());
88
89 xlog $self, "ENABLE UTF8=ACCEPT";
90 $res = $talk->_imap_cmd('ENABLE', 0, "enabled", "UTF8=ACCEPT");
91 $self->assert_num_equals(1, $res->{'utf8=accept'});
92
93 xlog $self, "Create a mailbox with denormalized mailbox name";
94 $res = $talk->_imap_cmd('CREATE', 0, "", "INBOX.Å");
95 $self->assert_str_equals('ok', $talk->get_last_completion_response());
96
97 xlog $self, "Create a child mailbox with normalized mailbox name";
98 $res = $talk->_imap_cmd('CREATE', 0, "", "INBOX.Å.B");
99 $self->assert_str_equals('ok', $talk->get_last_completion_response());
100
101 xlog $self, "Verify that LIST responses use UTF8 mailbox names";
102 $res = $talk->list("", "*");
103 $self->assert_mailbox_structure($res, '.', {
104 'INBOX' => [qw( \\HasChildren )],
105 'INBOX.☀' => [qw( \\HasNoChildren )],
106 'INBOX.Å' => [qw( \\HasChildren )],
107 "INBOX.Å.B" => [qw( \\HasNoChildren )],
108 });
109
110 xlog $self, "EXAMINE mailbox with UTF8 mailbox name";
111 $res = $talk->_imap_cmd('EXAMINE', 0, "", "INBOX.☀");
112 $self->assert_str_equals('ok', $talk->get_last_completion_response());
113 $talk->unselect();
114
115 xlog $self, "RENAME mailbox with denormalized mailbox names";
116 $res = $talk->_imap_cmd('RENAME', 0, "", "INBOX.Å", "INBOX.Ω");
117 $self->assert_str_equals('ok', $talk->get_last_completion_response());
118
119 xlog $self, "DELETE a child mailbox with normalized mailbox name";
120 $res = $talk->_imap_cmd('DELETE', 0, "", "INBOX.Ω.B");
121 $self->assert_str_equals('ok', $talk->get_last_completion_response());
122
123 xlog $self, "Verify that LIST responses use UTF8 mailbox names";
124 $res = $talk->list("", "*");
125 $self->assert_mailbox_structure($res, '.', {
126 'INBOX' => [qw( \\HasChildren )],
127 'INBOX.☀' => [qw( \\HasNoChildren )],
128 "INBOX.Ω" => [qw( \\HasNoChildren )],
129 });
130 }
131
132 sub test_append
133 :NoAltNameSpace :min_version_3_9
134 {
135 my ($self) = @_;
136
137 my $talk = $self->{store}->get_client();
138
139 my $MsgTxt = <<EOF;
140 From: blah\@xyz.com
141 To: whoever\@whereever.com
142 Subject: you are a ☀
143
144 Hello
145 EOF
146 $MsgTxt =~ s/\n/\015\012/g;
147
148 xlog $self, "Create mailbox with mUTF7 encoded name";
149 my $res = $talk->_imap_cmd('CREATE', 0, "", "INBOX.&JgA-");
150 $self->assert_str_equals('ok', $talk->get_last_completion_response());
151
152 # Using UTF8 before UTF8=ACCEPT should fail
153 xlog $self, "Attempt to append message with UTF-8 header to mailbox";
154 $res = $talk->_imap_cmd('APPEND', 0, "", "INBOX.&JgA-",
155 'UTF8', [ { Literal => $MsgTxt } ]);
156 $self->assert_str_equals('bad', $talk->get_last_completion_response());
157
158 xlog $self, "ENABLE UTF8=ACCEPT";
159 $res = $talk->_imap_cmd('ENABLE', 0, "enabled", "UTF8=ACCEPT");
160 $self->assert_num_equals(1, $res->{'utf8=accept'});
161
162 xlog $self, "Append message with UTF-8 header to mailbox";
163 $res = $talk->_imap_cmd('APPEND', 0, "", "INBOX.☀",
164 'UTF8', [ { Literal => $MsgTxt } ]);
165 $self->assert_str_equals('ok', $talk->get_last_completion_response());
166
167 xlog $self, "Catenate message with UTF-8 header to mailbox";
168 $res = $talk->_imap_cmd('APPEND', 0, "", "INBOX.☀",
169 'CATENATE', [ 'UTF8', [ { Literal => $MsgTxt } ] ]);
170 $self->assert_str_equals('ok', $talk->get_last_completion_response());
171 }
172
173 sub test_search_sort_thread
174 :NoAltNameSpace :min_version_3_9
175 {
176 my ($self) = @_;
177
178 xlog $self, "Make some messages";
179 my $uid = 1;
180 my %msgs;
181 for (1..10)
182 {
183 $msgs{$uid} = $self->make_message("Message $uid");
184 $msgs{$uid}->set_attribute('uid', $uid);
185 $uid++;
186 }
187
188 my $talk = $self->{store}->get_client();
189
190 # Verify that pre-ENABLE search/sort/thread work as expected
191 my $uids = $talk->search('charset', 'us-ascii', 'all');
192 $self->assert_str_equals('ok', $talk->get_last_completion_response());
193
194 $uids = $talk->sort('(size)', 'us-ascii', 'all');
195 $self->assert_str_equals('ok', $talk->get_last_completion_response());
196
197 $uids = $talk->thread('orderedsubject', 'us-ascii', 'all');
198 $self->assert_str_equals('ok', $talk->get_last_completion_response());
199
200 xlog $self, "ENABLE UTF8=ACCEPT";
201 my $res = $talk->_imap_cmd('ENABLE', 0, "enabled", "UTF8=ACCEPT");
202 $self->assert_num_equals(1, $res->{'utf8=accept'});
203
204 # Using CHARSET after UTF8=ACCEPT should fail
205 $uids = $talk->search('charset', 'us-ascii', 'all');
206 $self->assert_str_equals('bad', $talk->get_last_completion_response());
207
208 $uids = $talk->search('all');
209 $self->assert_str_equals('ok', $talk->get_last_completion_response());
210
211 # Using CHARSET other than UTF-8 after UTF8=ACCEPT should fail
212 $uids = $talk->sort('(size)', 'us-ascii', 'all');
213 $self->assert_str_equals('bad', $talk->get_last_completion_response());
214
215 $uids = $talk->sort('(size)', 'utf8', 'all');
216 $self->assert_str_equals('ok', $talk->get_last_completion_response());
217
218 # Using CHARSET other than UTF-8 after UTF8=ACCEPT should fail
219 $uids = $talk->thread('orderedsubject', 'us-ascii', 'all');
220 $self->assert_str_equals('bad', $talk->get_last_completion_response());
221
222 $uids = $talk->thread('orderedsubject', 'utf8', 'all');
223 $self->assert_str_equals('ok', $talk->get_last_completion_response());
224 }
225
226 1;