Debian Codegraph @

t/01live.t

raw ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 # -*- perl -*-
2
3 use Socket;
4 use Test::More;
5 use strict;
6
7 # This script assumes that `localhost' will resolve to a local IP
8 # address that may be bound to,
9
10 my $PORT = 40000 + int(rand(10000));
11 my $RUN_IPV6 = eval {
12 my $ipv6_host = get_localhost(AF_INET6);
13 socket my $sockh, Socket::PF_INET6(), SOCK_STREAM, 0 or die "Cannot socket(PF_INET6) - $!";
14 my ($err, @res) = Socket::getaddrinfo($ipv6_host, $PORT, { family => AF_INET6, socktype => SOCK_STREAM } );
15 die $err if $err;
16 for my $r (@res) {
17 next unless ($r->{'family'} == AF_INET6);
18 bind $sockh, $r->{'addr'} or die "Cannot bind - $!";
19 last;
20 }
21 return 1;
22 };
23 if ( $RUN_IPV6) {
24 plan tests => 34;
25 } else {
26 diag("Skipping IPv6");
27 plan tests => 17;
28 }
29 use HTTP::Server::Simple;
30
31 package SlowServer;
32 # This test class just waits a while before it starts
33 # accepting connections. This makes sure that CPAN #28122 is fixed:
34 # background() shouldn't return prematurely.
35
36 use base qw(HTTP::Server::Simple::CGI);
37 sub setup_listener {
38 my $self = shift;
39 $self->SUPER::setup_listener();
40 sleep 2;
41 }
42 1;
43 package main;
44
45 my $DEBUG = 1 if @ARGV;
46
47 my @pids = ();
48 my @classes = (qw(HTTP::Server::Simple SlowServer));
49 for my $class (@classes) {
50 run_server_tests($class, AF_INET);
51 $PORT++;
52 run_server_tests($class, AF_INET6) if $RUN_IPV6;
53 $PORT++; # don't reuse the port incase your bogus os doesn't release in time
54 }
55
56
57 for my $fam ( AF_INET, AF_INET6 ) {
58 next if ($fam == AF_INET6 && not $RUN_IPV6);
59 my $s=HTTP::Server::Simple::CGI->new($PORT, $fam);
60 is($fam, $s->family(), 'family OK');
61 $s->host(get_localhost($fam));
62 my $pid=$s->background();
63 diag("started server PID='$pid'") if ($ENV{'TEST_VERBOSE'});
64 like($pid, '/^-?\d+$/', 'pid is numeric');
65 select(undef,undef,undef,0.2); # wait a sec
66 SKIP: {
67 skip "No localhost for $fam", 4 unless defined $s->host;
68 my $content=fetch($fam, "GET / HTTP/1.1", "");
69 like($content, '/Congratulations/', "Returns a page");
70
71 eval {
72 like(fetch($fam, "GET a bogus request"),
73 '/bad request/i',
74 "knows what a request isn't");
75 };
76 fail("got exception in client: $@") if $@;
77
78 like(fetch($fam, "GET / HTTP/1.1", ""), '/Congratulations/',
79 "HTTP/1.1 request");
80
81 like(fetch($fam, "GET /"), '/Congratulations/',
82 "HTTP/0.9 request");
83 }
84
85 is(kill(9,$pid),1,'Signaled 1 process successfully');
86 }
87
88 is( kill( 9, $_ ), 1, "Killed PID: $_" ) for @pids;
89
90 # this function may look excessive, but hopefully will be very useful
91 # in identifying common problems
92 sub fetch {
93 my $family = shift;
94 my $hostname = get_localhost($family);
95 my $port = $PORT;
96 my $message = join "", map { "$_\015\012" } @_;
97 my $timeout = 5;
98 my $response;
99 my $proto = getprotobyname('tcp') || die "getprotobyname: $!";
100 my $socktype = SOCK_STREAM;
101
102 eval {
103 local $SIG{ALRM} = sub { die "early exit - SIGALRM caught" };
104 alarm $timeout*2; #twice longer than timeout used later by select()
105
106 my $paddr;
107 my ($err, @res) = Socket::getaddrinfo($hostname, $port, { family => $family,
108 socktype => $socktype,
109 protocol => $proto });
110 die "getaddrinfo: $err operating on [$hostname] [$port] [$family] [$socktype] [$proto]"
111 if ($err);
112 while ($a = shift(@res)) {
113 next unless ($family == $a->{'family'});
114 next unless ($proto == $a->{'protocol'});
115 next unless ($socktype == $a->{'socktype'});
116
117 $paddr = $a->{'addr'};
118 last
119 }
120 socket(SOCK, $family, $socktype, $proto) || die "socket: $!";
121 connect(SOCK, $paddr) || die "connect: $!";
122 (send SOCK, $message, 0) || die "send: $!";
123
124 my $rvec = '';
125 vec($rvec, fileno(SOCK), 1) = 1;
126 die "vec(): $!" unless $rvec;
127
128 $response = '';
129 for (;;) {
130 my $r = select($rvec, undef, undef, $timeout);
131 die "select: timeout - no data to read from server" unless ($r > 0);
132 my $l = sysread(SOCK, $response, 1024, length($response));
133 die "sysread: $!" unless defined($l);
134 last if ($l == 0);
135 }
136 $response =~ s/\015\012/\n/g;
137 (close SOCK) || die "close(): $!";
138 alarm 0;
139 };
140 if ($@) {
141 return "[ERROR] $@";
142 }
143 else {
144 return $response;
145 }
146 }
147
148 sub run_server_tests {
149 my $class = shift;
150 my $fam = shift;
151 my $s = $class->new($PORT, $fam);
152 is($s->family(), $fam, 'constructor set family properly');
153 is($s->port(),$PORT,"Constructor set port correctly");
154
155 my $localhost = get_localhost($fam);
156 $s->host($localhost); # otherwise we bind to * which doesn't work on all systems
157
158 my $pid=$s->background();
159 select(undef,undef,undef,0.2); # wait a sec
160
161 like($pid, '/^-?\d+$/', 'pid is numeric');
162
163 SKIP: {
164 skip "No localhost defined for $fam", 1 unless defined $localhost;
165 my $content=fetch($fam, "GET / HTTP/1.1", "");
166
167 like($content, '/Congratulations/', "Returns a page");
168 }
169 push @pids, $pid;
170 }
171
172 {
173 my %localhost;
174 sub get_localhost {
175 my $family = shift;
176
177 return $localhost{$family} if $localhost{$family};
178
179 if ($family == AF_INET) {
180 $localhost{$family} = gethostbyaddr(INADDR_LOOPBACK,$family);
181 } else {
182 $localhost{$family} = gethostbyaddr(Socket::IN6ADDR_LOOPBACK,$family);
183 }
184 return $localhost{$family};
185
186 }
187 }