SEC-2161: <ldap-server> creates unique dir for embedded LDAP

This commit is contained in:
Rob Winch 2013-04-25 11:14:12 -05:00
parent dd554e1842
commit 407b08956b
2 changed files with 51 additions and 2 deletions

View File

@ -1,3 +1,18 @@
/*
* Copyright 2002-2013 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.security.ldap.server; package org.springframework.security.ldap.server;
import static junit.framework.Assert.fail; import static junit.framework.Assert.fail;
@ -9,6 +24,7 @@ import org.junit.Test;
* Useful for debugging the container by itself. * Useful for debugging the container by itself.
* *
* @author Luke Taylor * @author Luke Taylor
* @author Rob Winch
* @since 3.0 * @since 3.0
*/ */
public class ApacheDSContainerTests { public class ApacheDSContainerTests {
@ -46,4 +62,22 @@ public class ApacheDSContainerTests {
}catch(Throwable t) {} }catch(Throwable t) {}
} }
} }
// SEC-2161
@Test
public void multipleInstancesSimultanciously() throws Exception {
ApacheDSContainer server1 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
ApacheDSContainer server2 = new ApacheDSContainer("dc=springframework,dc=org", "classpath:test-server.ldif");
try {
server1.afterPropertiesSet();
server2.afterPropertiesSet();
} finally {
try {
server1.destroy();
}catch(Throwable t) {}
try {
server2.destroy();
}catch(Throwable t) {}
}
}
} }

View File

@ -17,7 +17,6 @@ package org.springframework.security.ldap.server;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -119,7 +118,7 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life
String apacheWorkDir = System.getProperty("apacheDSWorkDir"); String apacheWorkDir = System.getProperty("apacheDSWorkDir");
if (apacheWorkDir == null) { if (apacheWorkDir == null) {
apacheWorkDir = System.getProperty("java.io.tmpdir") + File.separator + "apacheds-spring-security"; apacheWorkDir = createTempDirectory("apacheds-spring-security-");
} }
setWorkingDirectory(new File(apacheWorkDir)); setWorkingDirectory(new File(apacheWorkDir));
@ -267,6 +266,22 @@ public class ApacheDSContainer implements InitializingBean, DisposableBean, Life
} }
} }
private String createTempDirectory(String prefix) throws IOException {
String parentTempDir = System.getProperty("java.io.tmpdir");
String fileNamePrefix = prefix + System.nanoTime();
String fileName = fileNamePrefix;
for(int i=0;i<1000;i++) {
File tempDir = new File(parentTempDir, fileName);
if(!tempDir.exists()) {
return tempDir.getAbsolutePath();
}
fileName = fileNamePrefix + "~" + i;
}
throw new IOException("Failed to create a temporary directory for file at " + new File(parentTempDir, fileNamePrefix));
}
private boolean deleteDir(File dir) { private boolean deleteDir(File dir) {
if (dir.isDirectory()) { if (dir.isDirectory()) {
String[] children = dir.list(); String[] children = dir.list();