Search in sources :

Example 1 with MockSmtpServer

use of com.fsck.k9.mail.transport.mockServer.MockSmtpServer in project k-9 by k9mail.

the class SmtpTransportTest method sendMessage_withSingleRecipient.

@Test
public void sendMessage_withSingleRecipient() throws Exception {
    Message message = getDefaultMessage();
    MockSmtpServer server = createServerAndSetupForPlainAuthentication();
    server.expect("MAIL FROM:<user@localhost>");
    server.output("250 OK");
    server.expect("RCPT TO:<user2@localhost>");
    server.output("250 OK");
    server.expect("DATA");
    server.output("354 End data with <CR><LF>.<CR><LF>");
    server.expect("[message data]");
    server.expect(".");
    server.output("250 OK: queued as 12345");
    server.expect("QUIT");
    server.output("221 BYE");
    server.closeConnection();
    SmtpTransport transport = startServerAndCreateSmtpTransport(server);
    transport.sendMessage(message);
    server.verifyConnectionClosed();
    server.verifyInteractionCompleted();
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MockSmtpServer(com.fsck.k9.mail.transport.mockServer.MockSmtpServer) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Example 2 with MockSmtpServer

use of com.fsck.k9.mail.transport.mockServer.MockSmtpServer in project k-9 by k9mail.

the class SmtpTransportTest method open_withAuthPlainExtension.

@Test
public void open_withAuthPlainExtension() throws Exception {
    MockSmtpServer server = new MockSmtpServer();
    server.output("220 localhost Simple Mail Transfer Service Ready");
    server.expect("EHLO [127.0.0.1]");
    server.output("250-localhost Hello client.localhost");
    server.output("250 AUTH PLAIN LOGIN");
    server.expect("AUTH PLAIN AHVzZXIAcGFzc3dvcmQ=");
    server.output("235 2.7.0 Authentication successful");
    SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.PLAIN, ConnectionSecurity.NONE);
    transport.open();
    server.verifyConnectionStillOpen();
    server.verifyInteractionCompleted();
}
Also used : MockSmtpServer(com.fsck.k9.mail.transport.mockServer.MockSmtpServer) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Example 3 with MockSmtpServer

use of com.fsck.k9.mail.transport.mockServer.MockSmtpServer in project k-9 by k9mail.

the class SmtpTransportTest method open_withXoauth2Extension_shouldInvalidateAndRetryOnInvalidJsonResponse.

@Test
public void open_withXoauth2Extension_shouldInvalidateAndRetryOnInvalidJsonResponse() throws Exception {
    MockSmtpServer server = new MockSmtpServer();
    server.output("220 localhost Simple Mail Transfer Service Ready");
    server.expect("EHLO [127.0.0.1]");
    server.output("250-localhost Hello client.localhost");
    server.output("250 AUTH XOAUTH2");
    server.expect("AUTH XOAUTH2 dXNlcj11c2VyAWF1dGg9QmVhcmVyIG9sZFRva2VuAQE=");
    server.output("334 " + XOAuth2ChallengeParserTest.INVALID_RESPONSE);
    server.expect("");
    server.output("535-5.7.1 Username and Password not accepted. Learn more at");
    server.output("535 5.7.1 http://support.google.com/mail/bin/answer.py?answer=14257 hx9sm5317360pbc.68");
    server.expect("AUTH XOAUTH2 dXNlcj11c2VyAWF1dGg9QmVhcmVyIG5ld1Rva2VuAQE=");
    server.output("235 2.7.0 Authentication successful");
    SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.XOAUTH2, ConnectionSecurity.NONE);
    transport.open();
    InOrder inOrder = inOrder(oAuth2TokenProvider);
    inOrder.verify(oAuth2TokenProvider).getToken(eq(USERNAME), anyLong());
    inOrder.verify(oAuth2TokenProvider).invalidateToken(USERNAME);
    inOrder.verify(oAuth2TokenProvider).getToken(eq(USERNAME), anyLong());
    server.verifyConnectionStillOpen();
    server.verifyInteractionCompleted();
}
Also used : InOrder(org.mockito.InOrder) MockSmtpServer(com.fsck.k9.mail.transport.mockServer.MockSmtpServer) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Example 4 with MockSmtpServer

use of com.fsck.k9.mail.transport.mockServer.MockSmtpServer in project k-9 by k9mail.

the class SmtpTransportTest method startServerAndCreateSmtpTransport.

private SmtpTransport startServerAndCreateSmtpTransport(MockSmtpServer server, AuthType authenticationType, ConnectionSecurity connectionSecurity, String password) throws Exception {
    server.start();
    String host = server.getHost();
    int port = server.getPort();
    ServerSettings serverSettings = new ServerSettings("smtp", host, port, connectionSecurity, authenticationType, USERNAME, password, CLIENT_CERTIFICATE_ALIAS);
    return new SmtpTransport(serverSettings, socketFactory, oAuth2TokenProvider);
}
Also used : ServerSettings(com.fsck.k9.mail.ServerSettings) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString)

Example 5 with MockSmtpServer

use of com.fsck.k9.mail.transport.mockServer.MockSmtpServer in project k-9 by k9mail.

the class SmtpTransportTest method sendMessagePipelining_with250and550ReplyforRecipientsAnd250ForMessage_shouldThrow.

@Test
public void sendMessagePipelining_with250and550ReplyforRecipientsAnd250ForMessage_shouldThrow() throws Exception {
    Message message = getMessageWithTwoRecipients();
    MockSmtpServer server = createServerAndSetupForPlainAuthentication("PIPELINING");
    server.expect("MAIL FROM:<user@localhost>");
    server.expect("RCPT TO:<user2@localhost>");
    server.expect("RCPT TO:<user3@localhost>");
    server.output("250 OK");
    server.output("250 OK");
    server.output("550 remote mail to <user3@localhost> not allowed");
    server.expect("QUIT");
    server.output("221 BYE");
    server.closeConnection();
    SmtpTransport transport = startServerAndCreateSmtpTransport(server);
    try {
        transport.sendMessage(message);
        fail("Expected exception");
    } catch (NegativeSmtpReplyException e) {
        assertEquals(550, e.getReplyCode());
        assertEquals("remote mail to <user3@localhost> not allowed", e.getReplyText());
    }
    server.verifyConnectionClosed();
    server.verifyInteractionCompleted();
}
Also used : MimeMessage(com.fsck.k9.mail.internet.MimeMessage) Message(com.fsck.k9.mail.Message) MockSmtpServer(com.fsck.k9.mail.transport.mockServer.MockSmtpServer) XOAuth2ChallengeParserTest(com.fsck.k9.mail.XOAuth2ChallengeParserTest) Test(org.junit.Test)

Aggregations

MockSmtpServer (com.fsck.k9.mail.transport.mockServer.MockSmtpServer)35 XOAuth2ChallengeParserTest (com.fsck.k9.mail.XOAuth2ChallengeParserTest)34 Test (org.junit.Test)34 MimeMessage (com.fsck.k9.mail.internet.MimeMessage)12 Message (com.fsck.k9.mail.Message)11 MessagingException (com.fsck.k9.mail.MessagingException)5 InOrder (org.mockito.InOrder)5 AuthenticationFailedException (com.fsck.k9.mail.AuthenticationFailedException)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 CertificateValidationException (com.fsck.k9.mail.CertificateValidationException)1 ServerSettings (com.fsck.k9.mail.ServerSettings)1