public void testAuth() throws Exception {
m_testConfiguration.setAuthorizedAddresses("128.0.0.1/8");
m_testConfiguration.setAuthorizingAnnounce();
finishSetUp(m_testConfiguration);
SMTPClient smtpProtocol = new SMTPClient();
smtpProtocol.connect("127.0.0.1", m_smtpListenerPort);
smtpProtocol.sendCommand("ehlo", InetAddress.getLocalHost().toString());
String[] capabilityRes = smtpProtocol.getReplyStrings();
List<String> capabilitieslist = new ArrayList<String>();
for (int i = 1; i < capabilityRes.length; i++) {
capabilitieslist.add(capabilityRes[i].substring(4));
}
assertTrue("anouncing auth required", capabilitieslist.contains("AUTH LOGIN PLAIN"));
// is this required or just for compatibility?
// assertTrue("anouncing auth required",
// capabilitieslist.contains("AUTH=LOGIN PLAIN"));
String userName = "test_user_smtp";
String noexistUserName = "noexist_test_user_smtp";
String sender = "test_user_smtp@localhost";
smtpProtocol.sendCommand("AUTH FOO", null);
assertEquals("expected error: unrecognized authentication type", 504, smtpProtocol.getReplyCode());
smtpProtocol.setSender(sender);
smtpProtocol.addRecipient("mail@sample.com");
assertEquals("expected 530 error", 530, smtpProtocol.getReplyCode());
assertFalse("user not existing", m_usersRepository.contains(noexistUserName));
smtpProtocol.sendCommand("AUTH PLAIN");
smtpProtocol.sendCommand(Base64.encodeAsString("\0" + noexistUserName + "\0pwd\0"));
// smtpProtocol.sendCommand(noexistUserName+"pwd".toCharArray());
assertEquals("expected error", 535, smtpProtocol.getReplyCode());
m_usersRepository.addUser(userName, "pwd");
smtpProtocol.sendCommand("AUTH PLAIN");
smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0wrongpwd\0"));
assertEquals("expected error", 535, smtpProtocol.getReplyCode());
smtpProtocol.sendCommand("AUTH PLAIN");
smtpProtocol.sendCommand(Base64.encodeAsString("\0" + userName + "\0pwd\0"));
assertEquals("authenticated", 235, smtpProtocol.getReplyCode());
smtpProtocol.sendCommand("AUTH PLAIN");
assertEquals("expected error: User has previously authenticated.", 503, smtpProtocol.getReplyCode());
smtpProtocol.addRecipient("mail@sample.com");
smtpProtocol.sendShortMessageData("Subject: test\r\n\r\nTest body testAuth\r\n");
smtpProtocol.quit();
// mail was propagated by SMTPServer
assertNotNull("mail received by mail server", m_mailServer.getLastMail());
}