Mercurial > hg > nginx-tests
annotate lib/Test/Nginx/IMAP.pm @ 969:1edb092149e2
Tests: simplified parallel tests.
Now configurations are automatically scanned for "127.0.0.1:8xxx", and
ports are auto-replaced by free ones. The exact port specified is used
if it's free.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Fri, 08 Jul 2016 02:21:14 +0300 |
parents | 9361c7eddfc1 |
children | 575d39cc0e35 |
rev | line source |
---|---|
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
1 package Test::Nginx::IMAP; |
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 imap 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", |
969
1edb092149e2
Tests: simplified parallel tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
951
diff
changeset
|
24 PeerAddr => "127.0.0.1:" . port(8143), |
59
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/^\S+ 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 imap_test_daemon { |
951
9361c7eddfc1
Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
540
diff
changeset
|
92 my ($port) = @_; |
9361c7eddfc1
Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
540
diff
changeset
|
93 |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
94 my $server = IO::Socket::INET->new( |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
95 Proto => 'tcp', |
969
1edb092149e2
Tests: simplified parallel tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
951
diff
changeset
|
96 LocalAddr => '127.0.0.1:' . ($port || port(8144)), |
59
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
97 Listen => 5, |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
98 Reuse => 1 |
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 or die "Can't create listening socket: $!\n"; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
101 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
102 while (my $client = $server->accept()) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
103 $client->autoflush(1); |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
104 print $client "* OK fake imap server ready" . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
105 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
106 while (<$client>) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
107 my $tag = ''; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
108 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
109 $tag = $1 if m/^(\S+)/; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
110 s/^(\S+)\s+//; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
111 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
112 if (/^logout/i) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
113 print $client $tag . ' OK logout ok' . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
114 } elsif (/^login /i) { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
115 print $client $tag . ' OK login ok' . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
116 } else { |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
117 print $client $tag . ' ERR unknown command' . CRLF; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
118 } |
539
3fcad5e66735
Tests: whitespace fixes in mail modules.
Sergey Kandaurov <pluknet@nginx.com>
parents:
526
diff
changeset
|
119 } |
59
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 close $client; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
122 } |
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 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
125 ############################################################################### |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
126 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
127 1; |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
128 |
bc3351f157ef
Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff
changeset
|
129 ############################################################################### |