SEC-2606: ApacheDSServerIntegrationTests scan for available port

This commit is contained in:
Rob Winch 2014-05-21 06:49:00 -05:00
parent 519d85877c
commit d4e26864d9
4 changed files with 32 additions and 4 deletions

View File

@ -25,7 +25,8 @@ public abstract class AbstractLdapIntegrationTests {
@BeforeClass @BeforeClass
public static void createContextSource() throws Exception { public static void createContextSource() throws Exception {
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:53389/dc=springframework,dc=org"); int serverPort = ApacheDSServerIntegrationTests.getServerPort();
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:" + serverPort + "/dc=springframework,dc=org");
// OpenLDAP configuration // OpenLDAP configuration
// contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:22389/dc=springsource,dc=com"); // contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:22389/dc=springsource,dc=com");
// contextSource.setUserDn("cn=admin,dc=springsource,dc=com"); // contextSource.setUserDn("cn=admin,dc=springsource,dc=com");

View File

@ -1,5 +1,8 @@
package org.springframework.security.ldap; package org.springframework.security.ldap;
import java.io.IOException;
import java.net.ServerSocket;
import org.junit.*; import org.junit.*;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Suite; import org.junit.runners.Suite;
@ -26,6 +29,7 @@ import org.springframework.security.ldap.userdetails.LdapUserDetailsManagerTests
) )
public final class ApacheDSServerIntegrationTests { public final class ApacheDSServerIntegrationTests {
private static ApacheDSContainer server; private static ApacheDSContainer server;
private static Integer serverPort;
@BeforeClass @BeforeClass
public static void startServer() throws Exception { public static void startServer() throws Exception {
@ -34,12 +38,15 @@ public final class ApacheDSServerIntegrationTests {
// contextSource.setUserDn("cn=admin,dc=springsource,dc=com"); // contextSource.setUserDn("cn=admin,dc=springsource,dc=com");
// contextSource.setPassword("password"); // contextSource.setPassword("password");
server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif"); server = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
server.setPort(53389); int port = getAvailablePort();
server.setPort(port);
server.afterPropertiesSet(); server.afterPropertiesSet();
serverPort = port;
} }
@AfterClass @AfterClass
public static void stopServer() throws Exception { public static void stopServer() throws Exception {
serverPort = null;
if (server != null) { if (server != null) {
server.stop(); server.stop();
} }
@ -53,6 +60,12 @@ public final class ApacheDSServerIntegrationTests {
server.afterPropertiesSet(); server.afterPropertiesSet();
} }
public static int getServerPort() {
if(serverPort == null) {
throw new IllegalStateException("The ApacheDSContainer is not currently running");
}
return serverPort;
}
/* /*
@After @After
public final void reloadServerDataIfDirty() throws Exception { public final void reloadServerDataIfDirty() throws Exception {
@ -105,4 +118,18 @@ public final class ApacheDSServerIntegrationTests {
} }
} }
*/ */
private static int getAvailablePort() throws IOException {
ServerSocket serverSocket = null;
try {
serverSocket = new ServerSocket(0);
return serverSocket.getLocalPort();
} finally {
if(serverSocket != null) {
try {
serverSocket.close();
} catch (IOException e) {}
}
}
}
} }

View File

@ -71,7 +71,7 @@ public class DefaultSpringSecurityContextSourceTests extends AbstractLdapIntegra
@Test @Test
public void serverUrlWithSpacesIsSupported() throws Exception { public void serverUrlWithSpacesIsSupported() throws Exception {
DefaultSpringSecurityContextSource DefaultSpringSecurityContextSource
contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:53389/ou=space%20cadets,dc=springframework,dc=org"); contextSource = new DefaultSpringSecurityContextSource("ldap://127.0.0.1:" + ApacheDSServerIntegrationTests.getServerPort() + "/ou=space%20cadets,dc=springframework,dc=org");
contextSource.afterPropertiesSet(); contextSource.afterPropertiesSet();
contextSource.getContext("uid=space cadet,ou=space cadets,dc=springframework,dc=org", "spacecadetspassword"); contextSource.getContext("uid=space cadet,ou=space cadets,dc=springframework,dc=org", "spacecadetspassword");
} }

View File

@ -121,7 +121,7 @@ public class SpringSecurityLdapTemplateITests extends AbstractLdapIntegrationTes
public void nonSpringLdapSearchCodeTestMethod() throws Exception { public void nonSpringLdapSearchCodeTestMethod() throws Exception {
java.util.Hashtable<String, String> env = new java.util.Hashtable<String, String>(); java.util.Hashtable<String, String> env = new java.util.Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:53389"); env.put(Context.PROVIDER_URL, "ldap://localhost:" + ApacheDSServerIntegrationTests.getServerPort());
env.put(Context.SECURITY_PRINCIPAL, ""); env.put(Context.SECURITY_PRINCIPAL, "");
env.put(Context.SECURITY_CREDENTIALS, ""); env.put(Context.SECURITY_CREDENTIALS, "");