Mercurial > hg > nginx-tests
comparison lib/Test/Nginx/SMTP.pm @ 1861:7b7b64569f55
Tests: reworked mail SSL tests to use IO::Socket::SSL.
Relevant infrastructure is provided in Test::Nginx::IMAP (and also POP3
and SMTP for completeness). This also ensures that SSL handshake and
various read operations are guarded with timeouts.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 18 May 2023 18:07:08 +0300 |
parents | 3629eda94c1b |
children | 6d3a8f4eb9b2 |
comparison
equal
deleted
inserted
replaced
1860:58951cf933e1 | 1861:7b7b64569f55 |
---|---|
18 | 18 |
19 sub new { | 19 sub new { |
20 my $self = {}; | 20 my $self = {}; |
21 bless $self, shift @_; | 21 bless $self, shift @_; |
22 | 22 |
23 $self->{_socket} = IO::Socket::INET->new( | 23 my $port = {@_}->{'SSL'} ? 8465 : 8025; |
24 Proto => "tcp", | 24 |
25 PeerAddr => "127.0.0.1:" . port(8025), | 25 eval { |
26 @_ | 26 local $SIG{ALRM} = sub { die "timeout\n" }; |
27 ) | 27 local $SIG{PIPE} = sub { die "sigpipe\n" }; |
28 or die "Can't connect to nginx: $!\n"; | 28 alarm(8); |
29 | 29 |
30 if ({@_}->{'SSL'}) { | 30 $self->{_socket} = IO::Socket::INET->new( |
31 require IO::Socket::SSL; | 31 Proto => "tcp", |
32 IO::Socket::SSL->start_SSL($self->{_socket}, @_) | 32 PeerAddr => "127.0.0.1:" . port($port), |
33 or die $IO::Socket::SSL::SSL_ERROR . "\n"; | 33 @_ |
34 ) | |
35 or die "Can't connect to nginx: $!\n"; | |
36 | |
37 if ({@_}->{'SSL'}) { | |
38 require IO::Socket::SSL; | |
39 IO::Socket::SSL->start_SSL( | |
40 $self->{_socket}, | |
41 SSL_verify_mode => | |
42 IO::Socket::SSL::SSL_VERIFY_NONE(), | |
43 @_ | |
44 ) | |
45 or die $IO::Socket::SSL::SSL_ERROR . "\n"; | |
46 | |
47 my $s = $self->{_socket}; | |
48 log_in("ssl cipher: " . $s->get_cipher()); | |
49 log_in("ssl cert: " . $s->peer_certificate('issuer')); | |
50 } | |
51 | |
52 alarm(0); | |
53 }; | |
54 alarm(0); | |
55 if ($@) { | |
56 log_in("died: $@"); | |
34 } | 57 } |
35 | 58 |
36 $self->{_socket}->autoflush(1); | 59 $self->{_socket}->autoflush(1); |
37 $self->{_read_buffer} = ''; | 60 $self->{_read_buffer} = ''; |
38 | 61 |
39 return $self; | 62 return $self; |
63 } | |
64 | |
65 sub DESTROY { | |
66 my $self = shift; | |
67 $self->{_socket}->close(); | |
40 } | 68 } |
41 | 69 |
42 sub eof { | 70 sub eof { |
43 my $self = shift; | 71 my $self = shift; |
44 return $self->{_socket}->eof(); | 72 return $self->{_socket}->eof(); |
111 } | 139 } |
112 | 140 |
113 sub can_read { | 141 sub can_read { |
114 my ($self, $timo) = @_; | 142 my ($self, $timo) = @_; |
115 IO::Select->new($self->{_socket})->can_read($timo || 3); | 143 IO::Select->new($self->{_socket})->can_read($timo || 3); |
144 } | |
145 | |
146 sub socket { | |
147 my ($self) = @_; | |
148 $self->{_socket}; | |
116 } | 149 } |
117 | 150 |
118 ############################################################################### | 151 ############################################################################### |
119 | 152 |
120 sub smtp_test_daemon { | 153 sub smtp_test_daemon { |