Skip to content

Commit 98ae009

Browse files
author
Emmanuel Lécharny
committed
Added a unit test for SASL PLAIN bind
1 parent e539a5b commit 98ae009

File tree

2 files changed

+184
-2
lines changed

2 files changed

+184
-2
lines changed
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*
19+
*/
20+
21+
package org.apache.directory.shared.client.api.operations.bind;
22+
23+
24+
import static org.junit.jupiter.api.Assertions.assertEquals;
25+
import static org.junit.jupiter.api.Assertions.assertNotNull;
26+
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
28+
/**
29+
* TODO Test.
30+
*
31+
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
32+
*/
33+
import java.security.KeyStore;
34+
import java.security.KeyStoreException;
35+
import java.security.NoSuchAlgorithmException;
36+
import java.security.cert.CertificateException;
37+
import javax.net.ssl.TrustManagerFactory;
38+
import javax.security.auth.login.Configuration;
39+
40+
import org.apache.directory.api.ldap.model.constants.SupportedSaslMechanisms;
41+
import org.apache.directory.api.ldap.model.cursor.CursorException;
42+
import org.apache.directory.api.ldap.model.exception.LdapException;
43+
import org.apache.directory.api.ldap.model.message.BindResponse;
44+
import org.apache.directory.api.ldap.model.message.ResultCodeEnum;
45+
import org.apache.directory.api.util.Network;
46+
import org.apache.directory.ldap.client.api.LdapAsyncConnection;
47+
import org.apache.directory.ldap.client.api.LdapConnectionConfig;
48+
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
49+
import org.apache.directory.ldap.client.api.SaslGssApiRequest;
50+
import org.apache.directory.ldap.client.api.SaslPlainRequest;
51+
import org.apache.directory.server.annotations.CreateLdapServer;
52+
import org.apache.directory.server.annotations.CreateTransport;
53+
import org.apache.directory.server.annotations.SaslMechanism;
54+
import org.apache.directory.server.core.annotations.ApplyLdifs;
55+
import org.apache.directory.server.core.annotations.ContextEntry;
56+
import org.apache.directory.server.core.annotations.CreateDS;
57+
import org.apache.directory.server.core.annotations.CreateIndex;
58+
import org.apache.directory.server.core.annotations.CreatePartition;
59+
import org.apache.directory.server.core.integ.AbstractLdapTestUnit;
60+
import org.apache.directory.server.core.integ.ApacheDSTestExtension;
61+
import org.apache.directory.server.ldap.handlers.sasl.plain.PlainMechanismHandler;
62+
import org.junit.jupiter.api.AfterEach;
63+
import org.junit.jupiter.api.BeforeEach;
64+
import org.junit.jupiter.api.Test;
65+
import org.junit.jupiter.api.extension.ExtendWith;
66+
67+
68+
/**
69+
* Test the SASL BindRequest operation
70+
*
71+
* @author <a href="mailto:dev@directory.apache.org">Apache Directory Project</a>
72+
*/
73+
@ExtendWith( { ApacheDSTestExtension.class } )
74+
@CreateDS(
75+
name = "SASLBindDS",
76+
partitions =
77+
{
78+
@CreatePartition(
79+
name = "example",
80+
suffix = "dc=example,dc=com",
81+
contextEntry = @ContextEntry(
82+
entryLdif =
83+
"dn: dc=example,dc=com\n" +
84+
"dc: example\n" +
85+
"objectClass: top\n" +
86+
"objectClass: domain\n\n"),
87+
indexes =
88+
{
89+
@CreateIndex(attribute = "objectClass"),
90+
@CreateIndex(attribute = "sn"),
91+
@CreateIndex(attribute = "cn"),
92+
@CreateIndex(attribute = "displayName")
93+
})
94+
},
95+
enableChangeLog = true)
96+
@CreateLdapServer(
97+
saslMechanisms =
98+
{
99+
@SaslMechanism(name = SupportedSaslMechanisms.PLAIN, implClass = PlainMechanismHandler.class)
100+
},
101+
transports =
102+
{
103+
@CreateTransport(protocol = "LDAP"),
104+
@CreateTransport(protocol = "LDAPS")
105+
}
106+
)
107+
@ApplyLdifs(
108+
{
109+
// Entry # 1
110+
"dn: ou=users,dc=example,dc=com",
111+
"objectClass: organizationalUnit",
112+
"objectClass: top",
113+
"ou: users",
114+
"",
115+
116+
"dn: uid=superuser,ou=users,dc=example,dc=com",
117+
"objectClass: person",
118+
"objectClass: organizationalPerson",
119+
"objectClass: inetOrgPerson",
120+
"objectClass: top",
121+
"cn: superuser",
122+
"sn: administrator",
123+
"displayName: Directory Superuser",
124+
"uid: superuser",
125+
"userPassword: test",
126+
"",
127+
// Entry # 2
128+
"dn: uid=superuser2,ou=users,dc=example,dc=com",
129+
"objectClass: person",
130+
"objectClass: organizationalPerson",
131+
"objectClass: inetOrgPerson",
132+
"objectClass: top",
133+
"cn: superuser2",
134+
"sn: administrator",
135+
"displayName: Directory Superuser",
136+
"uid: superuser2",
137+
"userPassword: test1",
138+
"userPassword: test2" })
139+
public class SaslBindRequestTest extends AbstractLdapTestUnit
140+
{
141+
private LdapAsyncConnection connection;
142+
143+
/**
144+
* Create the LdapConnection
145+
*/
146+
@BeforeEach
147+
public void setup() throws Exception
148+
{
149+
connection = new LdapNetworkConnection( Network.LOOPBACK_HOSTNAME, getLdapServer().getPort() );
150+
}
151+
152+
153+
/**
154+
* Close the LdapConnection
155+
*/
156+
@AfterEach
157+
public void shutdown() throws Exception
158+
{
159+
if ( connection != null )
160+
{
161+
connection.close();
162+
}
163+
}
164+
165+
166+
/**
167+
* Test a successful SASL PLAIN bind request.
168+
*/
169+
@Test
170+
public void testSaslPlainBindRequest() throws Exception
171+
{
172+
SaslPlainRequest saslPlainRequest = new SaslPlainRequest();
173+
saslPlainRequest.setAuthorizationId(null);
174+
saslPlainRequest.setUsername( "superuser" );
175+
saslPlainRequest.setCredentials( "test" );
176+
177+
BindResponse bindResponse = connection.bind( saslPlainRequest );
178+
179+
assertNotNull( bindResponse );
180+
assertEquals( ResultCodeEnum.SUCCESS, bindResponse.getLdapResult().getResultCode() );
181+
assertTrue( connection.isAuthenticated() );
182+
}
183+
}

test-framework/src/main/java/org/apache/directory/server/core/integ/AbstractLdapTestUnit.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ public void setLdapServer( LdapServer ldapServer )
9292
{
9393
AbstractLdapTestUnit.ldapServer = ldapServer;
9494
}
95-
96-
95+
9796
public void changeCertificate( String keyStoreFile, String password, String issuerDn, String subjectDn, int days, String algorithm )
9897
throws IOException, GeneralSecurityException
9998
{

0 commit comments

Comments
 (0)