@@ -23,6 +23,11 @@ use Test::Nginx::Stream qw/ stream /;
23
23
select STDERR ; $| = 1;
24
24
select STDOUT ; $| = 1;
25
25
26
+ eval { require IO::Socket::SSL; };
27
+ plan(skip_all => ' IO::Socket::SSL not installed' ) if $@ ;
28
+ eval { IO::Socket::SSL::SSL_VERIFY_NONE(); };
29
+ plan(skip_all => ' IO::Socket::SSL too old' ) if $@ ;
30
+
26
31
my $t = Test::Nginx-> new()-> has(qw/ http http_ssl rewrite stream stream_return/ )
27
32
-> write_file_expand(' nginx.conf' , <<'EOF' );
28
33
@@ -57,6 +62,18 @@ http {
57
62
location /loc {
58
63
return 200 "You are at default.example.com.";
59
64
}
65
+
66
+ location /success {
67
+ return 200;
68
+ }
69
+
70
+ location /fail {
71
+ return 403;
72
+ }
73
+
74
+ location /backend {
75
+ return 200 "BACKEND OK";
76
+ }
60
77
}
61
78
62
79
server {
@@ -76,19 +93,20 @@ stream {
76
93
%%TEST_GLOBALS_STREAM%%
77
94
78
95
js_import test.js;
79
- js_preread test.preread;
80
96
js_var $message;
81
97
82
98
resolver 127.0.0.1:%%PORT_8981_UDP%%;
83
99
resolver_timeout 1s;
84
100
85
101
server {
86
102
listen 127.0.0.1:8082;
103
+ js_preread test.preread;
87
104
return "default CA $message";
88
105
}
89
106
90
107
server {
91
108
listen 127.0.0.1:8083;
109
+ js_preread test.preread;
92
110
return "my CA $message";
93
111
94
112
js_fetch_ciphers HIGH:!aNull:!MD5;
@@ -98,11 +116,38 @@ stream {
98
116
99
117
server {
100
118
listen 127.0.0.1:8084;
119
+ js_preread test.preread;
101
120
return "my CA with verify_depth=0 $message";
102
121
103
122
js_fetch_verify_depth 0;
104
123
js_fetch_trusted_certificate myca.crt;
105
124
}
125
+
126
+ server {
127
+ listen 127.0.0.1:8085;
128
+
129
+ js_access test.access_ok;
130
+ ssl_preread on;
131
+
132
+ js_fetch_ciphers HIGH:!aNull:!MD5;
133
+ js_fetch_protocols TLSv1.1 TLSv1.2;
134
+ js_fetch_trusted_certificate myca.crt;
135
+
136
+ proxy_pass 127.0.0.1:8081;
137
+ }
138
+
139
+ server {
140
+ listen 127.0.0.1:8086;
141
+
142
+ js_access test.access_nok;
143
+ ssl_preread on;
144
+
145
+ js_fetch_ciphers HIGH:!aNull:!MD5;
146
+ js_fetch_protocols TLSv1.1 TLSv1.2;
147
+ js_fetch_trusted_certificate myca.crt;
148
+
149
+ proxy_pass 127.0.0.1:8081;
150
+ }
106
151
}
107
152
108
153
EOF
@@ -137,7 +182,21 @@ $t->write_file('test.js', <<EOF);
137
182
});
138
183
}
139
184
140
- export default {njs: test_njs, preread};
185
+ async function access_ok(s) {
186
+ let r = await ngx.fetch('https://default.example.com:$p1 /success',
187
+ {body: s.remoteAddress});
188
+
189
+ (r.status == 200) ? s.allow(): s.deny();
190
+ }
191
+
192
+ async function access_nok(s) {
193
+ let r = await ngx.fetch('https://default.example.com:$p1 /fail',
194
+ {body: s.remoteAddress});
195
+
196
+ (r.status == 200) ? s.allow(): s.deny();
197
+ }
198
+
199
+ export default {njs: test_njs, preread, access_ok, access_nok};
141
200
EOF
142
201
143
202
my $d = $t -> testdir();
@@ -204,7 +263,7 @@ foreach my $name ('default.example.com', '1.example.com') {
204
263
. $t -> read_file(' intermediate.crt' ));
205
264
}
206
265
207
- $t -> try_run(' no njs.fetch' )-> plan(4 );
266
+ $t -> try_run(' no njs.fetch' )-> plan(6 );
208
267
209
268
$t -> run_daemon(\&dns_daemon, port(8981), $t );
210
269
$t -> waitforfile($t -> testdir . ' /' . port(8981));
@@ -223,6 +282,56 @@ like(stream("127.0.0.1:$p3")->io('GOlocalhost'),
223
282
like(stream(" 127.0.0.1:$p4 " )-> io(' GOdefaul.example.com' ),
224
283
qr / connect failed/ s , ' stream verify_depth too small' );
225
284
285
+ like(https_get(' default.example.com' , port(8085), ' /backend' ),
286
+ qr ! BACKEND OK! , ' access https fetch' );
287
+ is(https_get(' default.example.com' , port(8086), ' /backend' ), ' <conn failed>' ,
288
+ ' access https fetch not' );
289
+
290
+ # ##############################################################################
291
+
292
+ sub get_ssl_socket {
293
+ my ($host , $port ) = @_ ;
294
+ my $s ;
295
+
296
+ eval {
297
+ local $SIG {ALRM } = sub { die " timeout\n " };
298
+ local $SIG {PIPE } = sub { die " sigpipe\n " };
299
+ alarm(8);
300
+ $s = IO::Socket::SSL-> new(
301
+ Proto => ' tcp' ,
302
+ PeerAddr => ' 127.0.0.1:' . $port ,
303
+ SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
304
+ SSL_error_trap => sub { die $_ [1] }
305
+ );
306
+
307
+ alarm(0);
308
+ };
309
+
310
+ alarm(0);
311
+
312
+ if ($@ ) {
313
+ log_in(" died: $@ " );
314
+ return undef ;
315
+ }
316
+
317
+ return $s ;
318
+ }
319
+
320
+ sub https_get {
321
+ my ($host , $port , $url ) = @_ ;
322
+ my $s = get_ssl_socket($host , $port );
323
+
324
+ if (!$s ) {
325
+ return ' <conn failed>' ;
326
+ }
327
+
328
+ return http(<<EOF , socket => $s );
329
+ GET $url HTTP/1.0
330
+ Host: $host
331
+
332
+ EOF
333
+ }
334
+
226
335
# ##############################################################################
227
336
228
337
sub reply_handler {
0 commit comments