Repository: qpid-broker-j Updated Branches: refs/heads/master ff2980e2d -> ca088c28e
QPID-8046: [Broker-J] Add more tests Project: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/repo Commit: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/commit/ca088c28 Tree: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/tree/ca088c28 Diff: http://git-wip-us.apache.org/repos/asf/qpid-broker-j/diff/ca088c28 Branch: refs/heads/master Commit: ca088c28edee172f9d877d88905731070825c5bf Parents: ff2980e Author: Alex Rudyy <oru...@apache.org> Authored: Wed Nov 22 15:43:07 2017 +0000 Committer: Alex Rudyy <oru...@apache.org> Committed: Wed Nov 22 15:43:07 2017 +0000 ---------------------------------------------------------------------- .../auth/sasl/plain/PlainNegotiatorTest.java | 52 +++++++++++++++----- 1 file changed, 41 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/qpid-broker-j/blob/ca088c28/broker-core/src/test/java/org/apache/qpid/server/security/auth/sasl/plain/PlainNegotiatorTest.java ---------------------------------------------------------------------- diff --git a/broker-core/src/test/java/org/apache/qpid/server/security/auth/sasl/plain/PlainNegotiatorTest.java b/broker-core/src/test/java/org/apache/qpid/server/security/auth/sasl/plain/PlainNegotiatorTest.java index 5162073..ba53e60 100644 --- a/broker-core/src/test/java/org/apache/qpid/server/security/auth/sasl/plain/PlainNegotiatorTest.java +++ b/broker-core/src/test/java/org/apache/qpid/server/security/auth/sasl/plain/PlainNegotiatorTest.java @@ -20,7 +20,10 @@ package org.apache.qpid.server.security.auth.sasl.plain; +import static java.nio.charset.StandardCharsets.US_ASCII; import static org.junit.Assert.assertArrayEquals; +import static org.mockito.AdditionalMatchers.not; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; @@ -34,18 +37,23 @@ public class PlainNegotiatorTest extends QpidTestCase { private static final String VALID_PASSWORD = "testPassword"; private static final String VALID_USERNAME = "testUsername"; - private static final String VALID_RESPONSE = String.format("\0%s\0%s", VALID_USERNAME, VALID_PASSWORD); + public static final String RESPONSE_FORMAT_STRING = "\0%s\0%s"; + private static final String VALID_RESPONSE = String.format(RESPONSE_FORMAT_STRING, VALID_USERNAME, VALID_PASSWORD); private UsernamePasswordAuthenticationProvider _authenticationProvider; private PlainNegotiator _negotiator; - private AuthenticationResult _expectedResult; + private AuthenticationResult _successfulResult; + private AuthenticationResult _errorResult; @Override public void setUp() throws Exception { super.setUp(); - _expectedResult = mock(AuthenticationResult.class); + _successfulResult = mock(AuthenticationResult.class); + _errorResult = mock(AuthenticationResult.class); _authenticationProvider = mock(UsernamePasswordAuthenticationProvider.class); - when(_authenticationProvider.authenticate(eq(VALID_USERNAME), eq(VALID_PASSWORD))).thenReturn(_expectedResult); + when(_authenticationProvider.authenticate(eq(VALID_USERNAME), eq(VALID_PASSWORD))).thenReturn(_successfulResult); + when(_authenticationProvider.authenticate(eq(VALID_USERNAME), not(eq(VALID_PASSWORD)))).thenReturn(_errorResult); + when(_authenticationProvider.authenticate(not(eq(VALID_USERNAME)), anyString())).thenReturn(_errorResult); _negotiator = new PlainNegotiator(_authenticationProvider); } @@ -61,19 +69,41 @@ public class PlainNegotiatorTest extends QpidTestCase public void testHandleResponse() throws Exception { - final AuthenticationResult result = _negotiator.handleResponse(VALID_RESPONSE.getBytes()); + final AuthenticationResult result = _negotiator.handleResponse(VALID_RESPONSE.getBytes(US_ASCII)); verify(_authenticationProvider).authenticate(eq(VALID_USERNAME), eq(VALID_PASSWORD)); - assertEquals("Unexpected authentication result", _expectedResult, result); + assertEquals("Unexpected authentication result", _successfulResult, result); } public void testMultipleAuthenticationAttempts() throws Exception { - final AuthenticationResult firstResult = _negotiator.handleResponse(VALID_RESPONSE.getBytes()); - assertEquals("Unexpected first authentication result", _expectedResult, firstResult); - final AuthenticationResult secondResult = _negotiator.handleResponse(VALID_RESPONSE.getBytes()); + final AuthenticationResult firstResult = _negotiator.handleResponse(VALID_RESPONSE.getBytes(US_ASCII)); + assertEquals("Unexpected first authentication result", _successfulResult, firstResult); + final AuthenticationResult secondResult = _negotiator.handleResponse(VALID_RESPONSE.getBytes(US_ASCII)); assertEquals("Unexpected second authentication result", AuthenticationResult.AuthenticationStatus.ERROR, secondResult.getStatus()); } + public void testHandleInvalidUser() throws Exception + { + final AuthenticationResult result = _negotiator.handleResponse(String.format(RESPONSE_FORMAT_STRING, "invalidUser", VALID_PASSWORD).getBytes(US_ASCII)); + assertEquals("Unexpected authentication result", _errorResult, result); + } + + public void testHandleInvalidPassword() throws Exception + { + final AuthenticationResult result = _negotiator.handleResponse(String.format(RESPONSE_FORMAT_STRING, VALID_USERNAME, "invalidPassword").getBytes(US_ASCII)); + assertEquals("Unexpected authentication result", _errorResult, result); + } + + public void testHandleNeverSendAResponse() throws Exception + { + final AuthenticationResult firstResult = _negotiator.handleResponse(new byte[0]); + assertEquals("Unexpected authentication status", AuthenticationResult.AuthenticationStatus.CONTINUE, firstResult.getStatus()); + assertArrayEquals("Unexpected authentication challenge", new byte[0], firstResult.getChallenge()); + + final AuthenticationResult secondResult = _negotiator.handleResponse(new byte[0]); + assertEquals("Unexpected first authentication result", AuthenticationResult.AuthenticationStatus.ERROR, secondResult.getStatus()); + } + public void testHandleNoInitialResponse() throws Exception { final AuthenticationResult result = _negotiator.handleResponse(new byte[0]); @@ -81,7 +111,7 @@ public class PlainNegotiatorTest extends QpidTestCase assertArrayEquals("Unexpected authentication challenge", new byte[0], result.getChallenge()); final AuthenticationResult firstResult = _negotiator.handleResponse(VALID_RESPONSE.getBytes()); - assertEquals("Unexpected first authentication result", _expectedResult, firstResult); + assertEquals("Unexpected first authentication result", _successfulResult, firstResult); } public void testHandleNoInitialResponseNull() throws Exception @@ -91,6 +121,6 @@ public class PlainNegotiatorTest extends QpidTestCase assertArrayEquals("Unexpected authentication challenge", new byte[0], result.getChallenge()); final AuthenticationResult firstResult = _negotiator.handleResponse(VALID_RESPONSE.getBytes()); - assertEquals("Unexpected first authentication result", _expectedResult, firstResult); + assertEquals("Unexpected first authentication result", _successfulResult, firstResult); } } \ No newline at end of file --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@qpid.apache.org For additional commands, e-mail: commits-h...@qpid.apache.org