mod-auth.t 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 => 17;
  11. use LightyTest;
  12. my $tf = LightyTest->new();
  13. my $t;
  14. ok($tf->start_proc == 0, "Starting lighttpd") or die();
  15. $t->{REQUEST} = ( <<EOF
  16. GET /server-status HTTP/1.0
  17. EOF
  18. );
  19. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  20. ok($tf->handle_http($t) == 0, 'Missing Auth-token');
  21. $t->{REQUEST} = ( <<EOF
  22. GET /server-status HTTP/1.0
  23. Authorization: Basic \x80mFuOmphb
  24. EOF
  25. );
  26. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  27. ok($tf->handle_http($t) == 0, 'Basic-Auth: Invalid base64 Auth-token');
  28. $t->{REQUEST} = ( <<EOF
  29. GET /server-status HTTP/1.0
  30. Authorization: Basic amFuOmphb
  31. EOF
  32. );
  33. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  34. ok($tf->handle_http($t) == 0, 'Basic-Auth: Wrong Auth-token');
  35. $t->{REQUEST} = ( <<EOF
  36. GET /server-config HTTP/1.0
  37. Authorization: Basic amFuOmphbg==
  38. EOF
  39. );
  40. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  41. ok($tf->handle_http($t) == 0, 'Basic-Auth: Valid Auth-token - plain');
  42. $t->{REQUEST} = ( <<EOF
  43. GET /server-config HTTP/1.0
  44. Host: auth-htpasswd.example.org
  45. Authorization: Basic ZGVzOmRlcw==
  46. EOF
  47. );
  48. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  49. ok($tf->handle_http($t) == 0, 'Basic-Auth: Valid Auth-token - htpasswd (des)');
  50. $t->{REQUEST} = ( <<EOF
  51. GET /server-config HTTP/1.0
  52. Host: auth-htpasswd.example.org
  53. Authorization: basic ZGVzOmRlcw==
  54. EOF
  55. );
  56. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  57. ok($tf->handle_http($t) == 0, 'Basic-Auth: Valid Auth-token - htpasswd (des) (lowercase)');
  58. $t->{REQUEST} = ( <<EOF
  59. GET /server-config HTTP/1.0
  60. Host: auth-htpasswd.example.org
  61. Authorization: Basic c2hhOnNoYQ==
  62. EOF
  63. );
  64. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  65. ok($tf->handle_http($t) == 0, 'Basic-Auth: Valid Auth-token - htpasswd (sha)');
  66. $t->{REQUEST} = ( <<EOF
  67. GET /server-config HTTP/1.0
  68. Host: auth-htpasswd.example.org
  69. Authorization: Basic c2hhOnNoYg==
  70. EOF
  71. );
  72. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  73. ok($tf->handle_http($t) == 0, 'Basic-Auth: Valid Auth-token - htpasswd (sha, wrong password)');
  74. SKIP: {
  75. skip "no md5 for crypt under cygwin", 1 if $^O eq 'cygwin';
  76. $t->{REQUEST} = ( <<EOF
  77. GET /server-config HTTP/1.0
  78. Host: auth-htpasswd.example.org
  79. Authorization: Basic bWQ1Om1kNQ==
  80. EOF
  81. );
  82. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 200 } ];
  83. ok($tf->handle_http($t) == 0, 'Basic-Auth: Valid Auth-token - htpasswd (md5)');
  84. }
  85. $t->{REQUEST} = ( <<EOF
  86. GET /server-config HTTP/1.0
  87. Authorization: Basic bWQ1Om1kNA==
  88. EOF
  89. );
  90. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  91. ok($tf->handle_http($t) == 0, 'Basic-Auth: Valid Auth-token');
  92. ## this should not crash
  93. $t->{REQUEST} = ( <<EOF
  94. GET /server-status HTTP/1.0
  95. User-Agent: Wget/1.9.1
  96. Authorization: Digest username="jan", realm="jan", nonce="9a5428ccc05b086a08d918e73b01fc6f",
  97. uri="/server-status", response="ea5f7d9a30b8b762f9610ccb87dea74f"
  98. EOF
  99. );
  100. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  101. ok($tf->handle_http($t) == 0, 'Digest-Auth: missing qop, no crash');
  102. ## this should not crash
  103. $t->{REQUEST} = ( <<EOF
  104. GET /server-status HTTP/1.0
  105. User-Agent: Wget/1.9.1
  106. Authorization: Digest username="jan", realm="jan",
  107. nonce="b1d12348b4620437c43dd61c50ae4639",
  108. uri="/MJ-BONG.xm.mpc", qop=auth, noncecount=00000001",
  109. cnonce="036FCA5B86F7E7C4965C7F9B8FE714B7",
  110. response="29B32C2953C763C6D033C8A49983B87E"
  111. EOF
  112. );
  113. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 400 } ];
  114. ok($tf->handle_http($t) == 0, 'Digest-Auth: missing nc (noncecount instead), no crash');
  115. $t->{REQUEST} = ( <<EOF
  116. GET /server-status HTTP/1.0
  117. Authorization: Basic =
  118. EOF
  119. );
  120. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  121. ok($tf->handle_http($t) == 0, 'Basic-Auth: Invalid Base64');
  122. $t->{REQUEST} = ( <<EOF
  123. GET /server-status HTTP/1.0
  124. User-Agent: Wget/1.9.1
  125. Authorization: Digest username="jan", realm="jan",
  126. nonce="b1d12348b4620437c43dd61c50ae4639", algorithm="md5-sess",
  127. uri="/MJ-BONG.xm.mpc", qop=auth, noncecount=00000001",
  128. cnonce="036FCA5B86F7E7C4965C7F9B8FE714B7",
  129. nc="asd",
  130. response="29B32C2953C763C6D033C8A49983B87E"
  131. EOF
  132. );
  133. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  134. ok($tf->handle_http($t) == 0, 'Digest-Auth: md5-sess + missing cnonce');
  135. $t->{REQUEST} = ( <<EOF
  136. GET /server-status HTTP/1.0
  137. User-Agent: Wget/1.9.1
  138. Authorization: Digest username="jan", realm="jan",
  139. nonce="b1d12348b4620437c43dd61c50ae4639", algorithm="md5-sess",
  140. uri="/MJ-BONG.xm.mpc", qop=auth, noncecount=00000001",
  141. cnonce="036FCA5B86F7E7C4965C7F9B8FE714B7",
  142. nc="asd",
  143. response="29B32C2953C763C6D033C8A49983B87E"
  144. EOF
  145. );
  146. $t->{RESPONSE} = [ { 'HTTP-Protocol' => 'HTTP/1.0', 'HTTP-Status' => 401 } ];
  147. ok($tf->handle_http($t) == 0, 'Digest-Auth: trailing WS');
  148. ok($tf->stop_proc == 0, "Stopping lighttpd");