diff options
Diffstat (limited to 'upstream/odl-aaa-moon/aaa/aaa-shiro/src/test/java/org/opendaylight/aaa/shiro/filters')
2 files changed, 196 insertions, 0 deletions
diff --git a/upstream/odl-aaa-moon/aaa/aaa-shiro/src/test/java/org/opendaylight/aaa/shiro/filters/AuthenticationListenerTest.java b/upstream/odl-aaa-moon/aaa/aaa-shiro/src/test/java/org/opendaylight/aaa/shiro/filters/AuthenticationListenerTest.java new file mode 100644 index 00000000..1c823525 --- /dev/null +++ b/upstream/odl-aaa-moon/aaa/aaa-shiro/src/test/java/org/opendaylight/aaa/shiro/filters/AuthenticationListenerTest.java @@ -0,0 +1,72 @@ +/* + * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.aaa.shiro.filters; + +import static org.junit.Assert.*; + +import ch.qos.logback.classic.spi.LoggingEvent; + +import java.util.List; + +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.SimpleAuthenticationInfo; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.junit.Test; +import org.opendaylight.aaa.shiro.TestAppender; +import org.opendaylight.aaa.shiro.filters.AuthenticationListener; + +/** + * Test AuthenticationListener, which is responsible for logging Accounting events. + * + * @author Ryan Goulding (ryandgoulding@gmail.com) + */ +public class AuthenticationListenerTest { + + @Test + public void testOnSuccess() throws Exception { + // sets up a successful authentication attempt + final AuthenticationListener authenticationListener = new AuthenticationListener(); + final UsernamePasswordToken authenticationToken = new UsernamePasswordToken(); + authenticationToken.setUsername("successfulUser1"); + authenticationToken.setHost("successfulHost1"); + final SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(); + // the following call produces accounting output + authenticationListener.onSuccess(authenticationToken, simpleAuthenticationInfo); + + // grab the latest log output and make sure it is in line with what is expected + final List<LoggingEvent> loggingEvents = TestAppender.getCurrentInstance().getEvents(); + // the latest logging event is the one we need to inspect + final int whichLoggingEvent = loggingEvents.size() - 1; + final LoggingEvent latestLoggingEvent = loggingEvents.get(whichLoggingEvent); + final String latestLogMessage = latestLoggingEvent.getMessage(); + assertEquals("Successful authentication attempt by successfulUser1 from successfulHost1", + latestLogMessage); + } + + @Test + public void testOnFailure() throws Exception { + // variables for an unsucessful authentication attempt + final AuthenticationListener authenticationListener = new AuthenticationListener(); + final UsernamePasswordToken authenticationToken = new UsernamePasswordToken(); + authenticationToken.setUsername("unsuccessfulUser1"); + authenticationToken.setHost("unsuccessfulHost1"); + final AuthenticationException authenticationException = + new AuthenticationException("test auth exception"); + // produces unsuccessful authentication attempt output + authenticationListener.onFailure(authenticationToken, authenticationException); + + // grab the latest log output and ensure it is in line with what is expected + final List<LoggingEvent> loggingEvents = TestAppender.getCurrentInstance().getEvents(); + final int whichLoggingEvent = loggingEvents.size() - 1; + final LoggingEvent latestLoggingEvent = loggingEvents.get(whichLoggingEvent); + final String latestLogMessage = latestLoggingEvent.getMessage(); + assertEquals("Unsuccessful authentication attempt by unsuccessfulUser1 from unsuccessfulHost1", + latestLogMessage); + } +}
\ No newline at end of file diff --git a/upstream/odl-aaa-moon/aaa/aaa-shiro/src/test/java/org/opendaylight/aaa/shiro/filters/AuthenticationTokenUtilsTest.java b/upstream/odl-aaa-moon/aaa/aaa-shiro/src/test/java/org/opendaylight/aaa/shiro/filters/AuthenticationTokenUtilsTest.java new file mode 100644 index 00000000..09331c52 --- /dev/null +++ b/upstream/odl-aaa-moon/aaa/aaa-shiro/src/test/java/org/opendaylight/aaa/shiro/filters/AuthenticationTokenUtilsTest.java @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2016 Brocade Communications Systems, Inc. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ + +package org.opendaylight.aaa.shiro.filters; + +import static org.junit.Assert.*; + +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.authc.UsernamePasswordToken; +import org.junit.Test; +import org.opendaylight.aaa.shiro.filters.AuthenticationTokenUtils; + +/** + * Tests authentication token output utilities. + * + * @author Ryan Goulding (ryandgoulding@gmail.com) + */ +public class AuthenticationTokenUtilsTest { + + /** + * A sample non-UsernamePasswordToken implementation for testing. + */ + private final class NotUsernamePasswordToken implements AuthenticationToken { + + @Override + public Object getPrincipal() { + return null; + } + + @Override + public Object getCredentials() { + return null; + } + } + + @Test + public void testIsUsernamePasswordToken() throws Exception { + // null test + final AuthenticationToken nullUsernamePasswordToken = null; + assertFalse(AuthenticationTokenUtils.isUsernamePasswordToken(nullUsernamePasswordToken)); + + // alternate implementation of AuthenticationToken + final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken(); + assertFalse(AuthenticationTokenUtils.isUsernamePasswordToken(notUsernamePasswordToken)); + + // positive test case + final AuthenticationToken positiveUsernamePasswordToken = new UsernamePasswordToken(); + assertTrue(AuthenticationTokenUtils.isUsernamePasswordToken(positiveUsernamePasswordToken)); + + } + + @Test + public void testExtractUsername() throws Exception { + // null test + final AuthenticationToken nullAuthenticationToken = null; + assertEquals(AuthenticationTokenUtils.DEFAULT_TOKEN, + AuthenticationTokenUtils.extractUsername(nullAuthenticationToken)); + + // non-UsernamePasswordToken test + final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken(); + assertEquals(AuthenticationTokenUtils.DEFAULT_TOKEN, + AuthenticationTokenUtils.extractUsername(notUsernamePasswordToken)); + + // null username test + final UsernamePasswordToken nullUsername = new UsernamePasswordToken(); + nullUsername.setUsername(null); + assertEquals(AuthenticationTokenUtils.DEFAULT_USERNAME, + AuthenticationTokenUtils.extractUsername(nullUsername)); + + // positive test + final UsernamePasswordToken positiveUsernamePasswordToken = new UsernamePasswordToken(); + final String testUsername = "testUser1"; + positiveUsernamePasswordToken.setUsername(testUsername); + assertEquals(testUsername, AuthenticationTokenUtils.extractUsername(positiveUsernamePasswordToken)); + } + + @Test + public void testExtractHostname() throws Exception { + // null test + final AuthenticationToken nullAuthenticationToken = null; + assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME, + AuthenticationTokenUtils.extractHostname(nullAuthenticationToken)); + + // non-UsernamePasswordToken test + final AuthenticationToken notUsernamePasswordToken = new NotUsernamePasswordToken(); + assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME, + AuthenticationTokenUtils.extractHostname(notUsernamePasswordToken)); + + // null hostname test + final UsernamePasswordToken nullHostname = new UsernamePasswordToken(); + nullHostname.setHost(null); + assertEquals(AuthenticationTokenUtils.DEFAULT_HOSTNAME, + AuthenticationTokenUtils.extractHostname(nullHostname)); + + // positive test + final UsernamePasswordToken positiveUsernamePasswordToken = new UsernamePasswordToken(); + final String testUsername = "testHostname1"; + positiveUsernamePasswordToken.setHost(testUsername); + assertEquals(testUsername, AuthenticationTokenUtils.extractHostname(positiveUsernamePasswordToken)); + } + + @Test + public void testGenerateUnsuccessfulAuthenticationMessage() throws Exception { + final UsernamePasswordToken unsuccessfulToken = new UsernamePasswordToken(); + unsuccessfulToken.setUsername("unsuccessfulUser1"); + unsuccessfulToken.setHost("unsuccessfulHost1"); + assertEquals("Unsuccessful authentication attempt by unsuccessfulUser1 from unsuccessfulHost1", + AuthenticationTokenUtils.generateUnsuccessfulAuthenticationMessage(unsuccessfulToken)); + } + + @Test + public void testGenerateSuccessfulAuthenticationMessage() throws Exception { + final UsernamePasswordToken successfulToken = new UsernamePasswordToken(); + successfulToken.setUsername("successfulUser1"); + successfulToken.setHost("successfulHost1"); + assertEquals("Successful authentication attempt by successfulUser1 from successfulHost1", + AuthenticationTokenUtils.generateSuccessfulAuthenticationMessage(successfulToken)); + } +} |