blob: cc6b6c70a5860f79a3add637195601ce1f18262b [file] [log] [blame]
Sergey Kandaurovb7561342015-04-23 14:01:21 +03001#!/usr/bin/perl
2
3# (C) Sergey Kandaurov
4# (C) Nginx, Inc.
5
6# Stream tests for proxy to ssl backend.
7
8###############################################################################
9
10use warnings;
11use strict;
12
13use Test::More;
14
15BEGIN { use FindBin; chdir($FindBin::Bin); }
16
17use lib 'lib';
18use Test::Nginx;
Sergey Kandauroveec68712017-07-19 16:21:42 +030019use Test::Nginx::Stream qw/ stream /;
Sergey Kandaurovb7561342015-04-23 14:01:21 +030020
21###############################################################################
22
23select STDERR; $| = 1;
24select STDOUT; $| = 1;
25
26my $t = Test::Nginx->new()->has(qw/stream stream_ssl http http_ssl/)
Sergey Kandauroveec68712017-07-19 16:21:42 +030027 ->has(qw/stream_return/)
Sergey Kandaurov0ebf2bd2017-07-20 14:47:59 +030028 ->has_daemon('openssl')->plan(6);
Sergey Kandaurovb7561342015-04-23 14:01:21 +030029
30$t->write_file_expand('nginx.conf', <<'EOF');
31
32%%TEST_GLOBALS%%
33
34daemon off;
35
36events {
37}
38
39stream {
40 proxy_ssl on;
41 proxy_ssl_session_reuse on;
Sergey Kandaurov96fbc062015-12-07 13:38:52 +030042 proxy_connect_timeout 2s;
Sergey Kandaurovb7561342015-04-23 14:01:21 +030043
44 server {
Sergey Kandauroveec68712017-07-19 16:21:42 +030045 listen 127.0.0.1:8081;
46 proxy_pass 127.0.0.1:8083;
Sergey Kandaurovb7561342015-04-23 14:01:21 +030047 proxy_ssl_session_reuse off;
48 }
49
50 server {
Sergey Kandauroveec68712017-07-19 16:21:42 +030051 listen 127.0.0.1:8082;
52 proxy_pass 127.0.0.1:8083;
53 }
54
55 server {
56 listen 127.0.0.1:8083 ssl;
57 return $ssl_session_reused;
58
59 ssl_certificate_key localhost.key;
60 ssl_certificate localhost.crt;
61 ssl_session_cache builtin;
62 }
63
64 server {
65 listen 127.0.0.1:8080;
66 proxy_pass 127.0.0.1:8084;
Sergey Kandaurovb7561342015-04-23 14:01:21 +030067 }
68}
69
70http {
71 %%TEST_GLOBALS_HTTP%%
72
73 server {
Sergey Kandauroveec68712017-07-19 16:21:42 +030074 listen 127.0.0.1:8084 ssl;
Sergey Kandaurovb7561342015-04-23 14:01:21 +030075 server_name localhost;
76
77 ssl_certificate_key localhost.key;
78 ssl_certificate localhost.crt;
Sergey Kandaurovb7561342015-04-23 14:01:21 +030079 }
80}
81
82EOF
83
84$t->write_file('openssl.conf', <<EOF);
85[ req ]
Sergey Kandaurov571b1a52019-07-09 13:37:55 +030086default_bits = 2048
Sergey Kandaurovb7561342015-04-23 14:01:21 +030087encrypt_key = no
88distinguished_name = req_distinguished_name
89[ req_distinguished_name ]
90EOF
91
92$t->write_file('index.html', '');
93
94my $d = $t->testdir();
95
96foreach my $name ('localhost') {
97 system('openssl req -x509 -new '
Sergey Kandaurov2225e6e2017-09-20 14:46:51 +030098 . "-config $d/openssl.conf -subj /CN=$name/ "
99 . "-out $d/$name.crt -keyout $d/$name.key "
Sergey Kandaurovb7561342015-04-23 14:01:21 +0300100 . ">>$d/openssl.out 2>&1") == 0
101 or die "Can't create certificate for $name: $!\n";
102}
103
104$t->run();
105
106###############################################################################
107
Sergey Kandauroveec68712017-07-19 16:21:42 +0300108is(stream('127.0.0.1:' . port(8081))->read(), '.', 'ssl');
109is(stream('127.0.0.1:' . port(8081))->read(), '.', 'ssl 2');
Sergey Kandaurovb7561342015-04-23 14:01:21 +0300110
Sergey Kandauroveec68712017-07-19 16:21:42 +0300111is(stream('127.0.0.1:' . port(8082))->read(), '.', 'ssl session new');
112is(stream('127.0.0.1:' . port(8082))->read(), 'r', 'ssl session reused');
Sergey Kandaurov0ebf2bd2017-07-20 14:47:59 +0300113is(stream('127.0.0.1:' . port(8082))->read(), 'r', 'ssl session reused 2');
Sergey Kandaurovb7561342015-04-23 14:01:21 +0300114
Sergey Kandaurov275abbd2015-10-05 13:19:45 +0300115my $s = http('', start => 1);
116
Sergey Kandaurov96fbc062015-12-07 13:38:52 +0300117sleep 3;
Sergey Kandaurov275abbd2015-10-05 13:19:45 +0300118
Sergey Kandaurov4d1635d2015-10-07 18:56:09 +0300119like(http_get('/', socket => $s), qr/200 OK/, 'proxy connect timeout');
Sergey Kandaurov275abbd2015-10-05 13:19:45 +0300120
Sergey Kandaurovb7561342015-04-23 14:01:21 +0300121###############################################################################