Codebase list libpoe-component-sslify-perl / 4df07a7
add testcase for connref hooks Apocalypse 13 years ago
1 changed file(s) with 158 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #!/usr/bin/perl
1
2 # Thanks to ASCENT for this test!
3
4 use strict; use warnings;
5
6 my $numtests;
7 BEGIN {
8 $numtests = 20;
9
10 eval "use Test::NoWarnings";
11 if ( ! $@ ) {
12 # increment by one
13 $numtests++;
14
15 }
16 }
17
18 use Test::More tests => $numtests;
19
20 use POE 1.267;
21 use POE::Component::Client::TCP;
22 use POE::Component::Server::TCP;
23 use POE::Component::SSLify qw/Client_SSLify Server_SSLify SSLify_Options SSLify_GetCipher SSLify_ContextCreate SSLify_GetSocket SSLify_GetStatus/;
24
25 # TODO rewrite this to use Test::POE::Server::TCP and stuff :)
26
27 my $port;
28
29 POE::Component::Server::TCP->new
30 (
31 Alias => 'myserver',
32 Address => '127.0.0.1',
33 Port => 0,
34
35 Started => sub
36 {
37 use Socket qw/sockaddr_in/;
38 $port = (sockaddr_in($_[HEAP]->{listener}->getsockname))[0];
39 },
40 ClientConnected => sub
41 {
42 ok(1, 'SERVER: accepted');
43 },
44 ClientDisconnected => sub
45 {
46 ok(1, 'SERVER: client disconnected');
47 $_[KERNEL]->post( 'myserver' => 'shutdown');
48 },
49 ClientPreConnect => sub
50 {
51 eval { SSLify_Options('mylib/example.key', 'mylib/example.crt', 'sslv3') };
52 eval { SSLify_Options('../mylib/example.key', '../mylib/example.crt', 'sslv3') } if ($@);
53 ok(!$@, "SERVER: SSLify_Options $@");
54
55 my $socket = eval { Server_SSLify( $_[ARG0], sub {
56 my $socket = shift;
57 pass( "Got connect hook for server" );
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(1, 'SERVER: SSLify_GetCipher: '. SSLify_GetCipher($socket));
66 ok( SSLify_GetStatus($socket) == -1, "SERVER: SSLify_GetStatus is pending" );
67
68 # We pray that IO::Handle is sane...
69 ok( SSLify_GetSocket( $socket )->blocking == 0, 'SERVER: SSLified socket is non-blocking?');
70
71 return ($socket);
72 },
73 ClientInput => sub
74 {
75 my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0];
76
77 die "Unknown line from CLIENT: $line";
78 },
79 ClientError => sub
80 {
81 # Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0!
82 # The default PoCo::Server::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :(
83 my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ];
84
85 # TODO are there other "errors" that is harmless?
86 $error = "Normal disconnection" unless $error;
87 my $msg = "Got SERVER $syscall error $errno: $error";
88 unless ( $syscall eq 'read' and $errno == 0 ) {
89 fail( $msg );
90 } else {
91 diag( $msg ) if $ENV{TEST_VERBOSE};
92 }
93 },
94 );
95
96 POE::Component::Client::TCP->new
97 (
98 Alias => 'myclient',
99 RemoteAddress => '127.0.0.1',
100 RemotePort => $port,
101 Connected => sub
102 {
103 ok(1, 'CLIENT: connected');
104 },
105 PreConnect => sub
106 {
107 my $ctx = eval { SSLify_ContextCreate(undef, undef, 'sslv3') };
108 ok(!$@, "CLIENT: SSLify_ContextCreate $@");
109 my $socket = eval { Client_SSLify($_[ARG0], undef, undef, $ctx, sub {
110 my $socket = shift;
111
112 pass( "Got connect hook for client" );
113
114 ## At this point, connection MUST be encrypted.
115 my $cipher = SSLify_GetCipher($socket);
116 ok($cipher ne '(NONE)', "CLIENT: SSLify_GetCipher: $cipher");
117 ok( SSLify_GetStatus($socket) == 1, "CLIENT: SSLify_GetStatus is done" );
118
119 $poe_kernel->post( 'myclient' => 'shutdown' );
120 }) };
121 ok(!$@, "CLIENT: Client_SSLify $@");
122 ok(1, 'CLIENT: SSLify_GetCipher: '. SSLify_GetCipher($socket));
123 ok( SSLify_GetStatus($socket) == -1, "CLIENT: SSLify_GetStatus is pending" );
124
125 # We pray that IO::Handle is sane...
126 ok( SSLify_GetSocket( $socket )->blocking == 0, 'CLIENT: SSLified socket is non-blocking?');
127
128 return ($socket);
129 },
130 ServerInput => sub
131 {
132 my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0];
133
134 $kernel->yield('shutdown');
135 },
136 ServerError => sub
137 {
138 # Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0!
139 # The default PoCo::Client::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :(
140 my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ];
141
142 # TODO are there other "errors" that is harmless?
143 $error = "Normal disconnection" unless $error;
144 my $msg = "Got CLIENT $syscall error $errno: $error";
145 unless ( $syscall eq 'read' and $errno == 0 ) {
146 fail( $msg );
147 } else {
148 diag( $msg ) if $ENV{TEST_VERBOSE};
149 }
150 },
151 );
152
153 $poe_kernel->run();
154
155 pass( 'shut down sanely' );
156
157 exit 0;