annotate lib/Test/Nginx/IMAP.pm @ 1423:29d0961bc3f7

Tests: aligned generic read timeouts in stream and mail tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Tue, 25 Dec 2018 13:56:49 +0300
parents 575d39cc0e35
children d0025a0dead7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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//;
1244
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
13 use IO::Select;
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
14 use IO::Socket;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
15 use Socket qw/ CRLF /;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
16
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
17 use Test::Nginx;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
18
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
19 sub new {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
20 my $self = {};
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
21 bless $self, shift @_;
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
22
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
23 $self->{_socket} = IO::Socket::INET->new(
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
24 Proto => "tcp",
969
1edb092149e2 Tests: simplified parallel tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 951
diff changeset
25 PeerAddr => "127.0.0.1:" . port(8143),
59
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 )
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
28 or die "Can't connect to nginx: $!\n";
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
29
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
30 if ({@_}->{'SSL'}) {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
31 require IO::Socket::SSL;
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
32 IO::Socket::SSL->start_SSL($self->{_socket}, @_)
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
33 or die $IO::Socket::SSL::SSL_ERROR . "\n";
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
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
36 $self->{_socket}->autoflush(1);
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
37
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
38 return $self;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
39 }
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
40
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
41 sub eof {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
42 my $self = shift;
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
43 return $self->{_socket}->eof();
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
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
46 sub print {
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
47 my ($self, $cmd) = @_;
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
48 log_out($cmd);
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
49 $self->{_socket}->print($cmd);
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
50 }
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
51
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
52 sub send {
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
53 my ($self, $cmd) = @_;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
54 log_out($cmd);
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
55 $self->{_socket}->print($cmd . CRLF);
59
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
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
58 sub read {
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
59 my ($self) = @_;
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
60 my $socket = $self->{_socket};
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
61 eval {
209
5137f27935b4 Tests: log timeouts in IMAP/POP3/SMTP.
Maxim Dounin <mdounin@mdounin.ru>
parents: 162
diff changeset
62 local $SIG{ALRM} = sub { die "timeout\n" };
1423
29d0961bc3f7 Tests: aligned generic read timeouts in stream and mail tests.
Sergey Kandaurov <pluknet@nginx.com>
parents: 1244
diff changeset
63 alarm(8);
540
481d705b8610 Tests: SSL support in mail backends.
Sergey Kandaurov <pluknet@nginx.com>
parents: 539
diff changeset
64 while (<$socket>) {
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
65 log_in($_);
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
66 # XXX
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
67 next if m/^\d\d\d-/;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
68 last;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
69 }
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
70 alarm(0);
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
71 };
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
72 alarm(0);
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
73 if ($@) {
209
5137f27935b4 Tests: log timeouts in IMAP/POP3/SMTP.
Maxim Dounin <mdounin@mdounin.ru>
parents: 162
diff changeset
74 log_in("died: $@");
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
75 return undef;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
76 }
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
77 return $_;
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
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
80 sub check {
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
81 my ($self, $regex, $name) = @_;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
82 Test::More->builder->like($self->read(), $regex, $name);
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
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
85 sub ok {
243
de7338227832 Tests: removed trailing spaces.
Homutov Vladimir <vl@nginx.com>
parents: 209
diff changeset
86 my $self = shift;
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
87 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
88 }
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
89
1244
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
90 sub can_read {
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
91 my ($self, $timo) = @_;
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
92 IO::Select->new($self->{_socket})->can_read($timo || 3);
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
93 }
575d39cc0e35 Tests: worker_shutdown_timeout within the mail module.
Sergey Kandaurov <pluknet@nginx.com>
parents: 969
diff changeset
94
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
95 ###############################################################################
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
96
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
97 sub imap_test_daemon {
951
9361c7eddfc1 Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 540
diff changeset
98 my ($port) = @_;
9361c7eddfc1 Tests: parallel tests support.
Andrey Zelenkov <zelenkov@nginx.com>
parents: 540
diff changeset
99
59
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
100 my $server = IO::Socket::INET->new(
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
101 Proto => 'tcp',
969
1edb092149e2 Tests: simplified parallel tests.
Maxim Dounin <mdounin@mdounin.ru>
parents: 951
diff changeset
102 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
103 Listen => 5,
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
104 Reuse => 1
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 or die "Can't create listening socket: $!\n";
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
107
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
108 while (my $client = $server->accept()) {
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
109 $client->autoflush(1);
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
110 print $client "* OK fake imap server ready" . CRLF;
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 while (<$client>) {
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
113 my $tag = '';
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
114
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
115 $tag = $1 if m/^(\S+)/;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
116 s/^(\S+)\s+//;
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 if (/^logout/i) {
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
119 print $client $tag . ' OK logout ok' . CRLF;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
120 } elsif (/^login /i) {
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
121 print $client $tag . ' OK login ok' . CRLF;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
122 } else {
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
123 print $client $tag . ' ERR unknown command' . CRLF;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
124 }
539
3fcad5e66735 Tests: whitespace fixes in mail modules.
Sergey Kandaurov <pluknet@nginx.com>
parents: 526
diff changeset
125 }
59
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 close $client;
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 }
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
130
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
131 ###############################################################################
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
132
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
133 1;
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
134
bc3351f157ef Tests: add basic pop3 and imap tests.
Maxim Dounin <mdounin@mdounin.ru>
parents:
diff changeset
135 ###############################################################################