add more connect fail tests
Apocalypse
13 years ago
0 | #!/usr/bin/perl | |
1 | use strict; use warnings; | |
2 | ||
3 | # This tests in-situ sslification ( upgrade a non-ssl socket to ssl ) | |
4 | ||
5 | my $numtests; | |
6 | BEGIN { | |
7 | $numtests = 18; | |
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_ContextCreate SSLify_GetSocket/; | |
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 | ClientInput => sub | |
48 | { | |
49 | my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0]; | |
50 | ||
51 | if ( $line eq 'plaintext_ping' ) { | |
52 | ok(1, "SERVER: recv: $line"); | |
53 | $heap->{client}->put('plaintext_pong'); | |
54 | $heap->{client}->flush; # make sure we sent the pong | |
55 | ||
56 | # sslify it in-situ! | |
57 | eval { SSLify_Options('mylib/example.key', 'mylib/example.crt', 'sslv3') }; | |
58 | eval { SSLify_Options('../mylib/example.key', '../mylib/example.crt', 'sslv3') } if ($@); | |
59 | ok(!$@, "SERVER: SSLify_Options $@"); | |
60 | my $socket = eval { Server_SSLify($heap->{client}->get_output_handle) }; | |
61 | ok(!$@, "SERVER: Server_SSLify $@"); | |
62 | ok(1, 'SERVER: SSLify_GetCipher: '. SSLify_GetCipher($socket)); | |
63 | ||
64 | # We pray that IO::Handle is sane... | |
65 | ok( SSLify_GetSocket( $socket )->blocking == 0, 'SERVER: SSLified socket is non-blocking?'); | |
66 | ||
67 | # TODO evil code here, ha! | |
68 | # Should I ask rcaputo to add a $rw->replace_handle($socket) method? | |
69 | # if you don't do the undef and just replace it - you'll get a bad file descriptor error from POE! | |
70 | # <fh> select error: Bad file descriptor (hits=-1) | |
71 | undef $heap->{client}; | |
72 | $heap->{client} = POE::Wheel::ReadWrite->new( | |
73 | Handle => $socket, | |
74 | InputEvent => 'tcp_server_got_input', | |
75 | ErrorEvent => 'tcp_server_got_error', | |
76 | FlushedEvent => 'tcp_server_got_flush', | |
77 | ); | |
78 | } elsif ( $line eq 'ssl_ping' ) { | |
79 | ok(1, "SERVER: recv: $line"); | |
80 | ||
81 | ## At this point, connection MUST be encrypted. | |
82 | my $cipher = SSLify_GetCipher($heap->{client}->get_output_handle); | |
83 | ok($cipher ne '(NONE)', "SERVER: SSLify_GetCipher: $cipher"); | |
84 | ||
85 | $heap->{client}->put('ssl_pong'); | |
86 | } else { | |
87 | die "Unknown line from CLIENT: $line"; | |
88 | } | |
89 | }, | |
90 | ClientError => sub | |
91 | { | |
92 | # Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0! | |
93 | # The default PoCo::Server::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :( | |
94 | my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ]; | |
95 | ||
96 | # TODO are there other "errors" that is harmless? | |
97 | $error = "Normal disconnection" unless $error; | |
98 | my $msg = "Got SERVER $syscall error $errno: $error"; | |
99 | unless ( $syscall eq 'read' and $errno == 0 ) { | |
100 | fail( $msg ); | |
101 | } else { | |
102 | diag( $msg ) if $ENV{TEST_VERBOSE}; | |
103 | } | |
104 | }, | |
105 | ); | |
106 | ||
107 | POE::Component::Client::TCP->new | |
108 | ( | |
109 | Alias => 'myclient', | |
110 | RemoteAddress => '127.0.0.1', | |
111 | RemotePort => $port, | |
112 | Connected => sub | |
113 | { | |
114 | ok(1, 'CLIENT: connected'); | |
115 | ||
116 | $_[HEAP]->{server}->put("plaintext_ping"); | |
117 | }, | |
118 | ServerInput => sub | |
119 | { | |
120 | my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0]; | |
121 | ||
122 | if ( $line eq 'plaintext_pong' ) { | |
123 | ok(1, "CLIENT: recv: $line"); | |
124 | ||
125 | # sslify it in-situ! | |
126 | my $ctx = eval { SSLify_ContextCreate(undef, undef, 'sslv3') }; | |
127 | ok(!$@, "CLIENT: SSLify_ContextCreate $@"); | |
128 | my $socket = eval { Client_SSLify($heap->{server}->get_output_handle, undef, undef, $ctx) }; | |
129 | ok(!$@, "CLIENT: Client_SSLify $@"); | |
130 | ok(1, 'CLIENT: SSLify_GetCipher: '. SSLify_GetCipher($socket)); | |
131 | ||
132 | # We pray that IO::Handle is sane... | |
133 | ok( SSLify_GetSocket( $socket )->blocking == 0, 'CLIENT: SSLified socket is non-blocking?'); | |
134 | ||
135 | # TODO evil code here, ha! | |
136 | # Should I ask rcaputo to add a $rw->replace_handle($socket) method? | |
137 | # if you don't do the undef and just replace it - you'll get a bad file descriptor error from POE! | |
138 | # <fh> select error: Bad file descriptor (hits=-1) | |
139 | undef $heap->{server}; | |
140 | $heap->{server} = POE::Wheel::ReadWrite->new( | |
141 | Handle => $socket, | |
142 | InputEvent => 'got_server_input', | |
143 | ErrorEvent => 'got_server_error', | |
144 | FlushedEvent => 'got_server_flush', | |
145 | ); | |
146 | ||
147 | # Send the ssl ping! | |
148 | $heap->{server}->put('ssl_ping'); | |
149 | } elsif ( $line eq 'ssl_pong' ) { | |
150 | ok(1, "CLIENT: recv: $line"); | |
151 | ||
152 | ## At this point, connection MUST be encrypted. | |
153 | my $cipher = SSLify_GetCipher($heap->{server}->get_output_handle); | |
154 | ok($cipher ne '(NONE)', "CLIENT: SSLify_GetCipher: $cipher"); | |
155 | ||
156 | $kernel->yield('shutdown'); | |
157 | } else { | |
158 | die "Unknown line from SERVER: $line"; | |
159 | } | |
160 | }, | |
161 | ServerError => sub | |
162 | { | |
163 | # Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0! | |
164 | # The default PoCo::Client::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :( | |
165 | my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ]; | |
166 | ||
167 | # TODO are there other "errors" that is harmless? | |
168 | $error = "Normal disconnection" unless $error; | |
169 | my $msg = "Got CLIENT $syscall error $errno: $error"; | |
170 | unless ( $syscall eq 'read' and $errno == 0 ) { | |
171 | fail( $msg ); | |
172 | } else { | |
173 | diag( $msg ) if $ENV{TEST_VERBOSE}; | |
174 | } | |
175 | }, | |
176 | ); | |
177 | ||
178 | $poe_kernel->run(); | |
179 | ||
180 | pass( 'shut down sanely' ); | |
181 | ||
182 | exit 0; |
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 connect hook" ); | |
57 | is( $status, 'OK', "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 "Unknown line from CLIENT: $line"; | |
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 | Connected => sub | |
98 | { | |
99 | ok(1, 'CLIENT: connected'); | |
100 | }, | |
101 | PreConnect => sub | |
102 | { | |
103 | my $socket = eval { Client_SSLify($_[ARG0], sub { | |
104 | my( $socket, $status, $errval ) = @_; | |
105 | ||
106 | pass( "CLIENT: Got connect hook" ); | |
107 | is( $status, 'OK', "CLIENT: Status received from callback is OK" ); | |
108 | ||
109 | ## At this point, connection MUST be encrypted. | |
110 | my $cipher = SSLify_GetCipher($socket); | |
111 | ok($cipher ne '(NONE)', "CLIENT: SSLify_GetCipher: $cipher"); | |
112 | ok( SSLify_GetStatus($socket) == 1, "CLIENT: SSLify_GetStatus is done" ); | |
113 | ||
114 | $poe_kernel->post( 'myclient' => 'shutdown' ); | |
115 | }) }; | |
116 | ok(!$@, "CLIENT: Client_SSLify $@"); | |
117 | ok( SSLify_GetStatus($socket) == -1, "CLIENT: SSLify_GetStatus is pending" ); | |
118 | ||
119 | return ($socket); | |
120 | }, | |
121 | ServerInput => sub | |
122 | { | |
123 | my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0]; | |
124 | ||
125 | die "Should have never got any input from the server!"; | |
126 | }, | |
127 | ServerError => sub | |
128 | { | |
129 | # Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0! | |
130 | # The default PoCo::Client::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :( | |
131 | my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ]; | |
132 | ||
133 | # TODO are there other "errors" that is harmless? | |
134 | $error = "Normal disconnection" unless $error; | |
135 | my $msg = "Got CLIENT $syscall error $errno: $error"; | |
136 | unless ( $syscall eq 'read' and $errno == 0 ) { | |
137 | fail( $msg ); | |
138 | } else { | |
139 | diag( $msg ) if $ENV{TEST_VERBOSE}; | |
140 | } | |
141 | }, | |
142 | ); | |
143 | ||
144 | $poe_kernel->run(); | |
145 | ||
146 | pass( 'shut down sanely' ); | |
147 | ||
148 | exit 0; |
0 | #!/usr/bin/perl | |
1 | use strict; use warnings; | |
2 | ||
3 | # this tests the connection fail hook on the server-side | |
4 | ||
5 | my $numtests; | |
6 | BEGIN { | |
7 | $numtests = 8; | |
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/Server_SSLify SSLify_Options 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 | ClientPreConnect => sub | |
43 | { | |
44 | eval { SSLify_Options('mylib/example.key', 'mylib/example.crt') }; | |
45 | eval { SSLify_Options('../mylib/example.key', '../mylib/example.crt') } if ($@); | |
46 | ok(!$@, "SERVER: SSLify_Options $@"); | |
47 | ||
48 | my $socket = eval { Server_SSLify( $_[ARG0], sub { | |
49 | my( $socket, $status, $errval ) = @_; | |
50 | ||
51 | pass( "SERVER: Got connect hook" ); | |
52 | is( $status, 'ERR', "SERVER: Status received from callback is ERR - $errval" ); | |
53 | is( SSLify_GetStatus( $socket ), 0, "SERVER: SSLify_GetStatus is error" ); | |
54 | ||
55 | $poe_kernel->post( 'myserver' => 'shutdown'); | |
56 | } ) }; | |
57 | ok(!$@, "SERVER: Server_SSLify $@"); | |
58 | is( SSLify_GetStatus( $socket ), -1, "SERVER: SSLify_GetStatus is pending" ); | |
59 | ||
60 | return ($socket); | |
61 | }, | |
62 | ClientDisconnected => sub | |
63 | { | |
64 | ok(1, 'SERVER: client disconnected'); | |
65 | }, | |
66 | ClientInput => sub | |
67 | { | |
68 | my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0]; | |
69 | ||
70 | die "Should have never got any input from the client!"; | |
71 | }, | |
72 | ClientError => sub | |
73 | { | |
74 | # Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0! | |
75 | # The default PoCo::Server::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :( | |
76 | my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ]; | |
77 | ||
78 | # Since this test purposefully sends garbage, we expect a connection reset by peer | |
79 | # not ok 7 - Got SERVER read error 104: Connection reset by peer | |
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 == 104 ) { | |
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 | Connected => sub | |
98 | { | |
99 | ok(1, 'CLIENT: connected'); | |
100 | }, | |
101 | ServerInput => sub | |
102 | { | |
103 | my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0]; | |
104 | ||
105 | # purposefully send garbage so we screw up the ssl connect on the client-side | |
106 | $heap->{server}->put( 'garbage in, garbage out' ); | |
107 | }, | |
108 | ServerError => sub | |
109 | { | |
110 | # Thanks to H. Merijn Brand for spotting this FAIL in 5.12.0! | |
111 | # The default PoCo::Client::TCP handler will throw a warning, which causes Test::NoWarnings to FAIL :( | |
112 | my ($syscall, $errno, $error) = @_[ ARG0..ARG2 ]; | |
113 | ||
114 | # TODO are there other "errors" that is harmless? | |
115 | $error = "Normal disconnection" unless $error; | |
116 | my $msg = "Got CLIENT $syscall error $errno: $error"; | |
117 | unless ( $syscall eq 'read' and $errno == 0 ) { | |
118 | fail( $msg ); | |
119 | } else { | |
120 | diag( $msg ) if $ENV{TEST_VERBOSE}; | |
121 | } | |
122 | }, | |
123 | ); | |
124 | ||
125 | $poe_kernel->run(); | |
126 | ||
127 | pass( 'shut down sanely' ); | |
128 | ||
129 | exit 0; |