Package net.shibboleth.shared.testing
Class InMemoryDirectory
java.lang.Object
net.shibboleth.shared.testing.InMemoryDirectory
Manages an instance of the in-memory directory server for unit testing.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate static classServerSocketFactory wrapper class to track created sockets. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final InMemoryDirectory.CustomServerSocketFactoryServer socket factory to track created sockets.private final com.unboundid.ldap.listener.InMemoryDirectoryServerDirectory server.private final org.slf4j.LoggerClass logger. -
Constructor Summary
ConstructorsConstructorDescriptionInMemoryDirectory(String[] baseDNs, Resource ldif, int port) Constructor without STARTTLS support.InMemoryDirectory(String[] baseDNs, Resource ldif, int port, boolean requireAuthForSearch) Constructor without STARTTLS support.InMemoryDirectory(String[] baseDNs, Resource ldif, int port, boolean requireAuthForSearch, Resource keystore, Optional<Resource> truststore) Constructor with STARTTLS support.InMemoryDirectory(String[] baseDNs, Resource ldif, int port, Resource keystore, Optional<Resource> truststore) Constructor with STARTTLS support. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds the supplied LDIF lines to the directory server.private voidaddSuccessSaslBindHandlers(com.unboundid.ldap.listener.InMemoryDirectoryServerConfig config) Adds DIGEST-MD5 and EXTERNAL SASL bind handlers that always return success.private static KeyManagerFactorygetKeyManagerFactory(Resource keystore) Creates a KeyManagerFactory from the supplied resource.intReturns the port the server is listening on.private static TrustManagerFactorygetTrustManagerFactory(Resource keystore) Creates a TrustManagerFactory from the supplied resource.private static KeyStoreloadKeyStore(Resource keystore, String password) Creates a new KeyStore from the supplied resource.longReturns the number of open sockets.voidstart()Starts the directory server.voidstop(boolean closeConnections) Stops the directory server.
-
Field Details
-
log
@Nonnull private final org.slf4j.Logger logClass logger. -
directoryServer
@Nonnull private final com.unboundid.ldap.listener.InMemoryDirectoryServer directoryServerDirectory server. -
customServerSocketFactory
Server socket factory to track created sockets.
-
-
Constructor Details
-
InMemoryDirectory
public InMemoryDirectory(@ParameterName(name="baseDNs") @Nonnull String[] baseDNs, @ParameterName(name="ldif") @Nonnull Resource ldif, @ParameterName(name="port") @Positive int port) Constructor without STARTTLS support.- Parameters:
baseDNs- to use in the directory serverldif- the LDIF resource to be importedport- port to listen on- Throws:
RuntimeException- if the in-memory directory cannot be created
-
InMemoryDirectory
public InMemoryDirectory(@ParameterName(name="baseDNs") @Nonnull String[] baseDNs, @ParameterName(name="ldif") @Nonnull Resource ldif, @ParameterName(name="port") @Positive int port, @ParameterName(name="requireAuthForSearch") boolean requireAuthForSearch) Constructor without STARTTLS support.- Parameters:
baseDNs- to use in the directory serverldif- the LDIF resource to be importedport- port to listen onrequireAuthForSearch- whether to require authentication in order to perform searches- Throws:
RuntimeException- if the in-memory directory cannot be created
-
InMemoryDirectory
public InMemoryDirectory(@ParameterName(name="baseDNs") @Nonnull String[] baseDNs, @ParameterName(name="ldif") @Nonnull Resource ldif, @ParameterName(name="port") @Positive int port, @ParameterName(name="keystore") @Nonnull Resource keystore, @ParameterName(name="truststore") @Nonnull Optional<Resource> truststore) Constructor with STARTTLS support.- Parameters:
baseDNs- to use in the directory serverldif- the LDIF resource to be importedport- port to listen onkeystore- to use for startTLStruststore- to use for startTLS- Throws:
RuntimeException- if the in-memory directory cannot be created
-
InMemoryDirectory
public InMemoryDirectory(@ParameterName(name="baseDNs") @Nonnull String[] baseDNs, @ParameterName(name="ldif") @Nonnull Resource ldif, @ParameterName(name="port") @Positive int port, @ParameterName(name="requireAuthForSearch") boolean requireAuthForSearch, @ParameterName(name="keystore") @Nonnull Resource keystore, @ParameterName(name="truststore") @Nonnull Optional<Resource> truststore) Constructor with STARTTLS support.- Parameters:
baseDNs- to use in the directory serverldif- the LDIF resource to be importedport- port to listen onrequireAuthForSearch- whether to require authentication in order to perform searcheskeystore- to use for startTLStruststore- to use for startTLS- Throws:
RuntimeException- if the in-memory directory cannot be created
-
-
Method Details
-
addSuccessSaslBindHandlers
private void addSuccessSaslBindHandlers(com.unboundid.ldap.listener.InMemoryDirectoryServerConfig config) Adds DIGEST-MD5 and EXTERNAL SASL bind handlers that always return success.- Parameters:
config- to add SASL bind handlers to
-
add
Adds the supplied LDIF lines to the directory server.- Parameters:
ldifLines- to add- Throws:
RuntimeException- if an error occurs adding the LDIF
-
openConnectionCount
public long openConnectionCount()Returns the number of open sockets.- Returns:
- number of open sockets
-
start
public void start()Starts the directory server.- Throws:
RuntimeException- if the in-memory directory server cannot be started
-
getListenPort
public int getListenPort()Returns the port the server is listening on.- Returns:
- port number
-
stop
public void stop(boolean closeConnections) Stops the directory server. Note that in general resources should be configured so that LDAP connections are closed at the conclusion of a test method or test class.- Parameters:
closeConnections- whether to close existing connections
-
getKeyManagerFactory
private static KeyManagerFactory getKeyManagerFactory(Resource keystore) throws GeneralSecurityException, IOException Creates a KeyManagerFactory from the supplied resource. A keystore password of "changeit" is assumed.- Parameters:
keystore- resource to read- Returns:
- key manager factory built from the keystore
- Throws:
GeneralSecurityException- if the keystore password is incorrectIOException- if the resource cannot be read
-
getTrustManagerFactory
private static TrustManagerFactory getTrustManagerFactory(Resource keystore) throws GeneralSecurityException, IOException Creates a TrustManagerFactory from the supplied resource. A keystore password of "changeit" is assumed.- Parameters:
keystore- resource to read- Returns:
- trust manager factory built from the keystore
- Throws:
GeneralSecurityException- if the keystore password is incorrectIOException- if the resource cannot be read
-
loadKeyStore
private static KeyStore loadKeyStore(Resource keystore, String password) throws GeneralSecurityException, IOException Creates a new KeyStore from the supplied resource.- Parameters:
keystore- resource to readpassword- to unlock the keystore- Returns:
- keystore
- Throws:
GeneralSecurityException- if the keystore cannot be created from the resourceIOException- if the resource cannot be read
-