| 1 | #!/usr/bin/perl -w | ||
| 2 | # vim: ts=2 sw=2 expandtab | ||
| 3 | |||
| 4 | # Exercises Client and Server TCP components, which exercise | ||
| 5 | # SocketFactory in AF_INET6 mode. | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use lib qw(./mylib ../mylib); | ||
| 9 | |||
| 10 | BEGIN { | ||
| 11 | # under perl-5.6.2 the warning "leaks" from the eval, while newer versions don't... | ||
| 12 | # it's due to Exporter.pm behaving differently, so we have to shut it up | ||
| 13 | no warnings 'redefine'; | ||
| 14 | require Carp; | ||
| 15 | local *Carp::carp = sub { die @_ }; | ||
| 16 | eval { require Socket; Socket->import('AF_INET6') }; | ||
| 17 | if ($@) { | ||
| 18 | eval { require Socket6; Socket6->import('AF_INET6') }; | ||
| 19 | if ($@) { | ||
| 20 | print "1..0 # Skip Cannot find AF_INET6 support in Socket or Socket6.\n"; | ||
| 21 | CORE::exit(); | ||
| 22 | } | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 26 | # Second BEGIN block so that AF_INET6 is defined before this code is | ||
| 27 | # compiled. | ||
| 28 | |||
| 29 | BEGIN { | ||
| 30 | my $error; | ||
| 31 | |||
| 32 | eval 'use Socket::GetAddrInfo qw(:newapi getaddrinfo getnameinfo NI_NUMERICHOST NI_NUMERICSERV)'; | ||
| 33 | if ($@) { | ||
| 34 | $error = "Socket::GetAddrInfo is needed for IPv6 tests"; | ||
| 35 | } | ||
| 36 | elsif ($^O eq "cygwin") { | ||
| 37 | $error = ( | ||
| 38 | "IPv6 isn't available on Cygwin, even with Socket::GetAddrInfo installed" | ||
| 39 | ); | ||
| 40 | } | ||
| 41 | else { | ||
| 42 | my $addr; | ||
| 43 | eval { | ||
| 44 | my ($error, @addr) = getaddrinfo( | ||
| 45 | "localhost", 80, { family => AF_INET6 } | ||
| 46 | ); | ||
| 47 | $addr = $addr[0]{addr} if @addr; | ||
| 48 | }; | ||
| 49 | if ($@) { | ||
| 50 | $error = "error resolving localhost for IPv6: $@"; | ||
| 51 | } | ||
| 52 | elsif (!defined $addr) { | ||
| 53 | $error = "IPv6 tests require a configured IPv6 localhost address"; | ||
| 54 | } | ||
| 55 | elsif (!-f 'run_network_tests') { | ||
| 56 | $error = "Network access (and permission) required to run this test"; | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | # Not Test::More, because I'm pretty sure skip_all calls Perl's | ||
| 61 | # regular exit(). | ||
| 62 | if ($error) { | ||
| 63 | print "1..0 # Skip $error\n"; | ||
| 64 | CORE::exit(); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | |||
| 68 | sub POE::Kernel::ASSERT_DEFAULT () { 1 } | ||
| 69 | |||
| 70 | BEGIN { | ||
| 71 | package | ||
| 72 | POE::Kernel; | ||
| 73 | use constant TRACE_DEFAULT => exists($INC{'Devel/Cover.pm'}); | ||
| 74 | } | ||
| 75 | |||
| 76 | use POE qw( Component::Client::TCP Component::Server::TCP ); | ||
| 77 | |||
| 78 | my $tcp_server_port; | ||
| 79 | |||
| 80 | # Congratulations! We made it this far! | ||
| 81 | use Test::More tests => 3; | ||
| 82 | |||
| 83 | diag( "This test may hang if your firewall blocks IPv6" ); | ||
| 84 | diag( "packets across your localhost interface." ); | ||
| 85 | |||
| 86 | ############################################################################### | ||
| 87 | # Start the TCP server. | ||
| 88 | |||
| 89 | POE::Component::Server::TCP->new( | ||
| 90 | Port => 0, | ||
| 91 | Address => '::1', | ||
| 92 | Domain => AF_INET6, | ||
| 93 | Alias => 'server', | ||
| 94 | ClientConnected => \&server_got_connect, | ||
| 95 | ClientInput => \&server_got_input, | ||
| 96 | ClientFlushed => \&server_got_flush, | ||
| 97 | ClientDisconnected => \&server_got_disconnect, | ||
| 98 | Error => \&server_got_error, | ||
| 99 | ClientError => sub { }, # Hush a warning. | ||
| 100 | Started => sub { | ||
| 101 | eval { | ||
| 102 | my $socket_name = $_[HEAP]{listener}->getsockname(); | ||
| 103 | (my ($err, $host), $tcp_server_port) = getnameinfo( | ||
| 104 | $socket_name, NI_NUMERICHOST | NI_NUMERICSERV | ||
| 105 | ); | ||
| 106 | }; | ||
| 107 | if (!$tcp_server_port || $@) { | ||
| 108 | $tcp_server_port = undef; | ||
| 109 | SKIP: { | ||
| 110 | my $errstr = @$ || 'server port undefined'; | ||
| 111 | skip "AF_INET6 probably not supported or configured ($errstr)", 2; | ||
| 112 | } | ||
| 113 | } | ||
| 114 | }, | ||
| 115 | ); | ||
| 116 | |||
| 117 | sub server_got_connect { | ||
| 118 | my $heap = $_[HEAP]; | ||
| 119 | $heap->{server_test_one} = 1; | ||
| 120 | $heap->{flush_count} = 0; | ||
| 121 | $heap->{put_count} = 0; | ||
| 122 | } | ||
| 123 | |||
| 124 | sub server_got_input { | ||
| 125 | my ($heap, $line) = @_[HEAP, ARG0]; | ||
| 126 | $line =~ tr/a-zA-Z/n-za-mN-ZA-M/; # rot13 | ||
| 127 | $heap->{client}->put($line); | ||
| 128 | $heap->{put_count}++; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub server_got_flush { | ||
| 132 | $_[HEAP]->{flush_count}++; | ||
| 133 | } | ||
| 134 | |||
| 135 | sub server_got_disconnect { | ||
| 136 | my $heap = $_[HEAP]; | ||
| 137 | ok( | ||
| 138 | $heap->{put_count} == $heap->{flush_count}, | ||
| 139 | "server put_count matches flush_count" | ||
| 140 | ); | ||
| 141 | } | ||
| 142 | |||
| 143 | sub server_got_error { | ||
| 144 | my ($syscall, $errno, $error) = @_[ARG0..ARG2]; | ||
| 145 | SKIP: { | ||
| 146 | skip "AF_INET6 probably not supported ($syscall error $errno: $error)", 1 | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | ############################################################################### | ||
| 151 | # Start the TCP client. | ||
| 152 | |||
| 153 | if ($tcp_server_port) { | ||
| 154 | POE::Component::Client::TCP->new( | ||
| 155 | RemoteAddress => '::1', | ||
| 156 | RemotePort => $tcp_server_port, | ||
| 157 | Domain => AF_INET6, | ||
| 158 | BindAddress => '::1', | ||
| 159 | Connected => \&client_got_connect, | ||
| 160 | ServerInput => \&client_got_input, | ||
| 161 | ServerFlushed => \&client_got_flush, | ||
| 162 | Disconnected => \&client_got_disconnect, | ||
| 163 | ConnectError => \&client_got_connect_error, | ||
| 164 | ); | ||
| 165 | } | ||
| 166 | |||
| 167 | sub client_got_connect { | ||
| 168 | my $heap = $_[HEAP]; | ||
| 169 | $heap->{flush_count} = 0; | ||
| 170 | $heap->{put_count} = 1; | ||
| 171 | $heap->{server}->put( '1: this is a test' ); | ||
| 172 | } | ||
| 173 | |||
| 174 | sub client_got_input { | ||
| 175 | my ($kernel, $heap, $line) = @_[KERNEL, HEAP, ARG0]; | ||
| 176 | |||
| 177 | if ($line =~ s/^1: //) { | ||
| 178 | $heap->{put_count}++; | ||
| 179 | $heap->{server}->put( '2: ' . $line ); | ||
| 180 | } | ||
| 181 | elsif ($line =~ s/^2: //) { | ||
| 182 | ok( | ||
| 183 | $line eq "this is a test", | ||
| 184 | "received input" | ||
| 185 | ); | ||
| 186 | $kernel->post(server => "shutdown"); | ||
| 187 | $kernel->yield("shutdown"); | ||
| 188 | } | ||
| 189 | } | ||
| 190 | |||
| 191 | sub client_got_flush { | ||
| 192 | $_[HEAP]->{flush_count}++; | ||
| 193 | } | ||
| 194 | |||
| 195 | sub client_got_disconnect { | ||
| 196 | my $heap = $_[HEAP]; | ||
| 197 | ok( | ||
| 198 | $heap->{put_count} == $heap->{flush_count}, | ||
| 199 | "client put_count matches flush_count" | ||
| 200 | ); | ||
| 201 | } | ||
| 202 | |||
| 203 | sub client_got_connect_error { | ||
| 204 | my ($syscall, $errno, $error) = @_[ARG0..ARG2]; | ||
| 205 | SKIP: { | ||
| 206 | skip "AF_INET6 probably not supported ($syscall error $errno: $error)", 2; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | |||
| 210 | ### main loop | ||
| 211 | |||
| 212 | POE::Kernel->run(); | ||
| 213 | |||
| 214 | 1; |