core-condition.t 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #!/usr/bin/env perl
  2. BEGIN {
  3. # add current source dir to the include-path
  4. # we need this for make distcheck
  5. (my $srcdir = $0) =~ s,/[^/]+$,/,;
  6. unshift @INC, $srcdir;
  7. }
  8. use strict;
  9. use IO::Socket;
  10. use Test::More tests => 25;
  11. use LightyTest;
  12. my $tf = LightyTest->new();
  13. my $t;
  14. $ENV{"env_test"} = "good_env";
  15. $tf->{CONFIGFILE} = 'condition.conf';
  16. ok($tf->start_proc == 0, "Starting lighttpd") or die();
  17. $t->{REQUEST} = ( <<EOF
  18. GET /index.html HTTP/1.0
  19. Host: www.example.org
  20. EOF
  21. );
  22. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_1" } ];
  23. ok($tf->handle_http($t) == 0, 'config deny');
  24. $t->{REQUEST} = ( <<EOF
  25. GET /index.html HTTP/1.0
  26. Host: test1.example.org
  27. EOF
  28. );
  29. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_2" } ];
  30. ok($tf->handle_http($t) == 0, '2nd child of chaining');
  31. $t->{REQUEST} = ( <<EOF
  32. GET /index.html HTTP/1.0
  33. Host: test2.example.org
  34. EOF
  35. );
  36. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_3" } ];
  37. ok($tf->handle_http($t) == 0, '3rd child of chaining');
  38. $t->{REQUEST} = ( <<EOF
  39. GET /index.html HTTP/1.0
  40. Host: test3.example.org
  41. EOF
  42. );
  43. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_5" } ];
  44. ok($tf->handle_http($t) == 0, 'nesting');
  45. $t->{REQUEST} = ( <<EOF
  46. GET /subdir/index.html HTTP/1.0
  47. Host: test4.example.org
  48. EOF
  49. );
  50. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_7" } ];
  51. ok($tf->handle_http($t) == 0, 'url subdir');
  52. $t->{REQUEST} = ( <<EOF
  53. GET /subdir/../css/index.html HTTP/1.0
  54. Host: test4.example.org
  55. EOF
  56. );
  57. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_6" } ];
  58. ok($tf->handle_http($t) == 0, 'url subdir with path traversal');
  59. $t->{REQUEST} = ( <<EOF
  60. GET / HTTP/1.0
  61. EOF
  62. );
  63. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Server' => 'lighttpd-1.4.x' } ];
  64. ok($tf->handle_http($t) == 0, 'condition: handle if before else branches');
  65. $t->{REQUEST} = ( <<EOF
  66. GET /show/other/server-tag HTTP/1.0
  67. EOF
  68. );
  69. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Server' => 'special tag' } ];
  70. ok($tf->handle_http($t) == 0, 'condition: handle if before else branches #2');
  71. ## config includes
  72. $t->{REQUEST} = ( "GET /index.html HTTP/1.0\r\nHost: www.example.org\r\n" );
  73. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => "/match_1" } ];
  74. ok($tf->handle_http($t) == 0, 'basic test');
  75. my $myvar = "good";
  76. my $server_name = "test.example.org";
  77. my $mystr = "string";
  78. $mystr .= "_append";
  79. my $tests = {
  80. "include" => "/good_include",
  81. "concat" => "/good_" . "concat",
  82. "servername1" => "/good_" . $server_name,
  83. "servername2" => $server_name . "/good_",
  84. "servername3" => "/good_" . $server_name . "/",
  85. "var.myvar" => "/good_var_myvar" . $myvar,
  86. "myvar" => "/good_myvar" . $myvar,
  87. "env" => "/" . $ENV{"env_test"},
  88. "number1" => "/good_number" . "1",
  89. "number2" => "1" . "/good_number",
  90. "array_append" => "/good_array_append",
  91. "string_append" => "/good_" . $mystr,
  92. "number_append" => "/good_" . "2",
  93. "include_shell" => "/good_include_shell_" . "456"
  94. };
  95. foreach my $test (keys %{ $tests }) {
  96. my $expect = $tests->{$test};
  97. $t->{REQUEST} = ( <<EOF
  98. GET /$test HTTP/1.0
  99. Host: $server_name
  100. EOF
  101. );
  102. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 301, 'Location' => $expect } ];
  103. ok($tf->handle_http($t) == 0, $test);
  104. }
  105. ok($tf->stop_proc == 0, "Stopping lighttpd");