Debian Codegraph @

t/01live.t

raw ◀ ▶
Source reconstructed at patch state 0 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 run_server_tests($class, AF_INET6) if $RUN_IPV6;
52 $PORT++; # don't reuse the port incase your bogus os doesn't release in time
53 }
54
55
56 for my $fam ( AF_INET, AF_INET6 ) {
57 next if ($fam == AF_INET6 && not $RUN_IPV6);
58 my $s=HTTP::Server::Simple::CGI->new($PORT, $fam);
59 is($fam, $s->family(), 'family OK');
60 $s->host(get_localhost($fam));
61 my $pid=$s->background();
62 diag("started server PID='$pid'") if ($ENV{'TEST_VERBOSE'});
63 like($pid, '/^-?\d+$/', 'pid is numeric');
64 select(undef,undef,undef,0.2); # wait a sec
65 SKIP: {
66 skip "No localhost for $fam", 4 unless defined $s->host;
67 my $content=fetch($fam, "GET / HTTP/1.1", "");
68 like($content, '/Congratulations/', "Returns a page");
69
70 eval {
71 like(fetch($fam, "GET a bogus request"),
72 '/bad request/i',
73 "knows what a request isn't");
74 };
75 fail("got exception in client: $@") if $@;
76
77 like(fetch($fam, "GET / HTTP/1.1", ""), '/Congratulations/',
78 "HTTP/1.1 request");
79
80 like(fetch($fam, "GET /"), '/Congratulations/',
81 "HTTP/0.9 request");
82 }
83
84 is(kill(9,$pid),1,'Signaled 1 process successfully');
85 }
86
87 is( kill( 9, $_ ), 1, "Killed PID: $_" ) for @pids;
88
89 # this function may look excessive, but hopefully will be very useful
90 # in identifying common problems
91 sub fetch {
92 my $family = shift;
93 my $hostname = get_localhost($family);
94 my $port = $PORT;
95 my $message = join "", map { "$_\015\012" } @_;
96 my $timeout = 5;
97 my $response;
98 my $proto = getprotobyname('tcp') || die "getprotobyname: $!";
99 my $socktype = SOCK_STREAM;
100
101 eval {
102 local $SIG{ALRM} = sub { die "early exit - SIGALRM caught" };
103 alarm $timeout*2; #twice longer than timeout used later by select()
104
105 my $paddr;
106 my ($err, @res) = Socket::getaddrinfo($hostname, $port, { family => $family,
107 socktype => $socktype,
108 protocol => $proto });
109 die "getaddrinfo: $err operating on [$hostname] [$port] [$family] [$socktype] [$proto]"
110 if ($err);
111 while ($a = shift(@res)) {
112 next unless ($family == $a->{'family'});
113 next unless ($proto == $a->{'protocol'});
114 next unless ($socktype == $a->{'socktype'});
115
116 $paddr = $a->{'addr'};
117 last
118 }
119 socket(SOCK, $family, $socktype, $proto) || die "socket: $!";
120 connect(SOCK, $paddr) || die "connect: $!";
121 (send SOCK, $message, 0) || die "send: $!";
122
123 my $rvec = '';
124 vec($rvec, fileno(SOCK), 1) = 1;
125 die "vec(): $!" unless $rvec;
126
127 $response = '';
128 for (;;) {
129 my $r = select($rvec, undef, undef, $timeout);
130 die "select: timeout - no data to read from server" unless ($r > 0);
131 my $l = sysread(SOCK, $response, 1024, length($response));
132 die "sysread: $!" unless defined($l);
133 last if ($l == 0);
134 }
135 $response =~ s/\015\012/\n/g;
136 (close SOCK) || die "close(): $!";
137 alarm 0;
138 };
139 if ($@) {
140 return "[ERROR] $@";
141 }
142 else {
143 return $response;
144 }
145 }
146
147 sub run_server_tests {
148 my $class = shift;
149 my $fam = shift;
150 my $s = $class->new($PORT, $fam);
151 is($s->family(), $fam, 'constructor set family properly');
152 is($s->port(),$PORT,"Constructor set port correctly");
153
154 my $localhost = get_localhost($fam);
155 $s->host($localhost); # otherwise we bind to * which doesn't work on all systems
156
157 my $pid=$s->background();
158 select(undef,undef,undef,0.2); # wait a sec
159
160 like($pid, '/^-?\d+$/', 'pid is numeric');
161
162 SKIP: {
163 skip "No localhost defined for $fam", 1 unless defined $localhost;
164 my $content=fetch($fam, "GET / HTTP/1.1", "");
165
166 like($content, '/Congratulations/', "Returns a page");
167 }
168 push @pids, $pid;
169 }
170
171 {
172 my %localhost;
173 sub get_localhost {
174 my $family = shift;
175
176 return $localhost{$family} if $localhost{$family};
177
178 if ($family == AF_INET) {
179 $localhost{$family} = gethostbyaddr(INADDR_LOOPBACK,$family);
180 } else {
181 $localhost{$family} = gethostbyaddr(Socket::IN6ADDR_LOOPBACK,$family);
182 }
183 return $localhost{$family};
184
185 }
186 }