Package net.shibboleth.shared.testing

# Class InMemoryDirectory

java.lang.Object
net.shibboleth.shared.testing.InMemoryDirectory

public class InMemoryDirectory extends Object
Manages an instance of the in-memory directory server for unit testing.

## Nested Class Summary

Nested Classes
Modifier and Type
Class
Description
`private static class`
`InMemoryDirectory.CustomServerSocketFactory`
ServerSocketFactory wrapper class to track created sockets.

## Field Summary

Fields
Modifier and Type
Field
Description
`private final InMemoryDirectory.CustomServerSocketFactory`
`customServerSocketFactory`
Server socket factory to track created sockets.
`private final com.unboundid.ldap.listener.InMemoryDirectoryServer`
`directoryServer`
Directory server.
`private final org.slf4j.Logger`
`log`
Class logger.

## Constructor Summary

Constructors
Constructor
Description
`InMemoryDirectory(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 Type
Method
Description
`void`
`add(String... ldifLines)`
Adds the supplied LDIF lines to the directory server.
`private void`
`addSuccessSaslBindHandlers(com.unboundid.ldap.listener.InMemoryDirectoryServerConfig config)`
Adds DIGEST-MD5 and EXTERNAL SASL bind handlers that always return success.
`private static KeyManagerFactory`
`getKeyManagerFactory(Resource keystore)`
Creates a KeyManagerFactory from the supplied resource.
`int`
`getListenPort()`
Returns the port the server is listening on.
`private static TrustManagerFactory`
`getTrustManagerFactory(Resource keystore)`
Creates a TrustManagerFactory from the supplied resource.
`private static KeyStore`
`loadKeyStore(Resource keystore, String password)`
Creates a new KeyStore from the supplied resource.
`long`
`openConnectionCount()`
Returns the number of open sockets.
`void`
`start()`
Starts the directory server.
`void`
`stop(boolean closeConnections)`
Stops the directory server.

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## Field Details

### log

@Nonnull private final org.slf4j.Logger log
Class logger.

### directoryServer

@Nonnull private final com.unboundid.ldap.listener.InMemoryDirectoryServer directoryServer
Directory server.

### customServerSocketFactory

@Nonnull private final InMemoryDirectory.CustomServerSocketFactory 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 server
  `ldif` - the LDIF resource to be imported
  `port` - 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 server
  `ldif` - the LDIF resource to be imported
  `port` - port to listen on
  `requireAuthForSearch` - 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 server
  `ldif` - the LDIF resource to be imported
  `port` - port to listen on
  `keystore` - to use for startTLS
  `truststore` - 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 server
  `ldif` - the LDIF resource to be imported
  `port` - port to listen on
  `requireAuthForSearch` - whether to require authentication in order to perform searches
  `keystore` - to use for startTLS
  `truststore` - 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

public void add(String... ldifLines)
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 incorrect
  `IOException` - 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 incorrect
  `IOException` - 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 read
  `password` - to unlock the keystore
Returns:
  keystore
Throws:
  `GeneralSecurityException` - if the keystore cannot be created from the resource
  `IOException` - if the resource cannot be read