From b9ca1400e3060f512bbf46097aa136c0680cc5c4 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Thu, 4 Oct 2018 16:27:00 -0600 Subject: [PATCH] UnboundIdContainer Random Port Support Setting a port of 0 will now induce the container to select a random port. Fixes: gh-5920 --- .../ldap/server/UnboundIdContainerTests.java | 15 ++++++++++++++- .../security/ldap/server/UnboundIdContainer.java | 3 ++- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerTests.java b/ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerTests.java index dcba728cd1..4d9b6ab569 100644 --- a/ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerTests.java +++ b/ldap/src/integration-test/java/org/springframework/security/ldap/server/UnboundIdContainerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -47,6 +47,19 @@ public class UnboundIdContainerTests { } } + @Test + public void afterPropertiesSetWhenPortIsZeroThenRandomPortIsSelected() throws Exception { + UnboundIdContainer server = new UnboundIdContainer("dc=springframework,dc=org", null); + server.setPort(0); + + try { + server.afterPropertiesSet(); + assertThat(server.getPort()).isNotEqualTo(0); + } finally { + server.destroy(); + } + } + private List getDefaultPorts(int count) throws IOException { List connections = new ArrayList<>(); List availablePorts = new ArrayList<>(count); diff --git a/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java b/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java index 4fd5abc612..d392d3b406 100644 --- a/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java +++ b/ldap/src/main/java/org/springframework/security/ldap/server/UnboundIdContainer.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2017 the original author or authors. + * Copyright 2002-2018 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. @@ -103,6 +103,7 @@ public class UnboundIdContainer implements InitializingBean, DisposableBean, Lif directoryServer.add(entry); importLdif(directoryServer); directoryServer.startListening(); + this.port = directoryServer.getListenPort(); this.directoryServer = directoryServer; this.running = true; } catch (LDAPException ex) {