Allow port=0 for ApacheDSContainer
Fixes gh-8144
This commit is contained in:
committed by
Rob Winch
parent
06254a4fd4
commit
0fa339f75b
+18
-1
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -39,6 +39,7 @@ import org.springframework.util.FileCopyUtils;
|
||||
* @author Luke Taylor
|
||||
* @author Rob Winch
|
||||
* @author Gunnar Hillert
|
||||
* @author Evgeniy Cheban
|
||||
* @since 3.0
|
||||
*/
|
||||
public class ApacheDSContainerTests {
|
||||
@@ -212,4 +213,20 @@ public class ApacheDSContainerTests {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void afterPropertiesSetWhenPortIsZeroThenRandomPortIsSelected() throws Exception {
|
||||
ApacheDSContainer server = new ApacheDSContainer("dc=springframework,dc=org",
|
||||
"classpath:test-server.ldif");
|
||||
server.setPort(0);
|
||||
try {
|
||||
server.afterPropertiesSet();
|
||||
|
||||
assertThat(server.getPort()).isEqualTo(0);
|
||||
assertThat(server.getLocalPort()).isNotEqualTo(0);
|
||||
}
|
||||
finally {
|
||||
server.destroy();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2016 the original author or authors.
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -15,6 +15,7 @@
|
||||
*/
|
||||
package org.springframework.security.ldap.server;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
@@ -39,6 +40,7 @@ import org.apache.directory.server.protocol.shared.store.LdifFileLoader;
|
||||
import org.apache.directory.server.protocol.shared.transport.TcpTransport;
|
||||
import org.apache.directory.shared.ldap.exception.LdapNameNotFoundException;
|
||||
import org.apache.directory.shared.ldap.name.LdapDN;
|
||||
import org.apache.mina.transport.socket.SocketAcceptor;
|
||||
import org.springframework.beans.BeansException;
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
@@ -69,6 +71,7 @@ import org.springframework.util.Assert;
|
||||
* @author Luke Taylor
|
||||
* @author Rob Winch
|
||||
* @author Gunnar Hillert
|
||||
* @author Evgeniy Cheban
|
||||
* @deprecated Use {@link UnboundIdContainer} instead because ApacheDS 1.x is no longer
|
||||
* supported with no GA version to replace it.
|
||||
*/
|
||||
@@ -80,6 +83,7 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life
|
||||
final DefaultDirectoryService service;
|
||||
LdapServer server;
|
||||
|
||||
private TcpTransport transport;
|
||||
private ApplicationContext ctxt;
|
||||
private File workingDir;
|
||||
|
||||
@@ -88,6 +92,7 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life
|
||||
private final JdbmPartition partition;
|
||||
private final String root;
|
||||
private int port = 53389;
|
||||
private int localPort;
|
||||
|
||||
private boolean ldapOverSslEnabled;
|
||||
private File keyStoreFile;
|
||||
@@ -143,7 +148,7 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life
|
||||
server.setDirectoryService(service);
|
||||
// AbstractLdapIntegrationTests assume IPv4, so we specify the same here
|
||||
|
||||
TcpTransport transport = new TcpTransport(port);
|
||||
this.transport = new TcpTransport(port);
|
||||
if (ldapOverSslEnabled) {
|
||||
transport.setEnableSSL(true);
|
||||
server.setKeystoreFile(this.keyStoreFile.getAbsolutePath());
|
||||
@@ -190,6 +195,15 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life
|
||||
return this.port;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the port that is resolved by {@link TcpTransport}.
|
||||
*
|
||||
* @return the port that is resolved by {@link TcpTransport}
|
||||
*/
|
||||
public int getLocalPort() {
|
||||
return this.localPort;
|
||||
}
|
||||
|
||||
/**
|
||||
* If set to {@code true} will enable LDAP over SSL (LDAPs). If set to {@code true}
|
||||
* {@link ApacheDSContainer#setCertificatePassord(String)} must be set as well.
|
||||
@@ -262,6 +276,10 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life
|
||||
logger.error("Lookup failed", e);
|
||||
}
|
||||
|
||||
SocketAcceptor socketAcceptor = this.server.getSocketAcceptor(this.transport);
|
||||
InetSocketAddress localAddress = socketAcceptor.getLocalAddress();
|
||||
this.localPort = localAddress.getPort();
|
||||
|
||||
running = true;
|
||||
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user