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