| 1 |
#!/usr/bin/perl |
| 2 |
# |
| 3 |
# This file is part of POE-Component-SSLify |
| 4 |
# |
| 5 |
# This software is copyright (c) 2014 by Apocalypse. |
| 6 |
# |
| 7 |
# This is free software; you can redistribute it and/or modify it under |
| 8 |
# the same terms as the Perl 5 programming language system itself. |
| 9 |
# |
| 10 |
use strict; use warnings; |
| 11 |
use strict; use warnings; |
| 12 |
|
| 13 |
# Thanks to ASCENT for this test! |
| 14 |
# This test adds renegotiation to the connection from client-side |
| 15 |
# Since this is not supported on all platforms, it's marked TODO and adds custom logic |
| 16 |
# to make sure it doesn't FAIL if it's not supported. |
| 17 |
|
| 18 |
use Test::FailWarnings; |
| 19 |
use Test::More 1.001002; # new enough for sanity in done_testing() |
| 20 |
|
| 21 |
use POE 1.267; |
| 22 |
use POE::Component::Client::TCP; |
| 23 |
use POE::Component::Server::TCP; |
| 24 |
use POE::Component::SSLify qw/Client_SSLify Server_SSLify SSLify_Options SSLify_GetCipher SSLify_ContextCreate SSLify_GetSocket SSLify_GetSSL/; |
| 25 |
use Net::SSLeay qw/ERROR_WANT_READ ERROR_WANT_WRITE/; |
| 26 |
|
| 27 |
# TODO rewrite this to use Test::POE::Server::TCP and stuff :) |
| 28 |
|
| 29 |
my $port; |
| 30 |
my $server_ping2; |
| 31 |
my $client_ping2; |
| 32 |
|
| 33 |
POE::Component::Server::TCP->new |
| 34 |
( |
| 35 |
Alias => 'myserver', |
| 36 |
Address => '127.0.0.1', |
| 37 |
Port => 0, |
| 38 |
|
| 39 |
Started => sub |
| 40 |
{ |
| 41 |
use Socket qw/sockaddr_in/; |
| 42 |
$port = (sockaddr_in($_[HEAP]->{listener}->getsockname))[0]; |
| 43 |
}, |
| 44 |
ClientConnected => sub |
| 45 |
{ |
| 46 |
ok(1, 'SERVER: accepted'); |
| 47 |
}, |
| 48 |
ClientDisconnected => sub |
| 49 |
{ |
| 50 |
ok(1, 'SERVER: client disconnected'); |
| 51 |
$_[KERNEL]->post(myserver => 'shutdown'); |
| 52 |
}, |
| 53 |
ClientPreConnect => sub |
| 54 |
{ |
| 55 |
eval { SSLify_Options('mylib/example.key', 'mylib/example.crt', 'sslv3') }; |
| 56 |
eval { SSLify_Options('../mylib/example.key', '../mylib/example.crt', 'sslv3') } if ($@); |
| 57 |
ok(!$@, "SERVER: SSLify_Options $@"); |
| 58 |
|
| 59 |
my $socket = eval { Server_SSLify($_[ARG0]) }; |
| 60 |
ok(!$@, "SERVER: Server_SSLify $@"); |
| 61 |
ok(1, 'SERVER: SSLify_GetCipher: '. SSLify_GetCipher($socket)); |
| 62 |
|
| 63 |
# We pray that IO::Handle is sane... |
| 64 |
ok( SSLify_GetSocket( $socket )->blocking == 0, 'SERVER: SSLified socket is non-blocking?') if $^O ne 'MSWin32'; |
| 65 |
|
| 66 |
return ($socket); |
| 67 |
}, |
| 68 |
ClientInput => sub |
| 69 |
{ |
| 70 |
my ($kernel, $heap, $request) = @_[KERNEL, HEAP, ARG0]; |
| 71 |
|
| 72 |
## At this point, connection MUST be encrypted. |
| 73 |
my $cipher = SSLify_GetCipher($heap->{client}->get_output_handle); |
| 74 |
ok($cipher ne '(NONE)', "SERVER: SSLify_GetCipher: $cipher"); |
| 75 |
|
| 76 |
if ($request eq 'ping') |
| 77 |
{ |
| 78 |
ok(1, "SERVER: recv: $request"); |
| 79 |
$heap->{client}->put("pong"); |
| 80 |
} |
| 81 |
elsif ($request eq 'ping2') |
| 82 |
{ |
| 83 |
ok(1, "SERVER: recv: $request"); |
| 84 |
$server_ping2++; |
| 85 |
$heap->{client}->put("pong2"); |
| 86 |
} |
| 87 |
}, |
| 88 |
ClientError => sub |
| 89 |
{ |
| 90 |
# Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0! |
| 91 |
# The default PoCo::Server::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :( |
| 92 |
my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ]; |
| 93 |
|
| 94 |
# TODO are there other "errors" that is harmless? |
| 95 |
$error = "Normal disconnection" unless $error; |
| 96 |
my $msg = "Got SERVER $syscall error $errno: $error"; |
| 97 |
unless ( $syscall eq 'read' and $errno == 0 ) { |
| 98 |
fail( $msg ); |
| 99 |
} else { |
| 100 |
diag( $msg ) if $ENV{TEST_VERBOSE}; |
| 101 |
} |
| 102 |
}, |
| 103 |
); |
| 104 |
|
| 105 |
POE::Component::Client::TCP->new |
| 106 |
( |
| 107 |
Alias => 'myclient', |
| 108 |
RemoteAddress => '127.0.0.1', |
| 109 |
RemotePort => $port, |
| 110 |
|
| 111 |
Connected => sub |
| 112 |
{ |
| 113 |
ok(1, 'CLIENT: connected'); |
| 114 |
|
| 115 |
$_[HEAP]->{server}->put("ping"); |
| 116 |
}, |
| 117 |
PreConnect => sub |
| 118 |
{ |
| 119 |
my $ctx = eval { SSLify_ContextCreate(undef, undef, 'sslv3') }; |
| 120 |
ok(!$@, "CLIENT: SSLify_ContextCreate $@"); |
| 121 |
my $socket = eval { Client_SSLify($_[ARG0], undef, undef, $ctx) }; |
| 122 |
ok(!$@, "CLIENT: Client_SSLify $@"); |
| 123 |
ok(1, 'CLIENT: SSLify_GetCipher: '. SSLify_GetCipher($socket)); |
| 124 |
|
| 125 |
# We pray that IO::Handle is sane... |
| 126 |
ok( SSLify_GetSocket( $socket )->blocking == 0, 'CLIENT: SSLified socket is non-blocking?') if $^O ne 'MSWin32'; |
| 127 |
|
| 128 |
return ($socket); |
| 129 |
}, |
| 130 |
ServerInput => sub |
| 131 |
{ |
| 132 |
my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0]; |
| 133 |
|
| 134 |
## At this point, connection MUST be encrypted. |
| 135 |
my $cipher = SSLify_GetCipher($heap->{server}->get_output_handle); |
| 136 |
ok($cipher ne '(NONE)', "CLIENT: SSLify_GetCipher: $cipher"); |
| 137 |
|
| 138 |
if ($line eq 'pong') |
| 139 |
{ |
| 140 |
ok(1, "CLIENT: recv: $line"); |
| 141 |
|
| 142 |
# Skip 2 Net::SSLeay::renegotiate() tests on FreeBSD because of |
| 143 |
# http://security.freebsd.org/advisories/FreeBSD-SA-09:15.ssl.asc |
| 144 |
TODO: { |
| 145 |
local $TODO = "Net::SSLeay::renegotiate() does not work on all platforms"; |
| 146 |
|
| 147 |
## Force SSL renegotiation |
| 148 |
my $ssl = SSLify_GetSSL( $heap->{server}->get_output_handle ); |
| 149 |
my $reneg_num = Net::SSLeay::num_renegotiations($ssl); |
| 150 |
|
| 151 |
ok(1 == Net::SSLeay::renegotiate($ssl), 'CLIENT: SSL renegotiation'); |
| 152 |
my $handshake = Net::SSLeay::do_handshake($ssl); |
| 153 |
my $err = Net::SSLeay::get_error($ssl, $handshake); |
| 154 |
|
| 155 |
## 1 == Successful handshake, ERROR_WANT_(READ|WRITE) == non-blocking. |
| 156 |
ok($handshake == 1 || $err == ERROR_WANT_READ || $err == ERROR_WANT_WRITE, 'CLIENT: SSL handshake'); |
| 157 |
ok($reneg_num < Net::SSLeay::num_renegotiations($ssl), 'CLIENT: Increased number of negotiations'); |
| 158 |
} |
| 159 |
|
| 160 |
$heap->{server}->put('ping2'); |
| 161 |
} |
| 162 |
|
| 163 |
elsif ($line eq 'pong2') |
| 164 |
{ |
| 165 |
ok(1, "CLIENT: recv: $line"); |
| 166 |
$client_ping2++; |
| 167 |
$kernel->yield('shutdown'); |
| 168 |
} |
| 169 |
}, |
| 170 |
ServerError => sub |
| 171 |
{ |
| 172 |
# Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0! |
| 173 |
# The default PoCo::Client::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :( |
| 174 |
my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ]; |
| 175 |
|
| 176 |
# TODO are there other "errors" that is harmless? |
| 177 |
$error = "Normal disconnection" unless $error; |
| 178 |
my $msg = "Got CLIENT $syscall error $errno: $error"; |
| 179 |
unless ( $syscall eq 'read' and $errno == 0 ) { |
| 180 |
fail( $msg ); |
| 181 |
} else { |
| 182 |
diag( $msg ) if $ENV{TEST_VERBOSE}; |
| 183 |
} |
| 184 |
}, |
| 185 |
); |
| 186 |
|
| 187 |
$poe_kernel->run(); |
| 188 |
|
| 189 |
# Add extra pass() to make the test harness happy if renegotiate did not work |
| 190 |
if ( ! $server_ping2 ) { |
| 191 |
local $TODO = "Net::SSLeay::renegotiate() does not work on all platforms"; |
| 192 |
fail( "SERVER: Failed SSL renegotiation" ); |
| 193 |
} |
| 194 |
if ( ! $client_ping2 ) { |
| 195 |
local $TODO = "Net::SSLeay::renegotiate() does not work on all platforms"; |
| 196 |
fail( "CLIENT: Failed SSL renegotiation" ); |
| 197 |
} |
| 198 |
if ( ! $server_ping2 or ! $client_ping2 ) { |
| 199 |
diag( "WARNING: Your platform/SSL library does not support renegotiation of the SSL socket." ); |
| 200 |
diag( "This test harness detected that trying to renegotiate resulted in a disconnected socket." ); |
| 201 |
diag( "POE::Component::SSLify will work on your system, but please do not attempt a SSL renegotiate." ); |
| 202 |
diag( "Please talk with the author to figure out if this issue can be worked around, thank you!" ); |
| 203 |
} |
| 204 |
|
| 205 |
done_testing; |