Mercurial > hg > nginx-tests
annotate stream_udp_upstream_least_conn.t @ 1571:1b4ceab9cb1c
Tests: fixed ssl_certificate.t with LibreSSL client.
Net::SSLeay::connect() that manages TLS handshake could return unexpected
error when receiving server alert, as seen in server certificate tests if
it could not been selected. Typically, it returns the expected error -1,
but with certain libssl implementations it can be 0, as explained below.
The error is propagated from libssl's SSL_connect(), which is usually -1.
In modern OpenSSL versions, it is the default error code used in the state
machine returned when something went wrong with parsing TLS message header.
In versions up to OpenSSL 1.0.2, with SSLv23_method() used by default, -1
is the only error code in the ssl_connect() method implementation which is
used as well if receiving alert while parsing ServerHello. BoringSSL also
seems to return -1. But it is not so with LibreSSL that returns zero.
Previously, tests failed with client built with LibreSSL with SSLv3 removed.
Here, the error is propagated directly from ssl_read_bytes() method, which
is always implemented as ssl3_read_bytes() in all TLS methods. It could be
also seen with OpenSSL up to 1.0.2 with non-default methods explicitly set.
author | Sergey Kandaurov <pluknet@nginx.com> |
---|---|
date | Fri, 29 May 2020 23:10:20 +0300 |
parents | e4974af3fb12 |
children | f3ba4c74de31 |
rev | line source |
---|---|
868 | 1 #!/usr/bin/perl |
2 | |
3 # (C) Sergey Kandaurov | |
4 # (C) Nginx, Inc. | |
5 | |
6 # Stream tests for upstream least_conn balancer module with datagrams. | |
7 | |
8 ############################################################################### | |
9 | |
10 use warnings; | |
11 use strict; | |
12 | |
13 use Test::More; | |
14 | |
15 BEGIN { use FindBin; chdir($FindBin::Bin); } | |
16 | |
17 use lib 'lib'; | |
18 use Test::Nginx; | |
19 use Test::Nginx::Stream qw/ dgram /; | |
20 | |
21 ############################################################################### | |
22 | |
23 select STDERR; $| = 1; | |
24 select STDOUT; $| = 1; | |
25 | |
947
b9e42c554ba7
Tests: skip UDP tests on win32, udp feature introduced.
Sergey Kandaurov <pluknet@nginx.com>
parents:
868
diff
changeset
|
26 my $t = Test::Nginx->new()->has(qw/stream stream_upstream_least_conn udp/) |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
27 ->plan(2)->write_file_expand('nginx.conf', <<'EOF'); |
868 | 28 |
29 %%TEST_GLOBALS%% | |
30 | |
31 daemon off; | |
32 | |
33 events { | |
34 } | |
35 | |
36 stream { | |
37 proxy_responses 1; | |
38 proxy_timeout 1s; | |
39 | |
40 upstream u { | |
41 least_conn; | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
42 server 127.0.0.1:%%PORT_8981_UDP%%; |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
43 server 127.0.0.1:%%PORT_8982_UDP%%; |
868 | 44 } |
45 | |
46 server { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
47 listen 127.0.0.1:%%PORT_8980_UDP%% udp; |
868 | 48 proxy_pass u; |
49 } | |
50 } | |
51 | |
52 EOF | |
53 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
54 $t->run_daemon(\&udp_daemon, port(8981), $t); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
55 $t->run_daemon(\&udp_daemon, port(8982), $t); |
1020
196d33c2bb45
Tests: removed TODO and try_run() checks for legacy versions.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
974
diff
changeset
|
56 $t->run(); |
868 | 57 |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
58 $t->waitforfile($t->testdir . '/' . port(8981)); |
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
59 $t->waitforfile($t->testdir . '/' . port(8982)); |
868 | 60 |
61 ############################################################################### | |
62 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
63 my @ports = my ($port1, $port2) = (port(8981), port(8982)); |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
64 |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
65 is(many(10), "$port1: 5, $port2: 5", 'balanced'); |
868 | 66 |
67 my @sockets; | |
68 for (1 .. 2) { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
69 my $s = dgram('127.0.0.1:' . port(8980)); |
868 | 70 $s->write('w'); |
71 push @sockets, $s; | |
72 } | |
73 | |
74 select undef, undef, undef, 0.2; | |
75 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
76 is(many(10), "$port2: 10", 'least_conn'); |
868 | 77 |
78 ############################################################################### | |
79 | |
80 sub many { | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
81 my ($count) = @_; |
868 | 82 my (%ports); |
83 | |
84 for (1 .. $count) { | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
85 if (dgram('127.0.0.1:' . port(8980))->io('.') =~ /(\d+)/) { |
868 | 86 $ports{$1} = 0 unless defined $ports{$1}; |
87 $ports{$1}++; | |
88 } | |
89 } | |
90 | |
952
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
91 my @keys = map { my $p = $_; grep { $p == $_ } keys %ports } @ports; |
e9064d691790
Tests: converted tests to run in parallel.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
947
diff
changeset
|
92 return join ', ', map { $_ . ": " . $ports{$_} } @keys; |
868 | 93 } |
94 | |
95 ############################################################################### | |
96 | |
97 sub udp_daemon { | |
98 my ($port, $t) = @_; | |
99 | |
100 my $server = IO::Socket::INET->new( | |
101 Proto => 'udp', | |
102 LocalAddr => '127.0.0.1:' . $port, | |
103 Reuse => 1, | |
104 ) | |
105 or die "Can't create listening socket: $!\n"; | |
106 | |
107 # signal we are ready | |
108 | |
109 open my $fh, '>', $t->testdir() . '/' . $port; | |
110 close $fh; | |
111 | |
112 while (1) { | |
113 $server->recv(my $buffer, 65536); | |
114 | |
115 my $port = $server->sockport(); | |
116 | |
1237
e4974af3fb12
Tests: adjusted udp ports to match allocated ports range.
Andrey Zelenkov <zelenkov@nginx.com>
parents:
1235
diff
changeset
|
117 if ($buffer =~ /w/ && $port == port(8981)) { |
868 | 118 select undef, undef, undef, 2.5; |
119 } | |
120 | |
121 $buffer = $port; | |
122 | |
123 $server->send($buffer); | |
124 } | |
125 } | |
126 | |
127 ############################################################################### |