LightyTest.pm 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537
  1. package LightyTest;
  2. use strict;
  3. use IO::Socket ();
  4. use Test::More; # diag()
  5. use Socket;
  6. use Cwd 'abs_path';
  7. sub find_program {
  8. my @DEFAULT_PATHS = ('/usr/bin/', '/usr/local/bin/');
  9. my ($envname, $program) = @_;
  10. my $location;
  11. if (defined $ENV{$envname}) {
  12. $location = $ENV{$envname};
  13. } else {
  14. $location = `which "$program" 2>/dev/null`;
  15. chomp $location;
  16. if (! -x $location) {
  17. for my $path (@DEFAULT_PATHS) {
  18. $location = $path . $program;
  19. last if -x $location;
  20. }
  21. }
  22. }
  23. if (-x $location) {
  24. $ENV{$envname} = $location;
  25. return 1;
  26. } else {
  27. delete $ENV{$envname};
  28. return 0;
  29. }
  30. }
  31. BEGIN {
  32. our $HAVE_PERL = find_program('PERL', 'perl');
  33. if (!$HAVE_PERL) {
  34. die "Couldn't find path to perl, but it obviously seems to be running";
  35. }
  36. }
  37. sub mtime {
  38. my $file = shift;
  39. my @stat = stat $file;
  40. return @stat ? $stat[9] : 0;
  41. }
  42. sub new {
  43. my $class = shift;
  44. my $self = {};
  45. my $lpath;
  46. $self->{CONFIGFILE} = 'lighttpd.conf';
  47. $lpath = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'} : '..');
  48. $self->{BASEDIR} = abs_path($lpath);
  49. $lpath = (defined $ENV{'top_builddir'} ? $ENV{'top_builddir'}."/tests/" : '.');
  50. $self->{TESTDIR} = abs_path($lpath);
  51. $lpath = (defined $ENV{'srcdir'} ? $ENV{'srcdir'} : '.');
  52. $self->{SRCDIR} = abs_path($lpath);
  53. if (mtime($self->{BASEDIR}.'/src/lighttpd') > mtime($self->{BASEDIR}.'/build/lighttpd')) {
  54. $self->{BINDIR} = $self->{BASEDIR}.'/src';
  55. if (mtime($self->{BASEDIR}.'/src/.libs')) {
  56. $self->{MODULES_PATH} = $self->{BASEDIR}.'/src/.libs';
  57. } else {
  58. $self->{MODULES_PATH} = $self->{BASEDIR}.'/src';
  59. }
  60. } else {
  61. $self->{BINDIR} = $self->{BASEDIR}.'/build';
  62. $self->{MODULES_PATH} = $self->{BASEDIR}.'/build';
  63. }
  64. $self->{LIGHTTPD_PATH} = $self->{BINDIR}.'/lighttpd';
  65. if (exists $ENV{LIGHTTPD_EXE_PATH}) {
  66. $self->{LIGHTTPD_PATH} = $ENV{LIGHTTPD_EXE_PATH};
  67. }
  68. my ($name, $aliases, $addrtype, $net) = gethostbyaddr(inet_aton("127.0.0.1"), AF_INET);
  69. $self->{HOSTNAME} = $name;
  70. bless($self, $class);
  71. return $self;
  72. }
  73. sub listening_on {
  74. my $self = shift;
  75. my $port = shift;
  76. local $@;
  77. local $SIG{ALRM} = sub { };
  78. eval {
  79. local $SIG{ALRM} = sub { die 'alarm()'; };
  80. alarm(1);
  81. my $remote = IO::Socket::INET->new(
  82. Timeout => 1,
  83. Proto => "tcp",
  84. PeerAddr => "127.0.0.1",
  85. PeerPort => $port) || do { alarm(0); die 'socket()'; };
  86. close $remote;
  87. alarm(0);
  88. };
  89. alarm(0);
  90. return (defined($@) && $@ eq "");
  91. }
  92. sub stop_proc {
  93. my $self = shift;
  94. my $pid = $self->{LIGHTTPD_PID};
  95. if (defined $pid && $pid != -1) {
  96. kill('USR1', $pid) if (($ENV{"TRACEME"}||'') eq 'strace');
  97. kill('TERM', $pid) or return -1;
  98. return -1 if ($pid != waitpid($pid, 0));
  99. } else {
  100. diag("\nProcess not started, nothing to stop");
  101. return -1;
  102. }
  103. return 0;
  104. }
  105. sub wait_for_port_with_proc {
  106. my $self = shift;
  107. my $port = shift;
  108. my $child = shift;
  109. my $timeout = 10*100; # 10 secs (valgrind might take a while), select waits 0.01 s
  110. while (0 == $self->listening_on($port)) {
  111. select(undef, undef, undef, 0.01);
  112. $timeout--;
  113. # the process is gone, we failed
  114. require POSIX;
  115. if (0 != waitpid($child, POSIX::WNOHANG())) {
  116. return -1;
  117. }
  118. if (0 >= $timeout) {
  119. diag("\nTimeout while trying to connect; killing child");
  120. kill('TERM', $child);
  121. return -1;
  122. }
  123. }
  124. return 0;
  125. }
  126. sub bind_ephemeral_tcp_socket {
  127. my $SOCK;
  128. socket($SOCK, PF_INET, SOCK_STREAM, 0) || die "socket: $!";
  129. setsockopt($SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
  130. bind($SOCK, sockaddr_in(0, INADDR_LOOPBACK)) || die "bind: $!";
  131. my($port) = sockaddr_in(getsockname($SOCK));
  132. return ($SOCK, $port);
  133. }
  134. sub get_ephemeral_tcp_port {
  135. # bind to an ephemeral port, close() it, and return port that was used
  136. # (While there is a race condition before caller may reuse the port,
  137. # the port is likely to remain available for the serialized tests)
  138. my $port;
  139. (undef, $port) = bind_ephemeral_tcp_socket();
  140. return $port;
  141. }
  142. sub start_proc {
  143. my $self = shift;
  144. # kill old proc if necessary
  145. #$self->stop_proc;
  146. # listen on localhost and kernel-assigned ephemeral port
  147. my $SOCK;
  148. ($SOCK, $self->{PORT}) = bind_ephemeral_tcp_socket();
  149. # pre-process configfile if necessary
  150. #
  151. $ENV{'SRCDIR'} = $self->{BASEDIR}.'/tests';
  152. $ENV{'PORT'} = $self->{PORT};
  153. my @cmdline = ($self->{LIGHTTPD_PATH}, "-D", "-f", $self->{SRCDIR}."/".$self->{CONFIGFILE}, "-m", $self->{MODULES_PATH});
  154. splice(@cmdline, -2) if exists $ENV{LIGHTTPD_EXE_PATH};
  155. if (defined $ENV{"TRACEME"} && $ENV{"TRACEME"} eq 'strace') {
  156. @cmdline = (qw(strace -tt -s 4096 -o strace -f -v), @cmdline);
  157. } elsif (defined $ENV{"TRACEME"} && $ENV{"TRACEME"} eq 'truss') {
  158. @cmdline = (qw(truss -a -l -w all -v all -o strace), @cmdline);
  159. } elsif (defined $ENV{"TRACEME"} && $ENV{"TRACEME"} eq 'gdb') {
  160. @cmdline = ('gdb', '--batch', '--ex', 'run', '--ex', 'bt full', '--args', @cmdline);
  161. } elsif (defined $ENV{"TRACEME"} && $ENV{"TRACEME"} eq 'valgrind') {
  162. @cmdline = (qw(valgrind --tool=memcheck --track-origins=yes --show-reachable=yes --leak-check=yes --log-file=valgrind.%p), @cmdline);
  163. }
  164. # diag("\nstarting lighttpd at :".$self->{PORT}.", cmdline: @cmdline");
  165. my $child = fork();
  166. if (not defined $child) {
  167. diag("\nFork failed");
  168. close($SOCK);
  169. return -1;
  170. }
  171. if ($child == 0) {
  172. if ($^O eq "MSWin32") {
  173. # On platforms where systemd socket activation is not supported
  174. # or inconvenient for testing (i.e. cygwin <-> native Windows exe),
  175. # there is a race condition with close() before server start,
  176. # but port specific port is unlikely to be reused so quickly,
  177. # and the point is to avoid a port which is already in use.
  178. close($SOCK);
  179. my $CONF;
  180. open($CONF,'>',"$ENV{'SRCDIR'}/tmp/bind.conf") || die "open: $!";
  181. print $CONF <<BIND_OVERRIDE;
  182. server.systemd-socket-activation := "disable"
  183. server.bind = "127.0.0.1"
  184. server.port = $ENV{'PORT'}
  185. BIND_OVERRIDE
  186. }
  187. else {
  188. # set up systemd socket activation env vars
  189. $ENV{LISTEN_FDS} = "1";
  190. $ENV{LISTEN_PID} = $$;
  191. if (defined($ENV{"TRACEME"}) && $ENV{"TRACEME"} ne "valgrind") {
  192. $ENV{LISTEN_PID} = "parent:$$"; # lighttpd extension
  193. }
  194. listen($SOCK, 1024) || die "listen: $!";
  195. if (fileno($SOCK) != 3) { # SD_LISTEN_FDS_START 3
  196. require POSIX;
  197. POSIX::dup2(fileno($SOCK), 3) || die "dup2: $!";
  198. close($SOCK);
  199. }
  200. else {
  201. require Fcntl;
  202. fcntl($SOCK, Fcntl::F_SETFD(), 0); # clr FD_CLOEXEC
  203. }
  204. }
  205. exec @cmdline or die($?);
  206. }
  207. close($SOCK);
  208. if (0 != $self->wait_for_port_with_proc($self->{PORT}, $child)) {
  209. diag(sprintf('\nThe process %i is not up', $child));
  210. return -1;
  211. }
  212. $self->{LIGHTTPD_PID} = $child;
  213. 0;
  214. }
  215. sub handle_http {
  216. my $self = shift;
  217. my $t = shift;
  218. my $EOL = "\015\012";
  219. my $BLANK = $EOL x 2;
  220. my $host = "127.0.0.1";
  221. my @request = $t->{REQUEST};
  222. my @response = $t->{RESPONSE};
  223. my $slow = defined $t->{SLOWREQUEST};
  224. my $is_debug = $ENV{"TRACE_HTTP"};
  225. my $remote =
  226. IO::Socket::INET->new(
  227. Proto => "tcp",
  228. PeerAddr => $host,
  229. PeerPort => $self->{PORT});
  230. if (not defined $remote) {
  231. diag("\nconnect failed: $!");
  232. return -1;
  233. }
  234. $remote->autoflush(1);
  235. if (!$slow) {
  236. diag("\nsending request header to ".$host.":".$self->{PORT}) if $is_debug;
  237. foreach(@request) {
  238. # pipeline requests
  239. s/\r//g;
  240. s/\n/$EOL/g;
  241. print $remote $_.$BLANK;
  242. diag("\n<< ".$_) if $is_debug;
  243. }
  244. shutdown($remote, 1) if ($^O ne "openbsd" && $^O ne "dragonfly"); # I've stopped writing data
  245. } else {
  246. diag("\nsending request header to ".$host.":".$self->{PORT}) if $is_debug;
  247. foreach(@request) {
  248. # pipeline requests
  249. chomp;
  250. s/\r//g;
  251. s/\n/$EOL/g;
  252. print $remote $_;
  253. diag("<< ".$_."\n") if $is_debug;
  254. select(undef, undef, undef, 0.0001);
  255. print $remote "\015";
  256. select(undef, undef, undef, 0.0001);
  257. print $remote "\012";
  258. select(undef, undef, undef, 0.0001);
  259. print $remote "\015";
  260. select(undef, undef, undef, 0.0001);
  261. print $remote "\012";
  262. select(undef, undef, undef, 0.0001);
  263. }
  264. }
  265. diag("\n... done") if $is_debug;
  266. my $lines = "";
  267. diag("\nreceiving response") if $is_debug;
  268. # read everything
  269. while(<$remote>) {
  270. $lines .= $_;
  271. diag(">> ".$_) if $is_debug;
  272. }
  273. diag("\n... done") if $is_debug;
  274. close $remote;
  275. my $full_response = $lines;
  276. my $href;
  277. foreach $href ( @{ $t->{RESPONSE} }) {
  278. # first line is always response header
  279. my %resp_hdr;
  280. my $resp_body;
  281. my $resp_line;
  282. my $conditions = $_;
  283. for (my $ln = 0; defined $lines; $ln++) {
  284. (my $line, $lines) = split($EOL, $lines, 2);
  285. # header finished
  286. last if(!defined $line or length($line) == 0);
  287. if ($ln == 0) {
  288. # response header
  289. $resp_line = $line;
  290. } else {
  291. # response vars
  292. if ($line =~ /^([^:]+):\s*(.+)$/) {
  293. (my $h = $1) =~ tr/[A-Z]/[a-z]/;
  294. if (defined $resp_hdr{$h}) {
  295. # diag(sprintf("\nheader '%s' is duplicated: '%s' and '%s'\n",
  296. # $h, $resp_hdr{$h}, $2));
  297. $resp_hdr{$h} .= ', '.$2;
  298. } else {
  299. $resp_hdr{$h} = $2;
  300. }
  301. } else {
  302. diag(sprintf("\nunexpected line '%s'", $line));
  303. return -1;
  304. }
  305. }
  306. }
  307. if (not defined($resp_line)) {
  308. diag(sprintf("\nempty response"));
  309. return -1;
  310. }
  311. $t->{etag} = $resp_hdr{'etag'};
  312. $t->{date} = $resp_hdr{'date'};
  313. # check length
  314. if (defined $resp_hdr{"content-length"}) {
  315. $resp_body = substr($lines, 0, $resp_hdr{"content-length"});
  316. if (length($lines) < $resp_hdr{"content-length"}) {
  317. $lines = "";
  318. } else {
  319. $lines = substr($lines, $resp_hdr{"content-length"});
  320. }
  321. undef $lines if (length($lines) == 0);
  322. } else {
  323. $resp_body = $lines;
  324. undef $lines;
  325. }
  326. # check conditions
  327. if ($resp_line =~ /^(HTTP\/1\.[01]) ([0-9]{3}) .+$/) {
  328. if ($href->{'HTTP-Protocol'} ne $1) {
  329. diag(sprintf("\nproto failed: expected '%s', got '%s'", $href->{'HTTP-Protocol'}, $1));
  330. return -1;
  331. }
  332. if ($href->{'HTTP-Status'} ne $2) {
  333. diag(sprintf("\nstatus failed: expected '%s', got '%s'", $href->{'HTTP-Status'}, $2));
  334. return -1;
  335. }
  336. } else {
  337. diag(sprintf("\nunexpected resp_line '%s'", $resp_line));
  338. return -1;
  339. }
  340. if (defined $href->{'HTTP-Content'}) {
  341. $resp_body = "" unless defined $resp_body;
  342. if ($href->{'HTTP-Content'} ne $resp_body) {
  343. diag(sprintf("\nbody failed: expected '%s', got '%s'", $href->{'HTTP-Content'}, $resp_body));
  344. return -1;
  345. }
  346. }
  347. if (defined $href->{'-HTTP-Content'}) {
  348. if (defined $resp_body && $resp_body ne '') {
  349. diag(sprintf("\nbody failed: expected empty body, got '%s'", $resp_body));
  350. return -1;
  351. }
  352. }
  353. foreach (keys %{ $href }) {
  354. next if $_ eq 'HTTP-Protocol';
  355. next if $_ eq 'HTTP-Status';
  356. next if $_ eq 'HTTP-Content';
  357. next if $_ eq '-HTTP-Content';
  358. (my $k = $_) =~ tr/[A-Z]/[a-z]/;
  359. my $verify_value = 1;
  360. my $key_inverted = 0;
  361. if (substr($k, 0, 1) eq '+') {
  362. $k = substr($k, 1);
  363. $verify_value = 0;
  364. } elsif (substr($k, 0, 1) eq '-') {
  365. ## the key should NOT exist
  366. $k = substr($k, 1);
  367. $key_inverted = 1;
  368. $verify_value = 0; ## skip the value check
  369. }
  370. if ($key_inverted) {
  371. if (defined $resp_hdr{$k}) {
  372. diag(sprintf("\nheader '%s' MUST not be set", $k));
  373. return -1;
  374. }
  375. } else {
  376. if (not defined $resp_hdr{$k}) {
  377. diag(sprintf("\nrequired header '%s' is missing", $k));
  378. return -1;
  379. }
  380. }
  381. if ($verify_value) {
  382. if ($href->{$_} =~ /^\/(.+)\/$/) {
  383. if ($resp_hdr{$k} !~ /$1/) {
  384. diag(sprintf(
  385. "\nresponse-header failed: expected '%s', got '%s', regex: %s",
  386. $href->{$_}, $resp_hdr{$k}, $1));
  387. return -1;
  388. }
  389. } elsif ($href->{$_} ne $resp_hdr{$k}) {
  390. diag(sprintf(
  391. "\nresponse-header failed: expected '%s', got '%s'",
  392. $href->{$_}, $resp_hdr{$k}));
  393. return -1;
  394. }
  395. }
  396. }
  397. }
  398. # we should have sucked up everything
  399. if (defined $lines) {
  400. diag(sprintf("\nunexpected lines '%s'", $lines));
  401. return -1;
  402. }
  403. return 0;
  404. }
  405. sub spawnfcgi {
  406. my ($self, $binary, $port) = @_;
  407. my $child = fork();
  408. if (not defined $child) {
  409. diag("\nCouldn't fork");
  410. return -1;
  411. }
  412. if ($child == 0) {
  413. my $iaddr = inet_aton('localhost') || die "no host: localhost";
  414. my $proto = getprotobyname('tcp');
  415. socket(SOCK, PF_INET, SOCK_STREAM, $proto) || die "socket: $!";
  416. setsockopt(SOCK, SOL_SOCKET, SO_REUSEADDR, pack("l", 1)) || die "setsockopt: $!";
  417. bind(SOCK, sockaddr_in($port, $iaddr)) || die "bind: $!";
  418. listen(SOCK, 1024) || die "listen: $!";
  419. require POSIX;
  420. POSIX::dup2(fileno(SOCK), 0) || die "dup2: $!";
  421. exec { $binary } ($binary) or die($?);
  422. } else {
  423. if (0 != $self->wait_for_port_with_proc($port, $child)) {
  424. diag(sprintf("\nThe process %i is not up (port %i, %s)", $child, $port, $binary));
  425. return -1;
  426. }
  427. return $child;
  428. }
  429. }
  430. sub endspawnfcgi {
  431. my ($self, $pid) = @_;
  432. return -1 if (-1 == $pid);
  433. kill(2, $pid);
  434. waitpid($pid, 0);
  435. return 0;
  436. }
  437. sub has_feature {
  438. # quick-n-dirty crude parse of "lighttpd -V"
  439. # (XXX: should be run on demand and only once per instance, then cached)
  440. my ($self, $feature) = @_;
  441. my $FH;
  442. open($FH, "-|",$self->{LIGHTTPD_PATH}, "-V") || return 0;
  443. while (<$FH>) {
  444. return ($1 eq '+') if (/([-+]) \Q$feature\E/);
  445. }
  446. close $FH;
  447. return 0;
  448. }
  449. sub has_crypto {
  450. # quick-n-dirty crude parse of "lighttpd -V"
  451. # (XXX: should be run on demand and only once per instance, then cached)
  452. my ($self) = @_;
  453. my $FH;
  454. open($FH, "-|",$self->{LIGHTTPD_PATH}, "-V") || return 0;
  455. while (<$FH>) {
  456. #return 1 if (/[+] (?i:OpenSSL|mbedTLS|GnuTLS|WolfSSL|Nettle|NSS crypto) support/);
  457. return 1 if (/[+] (?i:OpenSSL|mbedTLS|GnuTLS|WolfSSL|Nettle) support/);
  458. }
  459. close $FH;
  460. return 0;
  461. }
  462. 1;