| 1 |
#!/usr/bin/perl -w |
| 2 |
use strict; |
| 3 |
use warnings; |
| 4 |
|
| 5 |
use lib 't'; |
| 6 |
use lib 'lib'; |
| 7 |
|
| 8 |
use Data::Dumper; |
| 9 |
use Digest::HMAC_MD5 qw(hmac_md5_hex); |
| 10 |
use English qw/ -no_match_vars /; |
| 11 |
use File::Path; |
| 12 |
|
| 13 |
use Qpsmtpd::Constants; |
| 14 |
use Scalar::Util qw( openhandle ); |
| 15 |
use Test::More qw(no_plan); |
| 16 |
|
| 17 |
use_ok('Test::Qpsmtpd'); |
| 18 |
use_ok('Qpsmtpd::Auth'); |
| 19 |
|
| 20 |
my ($smtpd, $conn) = Test::Qpsmtpd->new_conn(); |
| 21 |
|
| 22 |
ok($smtpd, "get new connection ($smtpd)"); |
| 23 |
isa_ok($conn, 'Qpsmtpd::Connection', "get new connection"); |
| 24 |
|
| 25 |
#warn Dumper($smtpd) and exit; |
| 26 |
#my $hooks = $smtpd->hooks; |
| 27 |
#warn Dumper($hooks) and exit; |
| 28 |
|
| 29 |
my $r; |
| 30 |
my $user = '[email protected]'; |
| 31 |
my $pass = 'good_pass'; |
| 32 |
my $enc_plain = Qpsmtpd::Auth::e64(join("\0", '', $user, $pass)); |
| 33 |
|
| 34 |
# get_auth_details_plain: plain auth method handles credentials properly |
| 35 |
my ($loginas, $ruser, $passClear) = |
| 36 |
Qpsmtpd::Auth::get_auth_details_plain($smtpd, $enc_plain); |
| 37 |
cmp_ok($user, 'eq', $user, "get_auth_details_plain, user"); |
| 38 |
cmp_ok($passClear, 'eq', $pass, "get_auth_details_plain, password"); |
| 39 |
|
| 40 |
my $bad_auth = Qpsmtpd::Auth::e64(join("\0", 'loginas', 'user@foo', 'passer')); |
| 41 |
($loginas, $ruser, $passClear) = |
| 42 |
Qpsmtpd::Auth::get_auth_details_plain($smtpd, $bad_auth); |
| 43 |
ok(!$loginas, "get_auth_details_plain, loginas -"); |
| 44 |
ok(!$ruser, "get_auth_details_plain, user -"); |
| 45 |
ok(!$passClear, "get_auth_details_plain, pass -"); |
| 46 |
|
| 47 |
# these plugins test against whicever loaded plugin provides their selected |
| 48 |
# auth type. Right now, they end up testing against auth_flat_file. |
| 49 |
|
| 50 |
# PLAIN |
| 51 |
$r = Qpsmtpd::Auth::SASL($smtpd, 'plain', $enc_plain); |
| 52 |
cmp_ok(OK, '==', $r, "plain auth"); |
| 53 |
|
| 54 |
if ($ENV{QPSMTPD_DEVELOPER} && is_interactive()) { |
| 55 |
|
| 56 |
# same thing, but must be entered interactively |
| 57 |
print "answer: $enc_plain\n"; |
| 58 |
$r = Qpsmtpd::Auth::SASL($smtpd, 'plain', ''); |
| 59 |
cmp_ok(OK, '==', $r, "SASL, plain"); |
| 60 |
} |
| 61 |
|
| 62 |
# LOGIN |
| 63 |
|
| 64 |
if ($ENV{QPSMTPD_DEVELOPER} && is_interactive()) { |
| 65 |
|
| 66 |
my $enc_user = Qpsmtpd::Auth::e64($user); |
| 67 |
my $enc_pass = Qpsmtpd::Auth::e64($pass); |
| 68 |
|
| 69 |
# get_base64_response |
| 70 |
print "answer: $enc_user\n"; |
| 71 |
$r = Qpsmtpd::Auth::get_base64_response($smtpd, 'Username'); |
| 72 |
cmp_ok($r, 'eq', $user, "get_base64_response +"); |
| 73 |
|
| 74 |
# get_auth_details_login |
| 75 |
print "answer: $enc_pass\n"; |
| 76 |
($ruser, $passClear) = |
| 77 |
Qpsmtpd::Auth::get_auth_details_login($smtpd, $enc_user); |
| 78 |
cmp_ok($ruser, 'eq', $user, "get_auth_details_login, user +"); |
| 79 |
cmp_ok($passClear, 'eq', $pass, "get_auth_details_login, pass +"); |
| 80 |
|
| 81 |
print "encoded pass: $enc_pass\n"; |
| 82 |
$r = Qpsmtpd::Auth::SASL($smtpd, 'login', $enc_user); |
| 83 |
cmp_ok(OK, '==', $r, "SASL, login"); |
| 84 |
} |
| 85 |
|
| 86 |
# CRAM-MD5 |
| 87 |
|
| 88 |
if ($ENV{QPSMTPD_DEVELOPER} && is_interactive()) { |
| 89 |
print "starting SASL\n"; |
| 90 |
|
| 91 |
# since we don't have bidirection communication here, we pre-generate a ticket |
| 92 |
my $ticket = |
| 93 |
sprintf('<%x.%x@%s>', rand(1000000), time(), $smtpd->config('me')); |
| 94 |
my $hash_pass = hmac_md5_hex($ticket, $pass); |
| 95 |
my $enc_answer = Qpsmtpd::Auth::e64(join(' ', $user, $hash_pass)); |
| 96 |
print "answer: $enc_answer\n"; |
| 97 |
my (@r) = Qpsmtpd::Auth::get_auth_details_cram_md5($smtpd, $ticket); |
| 98 |
cmp_ok($r[0], 'eq', $ticket, "get_auth_details_cram_md5, ticket"); |
| 99 |
cmp_ok($r[1], 'eq', $user, "get_auth_details_cram_md5, user"); |
| 100 |
cmp_ok($r[2], 'eq', $hash_pass, "get_auth_details_cram_md5, passHash"); |
| 101 |
|
| 102 |
#warn Data::Dumper::Dumper(\@r); |
| 103 |
|
| 104 |
# this isn't going to work without bidirection communication to get the ticket |
| 105 |
#$r = Qpsmtpd::Auth::SASL($smtpd, 'cram-md5' ); |
| 106 |
#cmp_ok( OK, '==', $r, "login auth"); |
| 107 |
} |
| 108 |
|
| 109 |
sub is_interactive { |
| 110 |
|
| 111 |
## no critic |
| 112 |
# borrowed from IO::Interactive |
| 113 |
my ($out_handle) = (@_, select); # Default to default output handle |
| 114 |
|
| 115 |
# Not interactive if output is not to terminal... |
| 116 |
return if not -t $out_handle; |
| 117 |
|
| 118 |
# If *ARGV is opened, we're interactive if... |
| 119 |
if (openhandle * ARGV) { |
| 120 |
|
| 121 |
# ...it's currently opened to the magic '-' file |
| 122 |
return -t *STDIN if defined $ARGV && $ARGV eq '-'; |
| 123 |
|
| 124 |
# ...it's at end-of-file and the next file is the magic '-' file |
| 125 |
return @ARGV > 0 && $ARGV[0] eq '-' && -t *STDIN if eof *ARGV; |
| 126 |
|
| 127 |
# ...it's directly attached to the terminal |
| 128 |
return -t *ARGV; |
| 129 |
} |
| 130 |
|
| 131 |
# If *ARGV isn't opened, it will be interactive if *STDIN is attached |
| 132 |
# to a terminal and either there are no files specified on the command line |
| 133 |
# or if there are files and the first is the magic '-' file |
| 134 |
return -t *STDIN && (@ARGV == 0 || $ARGV[0] eq '-'); |
| 135 |
} |
| 136 |
|
| 137 |
__END__ |
| 138 |
|
| 139 |
if ( ref $r ) { |
| 140 |
} else { |
| 141 |
warn $r; |
| 142 |
} |
| 143 |
#print Data::Dumper::Dumper($conn); |
| 144 |
#print Data::Dumper::Dumper($smtpd); |
| 145 |
|