/usr/share/perl5/Test/TCP.pm is in libtest-tcp-perl 2.19-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 | package Test::TCP;
use strict;
use warnings;
use 5.00800;
our $VERSION = '2.19';
use base qw/Exporter/;
use Test::SharedFork 0.12;
use Test::More ();
use Config;
use POSIX;
use Time::HiRes ();
use Carp ();
use Net::EmptyPort qw(empty_port check_port);
our @EXPORT = qw/ empty_port test_tcp wait_port /;
# process does not die when received SIGTERM, on win32.
my $TERMSIG = $^O eq 'MSWin32' ? 'KILL' : 'TERM';
sub test_tcp {
my %args = @_;
for my $k (qw/client server/) {
die "missing mandatory parameter $k" unless exists $args{$k};
}
my $server_code = delete $args{server};
my $client_code = delete $args{client};
my $server = Test::TCP->new(
code => $server_code,
%args,
);
$client_code->($server->port, $server->pid);
undef $server; # make sure
}
sub wait_port {
my ($host, $port, $max_wait);
if (@_ && ref $_[0] eq 'HASH') {
$host = $_[0]->{host};
$port = $_[0]->{port};
$max_wait = $_[0]->{max_wait};
} elsif (@_ == 3) {
# backward compat
($port, (my $sleep), (my $retry)) = @_;
$max_wait = $sleep * $retry;
} else {
($port, $max_wait) = @_;
}
$host = '127.0.0.1'
unless defined $host;
$max_wait ||= 10;
Net::EmptyPort::wait_port({ host => $host, port => $port, max_wait => $max_wait })
or die "cannot open port: $host:$port";
}
# -------------------------------------------------------------------------
# OO-ish interface
sub new {
my $class = shift;
my %args = @_==1 ? %{$_[0]} : @_;
Carp::croak("missing mandatory parameter 'code'") unless exists $args{code};
my $self = bless {
auto_start => 1,
max_wait => 10,
host => '127.0.0.1',
_my_pid => $$,
%args,
}, $class;
if ($self->{listen}) {
$self->{socket} ||= Net::EmptyPort::listen_socket({
host => $self->{host},
proto => $self->{proto},
}) or die "Cannot listen: $!";
$self->{port} = $self->{socket}->sockport;
}
else {
$self->{port} ||= empty_port({ host => $self->{host} });
}
$self->start()
if $self->{auto_start};
return $self;
}
sub pid { $_[0]->{pid} }
sub port { $_[0]->{port} }
sub start {
my $self = shift;
my $pid = fork();
die "fork() failed: $!" unless defined $pid;
if ( $pid ) { # parent process.
$self->{pid} = $pid;
Test::TCP::wait_port({ host => $self->{host}, port => $self->port, max_wait => $self->{max_wait} })
unless $self->{socket};
return;
} else { # child process
$self->{code}->($self->{socket} || $self->port);
# should not reach here
if (kill 0, $self->{_my_pid}) { # warn only parent process still exists
warn("[Test::TCP] Child process does not block(PID: $$, PPID: $self->{_my_pid})");
}
exit 0;
}
}
sub stop {
my $self = shift;
return unless defined $self->{pid};
return unless $self->{_my_pid} == $$;
# This is a workaround for win32 fork emulation's bug.
#
# kill is inherently unsafe for pseudo-processes in Windows
# and the process calling kill(9, $pid) may be destabilized
# The call to Sleep will decrease the frequency of this problems
#
# SEE ALSO:
# http://www.gossamer-threads.com/lists/perl/porters/261805
# https://rt.cpan.org/Ticket/Display.html?id=67292
Win32::Sleep(0) if $^O eq "MSWin32"; # will relinquish the remainder of its time slice
kill $TERMSIG => $self->{pid};
Win32::Sleep(0) if $^O eq "MSWin32"; # will relinquish the remainder of its time slice
local $?; # waitpid modifies original $?.
LOOP: while (1) {
my $kid = waitpid( $self->{pid}, 0 );
if ($^O ne 'MSWin32') { # i'm not in hell
if (POSIX::WIFSIGNALED($?)) {
my $signame = (split(' ', $Config{sig_name}))[POSIX::WTERMSIG($?)];
if ($signame =~ /^(ABRT|PIPE)$/) {
Test::More::diag("your server received SIG$signame");
}
}
}
if ($kid == 0 || $kid == -1) {
last LOOP;
}
}
undef $self->{pid};
}
sub DESTROY {
my $self = shift;
local $@;
$self->stop();
}
1;
__END__
=for stopwords OO
=encoding utf8
=head1 NAME
Test::TCP - testing TCP program
=head1 SYNOPSIS
use Test::TCP;
my $server = Test::TCP->new(
listen => 1,
code => sub {
my $socket = shift;
...
},
);
my $client = MyClient->new(host => '127.0.0.1', port => $server->port);
undef $server; # kill child process on DESTROY
If using a server that can only accept a port number, e.g. memcached:
use Test::TCP;
my $memcached = Test::TCP->new(
code => sub {
my $port = shift;
exec $bin, '-p' => $port;
die "cannot execute $bin: $!";
},
);
my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]});
...
B<N.B.>: This is vulnerable to race conditions, if another process binds
to the same port after L<Net::EmptyPort> found it available.
And functional interface is available:
use Test::TCP;
test_tcp(
listen => 1,
client => sub {
my ($port, $server_pid) = @_;
# send request to the server
},
server => sub {
my $socket = shift;
# run server, calling $socket->accept
},
);
test_tcp(
client => sub {
my ($port, $server_pid) = @_;
# send request to the server
},
server => sub {
my $port = shift;
# run server, binding to $port
},
);
=head1 DESCRIPTION
Test::TCP is a test utility to test TCP/IP-based server programs.
=head1 METHODS
=over 4
=item test_tcp
Functional interface.
test_tcp(
listen => 1,
client => sub {
my $port = shift;
# send request to the server
},
server => sub {
my $socket = shift;
# run server
},
# optional
host => '127.0.0.1', # specify '::1' to test using IPv6
port => 8080,
max_wait => 3, # seconds
);
If C<listen> is false, C<server> is instead passed a port number that
was free before it was called.
=item wait_port
wait_port(8080);
Waits for a particular port is available for connect.
=back
=head1 Object Oriented interface
=over 4
=item my $server = Test::TCP->new(%args);
Create new instance of Test::TCP.
Arguments are following:
=over 4
=item $args{auto_start}: Boolean
Call C<< $server->start() >> after create instance.
Default: true
=item $args{code}: CodeRef
The callback function. Argument for callback function is:
C<< $code->($socket) >> or C<< $code->($port) >>,
depending on the value of C<listen>.
This parameter is required.
=item $args{max_wait} : Number
Will wait for at most C<$max_wait> seconds before checking port.
See also L<Net::EmptyPort>.
I<Default: 10>
=item $args{listen} : Boolean
If true, open a listening socket and pass this to the callback.
Otherwise find a free port and pass the number of it to the callback.
=back
=item $server->start()
Start the server process. Normally, you don't need to call this method.
=item $server->stop()
Stop the server process.
=item my $pid = $server->pid();
Get the pid of child process.
=item my $port = $server->port();
Get the port number of child process.
=back
=head1 FAQ
=over 4
=item How to invoke two servers?
You can call test_tcp() twice!
test_tcp(
client => sub {
my $port1 = shift;
test_tcp(
client => sub {
my $port2 = shift;
# some client code here
},
server => sub {
my $port2 = shift;
# some server2 code here
},
);
},
server => sub {
my $port1 = shift;
# some server1 code here
},
);
Or use the OO interface instead.
my $server1 = Test::TCP->new(code => sub {
my $port1 = shift;
...
});
my $server2 = Test::TCP->new(code => sub {
my $port2 = shift;
...
});
# your client code here.
...
=item How do you test server program written in other languages like memcached?
You can use C<exec()> in child process.
use strict;
use warnings;
use utf8;
use Test::More;
use Test::TCP 1.08;
use File::Which;
my $bin = scalar which 'memcached';
plan skip_all => 'memcached binary is not found' unless defined $bin;
my $memcached = Test::TCP->new(
code => sub {
my $port = shift;
exec $bin, '-p' => $port;
die "cannot execute $bin: $!";
},
);
use Cache::Memcached;
my $memd = Cache::Memcached->new({servers => ['127.0.0.1:' . $memcached->port]});
$memd->set(foo => 'bar');
is $memd->get('foo'), 'bar';
done_testing;
=item How do I use address other than "127.0.0.1" for testing?
You can use the C<< host >> parameter to specify the bind address.
# let the server bind to "0.0.0.0" for testing
test_tcp(
client => sub {
...
},
server => sub {
...
},
host => '0.0.0.0',
);
=item How should I write IPv6 tests?
You should use the `Net::EmptyPort::can_bind` function to check if the program can bind to the loopback address of IPv6, as well as the `host` parameter of the `test_tcp` function to specify the same address as the bind address.
use Net::EmptyPort qw(can_bind);
plan skip_all => "IPv6 not available"
unless can_bind('::1');
test_tcp(
client => sub {
...
},
server => sub {
...
},
host => '::1',
);
=back
=head1 AUTHOR
Tokuhiro Matsuno E<lt>tokuhirom@gmail.comE<gt>
=head1 THANKS TO
kazuhooku
dragon3
charsbar
Tatsuhiko Miyagawa
lestrrat
=head1 SEE ALSO
=head1 LICENSE
This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.
=cut
|