changeset 1109:59d5c8ca7e4d

Tests: auth cram-md5 mail tests, pop3 user/pass and apop tests.
author Sergey Kandaurov <pluknet@nginx.com>
date Thu, 12 Jan 2017 11:46:11 +0300
parents ff1a37f37419
children b9b115a2a28d
files mail_imap.t mail_pop3.t mail_smtp.t
diffstat 3 files changed, 83 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/mail_imap.t
+++ b/mail_imap.t
@@ -26,7 +26,7 @@ select STDOUT; $| = 1;
 
 local $SIG{PIPE} = 'IGNORE';
 
-my $t = Test::Nginx->new()->has(qw/mail imap http rewrite/)->plan(9)
+my $t = Test::Nginx->new()->has(qw/mail imap http rewrite/)->plan(11)
 	->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
@@ -43,6 +43,7 @@ mail {
     server {
         listen     127.0.0.1:8143;
         protocol   imap;
+        imap_auth  plain cram-md5;
     }
 }
 
@@ -55,6 +56,7 @@ http {
 
         location = /mail/auth {
             set $reply ERROR;
+            set $passw "";
 
             if ($http_auth_smtp_to ~ example.com) {
                 set $reply OK;
@@ -65,9 +67,16 @@ http {
                 set $reply OK;
             }
 
+            set $userpass "$http_auth_user:$http_auth_salt:$http_auth_pass";
+            if ($userpass ~ '^test@example.com:<.*@.*>:0{32}$') {
+                set $reply OK;
+                set $passw secret;
+            }
+
             add_header Auth-Status $reply;
             add_header Auth-Server 127.0.0.1;
             add_header Auth-Port %%PORT_8144%%;
+            add_header Auth-Pass $passw;
             add_header Auth-Wait 1;
             return 204;
         }
@@ -122,4 +131,15 @@ my $s = Test::Nginx::IMAP->new();
 $s->send(encode_base64('secret', ''));
 $s->ok('auth login with username');
 
+# auth cram-md5
+
+$s = Test::Nginx::IMAP->new();
+$s->read();
+
+$s->send('1 AUTHENTICATE CRAM-MD5');
+$s->check(qr/\+ /, 'auth cram-md5 challenge');
+
+$s->send(encode_base64('test@example.com ' . ('0' x 32), ''));
+$s->ok('auth cram-md5');
+
 ###############################################################################
--- a/mail_pop3.t
+++ b/mail_pop3.t
@@ -26,7 +26,7 @@ select STDOUT; $| = 1;
 
 local $SIG{PIPE} = 'IGNORE';
 
-my $t = Test::Nginx->new()->has(qw/mail pop3 http rewrite/)->plan(8)
+my $t = Test::Nginx->new()->has(qw/mail pop3 http rewrite/)->plan(15)
 	->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
@@ -43,6 +43,7 @@ mail {
     server {
         listen     127.0.0.1:8110;
         protocol   pop3;
+        pop3_auth  plain apop cram-md5;
     }
 }
 
@@ -55,6 +56,7 @@ http {
 
         location = /mail/auth {
             set $reply ERROR;
+            set $passw "";
 
             if ($http_auth_smtp_to ~ example.com) {
                 set $reply OK;
@@ -65,9 +67,16 @@ http {
                 set $reply OK;
             }
 
+            set $userpass "$http_auth_user:$http_auth_salt:$http_auth_pass";
+            if ($userpass ~ '^test@example.com:<.*@.*>:0{32}$') {
+                set $reply OK;
+                set $passw secret;
+            }
+
             add_header Auth-Status $reply;
             add_header Auth-Server 127.0.0.1;
             add_header Auth-Port %%PORT_8111%%;
+            add_header Auth-Pass $passw;
             add_header Auth-Wait 1;
             return 204;
         }
@@ -84,8 +93,30 @@ EOF
 my $s = Test::Nginx::POP3->new();
 $s->ok('greeting');
 
+# user / pass
+
+$s->send('USER test@example.com');
+$s->ok('user');
+
+$s->send('PASS secret');
+$s->ok('pass');
+
+# apop
+
+$s = Test::Nginx::POP3->new();
+$s->check(qr/<.*\@.*>/, 'apop salt');
+
+$s->send('APOP test@example.com ' . ('1' x 32));
+$s->check(qr/^-ERR/, 'apop error');
+
+$s->send('APOP test@example.com ' . ('0' x 32));
+$s->ok('apop');
+
 # auth plain
 
+$s = Test::Nginx::POP3->new();
+$s->read();
+
 $s->send('AUTH PLAIN ' . encode_base64("\0test\@example.com\0bad", ''));
 $s->check(qr/^-ERR/, 'auth plain with bad password');
 
@@ -117,4 +148,15 @@ my $s = Test::Nginx::POP3->new();
 $s->send(encode_base64('secret', ''));
 $s->ok('auth login with username');
 
+# auth cram-md5
+
+$s = Test::Nginx::POP3->new();
+$s->read();
+
+$s->send('AUTH CRAM-MD5');
+$s->check(qr/\+ /, 'auth cram-md5 challenge');
+
+$s->send(encode_base64('test@example.com ' . ('0' x 32), ''));
+$s->ok('auth cram-md5');
+
 ###############################################################################
--- a/mail_smtp.t
+++ b/mail_smtp.t
@@ -27,7 +27,7 @@ select STDOUT; $| = 1;
 
 local $SIG{PIPE} = 'IGNORE';
 
-my $t = Test::Nginx->new()->has(qw/mail smtp http rewrite/)->plan(25)
+my $t = Test::Nginx->new()->has(qw/mail smtp http rewrite/)->plan(27)
 	->write_file_expand('nginx.conf', <<'EOF');
 
 %%TEST_GLOBALS%%
@@ -45,7 +45,7 @@ mail {
     server {
         listen     127.0.0.1:8025;
         protocol   smtp;
-        smtp_auth  login plain none;
+        smtp_auth  login plain none cram-md5;
     }
 }
 
@@ -68,6 +68,11 @@ http {
                 set $reply OK;
             }
 
+            set $userpass "$http_auth_user:$http_auth_salt:$http_auth_pass";
+            if ($userpass ~ '^test@example.com:<.*@.*>:0{32}$') {
+                set $reply OK;
+            }
+
             add_header Auth-Status $reply;
             add_header Auth-Server 127.0.0.1;
             add_header Auth-Port %%PORT_8026%%;
@@ -139,6 +144,18 @@ my $s = Test::Nginx::SMTP->new();
 $s->send(encode_base64('secret', ''));
 $s->authok('auth login with username');
 
+# Try auth cram-md5
+
+$s = Test::Nginx::SMTP->new();
+$s->read();
+$s->send('EHLO example.com');
+$s->read();
+
+$s->send('AUTH CRAM-MD5');
+$s->check(qr/^334 /, 'auth cram-md5 challenge');
+$s->send(encode_base64('test@example.com ' . ('0' x 32), ''));
+$s->authok('auth cram-md5');
+
 # Try auth plain with pipelining
 
 $s = Test::Nginx::SMTP->new();