|
0 |
#!/usr/bin/perl
|
|
1 |
use strict; use warnings;
|
|
2 |
|
|
3 |
# This tests the connection OK hook on both server/client
|
|
4 |
|
|
5 |
my $numtests;
|
|
6 |
BEGIN {
|
|
7 |
$numtests = 17;
|
|
8 |
|
|
9 |
eval "use Test::NoWarnings";
|
|
10 |
if ( ! $@ ) {
|
|
11 |
# increment by one
|
|
12 |
$numtests++;
|
|
13 |
}
|
|
14 |
}
|
|
15 |
|
|
16 |
use Test::More tests => $numtests;
|
|
17 |
|
|
18 |
use POE 1.267;
|
|
19 |
use POE::Component::Client::TCP;
|
|
20 |
use POE::Component::Server::TCP;
|
|
21 |
use POE::Component::SSLify qw/Client_SSLify Server_SSLify SSLify_Options SSLify_GetCipher SSLify_GetSocket SSLify_GetStatus/;
|
|
22 |
|
|
23 |
# TODO rewrite this to use Test::POE::Server::TCP and stuff :)
|
|
24 |
|
|
25 |
my $port;
|
|
26 |
|
|
27 |
POE::Component::Server::TCP->new
|
|
28 |
(
|
|
29 |
Alias => 'myserver',
|
|
30 |
Address => '127.0.0.1',
|
|
31 |
Port => 0,
|
|
32 |
|
|
33 |
Started => sub
|
|
34 |
{
|
|
35 |
use Socket qw/sockaddr_in/;
|
|
36 |
$port = (sockaddr_in($_[HEAP]->{listener}->getsockname))[0];
|
|
37 |
},
|
|
38 |
ClientConnected => sub
|
|
39 |
{
|
|
40 |
ok(1, 'SERVER: accepted');
|
|
41 |
},
|
|
42 |
ClientDisconnected => sub
|
|
43 |
{
|
|
44 |
ok(1, 'SERVER: client disconnected');
|
|
45 |
$_[KERNEL]->post( 'myserver' => 'shutdown');
|
|
46 |
},
|
|
47 |
ClientPreConnect => sub
|
|
48 |
{
|
|
49 |
eval { SSLify_Options('mylib/example.key', 'mylib/example.crt') };
|
|
50 |
eval { SSLify_Options('../mylib/example.key', '../mylib/example.crt') } if ($@);
|
|
51 |
ok(!$@, "SERVER: SSLify_Options $@");
|
|
52 |
|
|
53 |
my $socket = eval { Server_SSLify( $_[ARG0], sub {
|
|
54 |
my( $socket, $status, $errval ) = @_;
|
|
55 |
|
|
56 |
pass( "SERVER: Got callback hook" );
|
|
57 |
is( $status, 1, "SERVER: Status received from callback is OK" );
|
|
58 |
|
|
59 |
## At this point, connection MUST be encrypted.
|
|
60 |
my $cipher = SSLify_GetCipher($socket);
|
|
61 |
ok($cipher ne '(NONE)', "SERVER: SSLify_GetCipher: $cipher");
|
|
62 |
ok( SSLify_GetStatus($socket) == 1, "SERVER: SSLify_GetStatus is done" );
|
|
63 |
} ) };
|
|
64 |
ok(!$@, "SERVER: Server_SSLify $@");
|
|
65 |
ok( SSLify_GetStatus($socket) == -1, "SERVER: SSLify_GetStatus is pending" );
|
|
66 |
|
|
67 |
return ($socket);
|
|
68 |
},
|
|
69 |
ClientInput => sub
|
|
70 |
{
|
|
71 |
my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0];
|
|
72 |
|
|
73 |
die "Should have never got any input from the client!";
|
|
74 |
},
|
|
75 |
ClientError => sub
|
|
76 |
{
|
|
77 |
# Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0!
|
|
78 |
# The default PoCo::Server::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :(
|
|
79 |
my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ];
|
|
80 |
|
|
81 |
# TODO are there other "errors" that is harmless?
|
|
82 |
$error = "Normal disconnection" unless $error;
|
|
83 |
my $msg = "Got SERVER $syscall error $errno: $error";
|
|
84 |
unless ( $syscall eq 'read' and $errno == 0 ) {
|
|
85 |
fail( $msg );
|
|
86 |
} else {
|
|
87 |
diag( $msg ) if $ENV{TEST_VERBOSE};
|
|
88 |
}
|
|
89 |
},
|
|
90 |
);
|
|
91 |
|
|
92 |
POE::Component::Client::TCP->new
|
|
93 |
(
|
|
94 |
Alias => 'myclient',
|
|
95 |
RemoteAddress => '127.0.0.1',
|
|
96 |
RemotePort => $port,
|
|
97 |
|
|
98 |
Connected => sub
|
|
99 |
{
|
|
100 |
ok(1, 'CLIENT: connected');
|
|
101 |
},
|
|
102 |
PreConnect => sub
|
|
103 |
{
|
|
104 |
my $socket = eval { Client_SSLify($_[ARG0], sub {
|
|
105 |
my( $socket, $status, $errval ) = @_;
|
|
106 |
|
|
107 |
pass( "CLIENT: Got callback hook" );
|
|
108 |
is( $status, 1, "CLIENT: Status received from callback is OK" );
|
|
109 |
|
|
110 |
## At this point, connection MUST be encrypted.
|
|
111 |
my $cipher = SSLify_GetCipher($socket);
|
|
112 |
ok($cipher ne '(NONE)', "CLIENT: SSLify_GetCipher: $cipher");
|
|
113 |
ok( SSLify_GetStatus($socket) == 1, "CLIENT: SSLify_GetStatus is done" );
|
|
114 |
|
|
115 |
$poe_kernel->post( 'myclient' => 'shutdown' );
|
|
116 |
}) };
|
|
117 |
ok(!$@, "CLIENT: Client_SSLify $@");
|
|
118 |
ok( SSLify_GetStatus($socket) == -1, "CLIENT: SSLify_GetStatus is pending" );
|
|
119 |
|
|
120 |
return ($socket);
|
|
121 |
},
|
|
122 |
ServerInput => sub
|
|
123 |
{
|
|
124 |
my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0];
|
|
125 |
|
|
126 |
die "Should have never got any input from the server!";
|
|
127 |
},
|
|
128 |
ServerError => sub
|
|
129 |
{
|
|
130 |
# Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0!
|
|
131 |
# The default PoCo::Client::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :(
|
|
132 |
my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ];
|
|
133 |
|
|
134 |
# TODO are there other "errors" that is harmless?
|
|
135 |
$error = "Normal disconnection" unless $error;
|
|
136 |
my $msg = "Got CLIENT $syscall error $errno: $error";
|
|
137 |
unless ( $syscall eq 'read' and $errno == 0 ) {
|
|
138 |
fail( $msg );
|
|
139 |
} else {
|
|
140 |
diag( $msg ) if $ENV{TEST_VERBOSE};
|
|
141 |
}
|
|
142 |
},
|
|
143 |
);
|
|
144 |
|
|
145 |
$poe_kernel->run();
|
|
146 |
|
|
147 |
pass( 'shut down sanely' );
|
|
148 |
|
|
149 |
exit 0;
|