Skip to content

Commit dcfe53a

Browse files
committed
Tests: simplified http SSL tests with IO::Socket::SSL.
The http SSL tests which previously used IO::Socket::SSL were converted to use improved IO::Socket::SSL infrastructure in Test::Nginx.
1 parent a28ff69 commit dcfe53a

12 files changed

+139
-443
lines changed

ssl.t

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use strict;
1414
use Test::More;
1515

1616
use Socket qw/ CRLF /;
17+
use IO::Select;
1718

1819
BEGIN { use FindBin; chdir($FindBin::Bin); }
1920

@@ -278,11 +279,9 @@ sub test_tls13 {
278279
}
279280

280281
sub get {
281-
my ($uri, $port, $ctx) = @_;
282-
my $s = get_ssl_socket($port, $ctx) or return;
283-
my $r = http_get($uri, socket => $s);
284-
$s->close();
285-
return $r;
282+
my ($uri, $port, $ctx, %extra) = @_;
283+
my $s = get_ssl_socket($port, $ctx, %extra) or return;
284+
return http_get($uri, socket => $s);
286285
}
287286

288287
sub get_body {
@@ -297,16 +296,16 @@ sub get_body {
297296
http($chs . CRLF . $body x $len . CRLF, socket => $s, start => 1)
298297
for 1 .. $n;
299298
my $r = http("0" . CRLF . CRLF, socket => $s);
300-
$s->close();
301299
return $r;
302300
}
303301

304302
sub cert {
305303
my ($uri, $port) = @_;
306-
my $s = get_ssl_socket($port, undef,
304+
return get(
305+
$uri, $port, undef,
307306
SSL_cert_file => "$d/subject.crt",
308-
SSL_key_file => "$d/subject.key") or return;
309-
http_get($uri, socket => $s);
307+
SSL_key_file => "$d/subject.key"
308+
);
310309
}
311310

312311
sub get_ssl_context {
@@ -318,45 +317,32 @@ sub get_ssl_context {
318317

319318
sub get_ssl_socket {
320319
my ($port, $ctx, %extra) = @_;
321-
my $s;
322-
323-
eval {
324-
local $SIG{ALRM} = sub { die "timeout\n" };
325-
local $SIG{PIPE} = sub { die "sigpipe\n" };
326-
alarm(8);
327-
$s = IO::Socket::SSL->new(
328-
Proto => 'tcp',
329-
PeerAddr => '127.0.0.1',
330-
PeerPort => port($port),
331-
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
332-
SSL_reuse_ctx => $ctx,
333-
SSL_error_trap => sub { die $_[1] },
334-
%extra
335-
);
336-
alarm(0);
337-
};
338-
alarm(0);
339-
340-
if ($@) {
341-
log_in("died: $@");
342-
return undef;
343-
}
344-
345-
return $s;
320+
return http(
321+
'', PeerAddr => '127.0.0.1:' . port($port), start => 1,
322+
SSL => 1,
323+
SSL_reuse_ctx => $ctx,
324+
%extra
325+
);
346326
}
347327

348328
sub get_ssl_shutdown {
349329
my ($port) = @_;
350330

351-
my $s = IO::Socket::INET->new('127.0.0.1:' . port($port));
352-
my $ctx = Net::SSLeay::CTX_new() or die("Failed to create SSL_CTX $!");
353-
my $ssl = Net::SSLeay::new($ctx) or die("Failed to create SSL $!");
354-
Net::SSLeay::set_fd($ssl, fileno($s));
355-
Net::SSLeay::connect($ssl) or die("ssl connect");
356-
Net::SSLeay::write($ssl, 'GET /' . CRLF . 'extra');
357-
Net::SSLeay::read($ssl);
358-
Net::SSLeay::set_shutdown($ssl, 1);
359-
Net::SSLeay::shutdown($ssl);
331+
my $s = http(
332+
'GET /' . CRLF . 'extra',
333+
PeerAddr => '127.0.0.1:' . port($port), start => 1,
334+
SSL => 1
335+
);
336+
337+
$s->blocking(0);
338+
while (IO::Select->new($s)->can_read(8)) {
339+
my $n = $s->sysread(my $buf, 16384);
340+
next if !defined $n && $!{EWOULDBLOCK};
341+
last;
342+
}
343+
$s->blocking(1);
344+
345+
return $s->stop_SSL();
360346
}
361347

362348
###############################################################################

ssl_certificate_chain.t

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -133,41 +133,27 @@ $t->run();
133133

134134
###############################################################################
135135

136-
is(get_ssl_socket(port(8080)), undef, 'incomplete chain');
137-
ok(get_ssl_socket(port(8081)), 'intermediate');
138-
ok(get_ssl_socket(port(8082)), 'intermediate server');
136+
ok(!get_ssl_socket(8080), 'incomplete chain');
137+
ok(get_ssl_socket(8081), 'intermediate');
138+
ok(get_ssl_socket(8082), 'intermediate server');
139139

140140
###############################################################################
141141

142142
sub get_ssl_socket {
143143
my ($port) = @_;
144-
my ($s, $verify);
145-
146-
eval {
147-
local $SIG{ALRM} = sub { die "timeout\n" };
148-
local $SIG{PIPE} = sub { die "sigpipe\n" };
149-
alarm(8);
150-
$s = IO::Socket::SSL->new(
151-
Proto => 'tcp',
152-
PeerAddr => '127.0.0.1',
153-
PeerPort => $port,
154-
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(),
155-
SSL_ca_file => "$d/root.crt",
156-
SSL_verify_callback => sub {
157-
my ($ok) = @_;
158-
$verify = $ok;
159-
return $ok;
160-
},
161-
SSL_error_trap => sub { die $_[1] }
162-
);
163-
alarm(0);
164-
};
165-
alarm(0);
166-
167-
if ($@) {
168-
log_in("died: $@");
169-
return undef;
170-
}
144+
my ($verify);
145+
146+
http(
147+
'', PeerAddr => '127.0.0.1:' . port($port), start => 1,
148+
SSL => 1,
149+
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER(),
150+
SSL_ca_file => "$d/root.crt",
151+
SSL_verify_callback => sub {
152+
my ($ok) = @_;
153+
$verify = $ok;
154+
return $ok;
155+
}
156+
);
171157

172158
return $verify;
173159
}

ssl_client_escaped_cert.t

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,31 +91,12 @@ is($escaped, $cert, 'ssl_client_escaped_cert unescape match');
9191

9292
sub cert {
9393
my ($uri) = @_;
94-
my $s;
95-
96-
eval {
97-
local $SIG{ALRM} = sub { die "timeout\n" };
98-
local $SIG{PIPE} = sub { die "sigpipe\n" };
99-
alarm(8);
100-
$s = IO::Socket::SSL->new(
101-
Proto => 'tcp',
102-
PeerAddr => '127.0.0.1',
103-
PeerPort => port(8443),
104-
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
105-
SSL_cert_file => "$d/localhost.crt",
106-
SSL_key_file => "$d/localhost.key",
107-
SSL_error_trap => sub { die $_[1] },
108-
);
109-
alarm(0);
110-
};
111-
alarm(0);
112-
113-
if ($@) {
114-
log_in("died: $@");
115-
return undef;
116-
}
117-
118-
http_get($uri, socket => $s);
94+
return http_get(
95+
$uri,
96+
SSL => 1,
97+
SSL_cert_file => "$d/localhost.crt",
98+
SSL_key_file => "$d/localhost.key"
99+
);
119100
}
120101

121102
###############################################################################

ssl_crl.t

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -162,37 +162,12 @@ like(get(8082, 'end'), qr/FAILED/, 'crl - intermediate cert revoked');
162162

163163
sub get {
164164
my ($port, $cert) = @_;
165-
my $s = get_ssl_socket($port, $cert) or return;
166-
http_get('/t', socket => $s);
167-
}
168-
169-
sub get_ssl_socket {
170-
my ($port, $cert) = @_;
171-
my ($s);
172-
173-
eval {
174-
local $SIG{ALRM} = sub { die "timeout\n" };
175-
local $SIG{PIPE} = sub { die "sigpipe\n" };
176-
alarm(8);
177-
$s = IO::Socket::SSL->new(
178-
Proto => 'tcp',
179-
PeerAddr => '127.0.0.1',
180-
PeerPort => port($port),
181-
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
182-
SSL_cert_file => "$d/$cert.crt",
183-
SSL_key_file => "$d/$cert.key",
184-
SSL_error_trap => sub { die $_[1] }
185-
);
186-
alarm(0);
187-
};
188-
alarm(0);
189-
190-
if ($@) {
191-
log_in("died: $@");
192-
return undef;
193-
}
194-
195-
return $s;
165+
http_get(
166+
'/t', PeerAddr => '127.0.0.1:' . port($port),
167+
SSL => 1,
168+
SSL_cert_file => "$d/$cert.crt",
169+
SSL_key_file => "$d/$cert.key"
170+
);
196171
}
197172

198173
###############################################################################

ssl_curve.t

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -75,43 +75,6 @@ $t->try_run('no $ssl_curve')->plan(1);
7575

7676
###############################################################################
7777

78-
like(get('/curve'), qr/^prime256v1 /m, 'ssl curve');
79-
80-
###############################################################################
81-
82-
sub get {
83-
my ($uri, $port, $ctx) = @_;
84-
my $s = get_ssl_socket($port) or return;
85-
my $r = http_get($uri, socket => $s);
86-
$s->close();
87-
return $r;
88-
}
89-
90-
sub get_ssl_socket {
91-
my ($port, $ctx) = @_;
92-
my $s;
93-
94-
eval {
95-
local $SIG{ALRM} = sub { die "timeout\n" };
96-
local $SIG{PIPE} = sub { die "sigpipe\n" };
97-
alarm(8);
98-
$s = IO::Socket::SSL->new(
99-
Proto => 'tcp',
100-
PeerAddr => '127.0.0.1',
101-
PeerPort => port(8443),
102-
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
103-
SSL_error_trap => sub { die $_[1] },
104-
);
105-
alarm(0);
106-
};
107-
alarm(0);
108-
109-
if ($@) {
110-
log_in("died: $@");
111-
return undef;
112-
}
113-
114-
return $s;
115-
}
78+
like(http_get('/curve', SSL => 1), qr/^prime256v1 /m, 'ssl curve');
11679

11780
###############################################################################

ssl_password_file.t

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ http {
4949
ssl_password_file password_http;
5050
5151
server {
52-
listen 127.0.0.1:8081 ssl;
52+
listen 127.0.0.1:8443 ssl;
5353
listen 127.0.0.1:8080;
5454
server_name localhost;
5555
@@ -132,33 +132,6 @@ is($@, '', 'ssl_password_file works');
132132
# simple tests to ensure that nothing broke with ssl_password_file directive
133133

134134
like(http_get('/'), qr/200 OK.*http/ms, 'http');
135-
like(http_get('/', socket => get_ssl_socket()), qr/200 OK.*https/ms, 'https');
136-
137-
###############################################################################
138-
139-
sub get_ssl_socket {
140-
my $s;
141-
142-
eval {
143-
local $SIG{ALRM} = sub { die "timeout\n" };
144-
local $SIG{PIPE} = sub { die "sigpipe\n" };
145-
alarm(8);
146-
$s = IO::Socket::SSL->new(
147-
Proto => 'tcp',
148-
PeerAddr => '127.0.0.1:' . port(8081),
149-
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
150-
SSL_error_trap => sub { die $_[1] }
151-
);
152-
alarm(0);
153-
};
154-
alarm(0);
155-
156-
if ($@) {
157-
log_in("died: $@");
158-
return undef;
159-
}
160-
161-
return $s;
162-
}
135+
like(http_get('/', SSL => 1), qr/200 OK.*https/ms, 'https');
163136

164137
###############################################################################

ssl_proxy_protocol.t

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,24 +148,7 @@ sub pp_get {
148148

149149
my $s = http($proxy, start => 1);
150150

151-
eval {
152-
local $SIG{ALRM} = sub { die "timeout\n" };
153-
local $SIG{PIPE} = sub { die "sigpipe\n" };
154-
alarm(8);
155-
IO::Socket::SSL->start_SSL($s,
156-
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE(),
157-
SSL_error_trap => sub { die $_[1] }
158-
);
159-
alarm(0);
160-
};
161-
alarm(0);
162-
163-
if ($@) {
164-
log_in("died: $@");
165-
return undef;
166-
}
167-
168-
return http(<<EOF, socket => $s);
151+
return http(<<EOF, socket => $s, SSL => 1);
169152
GET $url HTTP/1.0
170153
Host: localhost
171154

0 commit comments

Comments
 (0)