Mercurial > hg > nginx-tests
annotate lib/Test/Nginx/POP3.pm @ 540:481d705b8610
Tests: SSL support in mail backends.
Socket is now embedded into every mail module.
Socket methods are wrapped where appropriate.
The new "SSL" extra flag specifies to accept connection over SSL.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Fri, 03 Apr 2015 00:11:38 +0300 |
parents | 3fcad5e66735 |
children | 9361c7eddfc1 |
rev | line source |
---|---|
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
1 package Test::Nginx::POP3; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
2 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
3 # (C) Maxim Dounin |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
4 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
5 # Module for nginx pop3 tests. |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
6 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
7 ############################################################################### |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
8 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
9 use warnings; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
10 use strict; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
11 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
12 use Test::More qw//; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
13 use IO::Socket; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
14 use Socket qw/ CRLF /; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
15 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
16 use Test::Nginx; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
17 |
540
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
18 sub new { |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
19 my $self = {}; |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
20 bless $self, shift @_; |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
21 |
540
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
22 $self->{_socket} = IO::Socket::INET->new( |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
23 Proto => "tcp", |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
24 PeerAddr => "127.0.0.1:8110", |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
25 @_ |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
26 ) |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
27 or die "Can't connect to nginx: $!\n"; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
28 |
540
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
29 if ({@_}->{'SSL'}) { |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
30 require IO::Socket::SSL; |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
31 IO::Socket::SSL->start_SSL($self->{_socket}, @_) |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
32 or die $IO::Socket::SSL::SSL_ERROR . "\n"; |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
33 } |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
34 |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
35 $self->{_socket}->autoflush(1); |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
36 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
37 return $self; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
38 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
39 |
540
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
40 sub eof { |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
41 my $self = shift; |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
42 return $self->{_socket}->eof(); |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
43 } |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
44 |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
45 sub print { |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
46 my ($self, $cmd) = @_; |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
47 log_out($cmd); |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
48 $self->{_socket}->print($cmd); |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
49 } |
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
50 |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
51 sub send { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
52 my ($self, $cmd) = @_; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
53 log_out($cmd); |
540
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
54 $self->{_socket}->print($cmd . CRLF); |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
55 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
56 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
57 sub read { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
58 my ($self) = @_; |
540
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
59 my $socket = $self->{_socket}; |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
60 eval { |
209
5137f27935b4
Tests: log timeouts in IMAP/POP3/SMTP.
Maxim Dounin <mdounin@mdounin.ru>
parents:
162
diff
changeset
|
61 local $SIG{ALRM} = sub { die "timeout\n" }; |
339
9d0a2fa47ac6
Tests: change http timeout to 5s, imap/pop3/smtp to 3s.
Maxim Dounin <mdounin@mdounin.ru>
parents:
243
diff
changeset
|
62 alarm(3); |
540
481d705b8610
Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents:
539
diff
changeset
|
63 while (<$socket>) { |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
64 log_in($_); |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
65 # XXX |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
66 next if m/^\d\d\d-/; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
67 last; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
68 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
69 alarm(0); |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
70 }; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
71 alarm(0); |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
72 if ($@) { |
209
5137f27935b4
Tests: log timeouts in IMAP/POP3/SMTP.
Maxim Dounin <mdounin@mdounin.ru>
parents:
162
diff
changeset
|
73 log_in("died: $@"); |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
74 return undef; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
75 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
76 return $_; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
77 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
78 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
79 sub check { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
80 my ($self, $regex, $name) = @_; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
81 Test::More->builder->like($self->read(), $regex, $name); |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
82 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
83 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
84 sub ok { |
243
de7338227832
Tests: removed trailing spaces.
Homutov Vladimir <vl@nginx.com>
parents:
209
diff
changeset
|
85 my $self = shift; |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
86 Test::More->builder->like($self->read(), qr/^\+OK/, @_); |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
87 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
88 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
89 ############################################################################### |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
90 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
91 sub pop3_test_daemon { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
92 my $server = IO::Socket::INET->new( |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
93 Proto => 'tcp', |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
94 LocalAddr => '127.0.0.1:8111', |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
95 Listen => 5, |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
96 Reuse => 1 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
97 ) |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
98 or die "Can't create listening socket: $!\n"; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
99 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
100 while (my $client = $server->accept()) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
101 $client->autoflush(1); |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
102 print $client "+OK fake pop3 server ready" . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
103 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
104 while (<$client>) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
105 if (/^quit/i) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
106 print $client '+OK quit ok' . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
107 } elsif (/^user test\@example.com/i) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
108 print $client '+OK user ok' . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
109 } elsif (/^pass secret/i) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
110 print $client '+OK pass ok' . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
111 } else { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
112 print $client "-ERR unknown command" . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
113 } |
539
3fcad5e66735
Tests: whitespace fixes in mail modules.
Sergey Kandaurov <pluknet@nginx.com>
parents:
526
diff
changeset
|
114 } |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
115 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
116 close $client; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
117 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
118 } |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
119 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
120 ############################################################################### |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
121 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
122 1; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
123 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
124 ############################################################################### |