private void postAuthenticationDialogRequestingCapabilities(MockImapServer server) { postAuthenticationDialogRequestingCapabilities(server, 3); }
@Test public void open_authPlainWithoutAuthPlainCapability_shouldUseLoginMethod() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server); server.expect("2 LOGIN \"" + USERNAME + "\" \"" + PASSWORD + "\""); server.output("2 OK LOGIN completed"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_authXoauthWithSaslIr() throws Exception { settings.setAuthType(AuthType.XOAUTH2); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2"); server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING); server.output("2 OK Success"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_afterCloseWasCalled_shouldThrow() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server); server.expect("2 LOGIN \"" + USERNAME + "\" \"" + PASSWORD + "\""); server.output("2 OK LOGIN completed"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); imapConnection.close(); try { imapConnection.open(); fail("Expected exception"); } catch (IllegalStateException e) { assertEquals("open() called after close(). Check wrapped exception to see where close() was called.", e.getMessage()); } }
@Test public void open_authExternal() throws Exception { settings.setAuthType(AuthType.EXTERNAL); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=EXTERNAL"); server.expect("2 AUTHENTICATE EXTERNAL " + ByteString.encodeUtf8(USERNAME).base64()); server.output("2 OK Success"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_authPlain() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=PLAIN"); server.expect("2 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("2 OK Success"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_withCapabilitiesInInitialResponse_shouldNotIssuePreAuthCapabilitiesCommand() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); server.output("* OK [CAPABILITY IMAP4 IMAP4REV1 AUTH=PLAIN]"); server.expect("1 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("1 OK Success"); postAuthenticationDialogRequestingCapabilities(server, 2); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_authCramMd5() throws Exception { settings.setAuthType(AuthType.CRAM_MD5); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=CRAM-MD5"); server.expect("2 AUTHENTICATE CRAM-MD5"); server.output("+ " + ByteString.encodeUtf8("<0000.000000000@example.org>").base64()); server.expect("dXNlciA2ZjdiOTcyYjk5YTI4NDk4OTRhN2YyMmE3MGRhZDg0OQ=="); server.output("2 OK Success"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_authXoauthWithSaslIrInvalidatesAndRetriesNewTokenOn400Response() throws Exception { settings.setAuthType(AuthType.XOAUTH2); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2"); server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING); server.output("+ " + XOAuth2ChallengeParserTest.STATUS_400_RESPONSE); server.expect(""); server.output("2 NO SASL authentication failed"); server.expect("3 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING_RETRY); server.output("3 OK Success"); postAuthenticationDialogRequestingCapabilities(server, 4); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_authPlainWithAuthenticationFailure_shouldFallbackToLogin() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=PLAIN"); server.expect("2 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("2 NO Login Failure"); server.expect("3 LOGIN \"" + USERNAME + "\" \"" + PASSWORD + "\""); server.output("3 OK LOGIN completed"); postAuthenticationDialogRequestingCapabilities(server, 4); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_withNoCapabilitiesInInitialResponse_shouldIssuePreAuthCapabilitiesCommand() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); server.output("* OK example.org server"); server.expect("1 CAPABILITY"); server.output("* CAPABILITY IMAP4 IMAP4REV1 AUTH=PLAIN"); server.output("1 OK CAPABILITY Completed"); server.expect("2 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("2 OK Success"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); imapConnection.open(); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_authXoauthWithSaslIrWithOldTokenThrowsExceptionIfRetryFails() throws Exception { settings.setAuthType(AuthType.XOAUTH2); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "SASL-IR AUTH=XOAUTH AUTH=XOAUTH2"); server.expect("2 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING); server.output("+ r3j3krj3irj3oir3ojo"); server.expect(""); server.output("2 NO SASL authentication failed"); server.expect("3 AUTHENTICATE XOAUTH2 " + XOAUTH_STRING_RETRY); server.output("+ 433ba3a3a"); server.expect(""); server.output("3 NO SASL authentication failed"); postAuthenticationDialogRequestingCapabilities(server); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail(); } catch (AuthenticationFailedException e) { assertEquals("Command: AUTHENTICATE XOAUTH2; response: #3# [NO, SASL authentication failed]", e.getMessage()); } }