auth_basic.t 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for auth basic module.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More;
  8. use MIME::Base64;
  9. BEGIN { use FindBin; chdir($FindBin::Bin); }
  10. use lib 'lib';
  11. use Test::Nginx;
  12. ###############################################################################
  13. select STDERR; $| = 1;
  14. select STDOUT; $| = 1;
  15. my $t = Test::Nginx->new()->has(qw/http auth_basic/)->plan(11)
  16. ->write_file_expand('nginx.conf', <<'EOF');
  17. %%TEST_GLOBALS%%
  18. master_process off;
  19. daemon off;
  20. events {
  21. }
  22. http {
  23. %%TEST_GLOBALS_HTTP%%
  24. server {
  25. listen 127.0.0.1:8080;
  26. server_name localhost;
  27. location / {
  28. auth_basic "closed site";
  29. auth_basic_user_file %%TESTDIR%%/htpasswd;
  30. }
  31. }
  32. }
  33. EOF
  34. my $d = $t->testdir();
  35. $t->write_file('index.html', 'SEETHIS');
  36. $t->write_file(
  37. 'htpasswd',
  38. 'crypt:' . crypt('password', 'salt') . "\n" .
  39. 'crypt1:' . crypt('password', '$1$salt$') . "\n" .
  40. 'apr1:' . '$apr1$salt$Xxd1irWT9ycqoYxGFn4cb.' . "\n" .
  41. 'plain:' . '{PLAIN}password' . "\n" .
  42. 'ssha:' . '{SSHA}yI6cZwQadOA1e+/f+T+H3eCQQhRzYWx0' . "\n"
  43. );
  44. $t->run();
  45. ###############################################################################
  46. like(http_get('/'), qr!401 Unauthorized!ms, 'rejects unathorized');
  47. like(http_get_auth('/', 'crypt', 'password'), qr!SEETHIS!, 'normal crypt');
  48. unlike(http_get_auth('/', 'crypt', '123'), qr!SEETHIS!, 'normal wrong');
  49. like(http_get_auth('/', 'crypt1', 'password'), qr!SEETHIS!, 'crypt $1$ (md5)');
  50. unlike(http_get_auth('/', 'crypt1', '123'), qr!SEETHIS!, 'crypt $1$ wrong');
  51. like(http_get_auth('/', 'apr1', 'password'), qr!SEETHIS!, 'apr1 md5');
  52. like(http_get_auth('/', 'plain', 'password'), qr!SEETHIS!, 'plain password');
  53. SKIP: {
  54. # SHA1 may not be available unless we have OpenSSL
  55. skip 'no sha1', 1 unless $t->has_module('--with-http_ssl_module')
  56. or $t->has_module('--with-sha1')
  57. or $t->has_module('--with-openssl');
  58. like(http_get_auth('/', 'ssha', 'password'), qr!SEETHIS!, 'ssha');
  59. }
  60. unlike(http_get_auth('/', 'apr1', '123'), qr!SEETHIS!, 'apr1 md5 wrong');
  61. unlike(http_get_auth('/', 'plain', '123'), qr!SEETHIS!, 'plain wrong');
  62. unlike(http_get_auth('/', 'ssha', '123'), qr!SEETHIS!, 'ssha wrong');
  63. ###############################################################################
  64. sub http_get_auth {
  65. my ($url, $user, $password) = @_;
  66. my $auth = encode_base64($user . ':' . $password);
  67. my $r = http(<<EOF);
  68. GET $url HTTP/1.0
  69. Host: localhost
  70. Authorization: Basic $auth
  71. EOF
  72. }
  73. ###############################################################################