mail_pop3.t 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for nginx mail pop3 module.
  4. ###############################################################################
  5. use warnings;
  6. use strict;
  7. use Test::More;
  8. use IO::Socket;
  9. use MIME::Base64;
  10. use Socket qw/ CRLF /;
  11. BEGIN { use FindBin; chdir($FindBin::Bin); }
  12. use lib 'lib';
  13. use Test::Nginx;
  14. use Test::Nginx::POP3;
  15. ###############################################################################
  16. select STDERR; $| = 1;
  17. select STDOUT; $| = 1;
  18. local $SIG{PIPE} = 'IGNORE';
  19. my $t = Test::Nginx->new()
  20. ->has(qw/mail pop3 http rewrite/)->plan(8)
  21. ->run_daemon(\&Test::Nginx::POP3::pop3_test_daemon)
  22. ->write_file_expand('nginx.conf', <<'EOF')->run();
  23. %%TEST_GLOBALS%%
  24. master_process off;
  25. daemon off;
  26. events {
  27. }
  28. mail {
  29. proxy_pass_error_message on;
  30. auth_http http://127.0.0.1:8080/mail/auth;
  31. server {
  32. listen 127.0.0.1:8110;
  33. protocol pop3;
  34. }
  35. }
  36. http {
  37. %%TEST_GLOBALS_HTTP%%
  38. server {
  39. listen 127.0.0.1:8080;
  40. server_name localhost;
  41. location = /mail/auth {
  42. set $reply ERROR;
  43. if ($http_auth_smtp_to ~ example.com) {
  44. set $reply OK;
  45. }
  46. set $userpass "$http_auth_user:$http_auth_pass";
  47. if ($userpass ~ '^test@example.com:secret$') {
  48. set $reply OK;
  49. }
  50. add_header Auth-Status $reply;
  51. add_header Auth-Server 127.0.0.1;
  52. add_header Auth-Port 8111;
  53. add_header Auth-Wait 1;
  54. return 204;
  55. }
  56. }
  57. }
  58. EOF
  59. ###############################################################################
  60. my $s = Test::Nginx::POP3->new();
  61. $s->ok('greeting');
  62. # auth plain
  63. $s->send('AUTH PLAIN ' . encode_base64("\0test\@example.com\0bad", ''));
  64. $s->check(qr/^-ERR/, 'auth plain with bad password');
  65. $s->send('AUTH PLAIN ' . encode_base64("\0test\@example.com\0secret", ''));
  66. $s->ok('auth plain');
  67. # auth login simple
  68. $s = Test::Nginx::POP3->new();
  69. $s->read();
  70. $s->send('AUTH LOGIN');
  71. $s->check(qr/\+ VXNlcm5hbWU6/, 'auth login username challenge');
  72. $s->send(encode_base64('test@example.com', ''));
  73. $s->check(qr/\+ UGFzc3dvcmQ6/, 'auth login password challenge');
  74. $s->send(encode_base64('secret', ''));
  75. $s->ok('auth login simple');
  76. # auth login with username
  77. $s = Test::Nginx::POP3->new();
  78. $s->read();
  79. $s->send('AUTH LOGIN ' . encode_base64('test@example.com', ''));
  80. $s->check(qr/\+ UGFzc3dvcmQ6/, 'auth login with username password challenge');
  81. $s->send(encode_base64('secret', ''));
  82. $s->ok('auth login with username');
  83. ###############################################################################