mail_imap.t 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #!/usr/bin/perl
  2. # (C) Maxim Dounin
  3. # Tests for nginx mail imap 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::IMAP;
  15. ###############################################################################
  16. select STDERR; $| = 1;
  17. select STDOUT; $| = 1;
  18. local $SIG{PIPE} = 'IGNORE';
  19. my $t = Test::Nginx->new()
  20. ->has(qw/mail imap http rewrite/)->plan(9)
  21. ->run_daemon(\&Test::Nginx::IMAP::imap_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:8143;
  33. protocol imap;
  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 8144;
  53. add_header Auth-Wait 1;
  54. return 204;
  55. }
  56. }
  57. }
  58. EOF
  59. ###############################################################################
  60. my $s = Test::Nginx::IMAP->new();
  61. $s->ok('greeting');
  62. # bad auth
  63. $s->send('1 AUTHENTICATE');
  64. $s->check(qr/^\S+ BAD/, 'auth without arguments');
  65. # auth plain
  66. $s->send('1 AUTHENTICATE PLAIN ' . encode_base64("\0test\@example.com\0bad", ''));
  67. $s->check(qr/^\S+ NO/, 'auth plain with bad password');
  68. $s->send('1 AUTHENTICATE PLAIN ' . encode_base64("\0test\@example.com\0secret", ''));
  69. $s->ok('auth plain');
  70. # auth login simple
  71. $s = Test::Nginx::IMAP->new();
  72. $s->read();
  73. $s->send('1 AUTHENTICATE LOGIN');
  74. $s->check(qr/\+ VXNlcm5hbWU6/, 'auth login username challenge');
  75. $s->send(encode_base64('test@example.com', ''));
  76. $s->check(qr/\+ UGFzc3dvcmQ6/, 'auth login password challenge');
  77. $s->send(encode_base64('secret', ''));
  78. $s->ok('auth login simple');
  79. # auth login with username
  80. $s = Test::Nginx::IMAP->new();
  81. $s->read();
  82. $s->send('1 AUTHENTICATE LOGIN ' . encode_base64('test@example.com', ''));
  83. $s->check(qr/\+ UGFzc3dvcmQ6/, 'auth login with username password challenge');
  84. $s->send(encode_base64('secret', ''));
  85. $s->ok('auth login with username');
  86. ###############################################################################