Mercurial > hg > nginx-tests
comparison stream_ssl_conf_command.t @ 1863:dbb7561a9441
Tests: reworked stream SSL tests to use IO::Socket::SSL.
Relevant infrastructure is provided in Test::Nginx::Stream. This also
ensures that SSL handshake and various read operations are guarded
with timeouts.
The stream_ssl_verify_client.t test uses IO::Socket::SSL::_get_ssl_object()
to access the Net::SSLeay object directly, as it seems to be the only
way to obtain CA list with IO::Socket::SSL. While not exactly correct,
this seems to be good enough for tests.
author | Maxim Dounin <mdounin@mdounin.ru> |
---|---|
date | Thu, 18 May 2023 18:07:12 +0300 |
parents | 58951cf933e1 |
children | 3cae7b54841e |
comparison
equal
deleted
inserted
replaced
1862:7681a970f6bd | 1863:dbb7561a9441 |
---|---|
14 | 14 |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | 15 BEGIN { use FindBin; chdir($FindBin::Bin); } |
16 | 16 |
17 use lib 'lib'; | 17 use lib 'lib'; |
18 use Test::Nginx; | 18 use Test::Nginx; |
19 use Test::Nginx::Stream qw/ stream /; | |
19 | 20 |
20 ############################################################################### | 21 ############################################################################### |
21 | 22 |
22 select STDERR; $| = 1; | 23 select STDERR; $| = 1; |
23 select STDOUT; $| = 1; | 24 select STDOUT; $| = 1; |
24 | 25 |
25 eval { | |
26 require Net::SSLeay; | |
27 Net::SSLeay::load_error_strings(); | |
28 Net::SSLeay::SSLeay_add_ssl_algorithms(); | |
29 Net::SSLeay::randomize(); | |
30 }; | |
31 plan(skip_all => 'Net::SSLeay not installed') if $@; | |
32 | |
33 my $t = Test::Nginx->new() | 26 my $t = Test::Nginx->new() |
34 ->has(qw/stream stream_ssl stream_return openssl:1.0.2/) | 27 ->has(qw/stream stream_ssl stream_return openssl:1.0.2/) |
28 ->has(qw/socket_ssl_reused/) | |
35 ->has_daemon('openssl'); | 29 ->has_daemon('openssl'); |
36 | 30 |
37 plan(skip_all => 'no ssl_conf_command') if $t->has_module('BoringSSL'); | 31 plan(skip_all => 'no ssl_conf_command') if $t->has_module('BoringSSL'); |
38 | 32 |
39 $t->write_file_expand('nginx.conf', <<'EOF'); | 33 $t->write_file_expand('nginx.conf', <<'EOF'); |
90 | 84 |
91 $t->run()->plan(3); | 85 $t->run()->plan(3); |
92 | 86 |
93 ############################################################################### | 87 ############################################################################### |
94 | 88 |
95 my $ctx = Net::SSLeay::CTX_new() or die("Failed to create SSL_CTX $!"); | 89 my $s; |
96 | 90 |
97 my ($s, $ssl) = get_ssl_socket(); | 91 $s = stream( |
98 like(Net::SSLeay::dump_peer_certificate($ssl), qr/CN=override/, 'Certificate'); | 92 PeerAddr => '127.0.0.1:' . port(8443), |
93 SSL => 1, | |
94 SSL_session_cache_size => 100 | |
95 ); | |
96 $s->read(); | |
99 | 97 |
100 my $ses = Net::SSLeay::get_session($ssl); | 98 like($s->socket()->dump_peer_certificate(), qr/CN=override/, 'Certificate'); |
101 ($s, $ssl) = get_ssl_socket(ses => $ses); | |
102 ok(Net::SSLeay::session_reused($ssl), 'SessionTicket'); | |
103 | 99 |
104 ($s, $ssl) = get_ssl_socket(ciphers => | 100 $s = stream( |
105 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384'); | 101 PeerAddr => '127.0.0.1:' . port(8443), |
106 is(Net::SSLeay::get_cipher($ssl), | 102 SSL => 1, |
103 SSL_reuse_ctx => $s->socket() | |
104 ); | |
105 ok($s->socket()->get_session_reused(), 'SessionTicket'); | |
106 | |
107 $s = stream( | |
108 PeerAddr => '127.0.0.1:' . port(8443), | |
109 SSL => 1, | |
110 SSL_cipher_list => | |
111 'ECDHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-GCM-SHA384' | |
112 ); | |
113 is($s->socket()->get_cipher(), | |
107 'ECDHE-RSA-AES128-GCM-SHA256', 'ServerPreference'); | 114 'ECDHE-RSA-AES128-GCM-SHA256', 'ServerPreference'); |
108 | 115 |
109 ############################################################################### | 116 ############################################################################### |
110 | |
111 sub get_ssl_socket { | |
112 my (%extra) = @_; | |
113 | |
114 my $s = IO::Socket::INET->new('127.0.0.1:' . port(8443)); | |
115 my $ssl = Net::SSLeay::new($ctx) or die("Failed to create SSL $!"); | |
116 Net::SSLeay::set_session($ssl, $extra{ses}) if $extra{ses}; | |
117 Net::SSLeay::set_cipher_list($ssl, $extra{ciphers}) if $extra{ciphers}; | |
118 Net::SSLeay::set_fd($ssl, fileno($s)); | |
119 Net::SSLeay::connect($ssl) or die("ssl connect"); | |
120 return ($s, $ssl); | |
121 } | |
122 | |
123 ############################################################################### |