LightyTest.pm 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. #! /usr/bin/perl -w
  2. package LightyTest;
  3. use strict;
  4. use IO::Socket;
  5. use Test::More;
  6. use Socket;
  7. use Cwd 'abs_path';
  8. use POSIX qw(:sys_wait_h dup2);
  9. use Errno qw(EADDRINUSE);
  10. sub mtime {
  11. my $file = shift;
  12. my @stat = stat $file;
  13. return @stat ? $stat[9] : 0;
  14. }
  15. sub new {
  16. my $class = shift;
  17. my $self = {};
  18. my $lpath;
  19. $self->{CONFIGFILE} = 'lighttpd.conf';
  20. $lpath = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'} : '..');
  21. $self->{BASEDIR} = abs_path($lpath);
  22. $lpath = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'}."/tests/" : '.');
  23. $self->{TESTDIR} = abs_path($lpath);
  24. $lpath = (defined $ENV{'srcdir'} ? $ENV{'srcdir'} : '.');
  25. $self->{SRCDIR} = abs_path($lpath);
  26. if (mtime($self->{BASEDIR}.'/src/lighttpd') > mtime($self->{BASEDIR}.'/build/lighttpd')) {
  27. $self->{BINDIR} = $self->{BASEDIR}.'/src';
  28. if (mtime($self->{BASEDIR}.'/src/.libs')) {
  29. $self->{MODULES_PATH} = $self->{BASEDIR}.'/src/.libs';
  30. } else {
  31. $self->{MODULES_PATH} = $self->{BASEDIR}.'/src';
  32. }
  33. } else {
  34. $self->{BINDIR} = $self->{BASEDIR}.'/build';
  35. $self->{MODULES_PATH} = $self->{BASEDIR}.'/build';
  36. }
  37. $self->{LIGHTTPD_PATH} = $self->{BINDIR}.'/lighttpd';
  38. $self->{PORT} = 2048;
  39. my ($name, $aliases, $addrtype, $net) = gethostbyaddr(inet_aton("127.0.0.1"), AF_INET);
  40. $self->{HOSTNAME} = $name;
  41. bless($self, $class);
  42. return $self;
  43. }
  44. sub listening_on {
  45. my $self = shift;
  46. my $port = shift;
  47. my $remote =
  48. IO::Socket::INET->new(Proto => "tcp",
  49. PeerAddr => "127.0.0.1",
  50. PeerPort => $port) or return 0;
  51. close $remote;
  52. return 1;
  53. }
  54. sub stop_proc {
  55. my $self = shift;
  56. my $pid = $self->{LIGHTTPD_PID};
  57. if (defined $pid && $pid != -1) {
  58. kill('TERM', $pid) or return -1;
  59. return -1 if ($pid != waitpid($pid, 0));
  60. } else {
  61. diag("\nProcess not started, nothing to stop");
  62. return -1;
  63. }
  64. return 0;
  65. }
  66. sub wait_for_port_with_proc {
  67. my $self = shift;
  68. my $port = shift;
  69. my $child = shift;
  70. my $timeout = 5*10; # 5 secs, select waits 0.1 s
  71. while (0 == $self->listening_on($port)) {
  72. select(undef, undef, undef, 0.1);
  73. $timeout--;
  74. # the process is gone, we failed
  75. if (0 != waitpid($child, WNOHANG)) {
  76. return -1;
  77. }
  78. if (0 >= $timeout) {
  79. diag("\nTimeout while trying to connect; killing child");
  80. kill('TERM', $child);
  81. return -1;
  82. }
  83. }
  84. return 0;
  85. }
  86. sub start_proc {
  87. my $self = shift;
  88. # kill old proc if necessary
  89. #$self->stop_proc;
  90. if ($self->listening_on($self->{PORT})) {
  91. diag("\nPort ".$self->{PORT}." already in use");
  92. return -1;
  93. }
  94. # pre-process configfile if necessary
  95. #
  96. $ENV{'SRCDIR'} = $self->{BASEDIR}.'/tests';
  97. $ENV{'PORT'} = $self->{PORT};
  98. my @cmdline = ($self->{LIGHTTPD_PATH}, "-D", "-f", $self->{SRCDIR}."/".$self->{CONFIGFILE}, "-m", $self->{MODULES_PATH});
  99. if (defined $ENV{"TRACEME"} && $ENV{"TRACEME"} eq 'strace') {
  100. @cmdline = (qw(strace -tt -s 512 -o strace), @cmdline);
  101. } elsif (defined $ENV{"TRACEME"} && $ENV{"TRACEME"} eq 'truss') {
  102. @cmdline = (qw(truss -a -l -w all -v all -o strace), @cmdline);
  103. } elsif (defined $ENV{"TRACEME"} && $ENV{"TRACEME"} eq 'gdb') {
  104. @cmdline = ('gdb', '--batch', '--ex', 'run', '--ex', 'bt full', '--args', @cmdline);
  105. } elsif (defined $ENV{"TRACEME"} && $ENV{"TRACEME"} eq 'valgrind') {
  106. @cmdline = (qw(valgrind --tool=memcheck --show-reachable=yes --leak-check=yes --log-file=valgrind), @cmdline);
  107. }
  108. # diag("\nstarting lighttpd at :".$self->{PORT}.", cmdline: ".@cmdline );
  109. my $child = fork();
  110. if (not defined $child) {
  111. diag("\nFork failed");
  112. return -1;
  113. }
  114. if ($child == 0) {
  115. exec @cmdline or die($?);
  116. }
  117. if (0 != $self->wait_for_port_with_proc($self->{PORT}, $child)) {
  118. diag(sprintf('\nThe process %i is not up', $child));
  119. return -1;
  120. }
  121. $self->{LIGHTTPD_PID} = $child;
  122. 0;
  123. }
  124. sub handle_http {
  125. my $self = shift;
  126. my $t = shift;
  127. my $EOL = "\015\012";
  128. my $BLANK = $EOL x 2;
  129. my $host = "127.0.0.1";
  130. my @request = $t->{REQUEST};
  131. my @response = $t->{RESPONSE};
  132. my $slow = defined $t->{SLOWREQUEST};
  133. my $is_debug = $ENV{"TRACE_HTTP"};
  134. my $remote =
  135. IO::Socket::INET->new(Proto => "tcp",
  136. PeerAddr => $host,
  137. PeerPort => $self->{PORT});
  138. if (not defined $remote) {
  139. diag("\nconnect failed: $!");
  140. return -1;
  141. }
  142. $remote->autoflush(1);
  143. if (!$slow) {
  144. diag("\nsending request header to ".$host.":".$self->{PORT}) if $is_debug;
  145. foreach(@request) {
  146. # pipeline requests
  147. s/\r//g;
  148. s/\n/$EOL/g;
  149. print $remote $_.$BLANK;
  150. diag("\n<< ".$_) if $is_debug;
  151. }
  152. shutdown($remote, 1); # I've stopped writing data
  153. } else {
  154. diag("\nsending request header to ".$host.":".$self->{PORT}) if $is_debug;
  155. foreach(@request) {
  156. # pipeline requests
  157. chomp;
  158. s/\r//g;
  159. s/\n/$EOL/g;
  160. print $remote $_;
  161. diag("<< ".$_."\n") if $is_debug;
  162. select(undef, undef, undef, 0.1);
  163. print $remote "\015";
  164. select(undef, undef, undef, 0.1);
  165. print $remote "\012";
  166. select(undef, undef, undef, 0.1);
  167. print $remote "\015";
  168. select(undef, undef, undef, 0.1);
  169. print $remote "\012";
  170. select(undef, undef, undef, 0.1);
  171. }
  172. }
  173. diag("\n... done") if $is_debug;
  174. my $lines = "";
  175. diag("\nreceiving response") if $is_debug;
  176. # read everything
  177. while(<$remote>) {
  178. $lines .= $_;
  179. diag(">> ".$_) if $is_debug;
  180. }
  181. diag("\n... done") if $is_debug;
  182. close $remote;
  183. my $full_response = $lines;
  184. my $href;
  185. foreach $href ( @{ $t->{RESPONSE} }) {
  186. # first line is always response header
  187. my %resp_hdr;
  188. my $resp_body;
  189. my $resp_line;
  190. my $conditions = $_;
  191. for (my $ln = 0; defined $lines; $ln++) {
  192. (my $line, $lines) = split($EOL, $lines, 2);
  193. # header finished
  194. last if(!defined $line or length($line) == 0);
  195. if ($ln == 0) {
  196. # response header
  197. $resp_line = $line;
  198. } else {
  199. # response vars
  200. if ($line =~ /^([^:]+):\s*(.+)$/) {
  201. (my $h = $1) =~ tr/[A-Z]/[a-z]/;
  202. if (defined $resp_hdr{$h}) {
  203. # diag(sprintf("\nheader '%s' is duplicated: '%s' and '%s'\n",
  204. # $h, $resp_hdr{$h}, $2));
  205. $resp_hdr{$h} .= ', '.$2;
  206. } else {
  207. $resp_hdr{$h} = $2;
  208. }
  209. } else {
  210. diag(sprintf("\nunexpected line '%s'", $line));
  211. return -1;
  212. }
  213. }
  214. }
  215. if (not defined($resp_line)) {
  216. diag(sprintf("\nempty response"));
  217. return -1;
  218. }
  219. $t->{etag} = $resp_hdr{'etag'};
  220. $t->{date} = $resp_hdr{'date'};
  221. # check length
  222. if (defined $resp_hdr{"content-length"}) {
  223. $resp_body = substr($lines, 0, $resp_hdr{"content-length"});
  224. if (length($lines) < $resp_hdr{"content-length"}) {
  225. $lines = "";
  226. } else {
  227. $lines = substr($lines, $resp_hdr{"content-length"});
  228. }
  229. undef $lines if (length($lines) == 0);
  230. } else {
  231. $resp_body = $lines;
  232. undef $lines;
  233. }
  234. # check conditions
  235. if ($resp_line =~ /^(HTTP\/1\.[01]) ([0-9]{3}) .+$/) {
  236. if ($href->{'HTTP-Protocol'} ne $1) {
  237. diag(sprintf("\nproto failed: expected '%s', got '%s'", $href->{'HTTP-Protocol'}, $1));
  238. return -1;
  239. }
  240. if ($href->{'HTTP-Status'} ne $2) {
  241. diag(sprintf("\nstatus failed: expected '%s', got '%s'", $href->{'HTTP-Status'}, $2));
  242. return -1;
  243. }
  244. } else {
  245. diag(sprintf("\nunexpected resp_line '%s'", $resp_line));
  246. return -1;
  247. }
  248. if (defined $href->{'HTTP-Content'}) {
  249. $resp_body = "" unless defined $resp_body;
  250. if ($href->{'HTTP-Content'} ne $resp_body) {
  251. diag(sprintf("\nbody failed: expected '%s', got '%s'", $href->{'HTTP-Content'}, $resp_body));
  252. return -1;
  253. }
  254. }
  255. if (defined $href->{'-HTTP-Content'}) {
  256. if (defined $resp_body && $resp_body ne '') {
  257. diag(sprintf("\nbody failed: expected empty body, got '%s'", $resp_body));
  258. return -1;
  259. }
  260. }
  261. foreach (keys %{ $href }) {
  262. next if $_ eq 'HTTP-Protocol';
  263. next if $_ eq 'HTTP-Status';
  264. next if $_ eq 'HTTP-Content';
  265. next if $_ eq '-HTTP-Content';
  266. (my $k = $_) =~ tr/[A-Z]/[a-z]/;
  267. my $verify_value = 1;
  268. my $key_inverted = 0;
  269. if (substr($k, 0, 1) eq '+') {
  270. $k = substr($k, 1);
  271. $verify_value = 0;
  272. } elsif (substr($k, 0, 1) eq '-') {
  273. ## the key should NOT exist
  274. $k = substr($k, 1);
  275. $key_inverted = 1;
  276. $verify_value = 0; ## skip the value check
  277. }
  278. if ($key_inverted) {
  279. if (defined $resp_hdr{$k}) {
  280. diag(sprintf("\nheader '%s' MUST not be set", $k));
  281. return -1;
  282. }
  283. } else {
  284. if (not defined $resp_hdr{$k}) {
  285. diag(sprintf("\nrequired header '%s' is missing", $k));
  286. return -1;
  287. }
  288. }
  289. if ($verify_value) {
  290. if ($href->{$_} =~ /^\/(.+)\/$/) {
  291. if ($resp_hdr{$k} !~ /$1/) {
  292. diag(sprintf("\nresponse-header failed: expected '%s', got '%s', regex: %s",
  293. $href->{$_}, $resp_hdr{$k}, $1));
  294. return -1;
  295. }
  296. } elsif ($href->{$_} ne $resp_hdr{$k}) {
  297. diag(sprintf("\nresponse-header failed: expected '%s', got '%s'",
  298. $href->{$_}, $resp_hdr{$k}));
  299. return -1;
  300. }
  301. }
  302. }
  303. }
  304. # we should have sucked up everything
  305. if (defined $lines) {
  306. diag(sprintf("\nunexpected lines '%s'", $lines));
  307. return -1;
  308. }
  309. return 0;
  310. }
  311. sub spawnfcgi {
  312. my ($self, $binary, $port) = @_;
  313. my $child = fork();
  314. if (not defined $child) {
  315. diag("\nCouldn't fork");
  316. return -1;
  317. }
  318. if ($child == 0) {
  319. my $iaddr = inet_aton('localhost') || die "no host: localhost";
  320. my $proto = getprotobyname('tcp');
  321. socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
  322. setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
  323. bind(SOCK, sockaddr_in($port, $iaddr)) || die "bind: $!";
  324. listen(SOCK, 1024) || die "listen: $!";
  325. dup2(fileno(SOCK), 0) || die "dup2: $!";
  326. exec { $binary } ($binary) or die($?);
  327. } else {
  328. if (0 != $self->wait_for_port_with_proc($port, $child)) {
  329. diag(sprintf("\nThe process %i is not up (port %i, %s)", $child, $port, $binary));
  330. return -1;
  331. }
  332. return $child;
  333. }
  334. }
  335. sub endspawnfcgi {
  336. my ($self, $pid) = @_;
  337. return -1 if (-1 == $pid);
  338. kill(2, $pid);
  339. waitpid($pid, 0);
  340. return 0;
  341. }
  342. 1;