Skip to content

Commit d87f910

Browse files
committed
Rebase from master
1 parent bd11526 commit d87f910

File tree

8 files changed

+336
-20
lines changed

8 files changed

+336
-20
lines changed

embedded-ldap-core/src/main/java/org/zapodot/junit/ldap/internal/AbstractEmbeddedLdapBuilder.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.unboundid.ldap.sdk.schema.Schema;
1010
import com.unboundid.ldif.LDIFException;
1111

12+
import javax.net.ssl.SSLSocketFactory;
1213
import java.io.File;
1314
import java.io.IOException;
1415
import java.net.InetAddress;
@@ -51,6 +52,12 @@ public abstract class AbstractEmbeddedLdapBuilder<Self extends AbstractEmbeddedL
5152

5253
private InMemoryListenerConfig listenerConfig = null;
5354

55+
protected boolean useTls = false;
56+
protected SSLSocketFactory socketFactory = null;
57+
58+
private Integer maxSizeLimit = null;
59+
60+
5461
/**
5562
* Sets a domainDsn to be used. May be multiple values. If not set, it will default to the value of the {@link #DEFAULT_DOMAIN DEFAULT_DOMAIN} field
5663
*
@@ -118,6 +125,11 @@ public Self bindingToAddress(final String address) {
118125
return getThis();
119126
}
120127

128+
public Self withMaxSizeLimit(final int maxSizeLimit) {
129+
this.maxSizeLimit = Integer.valueOf(maxSizeLimit);
130+
return getThis();
131+
}
132+
121133
/**
122134
* Avoid adding UnboundID's default schema that contains the most common LDAP elements defined through various RFC's.
123135
*
@@ -157,6 +169,11 @@ public Self withListener(InMemoryListenerConfig listenerConfig) {
157169
return getThis();
158170
}
159171

172+
public Self useTls(boolean useTls) {
173+
this.useTls = useTls;
174+
return getThis();
175+
}
176+
160177
protected abstract Self getThis();
161178

162179
protected InMemoryDirectoryServerConfig createInMemoryServerConfiguration() {
@@ -178,6 +195,9 @@ protected InMemoryDirectoryServerConfig createInMemoryServerConfiguration() {
178195
}
179196
inMemoryDirectoryServerConfig.setListenerConfigs(listenerConfig);
180197
inMemoryDirectoryServerConfig.setSchema(customSchema());
198+
if(maxSizeLimit != null) {
199+
inMemoryDirectoryServerConfig.setMaxSizeLimit(maxSizeLimit);
200+
}
181201
return inMemoryDirectoryServerConfig;
182202
} catch (LDAPException e) {
183203
throw new IllegalStateException(
@@ -236,5 +256,8 @@ public File apply(final String input) {
236256
}));
237257
}
238258

239-
259+
public Self withSocketFactory(SSLSocketFactory socketFactory) {
260+
this.socketFactory = socketFactory;
261+
return getThis();
262+
}
240263
}

embedded-ldap-core/src/main/java/org/zapodot/junit/ldap/internal/EmbeddedLdapServerImpl.java

Lines changed: 70 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import com.unboundid.ldap.sdk.LDAPConnection;
88
import com.unboundid.ldap.sdk.LDAPException;
99
import com.unboundid.ldap.sdk.LDAPInterface;
10+
import net.bytebuddy.ByteBuddy;
11+
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
12+
import net.bytebuddy.implementation.FixedValue;
1013
import org.slf4j.Logger;
1114
import org.slf4j.LoggerFactory;
1215
import org.zapodot.junit.ldap.EmbeddedLdapServer;
@@ -18,8 +21,15 @@
1821
import javax.naming.directory.DirContext;
1922
import javax.naming.directory.InitialDirContext;
2023
import javax.naming.ldap.LdapContext;
24+
import javax.net.SocketFactory;
25+
import javax.net.ssl.SSLSocketFactory;
26+
import java.io.IOException;
2127
import java.io.UnsupportedEncodingException;
28+
import java.lang.reflect.Modifier;
29+
import java.net.InetAddress;
30+
import java.net.Socket;
2231
import java.net.URLDecoder;
32+
import java.net.UnknownHostException;
2333
import java.util.Hashtable;
2434
import java.util.List;
2535

@@ -34,11 +44,16 @@ abstract class EmbeddedLdapServerImpl implements EmbeddedLdapServer {
3444
private LDAPConnection ldapConnection;
3545
private InitialDirContext initialDirContext;
3646
private boolean isStarted = false;
47+
private final boolean useTls;
48+
private final SSLSocketFactory socketFactory;
3749

3850
public EmbeddedLdapServerImpl(final InMemoryDirectoryServer inMemoryDirectoryServer,
39-
final AuthenticationConfiguration authenticationConfiguration1) {
51+
final AuthenticationConfiguration authenticationConfiguration1,
52+
final boolean useTls, SSLSocketFactory socketFactory) {
4053
this.inMemoryDirectoryServer = inMemoryDirectoryServer;
4154
this.authenticationConfiguration = authenticationConfiguration1;
55+
this.useTls = useTls;
56+
this.socketFactory = socketFactory;
4257
}
4358

4459
protected static InMemoryDirectoryServer createServer(final InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig,
@@ -112,10 +127,63 @@ private InitialDirContext createOrGetInitialDirContext() throws NamingException
112127
}
113128
}
114129

130+
public static abstract class AbstractDelegatingSocketFactory extends SocketFactory {
131+
public static AbstractDelegatingSocketFactory INSTANCE;
132+
133+
public static AbstractDelegatingSocketFactory getDefault() {
134+
return INSTANCE;
135+
}
136+
137+
protected abstract SocketFactory getDelegate();
138+
139+
@Override
140+
public Socket createSocket() throws IOException, UnknownHostException {
141+
return getDelegate().createSocket();
142+
}
143+
144+
@Override
145+
public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
146+
return getDelegate().createSocket(host, port);
147+
}
148+
149+
@Override
150+
public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
151+
throws IOException, UnknownHostException {
152+
return getDelegate().createSocket(host, port, localHost, localPort);
153+
}
154+
155+
@Override
156+
public Socket createSocket(InetAddress host, int port) throws IOException {
157+
return getDelegate().createSocket(host, port);
158+
}
159+
160+
@Override
161+
public Socket createSocket(InetAddress address, int port, InetAddress localAddress, int localPort)
162+
throws IOException {
163+
return getDelegate().createSocket(address, port, localAddress, localPort);
164+
}
165+
}
166+
115167
private Hashtable<String, String> createLdapEnvironment() {
116168
final Hashtable<String, String> environment = new Hashtable<>();
169+
if (socketFactory != null) {
170+
final Class<?> delegator = (new ByteBuddy()).subclass(AbstractDelegatingSocketFactory.class)
171+
.defineMethod("getDelegate", SocketFactory.class, Modifier.PROTECTED)
172+
.intercept(FixedValue.value(socketFactory))
173+
.make()
174+
.load(getClass().getClassLoader(), ClassLoadingStrategy.Default.INJECTION)
175+
.getLoaded();
176+
try {
177+
final Object instance = delegator.newInstance();
178+
delegator.getField("INSTANCE").set(instance, instance);
179+
} catch (InstantiationException | IllegalAccessException | NoSuchFieldException e) {
180+
throw new IllegalStateException(e);
181+
}
182+
environment.put("java.naming.ldap.factory.socket", delegator.getCanonicalName());
183+
}
117184
environment.put(LdapContext.CONTROL_FACTORIES, JAVA_RT_CONTROL_FACTORY);
118-
environment.put(Context.PROVIDER_URL, String.format("ldap://%s:%s",
185+
environment.put(Context.PROVIDER_URL, String.format("%s://%s:%s",
186+
useTls ? "ldaps" : "ldap",
119187
inMemoryDirectoryServer.getListenAddress().getHostName(),
120188
embeddedServerPort()));
121189
environment.put(Context.INITIAL_CONTEXT_FACTORY, JAVA_RT_CONTEXT_FACTORY);

embedded-ldap-core/src/test/java/org/zapodot/junit/ldap/internal/FakeEmbeddedLdapBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@ protected FakeEmbeddedLdapBuilder getThis() {
2020
EmbeddedLdapServer build() {
2121
try {
2222
InMemoryDirectoryServer server = createServer(createInMemoryServerConfiguration(), ldifsToImport);
23-
return new EmbeddedLdapServerImpl(
24-
server,
25-
authenticationConfiguration) {
23+
return new EmbeddedLdapServerImpl(server, authenticationConfiguration, false, null) {
2624

2725
};
2826
} catch (LDAPException e) {

embedded-ldap-junit/src/main/java/org/zapodot/junit/ldap/EmbeddedLdapRuleBuilder.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ protected EmbeddedLdapRuleBuilder getThis() {
3131
*/
3232
public EmbeddedLdapRule build() {
3333
Objects.requireNonNull(bindDSN, "\"bindDSN\" can not be null");
34-
return EmbeddedLdapRuleImpl.createForConfiguration(createInMemoryServerConfiguration(),
35-
authenticationConfiguration,
36-
ldifsToImport);
34+
return EmbeddedLdapRuleImpl.createForConfiguration(
35+
createInMemoryServerConfiguration(),
36+
authenticationConfiguration,
37+
ldifsToImport,
38+
useTls,
39+
socketFactory
40+
);
3741
}
3842

3943
}

embedded-ldap-junit/src/main/java/org/zapodot/junit/ldap/internal/EmbeddedLdapRuleImpl.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,23 +7,25 @@
77
import org.junit.runners.model.Statement;
88
import org.zapodot.junit.ldap.EmbeddedLdapRule;
99

10+
import javax.net.ssl.SSLSocketFactory;
1011
import java.util.List;
1112

1213
public class EmbeddedLdapRuleImpl extends EmbeddedLdapServerImpl implements EmbeddedLdapRule {
1314

1415
public static EmbeddedLdapRule createForConfiguration(final InMemoryDirectoryServerConfig inMemoryDirectoryServerConfig,
1516
final AuthenticationConfiguration authenticationConfiguration,
16-
final List<String> ldifs) {
17+
final List<String> ldifs, boolean useTls, SSLSocketFactory socketFactory) {
1718
try {
1819
return new EmbeddedLdapRuleImpl(EmbeddedLdapServerImpl.createServer(inMemoryDirectoryServerConfig, ldifs),
19-
authenticationConfiguration);
20+
authenticationConfiguration, useTls, socketFactory);
2021
} catch (LDAPException e) {
2122
throw new IllegalStateException("Can not initiate in-memory LDAP server due to an exception", e);
2223
}
2324
}
2425

25-
public EmbeddedLdapRuleImpl(InMemoryDirectoryServer inMemoryDirectoryServer, AuthenticationConfiguration authenticationConfiguration1) {
26-
super(inMemoryDirectoryServer, authenticationConfiguration1);
26+
public EmbeddedLdapRuleImpl(InMemoryDirectoryServer inMemoryDirectoryServer, AuthenticationConfiguration authenticationConfiguration1,
27+
boolean useTls, SSLSocketFactory socketFactory) {
28+
super(inMemoryDirectoryServer, authenticationConfiguration1, useTls, socketFactory);
2729
}
2830

2931

0 commit comments

Comments
 (0)