Debian Codegraph @

t/01live.t

raw ◀ ▶
Source reconstructed at patch state 2 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 # these functions may look excessive, but hopefully will be very useful
91 # in identifying common problems
92 sub fetch {
93 my $family = shift;
94 my $message = join "", map { "$_\015\012" } @_;
95 fetch_interactive($family, sub {
96 (send SOCK, $message, 0) || die "send: $!";
97 });
98 }
99
100 # more subtle version of fetch, that calls $send_callback after connect SOCK succeeds
101 sub fetch_interactive ($$) {
102 my ($family, $send_callback) = @_;
103 my $hostname = get_localhost($family);
104 my $port = $PORT;
105 my $timeout = 5;
106 my $response;
107 my $proto = getprotobyname('tcp') || die "getprotobyname: $!";
108 my $socktype = SOCK_STREAM;
109
110 eval {
111 local $SIG{ALRM} = sub { die "early exit - SIGALRM caught" };
112 alarm $timeout*2; #twice longer than timeout used later by select()
113
114 my $paddr;
115 my ($err, @res) = Socket::getaddrinfo($hostname, $port, { family => $family,
116 socktype => $socktype,
117 protocol => $proto });
118 die "getaddrinfo: $err operating on [$hostname] [$port] [$family] [$socktype] [$proto]"
119 if ($err);
120 while ($a = shift(@res)) {
121 next unless ($family == $a->{'family'});
122 next unless ($proto == $a->{'protocol'});
123 next unless ($socktype == $a->{'socktype'});
124
125 $paddr = $a->{'addr'};
126 last
127 }
128 socket(SOCK, $family, $socktype, $proto) || die "socket: $!";
129 connect(SOCK, $paddr) || die "connect: $!";
130 $send_callback->();
131
132 my $rvec = '';
133 vec($rvec, fileno(SOCK), 1) = 1;
134 die "vec(): $!" unless $rvec;
135
136 $response = '';
137 for (;;) {
138 my $r = select($rvec, undef, undef, $timeout);
139 die "select: timeout - no data to read from server" unless ($r > 0);
140 my $l = sysread(SOCK, $response, 1024, length($response));
141 die "sysread: $!" unless defined($l);
142 last if ($l == 0);
143 }
144 $response =~ s/\015\012/\n/g;
145 (close SOCK) || die "close(): $!";
146 alarm 0;
147 };
148 if ($@) {
149 return "[ERROR] $@";
150 }
151 else {
152 return $response;
153 }
154 }
155
156 sub run_server_tests {
157 my $class = shift;
158 my $fam = shift;
159 my $s = $class->new($PORT, $fam);
160 is($s->family(), $fam, 'constructor set family properly');
161 is($s->port(),$PORT,"Constructor set port correctly");
162
163 my $localhost = get_localhost($fam);
164 $s->host($localhost); # otherwise we bind to * which doesn't work on all systems
165
166 my $pid=$s->background();
167 select(undef,undef,undef,0.2); # wait a sec
168
169 like($pid, '/^-?\d+$/', 'pid is numeric');
170
171 SKIP: {
172 skip "No localhost defined for $fam", 1 unless defined $localhost;
173 my $content=fetch($fam, "GET / HTTP/1.1", "");
174
175 like($content, '/Congratulations/', "Returns a page");
176 }
177 push @pids, $pid;
178 }
179
180 {
181 my %localhost;
182 sub get_localhost {
183 my $family = shift;
184
185 return $localhost{$family} if $localhost{$family};
186
187 if ($family == AF_INET) {
188 $localhost{$family} = gethostbyaddr(INADDR_LOOPBACK,$family);
189 } else {
190 $localhost{$family} = gethostbyaddr(Socket::IN6ADDR_LOOPBACK,$family);
191 }
192 return $localhost{$family};
193
194 }
195 }