-
Notifications
You must be signed in to change notification settings - Fork 98
Expand file tree
/
Copy pathssl_proxy_upgrade.t
More file actions
363 lines (264 loc) · 7.63 KB
/
ssl_proxy_upgrade.t
File metadata and controls
363 lines (264 loc) · 7.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#!/usr/bin/perl
# (C) Maxim Dounin
# (C) Sergey Kandaurov
# (C) Nginx, Inc.
# Tests for http proxy upgrade support with http ssl module.
# In contrast to proxy_websocket.t, this test doesn't try to use binary
# WebSocket protocol, but uses simple plain text protocol instead.
###############################################################################
use warnings;
use strict;
use Test::More;
use IO::Poll;
use IO::Select;
use Socket qw/ CRLF /;
BEGIN { use FindBin; chdir($FindBin::Bin); }
use lib 'lib';
use Test::Nginx;
###############################################################################
select STDERR; $| = 1;
select STDOUT; $| = 1;
my $t = Test::Nginx->new()->has(qw/http proxy http_ssl socket_ssl/)
->has_daemon('openssl')
->write_file_expand('nginx.conf', <<'EOF')->plan(30);
%%TEST_GLOBALS%%
daemon off;
events {
}
http {
%%TEST_GLOBALS_HTTP%%
log_format test "$bytes_sent $body_bytes_sent";
access_log %%TESTDIR%%/cc.log test;
server {
listen 127.0.0.1:8080 ssl;
server_name localhost;
ssl_certificate_key localhost.key;
ssl_certificate localhost.crt;
location / {
proxy_pass http://127.0.0.1:8081;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "Upgrade";
proxy_read_timeout 2s;
send_timeout 2s;
}
}
}
EOF
$t->write_file('openssl.conf', <<EOF);
[ req ]
default_bits = 2048
encrypt_key = no
distinguished_name = req_distinguished_name
[ req_distinguished_name ]
EOF
my $d = $t->testdir();
foreach my $name ('localhost') {
system('openssl req -x509 -new '
. "-config $d/openssl.conf -subj /CN=$name/ "
. "-out $d/$name.crt -keyout $d/$name.key "
. ">>$d/openssl.out 2>&1") == 0
or die "Can't create certificate for $name: $!\n";
}
$t->run_daemon(\&upgrade_fake_daemon);
$t->run();
$t->waitforsocket('127.0.0.1:' . port(8081))
or die "Can't start test backend";
###############################################################################
# establish connection
my @r;
my $s = upgrade_connect();
ok($s, "handshake");
SKIP: {
skip "handshake failed", 22 unless $s;
# send a frame
upgrade_write($s, 'foo');
is(upgrade_read($s), 'bar', "upgrade response");
# send some big frame
upgrade_write($s, 'foo' x 16384);
like(upgrade_read($s), qr/^(bar){16384}$/, "upgrade big response");
# send multiple frames
for my $i (1 .. 10) {
upgrade_write($s, ('foo' x 16384) . $i, continue => 1);
upgrade_write($s, 'bazz' . $i, continue => $i != 10);
}
for my $i (1 .. 10) {
like(upgrade_read($s), qr/^(bar){16384}\d+$/, "upgrade $i");
is(upgrade_read($s), 'bazz' . $i, "upgrade small $i");
}
}
push @r, $s ? ${*$s}->{_upgrade_private}->{r} : 'failed';
undef $s;
# establish connection with some pipelined data
# and make sure they are correctly passed upstream
$s = upgrade_connect(message => "foo");
ok($s, "handshake pipelined");
SKIP: {
skip "handshake failed", 2 unless $s;
is(upgrade_read($s), "bar", "response pipelined");
upgrade_write($s, "foo");
is(upgrade_read($s), "bar", "next to pipelined");
}
push @r, $s ? ${*$s}->{_upgrade_private}->{r} : 'failed';
undef $s;
# connection should not be upgraded unless upgrade was actually
# requested and allowed by configuration
$s = upgrade_connect(noheader => 1);
ok(!$s, "handshake noupgrade");
# bytes sent on upgraded connection, fixed in c2c9a1c03 (1.7.11)
# verify with 1) data actually read by client, 2) expected data from backend
$t->stop();
open my $f, '<', "$d/cc.log" or die "Can't open cc.log: $!";
is($f->getline(), shift (@r) . " 540793\n", 'log - bytes');
is($f->getline(), shift (@r) . " 22\n", 'log - bytes pipelined');
like($f->getline(), qr/\d+ 0\n/, 'log - bytes noupgrade');
###############################################################################
sub upgrade_connect {
my (%opts) = @_;
my $s = IO::Socket::SSL->new(
Proto => 'tcp',
PeerAddr => '127.0.0.1:' . port(8080),
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
)
or die "Can't connect to nginx: $!\n";
# send request, $h->to_string
my $buf = "GET / HTTP/1.1" . CRLF
. "Host: localhost" . CRLF
. ($opts{noheader} ? '' : "Upgrade: foo" . CRLF)
. "Connection: Upgrade" . CRLF . CRLF;
$buf .= $opts{message} . CRLF . 'FIN' if defined $opts{message};
local $SIG{PIPE} = 'IGNORE';
log_out($buf);
$s->syswrite($buf);
# read response
my $got = '';
$buf = '';
while (1) {
$buf = upgrade_getline($s);
last unless defined $buf and length $buf;
log_in($buf);
$got .= $buf;
last if $got =~ /\x0d?\x0a\x0d?\x0a$/;
}
# parse server response
return if $got !~ m!HTTP/1.1 101!;
# make sure next line is "handshaked"
$buf = upgrade_read($s);
return if !defined $buf or $buf ne 'handshaked';
return $s;
}
sub upgrade_getline {
my ($s) = @_;
my ($h, $buf);
${*$s}->{_upgrade_private} ||= { b => '', r => 0 };
$h = ${*$s}->{_upgrade_private};
if ($h->{b} =~ /^(.*?\x0a)(.*)/ms) {
$h->{b} = $2;
return $1;
}
$s->blocking(0);
while (IO::Select->new($s)->can_read(3)) {
my $n = $s->sysread($buf, 16384);
if (!defined $n) {
next if $s->errstr() == IO::Socket::SSL->SSL_WANT_READ;
last;
} elsif (!$n) {
last;
}
$h->{b} .= $buf;
$h->{r} += $n;
if ($h->{b} =~ /^(.*?\x0a)(.*)/ms) {
$h->{b} = $2;
return $1;
}
};
}
sub upgrade_write {
my ($s, $message, %extra) = @_;
$message = $message . CRLF;
$message = $message . 'FIN' unless $extra{continue};
local $SIG{PIPE} = 'IGNORE';
$s->blocking(0);
while (IO::Select->new($s)->can_write(1.5)) {
my $n = $s->syswrite($message);
unless ($n) {
next if $s->errstr() == IO::Socket::SSL->SSL_WANT_WRITE;
last;
}
$message = substr($message, $n);
last unless length $message;
}
if (length $message) {
$s->close();
}
}
sub upgrade_read {
my ($s) = @_;
my $m = upgrade_getline($s);
$m =~ s/\x0d?\x0a// if defined $m;
log_in($m);
return $m;
}
###############################################################################
sub upgrade_fake_daemon {
my $server = IO::Socket::INET->new(
Proto => 'tcp',
LocalAddr => '127.0.0.1:' . port(8081),
Listen => 5,
Reuse => 1
)
or die "Can't create listening socket: $!\n";
while (my $client = $server->accept()) {
upgrade_handle_client($client);
}
}
sub upgrade_handle_client {
my ($client) = @_;
$client->autoflush(1);
$client->blocking(0);
my $poll = IO::Poll->new;
my $handshake = 1;
my $unfinished = '';
my $buffer = '';
my $n;
log2c("(new connection $client)");
while (1) {
$poll->mask($client => ($buffer ? POLLIN|POLLOUT : POLLIN));
my $p = $poll->poll(0.5);
log2c("(poll $p)");
foreach ($poll->handles(POLLIN)) {
$n = $client->sysread(my $chunk, 65536);
return unless $n;
log2i($chunk);
if ($handshake) {
$buffer .= $chunk;
next unless $buffer =~ /\x0d?\x0a\x0d?\x0a$/;
log2c("(handshake done)");
$handshake = 0;
$buffer = 'HTTP/1.1 101 Switching' . CRLF
. 'Upgrade: foo' . CRLF
. 'Connection: Upgrade' . CRLF . CRLF
. 'handshaked' . CRLF;
log2o($buffer);
next;
}
$unfinished .= $chunk;
if ($unfinished =~ m/\x0d?\x0aFIN\z/) {
$unfinished =~ s/FIN\z//;
$unfinished =~ s/foo/bar/g;
log2o($unfinished);
$buffer .= $unfinished;
$unfinished = '';
}
}
foreach my $writer ($poll->handles(POLLOUT)) {
next unless length $buffer;
$n = $writer->syswrite($buffer);
substr $buffer, 0, $n, '';
}
}
}
sub log2i { Test::Nginx::log_core('|| <<', @_); }
sub log2o { Test::Nginx::log_core('|| >>', @_); }
sub log2c { Test::Nginx::log_core('||', @_); }
###############################################################################