diff options
| author | Alex Rudyy <orudyy@apache.org> | 2013-04-25 15:35:25 +0000 |
|---|---|---|
| committer | Alex Rudyy <orudyy@apache.org> | 2013-04-25 15:35:25 +0000 |
| commit | ef33e61297ec7ea22d8c283e45d4f6d90f417f64 (patch) | |
| tree | a21cd4a39cfcab3af76cbbb817810e9d440afc07 /qpid/java/broker/src/test | |
| parent | 874b973366824fba1ae502b10c9a5b66c11e4779 (diff) | |
| download | qpid-python-ef33e61297ec7ea22d8c283e45d4f6d90f417f64.tar.gz | |
QPID-4596: Call AuthenticationManager#onCreate() only when authentication provider is created
git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk@1475825 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'qpid/java/broker/src/test')
3 files changed, 92 insertions, 14 deletions
diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactoryTest.java index eb721d93a0..4a76c1837c 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactoryTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/model/adapter/AuthenticationProviderFactoryTest.java @@ -23,6 +23,8 @@ package org.apache.qpid.server.model.adapter; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.Mockito.any; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.never; import java.util.Collections; import java.util.HashMap; @@ -45,20 +47,42 @@ public class AuthenticationProviderFactoryTest extends TestCase public void testCreatePasswordCredentialManagingAuthenticationProvider() { - AuthenticationProvider provider = testForFactory(mock(PrincipalDatabaseAuthenticationManager.class)); + AuthenticationManager am = mock(PrincipalDatabaseAuthenticationManager.class); + AuthenticationProvider provider = testForFactory(am, true); assertTrue("The created provider should match the factory's AuthenticationManager type", provider instanceof PasswordCredentialManagingAuthenticationProvider); + verify(am).onCreate(); } public void testCreateNonPasswordCredentialManagingAuthenticationProvider() { - AuthenticationProvider provider = testForFactory(mock(AuthenticationManager.class)); + AuthenticationManager am = mock(AuthenticationManager.class); + AuthenticationProvider provider = testForFactory(am, true); assertFalse("The created provider should match the factory's AuthenticationManager type", provider instanceof PasswordCredentialManagingAuthenticationProvider); + verify(am).onCreate(); + } + + public void testRecoverPasswordCredentialManagingAuthenticationProvider() + { + AuthenticationManager am = mock(PrincipalDatabaseAuthenticationManager.class); + AuthenticationProvider provider = testForFactory(am, false); + assertTrue("The created provider should match the factory's AuthenticationManager type", + provider instanceof PasswordCredentialManagingAuthenticationProvider); + verify(am, never()).onCreate(); + } + + public void testRecoverNonPasswordCredentialManagingAuthenticationProvider() + { + AuthenticationManager am = mock(AuthenticationManager.class); + AuthenticationProvider provider = testForFactory(am, false); + assertFalse("The created provider should match the factory's AuthenticationManager type", + provider instanceof PasswordCredentialManagingAuthenticationProvider); + verify(am, never()).onCreate(); } @SuppressWarnings("unchecked") - private AuthenticationProvider testForFactory(AuthenticationManager authenticationManager) + private AuthenticationProvider testForFactory(AuthenticationManager authenticationManager, boolean create) { UUID id = UUID.randomUUID(); Map<String, Object> attributes = new HashMap<String, Object>(); @@ -73,7 +97,16 @@ public class AuthenticationProviderFactoryTest extends TestCase when(authenticationManagerFactory.createInstance(attributes)).thenReturn(authenticationManager); AuthenticationProviderFactory providerFactory = new AuthenticationProviderFactory(authManagerFactoryServiceLoader); - AuthenticationProvider provider = providerFactory.create(id, broker, attributes); + + AuthenticationProvider provider = null; + if (create) + { + provider = providerFactory.create(id, broker, attributes); + } + else + { + provider = providerFactory.recover(id, attributes, broker); + } assertNotNull("Provider is not created", provider); assertEquals("Unexpected ID", id, provider.getId()); diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java index d428f8b211..cc11a94db8 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PlainPasswordFileAuthenticationManagerFactoryTest.java @@ -20,7 +20,6 @@ package org.apache.qpid.server.security.auth.manager; import java.io.File; -import java.io.FileNotFoundException; import java.util.HashMap; import java.util.Map; @@ -62,14 +61,11 @@ public class PlainPasswordFileAuthenticationManagerFactoryTest extends TestCase _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_TYPE, PlainPasswordFileAuthenticationManagerFactory.PROVIDER_TYPE); _configuration.put(AbstractPrincipalDatabaseAuthManagerFactory.ATTRIBUTE_PATH, _emptyPasswordFile.getAbsolutePath()); - try - { - _factory.createInstance(_configuration); - } - catch (RuntimeException re) - { - assertTrue(re.getCause() instanceof FileNotFoundException); - } + AuthenticationManager manager = _factory.createInstance(_configuration); + + assertNotNull(manager); + assertTrue(manager instanceof PrincipalDatabaseAuthenticationManager); + assertTrue(((PrincipalDatabaseAuthenticationManager)manager).getPrincipalDatabase() instanceof PlainPasswordFilePrincipalDatabase); } public void testReturnsNullWhenNoConfig() throws Exception diff --git a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java index 8025907e41..b505b361fd 100644 --- a/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java +++ b/qpid/java/broker/src/test/java/org/apache/qpid/server/security/auth/manager/PrincipalDatabaseAuthenticationManagerTest.java @@ -25,10 +25,14 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.security.Principal; import java.security.Provider; import java.security.Security; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.security.auth.callback.CallbackHandler; @@ -36,9 +40,11 @@ import javax.security.sasl.SaslException; import javax.security.sasl.SaslServer; import javax.security.sasl.SaslServerFactory; +import org.apache.qpid.server.configuration.IllegalConfigurationException; import org.apache.qpid.server.security.auth.AuthenticationResult; import org.apache.qpid.server.security.auth.AuthenticationResult.AuthenticationStatus; import org.apache.qpid.server.security.auth.UsernamePrincipal; +import org.apache.qpid.server.security.auth.database.PlainPasswordFilePrincipalDatabase; import org.apache.qpid.server.security.auth.database.PrincipalDatabase; import org.apache.qpid.server.security.auth.sasl.AuthenticationProviderInitialiser; import org.apache.qpid.server.security.auth.sasl.UsernamePasswordInitialiser; @@ -121,8 +127,51 @@ public class PrincipalDatabaseAuthenticationManagerTest extends QpidTestCase usernamePasswordInitialiser.initialise(_principalDatabase); - _manager = new PrincipalDatabaseAuthenticationManager(_principalDatabase, null); + _manager = new PrincipalDatabaseAuthenticationManager(_principalDatabase, _passwordFileLocation); + _manager.initialise(); + } + + public void testInitialiseWhenPasswordFileNotFound() throws Exception + { + _principalDatabase = new PlainPasswordFilePrincipalDatabase(); + _manager = new PrincipalDatabaseAuthenticationManager(_principalDatabase, _passwordFileLocation); + + try + { + _manager.initialise(); + fail("Initialisiation should fail when users file does not exist"); + } + catch (IllegalConfigurationException e) + { + assertTrue(e.getCause() instanceof FileNotFoundException); + } + } + + public void testInitialiseWhenPasswordFileExists() throws Exception + { + _principalDatabase = new PlainPasswordFilePrincipalDatabase(); + _manager = new PrincipalDatabaseAuthenticationManager(_principalDatabase, _passwordFileLocation); + + File f = new File(_passwordFileLocation); + f.createNewFile(); + FileOutputStream fos = null; + try + { + fos = new FileOutputStream(f); + fos.write("admin:admin".getBytes()); + } + finally + { + if (fos != null) + { + fos.close(); + } + } _manager.initialise(); + List<Principal> users = _principalDatabase.getUsers(); + assertEquals("Unexpected uses size", 1, users.size()); + Principal p = _principalDatabase.getUser("admin"); + assertEquals("Unexpected principal name", "admin", p.getName()); } /** |
