Debian Codegraph @

t/01live.t

raw ◀ ▶
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 => 36;
25 } else {
26 diag("Skipping IPv6");
•27 plan tests => 18;
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 $SIG{WINCH} = sub { note "SIGWINCH received"; }; # See EINTR test, below
57
58 for my $fam ( AF_INET, AF_INET6 ) {
59 next if ($fam == AF_INET6 && not $RUN_IPV6);
60 my $s=HTTP::Server::Simple::CGI->new($PORT, $fam);
61 is($fam, $s->family(), 'family OK');
62 $s->host(get_localhost($fam));
63 my $pid=$s->background();
64 diag("started server PID='$pid'") if ($ENV{'TEST_VERBOSE'});
65 like($pid, '/^-?\d+$/', 'pid is numeric');
66 select(undef,undef,undef,0.2); # wait a sec
67 SKIP: {
•68 skip "No localhost for $fam", 5 unless defined $s->host;
69 my $content=fetch($fam, "GET / HTTP/1.1", "");
70 like($content, '/Congratulations/', "Returns a page");
71
72 eval {
73 like(fetch($fam, "GET a bogus request"),
74 '/bad request/i',
75 "knows what a request isn't");
76 };
77 fail("got exception in client: $@") if $@;
78
79 like(fetch($fam, "GET / HTTP/1.1", ""), '/Congratulations/',
80 "HTTP/1.1 request");
81
82 like(fetch($fam, "GET /"), '/Congratulations/',
83 "HTTP/0.9 request");
•84
85 like(fetch_interactive($fam, sub {
86 # Arrange for the server to get EINTR, by sending part of the request,
87 # and then a SIGWINCH. (We choose SIGWINCH because it's a harmless signal
88 # that is normally ignored, but which an application might want to handle.)
89 # Tests for regression of https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=1143215
90 (send SOCK, "GET", 0) or die "send G: $!";
91 sleep 1;
92 kill('WINCH',$pid) or die "send SIGWINCH: $!";
93 sleep 1;
94 (send SOCK, " / HTTP/1.1\015\012\015\012", 0) or die "send ET etc.: $!";
95 }), '/Congratulations/', "HTTP/1.1 request with EINTR");
96 }
97
98 is(kill(9,$pid),1,'Signaled 1 process successfully');
99 }
100
101 is( kill( 9, $_ ), 1, "Killed PID: $_" ) for @pids;
102
•103 # these functions may look excessive, but hopefully will be very useful
104 # in identifying common problems
105 sub fetch {
106 my $family = shift;
•107 my $message = join "", map { "$_\015\012" } @_;
108 fetch_interactive($family, sub {
109 (send SOCK, $message, 0) || die "send: $!";
110 });
111 }
112
113 # more subtle version of fetch, that calls $send_callback after connect SOCK succeeds
114 sub fetch_interactive ($$) {
115 my ($family, $send_callback) = @_;
116 my $hostname = get_localhost($family);
117 my $port = $PORT;
118 my $timeout = 5;
119 my $response;
120 my $proto = getprotobyname('tcp') || die "getprotobyname: $!";
121 my $socktype = SOCK_STREAM;
122
123 eval {
124 local $SIG{ALRM} = sub { die "early exit - SIGALRM caught" };
125 alarm $timeout*2; #twice longer than timeout used later by select()
126
127 my $paddr;
128 my ($err, @res) = Socket::getaddrinfo($hostname, $port, { family => $family,
129 socktype => $socktype,
130 protocol => $proto });
131 die "getaddrinfo: $err operating on [$hostname] [$port] [$family] [$socktype] [$proto]"
132 if ($err);
133 while ($a = shift(@res)) {
134 next unless ($family == $a->{'family'});
135 next unless ($proto == $a->{'protocol'});
136 next unless ($socktype == $a->{'socktype'});
137
138 $paddr = $a->{'addr'};
139 last
140 }
141 socket(SOCK, $family, $socktype, $proto) || die "socket: $!";
142 connect(SOCK, $paddr) || die "connect: $!";
•143 $send_callback->();
144
145 my $rvec = '';
146 vec($rvec, fileno(SOCK), 1) = 1;
147 die "vec(): $!" unless $rvec;
148
149 $response = '';
150 for (;;) {
151 my $r = select($rvec, undef, undef, $timeout);
152 die "select: timeout - no data to read from server" unless ($r > 0);
153 my $l = sysread(SOCK, $response, 1024, length($response));
154 die "sysread: $!" unless defined($l);
155 last if ($l == 0);
156 }
157 $response =~ s/\015\012/\n/g;
158 (close SOCK) || die "close(): $!";
159 alarm 0;
160 };
161 if ($@) {
162 return "[ERROR] $@";
163 }
164 else {
165 return $response;
166 }
167 }
168
169 sub run_server_tests {
170 my $class = shift;
171 my $fam = shift;
172 my $s = $class->new($PORT, $fam);
173 is($s->family(), $fam, 'constructor set family properly');
174 is($s->port(),$PORT,"Constructor set port correctly");
175
176 my $localhost = get_localhost($fam);
177 $s->host($localhost); # otherwise we bind to * which doesn't work on all systems
178
179 my $pid=$s->background();
180 select(undef,undef,undef,0.2); # wait a sec
181
182 like($pid, '/^-?\d+$/', 'pid is numeric');
183
184 SKIP: {
185 skip "No localhost defined for $fam", 1 unless defined $localhost;
186 my $content=fetch($fam, "GET / HTTP/1.1", "");
187
188 like($content, '/Congratulations/', "Returns a page");
189 }
190 push @pids, $pid;
191 }
192
193 {
194 my %localhost;
195 sub get_localhost {
196 my $family = shift;
197
198 return $localhost{$family} if $localhost{$family};
199
200 if ($family == AF_INET) {
201 $localhost{$family} = gethostbyaddr(INADDR_LOOPBACK,$family);
202 } else {
203 $localhost{$family} = gethostbyaddr(Socket::IN6ADDR_LOOPBACK,$family);
204 }
205 return $localhost{$family};
206
207 }
208 }