Author: jawi
Date: Tue Feb 23 09:34:40 2016
New Revision: 1731819
URL: http://svn.apache.org/viewvc?rev=1731819&view=rev
Log:
Some cleanups:
- simplify the implementation to use COWAL instead of a synchronized list
implementation;
- improve a bit on the logging of the context;
- migrated all unit tests to use the Assert-based TestNG constructs.
Modified:
ace/trunk/org.apache.ace.authentication/impl.bnd
ace/trunk/org.apache.ace.authentication/src/org/apache/ace/authentication/impl/AuthenticationServiceImpl.java
ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/impl/AuthenticationServiceImplTest.java
ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/basicauth/BasicHttpAuthenticationProcessorTest.java
ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/clientcert/ClientCertAuthenticationProcessorTest.java
ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/password/PasswordAuthenticationProcessorTest.java
Modified: ace/trunk/org.apache.ace.authentication/impl.bnd
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication/impl.bnd?rev=1731819&r1=1731818&r2=1731819&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication/impl.bnd (original)
+++ ace/trunk/org.apache.ace.authentication/impl.bnd Tue Feb 23 09:34:40 2016
@@ -2,6 +2,6 @@
Private-Package: org.apache.ace.authentication.impl
Bundle-Activator: org.apache.ace.authentication.impl.Activator
-Bundle-Version: 1.0.1
+Bundle-Version: 1.0.2
Bundle-Name: Apache ACE Authentication Service
Bundle-Description: Registers a pluggable authentication service for Apache ACE
Modified: ace/trunk/org.apache.ace.authentication/src/org/apache/ace/authentication/impl/AuthenticationServiceImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication/src/org/apache/ace/authentication/impl/AuthenticationServiceImpl.java?rev=1731819&r1=1731818&r2=1731819&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication/src/org/apache/ace/authentication/impl/AuthenticationServiceImpl.java (original)
+++ ace/trunk/org.apache.ace.authentication/src/org/apache/ace/authentication/impl/AuthenticationServiceImpl.java Tue Feb 23 09:34:40 2016
@@ -18,16 +18,18 @@
*/
package org.apache.ace.authentication.impl;
-import java.lang.ref.WeakReference;
import java.util.ArrayList;
-import java.util.Collections;
+import java.util.Iterator;
import java.util.List;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import javax.servlet.http.HttpServletRequest;
import org.apache.ace.authentication.api.AuthenticationProcessor;
import org.apache.ace.authentication.api.AuthenticationService;
import org.apache.felix.dm.DependencyManager;
-import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
+import org.osgi.service.useradmin.Role;
import org.osgi.service.useradmin.User;
import org.osgi.service.useradmin.UserAdmin;
@@ -35,73 +37,16 @@ import org.osgi.service.useradmin.UserAd
* Provides a basic implementation for {@link AuthenticationService} that returns the first matching user.
*/
public class AuthenticationServiceImpl implements AuthenticationService {
-
- /**
- * Provides a small container for {@link AuthenticationProcessor} instances.
- */
- private static class AuthenticationProcessorHolder implements Comparable<AuthenticationProcessorHolder> {
- private final ServiceReference<AuthenticationProcessor> m_serviceRef;
- private final WeakReference<AuthenticationProcessor> m_processor;
-
- public AuthenticationProcessorHolder(ServiceReference<AuthenticationProcessor> serviceRef, AuthenticationProcessor processor) {
- m_serviceRef = serviceRef;
- m_processor = new WeakReference<>(processor);
- }
-
- /**
- * {@inheritDoc}
- */
- public int compareTo(AuthenticationProcessorHolder other) {
- ServiceReference<AuthenticationProcessor> thatServiceRef = other.m_serviceRef;
- ServiceReference<AuthenticationProcessor> thisServiceRef = m_serviceRef;
- // Sort in reverse order so that the highest rankings come first...
- return thatServiceRef.compareTo(thisServiceRef);
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public boolean equals(Object obj) {
- if (this == obj) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (!(obj instanceof AuthenticationProcessorHolder)) {
- return false;
- }
- AuthenticationProcessorHolder other = (AuthenticationProcessorHolder) obj;
- return m_serviceRef.equals(other.m_serviceRef);
- }
-
- /**
- * @return the {@link AuthenticationProcessor}, can be <code>null</code> if it has been GC'd before this method call.
- */
- public AuthenticationProcessor getAuthenticationProcessor() {
- return m_processor.get();
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public int hashCode() {
- return m_serviceRef.hashCode() ^ m_processor.hashCode();
- }
- }
-
private volatile UserAdmin m_userAdmin;
private volatile LogService m_log;
- private final List<AuthenticationProcessorHolder> m_processors;
+ private final CopyOnWriteArrayList<AuthenticationProcessor> m_processors;
/**
* Creates a new {@link AuthenticationServiceImpl} instance.
*/
public AuthenticationServiceImpl() {
- m_processors = new ArrayList<>();
+ m_processors = new CopyOnWriteArrayList<>();
}
/**
@@ -109,13 +54,14 @@ public class AuthenticationServiceImpl i
*/
AuthenticationServiceImpl(LogService log) {
m_log = log;
- m_processors = new ArrayList<>();
+ m_processors = new CopyOnWriteArrayList<>();
}
/**
* Authenticates a user based on the given context information.
* <p>
- * This implementation returns the first {@link User}-object that is returned by a {@link AuthenticationProcessor} instance that can handle the given context.
+ * This implementation returns the first {@link User}-object that is returned by a {@link AuthenticationProcessor}
+ * instance that can handle the given context.
* </p>
*/
public User authenticate(Object... context) {
@@ -124,72 +70,87 @@ public class AuthenticationServiceImpl i
}
User result = null;
-
- m_log.log(LogService.LOG_DEBUG, "Authenticating user for: " + context);
-
- final List<AuthenticationProcessor> processors = getProcessors(context);
-
- int size = processors.size();
- for (int i = 0; i < size; i++) {
- result = processors.get(i).authenticate(m_userAdmin, context);
+ for (AuthenticationProcessor processor : getProcessors(context)) {
+ result = processor.authenticate(m_userAdmin, context);
if (result != null) {
- m_log.log(LogService.LOG_DEBUG, "Authenticated user (" + context + ") as: " + result.getName());
break;
}
}
+ if (result != null) {
+ m_log.log(LogService.LOG_DEBUG, String.format("Context (%s) authenticated as user %s", describeContext(context), result.getName()));
+ }
+ else {
+ m_log.log(LogService.LOG_WARNING, String.format("Context (%s) NOT authenticated: no matching user found!", describeContext(context)));
+ }
+
return result;
}
/**
* Returns all applicable {@link AuthenticationProcessor}s for the given context.
*
- * @param context the context for which to return all applicable authentication processors, cannot be <code>null</code> or an empty array.
+ * @param context
+ * the context for which to return all applicable authentication processors, cannot be <code>null</code>
+ * or an empty array.
* @return an array of applicable authentication processors, never <code>null</code>.
*/
final List<AuthenticationProcessor> getProcessors(Object... context) {
- final List<AuthenticationProcessorHolder> processors;
- synchronized (m_processors) {
- processors = new ArrayList<>(m_processors);
- }
- // Sort on service ranking...
- Collections.sort(processors);
-
- int size = processors.size();
-
- List<AuthenticationProcessor> result = new ArrayList<>(size);
- for (int i = 0; i < size; i++) {
- AuthenticationProcessor authenticationProcessor = processors.get(i).getAuthenticationProcessor();
- // Can be null if it is already GC'd for some reason...
- if ((authenticationProcessor != null) && authenticationProcessor.canHandle(context)) {
- result.add(authenticationProcessor);
+ List<AuthenticationProcessor> processors = new ArrayList<>(m_processors);
+
+ Iterator<AuthenticationProcessor> iter = processors.iterator();
+ while (iter.hasNext()) {
+ AuthenticationProcessor authenticationProcessor = iter.next();
+ if (!authenticationProcessor.canHandle(context)) {
+ iter.remove();
}
}
-
- return result;
+ return processors;
}
/**
* Called by {@link DependencyManager} upon adding a new {@link AuthenticationProcessor}.
*
- * @param serviceRef the service reference of the authentication processor to add;
- * @param processor the authentication processor to add.
+ * @param processor
+ * the authentication processor to add.
*/
- protected void addAuthenticationProcessor(ServiceReference<AuthenticationProcessor> serviceRef, AuthenticationProcessor processor) {
- synchronized (m_processors) {
- m_processors.add(new AuthenticationProcessorHolder(serviceRef, processor));
- }
+ protected void addAuthenticationProcessor(AuthenticationProcessor processor) {
+ m_processors.addIfAbsent(processor);
}
/**
* Called by {@link DependencyManager} upon removal of a {@link AuthenticationProcessor}.
*
- * @param serviceRef the service reference of the authentication processor to remove;
- * @param processor the authentication processor to remove.
+ * @param processor
+ * the authentication processor to remove.
*/
- protected void removeAuthenticationProcessor(ServiceReference<AuthenticationProcessor> serviceRef, AuthenticationProcessor processor) {
- synchronized (m_processors) {
- m_processors.remove(new AuthenticationProcessorHolder(serviceRef, processor));
+ protected void removeAuthenticationProcessor(AuthenticationProcessor processor) {
+ m_processors.remove(processor);
+ }
+
+ private String describeContext(Object... context) {
+ StringBuilder sb = new StringBuilder("[");
+ for (Object obj : context) {
+ if (sb.length() > 1) {
+ sb.append(", ");
+ }
+ if (obj instanceof Role) {
+ sb.append(((Role) obj).getType() == Role.USER ? "User(" : "Group(");
+ sb.append(((Role) obj).getName());
+ sb.append(")");
+ }
+ else if (obj instanceof HttpServletRequest) {
+ sb.append("HttpServletRequest(");
+ sb.append(((HttpServletRequest) obj).getPathInfo());
+ sb.append(")");
+ }
+ else if (obj instanceof byte[]) {
+ sb.append("byte[]{").append(((byte[]) obj).length).append(")");
+ }
+ else {
+ sb.append(obj);
+ }
}
+ return sb.append("]").toString();
}
}
Modified: ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/impl/AuthenticationServiceImplTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/impl/AuthenticationServiceImplTest.java?rev=1731819&r1=1731818&r2=1731819&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/impl/AuthenticationServiceImplTest.java (original)
+++ ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/impl/AuthenticationServiceImplTest.java Tue Feb 23 09:34:40 2016
@@ -25,6 +25,10 @@ import static org.mockito.Matchers.anySt
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
import java.util.Date;
import java.util.List;
@@ -33,7 +37,6 @@ import org.apache.ace.authentication.api
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
-import org.osgi.framework.ServiceReference;
import org.osgi.service.log.LogService;
import org.osgi.service.useradmin.User;
import org.osgi.service.useradmin.UserAdmin;
@@ -44,7 +47,7 @@ import org.testng.annotations.Test;
* Test cases for {@link AuthenticationServiceImpl}.
*/
public class AuthenticationServiceImplTest {
-
+
private LogService m_log;
@BeforeMethod(alwaysRun = true)
@@ -65,7 +68,7 @@ public class AuthenticationServiceImplTe
*/
@Test(groups = { UNIT })
public void testAuthenticateFailsWithoutAuthProcessors() {
- assert createAuthenticationService().authenticate("foo", "bar") == null;
+ assertNull(createAuthenticationService().authenticate("foo", "bar"));
}
/**
@@ -82,22 +85,23 @@ public class AuthenticationServiceImplTe
@Test(groups = { UNIT })
public void testAuthenticateFailsWithSingleAuthProcessorAndWrongContext() {
AuthenticationServiceImpl authService = createAuthenticationService();
-
+
AuthenticationProcessor authProc = mock(AuthenticationProcessor.class);
when(authProc.canHandle(anyString())).thenReturn(Boolean.TRUE);
registerAuthProcessor(authService, authProc);
- assert authService.authenticate("foo", "bar") == null;
+ assertNull(authService.authenticate("foo", "bar"));
}
/**
- * Tests that with multiple authentication processors, authentication will take place if it is given the correct context.
+ * Tests that with multiple authentication processors, authentication will take place if it is given the correct
+ * context.
*/
@Test(groups = { UNIT })
public void testAuthenticateSucceedsWithMultipleAuthProcessors() {
Date now = new Date();
-
+
User user1 = mock(User.class);
User user2 = mock(User.class);
@@ -108,7 +112,7 @@ public class AuthenticationServiceImplTe
return (args.length == 1 && args[0] instanceof Date);
}
});
- when(authProc1.authenticate(Mockito.<UserAdmin>any(), eq(now))).thenReturn(user1);
+ when(authProc1.authenticate(Mockito.<UserAdmin> any(), eq(now))).thenReturn(user1);
AuthenticationProcessor authProc2 = mock(AuthenticationProcessor.class);
when(authProc2.canHandle(anyString())).thenAnswer(new Answer<Boolean>() {
@@ -117,7 +121,7 @@ public class AuthenticationServiceImplTe
return (args.length == 1 && args[0] instanceof String);
}
});
- when(authProc2.authenticate(Mockito.<UserAdmin>any(), eq("foo"))).thenReturn(user2);
+ when(authProc2.authenticate(Mockito.<UserAdmin> any(), eq("foo"))).thenReturn(user2);
AuthenticationServiceImpl authService = createAuthenticationService();
@@ -125,16 +129,17 @@ public class AuthenticationServiceImplTe
registerAuthProcessor(authService, authProc2);
User result = authService.authenticate("foo");
- assert result != null;
- assert user2 == result;
-
+ assertNotNull(result);
+ assertEquals(user2, result);
+
result = authService.authenticate(now);
- assert result != null;
- assert user1 == result;
+ assertNotNull(result);
+ assertEquals(user1, result);
}
/**
- * Tests that with a single authentication processors, authentication will take place if it is given the correct context.
+ * Tests that with a single authentication processors, authentication will take place if it is given the correct
+ * context.
*/
@Test(groups = { UNIT })
public void testAuthenticateSucceedsWithSingleAuthProcessorAndCorrectContext() {
@@ -144,11 +149,11 @@ public class AuthenticationServiceImplTe
AuthenticationProcessor authProc = mock(AuthenticationProcessor.class);
when(authProc.canHandle(anyString())).thenReturn(Boolean.TRUE);
- when(authProc.authenticate(Mockito.<UserAdmin>any(), eq("foo"))).thenReturn(user);
+ when(authProc.authenticate(Mockito.<UserAdmin> any(), eq("foo"))).thenReturn(user);
registerAuthProcessor(authService, authProc);
- assert authService.authenticate("foo") != null;
+ assertNotNull(authService.authenticate("foo"));
}
/**
@@ -157,7 +162,7 @@ public class AuthenticationServiceImplTe
@Test(groups = { UNIT })
public void testGetProcessorsSelectsCorrectProcessorsBasedOnContext() {
Date now = new Date();
-
+
User user1 = mock(User.class);
User user2 = mock(User.class);
@@ -168,7 +173,7 @@ public class AuthenticationServiceImplTe
return (args.length == 1 && args[0] instanceof Date);
}
});
- when(authProc1.authenticate(Mockito.<UserAdmin>any(), eq(now))).thenReturn(user1);
+ when(authProc1.authenticate(Mockito.<UserAdmin> any(), eq(now))).thenReturn(user1);
AuthenticationProcessor authProc2 = mock(AuthenticationProcessor.class);
when(authProc2.canHandle(anyString())).thenAnswer(new Answer<Boolean>() {
@@ -177,7 +182,7 @@ public class AuthenticationServiceImplTe
return (args.length == 1 && args[0] instanceof String);
}
});
- when(authProc2.authenticate(Mockito.<UserAdmin>any(), eq("foo"))).thenReturn(user2);
+ when(authProc2.authenticate(Mockito.<UserAdmin> any(), eq("foo"))).thenReturn(user2);
AuthenticationServiceImpl authService = createAuthenticationService();
@@ -185,16 +190,16 @@ public class AuthenticationServiceImplTe
registerAuthProcessor(authService, authProc2);
List<AuthenticationProcessor> processors = authService.getProcessors("foo");
- assert processors != null;
- assert 1 == processors.size();
-
+ assertNotNull(processors);
+ assertEquals(processors.size(), 1);
+
processors = authService.getProcessors(now);
- assert processors != null;
- assert 1 == processors.size();
-
+ assertNotNull(processors);
+ assertEquals(processors.size(), 1);
+
processors = authService.getProcessors(new Object());
- assert processors != null;
- assert processors.isEmpty();
+ assertNotNull(processors);
+ assertTrue(processors.isEmpty());
}
/**
@@ -203,13 +208,8 @@ public class AuthenticationServiceImplTe
private AuthenticationServiceImpl createAuthenticationService() {
return new AuthenticationServiceImpl(m_log);
}
-
- /**
- * @param authService
- * @param authProcessor
- */
+
private void registerAuthProcessor(AuthenticationServiceImpl authService, AuthenticationProcessor authProcessor) {
- ServiceReference<AuthenticationProcessor> sr = mock(ServiceReference.class);
- authService.addAuthenticationProcessor(sr, authProcessor);
+ authService.addAuthenticationProcessor(authProcessor);
}
}
Modified: ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/basicauth/BasicHttpAuthenticationProcessorTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/basicauth/BasicHttpAuthenticationProcessorTest.java?rev=1731819&r1=1731818&r2=1731819&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/basicauth/BasicHttpAuthenticationProcessorTest.java (original)
+++ ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/basicauth/BasicHttpAuthenticationProcessorTest.java Tue Feb 23 09:34:40 2016
@@ -25,6 +25,11 @@ import static org.apache.ace.test.utils.
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
import java.util.Dictionary;
import java.util.Hashtable;
@@ -42,7 +47,7 @@ import org.testng.annotations.Test;
* Test cases for {@link BasicHttpAuthenticationProcessor}.
*/
public class BasicHttpAuthenticationProcessorTest {
-
+
private UserAdmin m_userAdmin;
private HttpServletRequest m_servletRequest;
@@ -58,7 +63,7 @@ public class BasicHttpAuthenticationProc
@Test(groups = { UNIT })
public void testAuthenticateEmptyAuthenticationHeaderYieldsNull() {
User result = new BasicHttpAuthenticationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Expected no result!";
+ assertNull(result, "Expected no result!");
}
/**
@@ -67,15 +72,15 @@ public class BasicHttpAuthenticationProc
@Test(groups = { UNIT })
public void testAuthenticateInvalidAuthenticationHeaderYieldsNull() {
when(m_servletRequest.getHeader(AUTHORIZATION_HEADER)).thenReturn(createAuthHeaderValue("bob"));
-
+
User user = mock(User.class);
when(user.getName()).thenReturn("bob");
when(user.hasCredential(eq("password"), eq("secret"))).thenReturn(Boolean.TRUE);
when(m_userAdmin.getUser(eq("username"), eq("bob"))).thenReturn(user);
-
+
User result = new BasicHttpAuthenticationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Expected no result!";
+ assertNull(result, "Expected no result!");
}
/**
@@ -84,7 +89,7 @@ public class BasicHttpAuthenticationProc
@Test(groups = { UNIT })
public void testAuthenticateKnownUserWithInvalidPasswordYieldsNull() {
when(m_servletRequest.getHeader(AUTHORIZATION_HEADER)).thenReturn(createAuthHeaderValue("bob:secret"));
-
+
User user = mock(User.class);
when(user.getName()).thenReturn("bob");
when(user.hasCredential(eq("password"), eq("otherSecret"))).thenReturn(Boolean.TRUE);
@@ -92,7 +97,7 @@ public class BasicHttpAuthenticationProc
when(m_userAdmin.getUser(eq("username"), eq("bob"))).thenReturn(user);
User result = new BasicHttpAuthenticationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Expected no result!";
+ assertNull(result, "Expected no result!");
}
/**
@@ -101,7 +106,7 @@ public class BasicHttpAuthenticationProc
@Test(groups = { UNIT })
public void testAuthenticateKnownUserYieldsValidResult() {
when(m_servletRequest.getHeader(AUTHORIZATION_HEADER)).thenReturn(createAuthHeaderValue("bob:secret"));
-
+
User user = mock(User.class);
when(user.getName()).thenReturn("bob");
when(user.hasCredential(eq("password"), eq("secret"))).thenReturn(Boolean.TRUE);
@@ -109,9 +114,9 @@ public class BasicHttpAuthenticationProc
when(m_userAdmin.getUser(eq("username"), eq("bob"))).thenReturn(user);
User result = new BasicHttpAuthenticationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result != null : "Expected a valid user to be returned!";
-
- assert "bob".equals(user.getName()) : "Expected user bob to be returned!";
+ assertNotNull(result, "Expected a valid user to be returned!");
+
+ assertEquals(user.getName(), "bob", "Expected user bob to be returned!");
}
/**
@@ -120,9 +125,9 @@ public class BasicHttpAuthenticationProc
@Test(groups = { UNIT })
public void testAuthenticateNonBase64AuthenticationHeaderYieldsNull() {
when(m_servletRequest.getHeader(AUTHORIZATION_HEADER)).thenReturn("foo");
-
+
User result = new BasicHttpAuthenticationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Expected no result!";
+ assertNull(result, "Expected no result!");
}
/**
@@ -139,9 +144,9 @@ public class BasicHttpAuthenticationProc
@Test(groups = { UNIT })
public void testAuthenticateUnknownUserYieldsNull() {
when(m_servletRequest.getHeader(AUTHORIZATION_HEADER)).thenReturn(createAuthHeaderValue("alice:secret"));
-
+
User result = new BasicHttpAuthenticationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Expected no result!";
+ assertNull(result, "Expected no result!");
}
/**
@@ -149,7 +154,7 @@ public class BasicHttpAuthenticationProc
*/
@Test(groups = { UNIT })
public void testCanHandleDoesAcceptServletRequest() {
- assert new BasicHttpAuthenticationProcessor().canHandle(mock(HttpServletRequest.class));
+ assertTrue(new BasicHttpAuthenticationProcessor().canHandle(mock(HttpServletRequest.class)));
}
/**
@@ -167,61 +172,61 @@ public class BasicHttpAuthenticationProc
public void testCanHandleDoesNotAcceptNull() {
new BasicHttpAuthenticationProcessor().canHandle((Object[]) null);
}
-
+
/**
* Tests that canHandle yields false for any object other than {@link HttpServletRequest}.
*/
@Test(groups = { UNIT })
public void testCanHandleDoesNotAcceptUnhandledContext() {
- assert new BasicHttpAuthenticationProcessor().canHandle(new Object()) == false;
+ assertFalse(new BasicHttpAuthenticationProcessor().canHandle(new Object()));
}
-
+
/**
- * Tests that updated throws an exception for missing "key.username" property.
+ * Tests that updated throws an exception for missing "key.username" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptEmptyKeyUsername() throws ConfigurationException {
Dictionary<String, Object> props = new Hashtable<>();
props.put(PROPERTY_KEY_USERNAME, "");
props.put(PROPERTY_KEY_PASSWORD, "foo");
-
+
new BasicHttpAuthenticationProcessor().updated(props);
}
-
+
/**
- * Tests that updated throws an exception for missing "key.username" property.
+ * Tests that updated throws an exception for missing "key.username" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptMissingKeyUsername() throws ConfigurationException {
Dictionary<String, Object> props = new Hashtable<>();
props.put(PROPERTY_KEY_PASSWORD, "foo");
-
+
new BasicHttpAuthenticationProcessor().updated(props);
}
-
+
/**
- * Tests that updated throws an exception for missing "key.password" property.
+ * Tests that updated throws an exception for missing "key.password" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptMissingKeyPassword() throws ConfigurationException {
Dictionary<String, Object> props = new Hashtable<>();
props.put(PROPERTY_KEY_USERNAME, "foo");
-
+
new BasicHttpAuthenticationProcessor().updated(props);
}
-
+
/**
- * Tests that updated throws an exception for missing "key.password" property.
+ * Tests that updated throws an exception for missing "key.password" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptEmptyKeyPassword() throws ConfigurationException {
Dictionary<String, Object> props = new Hashtable<>();
props.put(PROPERTY_KEY_USERNAME, "foo");
props.put(PROPERTY_KEY_PASSWORD, "");
-
+
new BasicHttpAuthenticationProcessor().updated(props);
}
-
+
/**
* Tests that updated does not throw an exception for a correct configuration.
*/
@@ -229,18 +234,18 @@ public class BasicHttpAuthenticationProc
public void testUpdatedDoesAcceptCorrectProperties() throws ConfigurationException {
final String keyUsername = "foo";
final String keyPassword = "bar";
-
+
Dictionary<String, Object> props = new Hashtable<>();
props.put(PROPERTY_KEY_USERNAME, keyUsername);
props.put(PROPERTY_KEY_PASSWORD, keyPassword);
-
+
BasicHttpAuthenticationProcessor processor = new BasicHttpAuthenticationProcessor();
processor.updated(props);
-
+
// Test whether we can use the new properties...
when(m_servletRequest.getHeader(AUTHORIZATION_HEADER)).thenReturn(createAuthHeaderValue("bob:secret"));
-
+
User user = mock(User.class);
when(user.getName()).thenReturn("bob");
when(user.hasCredential(eq(keyPassword), eq("secret"))).thenReturn(Boolean.TRUE);
@@ -248,11 +253,10 @@ public class BasicHttpAuthenticationProc
when(m_userAdmin.getUser(eq(keyUsername), eq("bob"))).thenReturn(user);
User result = processor.authenticate(m_userAdmin, m_servletRequest);
- assert result != null : "Expected a valid user to be returned!";
-
- assert "bob".equals(user.getName()) : "Expected user bob to be returned!";
- }
+ assertNotNull(result, "Expected a valid user to be returned!");
+ assertEquals(user.getName(), "bob", "Expected user bob to be returned!");
+ }
/**
* @return the basic authentication header, never <code>null</code>.
Modified: ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/clientcert/ClientCertAuthenticationProcessorTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/clientcert/ClientCertAuthenticationProcessorTest.java?rev=1731819&r1=1731818&r2=1731819&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/clientcert/ClientCertAuthenticationProcessorTest.java (original)
+++ ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/clientcert/ClientCertAuthenticationProcessorTest.java Tue Feb 23 09:34:40 2016
@@ -27,6 +27,11 @@ import static org.apache.ace.test.utils.
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
import java.security.KeyPair;
import java.security.PublicKey;
@@ -133,7 +138,7 @@ public class ClientCertAuthenticationPro
@Test(groups = { UNIT })
public void testAuthenticateNoCertificateChainYieldsNull() {
User result = createAuthorizationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Did not expect a valid user to be returned!";
+ assertNull(result, "Did not expect a valid user to be returned!");
}
/**
@@ -144,7 +149,7 @@ public class ClientCertAuthenticationPro
when(m_servletRequest.getAttribute(ATTRIBUTE_X509_CERTIFICATE)).thenReturn(new X509Certificate[0]);
User result = createAuthorizationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Did not expect a valid user to be returned!";
+ assertNull(result, "Did not expect a valid user to be returned!");
}
/**
@@ -164,7 +169,7 @@ public class ClientCertAuthenticationPro
when(m_userAdmin.getUser(eq("username"), eq("bob"))).thenReturn(user);
User result = createAuthorizationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Did not expect a valid user to be returned!";
+ assertNull(result, "Did not expect a valid user to be returned!");
}
/**
@@ -185,7 +190,7 @@ public class ClientCertAuthenticationPro
when(m_userAdmin.getUser(eq("username"), eq("bob"))).thenReturn(user);
User result = createAuthorizationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Did not expect a valid user to be returned!";
+ assertNull(result, "Did not expect a valid user to be returned!");
}
/**
@@ -203,9 +208,9 @@ public class ClientCertAuthenticationPro
when(m_userAdmin.getUser(eq("username"), eq("bob"))).thenReturn(user);
User result = createAuthorizationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result != null : "Expected a valid user to be returned!";
+ assertNotNull(result, "Expected a valid user to be returned!");
- assert "bob".equals(user.getName()) : "Expected bob to be returned as user!";
+ assertEquals(user.getName(), "bob", "Expected bob to be returned as user!");
}
/**
@@ -235,9 +240,9 @@ public class ClientCertAuthenticationPro
when(m_userAdmin.getUser(eq(lookupKey), eq("DC=corp,DC=acme,OU=dev,CN=Bob"))).thenReturn(user);
User result = processor.authenticate(m_userAdmin, m_servletRequest);
- assert result != null : "Expected a valid user to be returned!";
+ assertNotNull(result, "Expected a valid user to be returned!");
- assert "bob".equals(user.getName()) : "Expected bob to be returned as user!";
+ assertEquals(user.getName(), "bob", "Expected bob to be returned as user!");
}
/**
@@ -249,7 +254,7 @@ public class ClientCertAuthenticationPro
when(m_servletRequest.getAttribute(ATTRIBUTE_X509_CERTIFICATE)).thenReturn(createValidCertificateChain("bob"));
User result = createAuthorizationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Did not expect a valid user to be returned!";
+ assertNull(result, "Did not expect a valid user to be returned!");
}
/**
@@ -268,7 +273,7 @@ public class ClientCertAuthenticationPro
when(m_servletRequest.getAttribute(ATTRIBUTE_X509_CERTIFICATE)).thenReturn(createValidCertificateChain("bob"));
User result = createAuthorizationProcessor().authenticate(m_userAdmin, m_servletRequest);
- assert result == null : "Did not expect a valid user to be returned!";
+ assertNull(result, "Did not expect a valid user to be returned!");
}
/**
@@ -278,7 +283,7 @@ public class ClientCertAuthenticationPro
public void testCanHandleDoesAcceptServletRequest() {
when(m_servletRequest.getAttribute(ATTRIBUTE_X509_CERTIFICATE)).thenReturn(createValidCertificateChain("alice"));
- assert createAuthorizationProcessor().canHandle(m_servletRequest);
+ assertTrue(createAuthorizationProcessor().canHandle(m_servletRequest));
}
/**
@@ -302,7 +307,7 @@ public class ClientCertAuthenticationPro
*/
@Test(groups = { UNIT })
public void testCanHandleDoesNotAcceptUnhandledContext() {
- assert createAuthorizationProcessor().canHandle(new Object()) == false;
+ assertFalse(createAuthorizationProcessor().canHandle(new Object()));
}
/**
@@ -334,9 +339,9 @@ public class ClientCertAuthenticationPro
when(m_userAdmin.getUser(eq(lookupKey), eq("alice"))).thenReturn(user);
User result = processor.authenticate(m_userAdmin, m_servletRequest);
- assert result != null : "Expected a valid user to be returned!";
+ assertNotNull(result, "Expected a valid user to be returned!");
- assert "alice".equals(user.getName()) : "Expected alice to be returned as user!";
+ assertEquals(user.getName(), "alice", "Expected alice to be returned as user!");
}
/**
@@ -432,9 +437,12 @@ public class ClientCertAuthenticationPro
/**
* Creates a new certificate.
*
- * @param name the (common) name of the certificate;
- * @param notBefore the date after which the certificate is valid;
- * @param notAfter the date until the certificate is valid.
+ * @param name
+ * the (common) name of the certificate;
+ * @param notBefore
+ * the date after which the certificate is valid;
+ * @param notAfter
+ * the date until the certificate is valid.
* @return a new {@link X509Certificate}, never <code>null</code>.
*/
private X509Certificate createCertificate(String name, final Date notBefore, final Date notAfter) {
@@ -445,23 +453,24 @@ public class ClientCertAuthenticationPro
/**
* Creates a new (valid) chain with certificate(s) valid from yesterday until tomorrow.
*
- * @param dns the distinguished names of the certificates in the returned chain.
+ * @param dns
+ * the distinguished names of the certificates in the returned chain.
* @return a new chain with {@link X509Certificate}s, never <code>null</code>.
*/
private X509Certificate[] createValidCertificateChainWithDN(String... dns) {
X509Certificate[] result = new X509Certificate[dns.length];
-
+
X500Principal signerDN = m_keystore.getCA_DN();
KeyPair signerKeyPair = m_keystore.getCA_KeyPair();
for (int i = 0; i < result.length; i++) {
KeyPair certKeyPair = m_keystore.generateKeyPair();
-
+
String dn = dns[i];
int idx = result.length - i - 1;
-
+
result[idx] = m_keystore.createCertificate(signerDN, signerKeyPair.getPrivate(), dn, yesterday(), tomorrow(), certKeyPair.getPublic());
-
+
signerDN = result[idx].getSubjectX500Principal();
signerKeyPair = certKeyPair;
}
@@ -471,7 +480,8 @@ public class ClientCertAuthenticationPro
/**
* Creates a new (valid) certificate valid from yesterday until tomorrow.
*
- * @param name the (common) name of the certificate;
+ * @param name
+ * the (common) name of the certificate;
* @return a new {@link X509Certificate}, never <code>null</code>.
*/
private X509Certificate[] createValidCertificateChain(String name) {
@@ -483,7 +493,8 @@ public class ClientCertAuthenticationPro
/**
* Creates a new (expired) certificate valid from two days ago until yesterday.
*
- * @param name the (common) name of the certificate;
+ * @param name
+ * the (common) name of the certificate;
* @return a new {@link X509Certificate}, never <code>null</code>.
*/
private X509Certificate[] createExpiredCertificateChain(String name) {
@@ -495,7 +506,8 @@ public class ClientCertAuthenticationPro
/**
* Creates a new (not yet valid) certificate valid from tomorrow until the day after tomorrow.
*
- * @param name the (common) name of the certificate;
+ * @param name
+ * the (common) name of the certificate;
* @return a new {@link X509Certificate}, never <code>null</code>.
*/
private X509Certificate[] createNotValidCertificateChain(String name) {
Modified: ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/password/PasswordAuthenticationProcessorTest.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/password/PasswordAuthenticationProcessorTest.java?rev=1731819&r1=1731818&r2=1731819&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/password/PasswordAuthenticationProcessorTest.java (original)
+++ ace/trunk/org.apache.ace.authentication/test/org/apache/ace/authentication/processor/password/PasswordAuthenticationProcessorTest.java Tue Feb 23 09:34:40 2016
@@ -25,6 +25,11 @@ import static org.apache.ace.test.utils.
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+import static org.testng.Assert.assertEquals;
+import static org.testng.Assert.assertFalse;
+import static org.testng.Assert.assertNotNull;
+import static org.testng.Assert.assertNull;
+import static org.testng.Assert.assertTrue;
import java.util.Dictionary;
import java.util.Hashtable;
@@ -43,7 +48,7 @@ import org.testng.annotations.Test;
* Test cases for {@link PasswordAuthenticationProcessor}.
*/
public class PasswordAuthenticationProcessorTest {
-
+
private UserAdmin m_userAdmin;
@BeforeMethod(alwaysRun = true)
@@ -58,7 +63,7 @@ public class PasswordAuthenticationProce
@Test(groups = { UNIT })
public void testAuthenticateEmptyUserNameYieldsNull() {
User result = new PasswordAuthenticationProcessor().authenticate(m_userAdmin, "", "secret");
- assert result == null : "Expected no valid user to be returned!";
+ assertNull(result, "Expected no valid user to be returned!");
}
/**
@@ -73,7 +78,7 @@ public class PasswordAuthenticationProce
when(m_userAdmin.getUser(eq("username"), eq("bob"))).thenReturn(user);
User result = new PasswordAuthenticationProcessor().authenticate(m_userAdmin, "bob", "secret");
- assert result == null : "Expected no valid user to be returned!";
+ assertNull(result, "Expected no valid user to be returned!");
}
/**
@@ -88,9 +93,9 @@ public class PasswordAuthenticationProce
when(m_userAdmin.getUser(eq("username"), eq("bob"))).thenReturn(user);
User result = new PasswordAuthenticationProcessor().authenticate(m_userAdmin, "bob", "secret");
- assert result != null : "Expected a valid user to be returned!";
-
- assert "bob".equals(user.getName()) : "Expected bob to be returned!";
+ assertNotNull(result, "Expected a valid user to be returned!");
+
+ assertEquals(user.getName(), "bob", "Expected bob to be returned!");
}
/**
@@ -99,7 +104,7 @@ public class PasswordAuthenticationProce
@Test(groups = { UNIT })
public void testAuthenticateNullPasswordYieldsNull() {
User result = new PasswordAuthenticationProcessor().authenticate(m_userAdmin, "bob", null);
- assert result == null : "Expected no valid user to be returned!";
+ assertNull(result, "Expected no valid user to be returned!");
}
/**
@@ -108,7 +113,7 @@ public class PasswordAuthenticationProce
@Test(groups = { UNIT })
public void testAuthenticateNullUserNameYieldsNull() {
User result = new PasswordAuthenticationProcessor().authenticate(m_userAdmin, null, "secret");
- assert result == null : "Expected no valid user to be returned!";
+ assertNull(result, "Expected no valid user to be returned!");
}
/**
@@ -125,7 +130,7 @@ public class PasswordAuthenticationProce
@Test(groups = { UNIT })
public void testAuthenticateUnknownUserYieldsNull() {
User result = new PasswordAuthenticationProcessor().authenticate(m_userAdmin, "alice", "secret");
- assert result == null : "Expected no valid user to be returned!";
+ assertNull(result, "Expected no valid user to be returned!");
}
/**
@@ -133,7 +138,7 @@ public class PasswordAuthenticationProce
*/
@Test(groups = { UNIT })
public void testCanHandleDoesAcceptStringAndByteArray() {
- assert new PasswordAuthenticationProcessor().canHandle("foo", "bar".getBytes()) : "Expected the processor to handle a byte array!";
+ assertTrue(new PasswordAuthenticationProcessor().canHandle("foo", "bar".getBytes()), "Expected the processor to handle a byte array!");
}
/**
@@ -141,7 +146,7 @@ public class PasswordAuthenticationProce
*/
@Test(groups = { UNIT })
public void testCanHandleDoesAcceptTwoStrings() {
- assert new PasswordAuthenticationProcessor().canHandle("foo", "bar") : "Expected the processor to handle a string!";
+ assertTrue(new PasswordAuthenticationProcessor().canHandle("foo", "bar"), "Expected the processor to handle a string!");
}
/**
@@ -161,19 +166,19 @@ public class PasswordAuthenticationProce
}
/**
- * Tests that canHandle yields false for too few arguments.
+ * Tests that canHandle yields false for too few arguments.
*/
@Test(groups = { UNIT })
public void testCanHandleDoesNotAcceptSingleArgument() {
- assert new PasswordAuthenticationProcessor().canHandle(new Object()) == false : "Expected the processor to NOT handle any object!";
+ assertFalse(new PasswordAuthenticationProcessor().canHandle(new Object()), "Expected the processor to NOT handle any object!");
}
-
+
/**
- * Tests that canHandle yields false for a string and other object.
+ * Tests that canHandle yields false for a string and other object.
*/
@Test(groups = { UNIT })
public void testCanHandleDoesNotAcceptStringAndOtherObject() {
- assert new PasswordAuthenticationProcessor().canHandle("foo", new Object()) == false : "Expected the processor to NOT handle any object!";
+ assertFalse(new PasswordAuthenticationProcessor().canHandle("foo", new Object()), "Expected the processor to NOT handle any object!");
}
/**
@@ -181,9 +186,9 @@ public class PasswordAuthenticationProce
*/
@Test(groups = { UNIT })
public void testCanHandleDoesNotAcceptWrongTypes() {
- assert new PasswordAuthenticationProcessor().canHandle(new Object(), new Object()) == false : "Expected the processor to NOT handle any object!";
+ assertFalse(new PasswordAuthenticationProcessor().canHandle(new Object(), new Object()), "Expected the processor to NOT handle any object!");
}
-
+
/**
* Tests that updated does not throw an exception for a correct configuration.
*/
@@ -191,7 +196,7 @@ public class PasswordAuthenticationProce
public void testUpdatedDoesAcceptCorrectProperties() throws ConfigurationException {
final String keyUsername = "foo";
final String keyPassword = "bar";
-
+
Dictionary<String, Object> props = new Hashtable<>();
props.put(PROPERTY_KEY_USERNAME, keyUsername);
@@ -203,7 +208,7 @@ public class PasswordAuthenticationProce
processor.updated(props);
byte[] hashedPw = DigestUtils.sha("secret");
-
+
// Test whether we can use the new properties...
User user = mock(User.class);
when(user.getName()).thenReturn("bob");
@@ -212,13 +217,13 @@ public class PasswordAuthenticationProce
when(m_userAdmin.getUser(eq(keyUsername), eq("bob"))).thenReturn(user);
User result = processor.authenticate(m_userAdmin, "bob", "secret");
- assert result != null : "Expected a valid user to be returned!";
-
- assert "bob".equals(user.getName()) : "Expected bob to be returned!";
+ assertNotNull(result, "Expected a valid user to be returned!");
+
+ assertEquals(user.getName(), "bob", "Expected bob to be returned!");
}
-
+
/**
- * Tests that updated throws an exception for missing "key.password" property.
+ * Tests that updated throws an exception for missing "key.password" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptEmptyKeyPassword() throws ConfigurationException {
@@ -227,12 +232,12 @@ public class PasswordAuthenticationProce
props.put(PROPERTY_KEY_USERNAME, "foo");
props.put(PROPERTY_KEY_PASSWORD, "");
props.put(PROPERTY_PASSWORD_HASHMETHOD, "none");
-
+
new PasswordAuthenticationProcessor().updated(props);
}
-
+
/**
- * Tests that updated throws an exception for missing "key.username" property.
+ * Tests that updated throws an exception for missing "key.username" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptEmptyKeyUsername() throws ConfigurationException {
@@ -241,12 +246,12 @@ public class PasswordAuthenticationProce
props.put(PROPERTY_KEY_USERNAME, "");
props.put(PROPERTY_KEY_PASSWORD, "foo");
props.put(PROPERTY_PASSWORD_HASHMETHOD, "none");
-
+
new PasswordAuthenticationProcessor().updated(props);
}
-
+
/**
- * Tests that updated throws an exception for missing "password.hashtype" property.
+ * Tests that updated throws an exception for missing "password.hashtype" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptEmptyPasswordHashType() throws ConfigurationException {
@@ -255,12 +260,12 @@ public class PasswordAuthenticationProce
props.put(PROPERTY_KEY_USERNAME, "foo");
props.put(PROPERTY_KEY_PASSWORD, "bar");
props.put(PROPERTY_PASSWORD_HASHMETHOD, "");
-
+
new PasswordAuthenticationProcessor().updated(props);
}
-
+
/**
- * Tests that updated throws an exception for missing "key.password" property.
+ * Tests that updated throws an exception for missing "key.password" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptMissingKeyPassword() throws ConfigurationException {
@@ -271,9 +276,9 @@ public class PasswordAuthenticationProce
new PasswordAuthenticationProcessor().updated(props);
}
-
+
/**
- * Tests that updated throws an exception for missing "key.username" property.
+ * Tests that updated throws an exception for missing "key.username" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptMissingKeyUsername() throws ConfigurationException {
@@ -284,9 +289,9 @@ public class PasswordAuthenticationProce
new PasswordAuthenticationProcessor().updated(props);
}
-
+
/**
- * Tests that updated throws an exception for missing "password.hashtype" property.
+ * Tests that updated throws an exception for missing "password.hashtype" property.
*/
@Test(groups = { UNIT }, expectedExceptions = ConfigurationException.class)
public void testUpdatedDoesNotAcceptMissingPasswordHashType() throws ConfigurationException {
|