Debian Codegraph @

cassandane/Cassandane/Cyrus/Pop3.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::Pop3;
41 use strict;
42 use warnings;
43 use DateTime;
44 use Net::POP3;
45
46 use lib '.';
47 use base qw(Cassandane::Cyrus::TestCase);
48 use Cassandane::Util::Log;
49
50 Cassandane::Cyrus::TestCase::magic(PopSubFolders => sub {
51 shift->config_set(popsubfolders => 1);
52 });
53
54 sub new
55 {
56 my ($class, @args) = @_;
57 return $class->SUPER::new({
58 # We need IMAP to be able to create the mailbox for POP
59 services => ['imap', 'pop3'],
60 }, @args);
61 }
62
63 sub set_up
64 {
65 my ($self) = @_;
66 $self->SUPER::set_up();
67
68 my $svc = $self->{instance}->get_service('pop3');
69 if (defined $svc)
70 {
71 $self->{pop_store} = $svc->create_store();
72 }
73 }
74
75 sub tear_down
76 {
77 my ($self) = @_;
78
79 if (defined $self->{pop_store})
80 {
81 $self->{pop_store}->disconnect();
82 $self->{pop_store} = undef;
83 }
84
85 $self->SUPER::tear_down();
86 }
87
88 sub test_top_args
89 {
90 my ($self) = @_;
91
92 xlog $self, "Testing whether the TOP command checks its arguments [Bug 3641]";
93 # Note, the POP client checks its arguments before sending
94 # them so we have to reach around it to do bad things.
95
96 xlog $self, "Ensure a message exists, before logging in to POP";
97 my %exp;
98 $exp{A} = $self->make_message('Message A');
99
100 my $client = $self->{pop_store}->get_client();
101
102 xlog $self, "TOP with no arguments should return an error";
103 my $r = $client->command('TOP')->response();
104 $self->assert_equals($r, Net::Cmd::CMD_ERROR);
105 $self->assert_equals($client->code(), 500);
106 $self->assert_matches(qr/Missing argument/, $client->message());
107
108 xlog $self, "TOP with 1 argument should return an error";
109 $r = $client->command('TOP', 1)->response();
110 $self->assert_equals($r, Net::Cmd::CMD_ERROR);
111 $self->assert_equals($client->code(), 500);
112 $self->assert_matches(qr/Missing argument/, $client->message());
113
114 xlog $self, "TOP with 2 correct arguments should actually work";
115 $r = $client->command('TOP', 1, 2)->response();
116 $self->assert_equals($r, Net::Cmd::CMD_OK);
117 $self->assert_equals($client->code(), 200);
118 my $lines = $client->read_until_dot();
119 my %actual;
120 $actual{'Message A'} = Cassandane::Message->new(lines => $lines,
121 attrs => { uid => 1 });
122 $self->check_messages(\%exp, actual => \%actual);
123
124 xlog $self, "TOP with 2 arguments, first one not a number, should return an error";
125 $r = $client->command('TOP', '1xyz', 2)->response();
126 $self->assert_equals($r, Net::Cmd::CMD_ERROR);
127 $self->assert_equals($client->code(), 500);
128
129 xlog $self, "TOP with 2 arguments, second one not a number, should return an error";
130 $r = $client->command('TOP', 1, '2xyz')->response();
131 $self->assert_equals($r, Net::Cmd::CMD_ERROR);
132 $self->assert_equals($client->code(), 500);
133
134 xlog $self, "TOP with 3 arguments should return an error";
135 $r = $client->command('TOP', 1, 2, 3)->response();
136 $self->assert_equals($r, Net::Cmd::CMD_ERROR);
137 $self->assert_equals($client->code(), 500);
138 $self->assert_matches(qr/Unexpected extra argument/, $client->message());
139 }
140
141 sub test_subfolder_login
142 :PopSubFolders :NoAltNameSpace
143 {
144 my ($self) = @_;
145
146 xlog $self, "Testing whether + address login gets subfolder";
147
148 my $imapclient = $self->{store}->get_client();
149
150 xlog $self, "Ensure a messages exist";
151 my %exp;
152 $exp{A} = $self->make_message('Message A');
153
154 $imapclient->create('INBOX.sub');
155 $self->{store}->set_folder('INBOX.sub');
156 # different mailbox, so reset generator's expected uid sequence
157 $self->{gen}->set_next_uid(1);
158
159 my %subexp;
160 $subexp{B} = $self->make_message('Message B');
161
162 my $popclient = $self->{pop_store}->get_client();
163
164 xlog $self, "Test regular TOP gets the right message";
165 my $r = $popclient->command('TOP', 1, 2)->response();
166 $self->assert_equals($r, Net::Cmd::CMD_OK);
167 $self->assert_equals($popclient->code(), 200);
168 my $lines = $popclient->read_until_dot();
169 my %actual;
170 $actual{'Message A'} = Cassandane::Message->new(lines => $lines,
171 attrs => { uid => 1 });
172 $self->check_messages(\%exp, actual => \%actual);
173
174 my $svc = $self->{instance}->get_service('pop3');
175 my $substore = $svc->create_store(folder => 'INBOX.sub');
176
177 # create a new client
178 my $subclient = $substore->get_client();
179
180
181 xlog $self, "Test subfolder TOP gets the right message";
182 my $subr = $subclient->command('TOP', 1, 2)->response();
183 $self->assert_equals($subr, Net::Cmd::CMD_OK);
184 $self->assert_equals($subclient->code(), 200);
185 my $sublines = $subclient->read_until_dot();
186 my %subactual;
187 $subactual{'Message B'} = Cassandane::Message->new(lines => $sublines,
188 attrs => { uid => 1 });
189 $self->check_messages(\%subexp, actual => \%subactual);
190 }
191
192 1;
193