1use lib 't/lib';
2use strict;
3use warnings;
4
5use Net::IMAP::Server::Test;
6my $t = "Net::IMAP::Server::Test";
7
8$t->start_server_ok;
9
10# Connect over SSL
11$t->connect_ok;
12
13# We support PLAIN auth by default
14my ($cap) = $t->cmd_like(
15    "CAPABILITY",
16    "* CAPABILITY",
17    "tag OK",
18);
19
20like($cap, qr/\bAUTH=PLAIN\b/, "Advertises AUTH=PLAIN");
21unlike($cap, qr/\bAUTH=BOGUS\b/, "Doesn't advertise AUTH=BOGUS");
22
23# Try a bogus auth type
24$t->cmd_like("AUTHENTICATE BOGUS aaa", "tag NO Authentication type not supported");
25
26# Fail the auth by not base64-encoding
27$t->cmd_like("AUTHENTICATE PLAIN bogus", "tag BAD Invalid base64");
28
29# Omit the password
30use MIME::Base64;
31my $base64 = encode_base64("authz\0username"); chomp $base64;
32$t->cmd_like("AUTHENTICATE PLAIN $base64", "tag BAD Protocol failure");
33
34# Wrong password
35$base64 = encode_base64("authz\0username\0wrong"); chomp $base64;
36$t->cmd_like("AUTHENTICATE PLAIN $base64", "tag NO Invalid login");
37
38# Correct login
39$base64 = encode_base64("authz\0username\0password"); chomp $base64;
40$t->cmd_like("AUTHENTICATE PLAIN $base64", "tag OK");
41
42# Can't login again
43$t->cmd_like("AUTHENTICATE PLAIN $base64", "tag BAD Already logged in");
44$t->cmd_ok("LOGOUT");
45
46# Do the auth over two lines
47$t->connect_ok;
48$t->cmd_like("AUTHENTICATE PLAIN", "+");
49$t->line_like($base64, "tag OK");
50$t->cmd_ok("LOGOUT");
51
52# Test cancelling auth
53$t->connect_ok;
54$t->cmd_like("AUTHENTICATE PLAIN", "+");
55$t->line_like("*", "tag BAD Login cancelled");
56$t->cmd_ok("LOGOUT");
57
58# AUTHENTICATE PLAIN is disabled over non-SSL
59$t->connect_ok( "Non-SSL connection OK",
60    Class => "IO::Socket::INET",
61    PeerPort => $t->PORT,
62);
63$t->cmd_like("AUTHENTICATE PLAIN $base64", "* BAD [ALERT]", "tag NO");
64
65done_testing;
66