summaryrefslogtreecommitdiffstats
path: root/src/test/java/com/gitblit/tests/LdapConnectionTest.java
blob: f8d2fed0db9d6f346b939146f584cbd7c8426909 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package com.gitblit.tests;

import static org.junit.Assume.assumeTrue;

import java.util.ArrayList;
import java.util.Arrays;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import com.gitblit.Keys;
import com.gitblit.ldap.LdapConnection;
import com.unboundid.ldap.sdk.BindResult;
import com.unboundid.ldap.sdk.LDAPException;
import com.unboundid.ldap.sdk.ResultCode;
import com.unboundid.ldap.sdk.SearchRequest;
import com.unboundid.ldap.sdk.SearchResult;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.unboundid.ldap.sdk.SearchScope;

/*
 * Test for the LdapConnection
 *
 * @author Florian Zschocke
 *
 */
@RunWith(Parameterized.class)
public class LdapConnectionTest extends LdapBasedUnitTest {

	@Test
	public void testEscapeLDAPFilterString() {
		// This test is independent from authentication mode, so run only once.
		assumeTrue(authMode == AuthMode.ANONYMOUS);

		// From: https://www.owasp.org/index.php/Preventing_LDAP_Injection_in_Java
		assertEquals("No special characters to escape", "Hi This is a test #çà", LdapConnection.escapeLDAPSearchFilter("Hi This is a test #çà"));
		assertEquals("LDAP Christams Tree", "Hi \\28This\\29 = is \\2a a \\5c test # ç à ô", LdapConnection.escapeLDAPSearchFilter("Hi (This) = is * a \\ test # ç à ô"));

		assertEquals("Injection", "\\2a\\29\\28userPassword=secret", LdapConnection.escapeLDAPSearchFilter("*)(userPassword=secret"));
	}


	@Test
	public void testConnect() {
		// This test is independent from authentication mode, so run only once.
		assumeTrue(authMode == AuthMode.ANONYMOUS);

		LdapConnection conn = new LdapConnection(settings);
		try {
			assertTrue(conn.connect());
		} finally {
			conn.close();
		}
	}


	@Test
	public void testBindAnonymous() {
		// This test tests for anonymous bind, so run only in authentication mode ANONYMOUS.
		assumeTrue(authMode == AuthMode.ANONYMOUS);

		LdapConnection conn = new LdapConnection(settings);
		try {
			assertTrue(conn.connect());

			BindResult br = conn.bind();
			assertNotNull(br);
			assertEquals(ResultCode.SUCCESS, br.getResultCode());
			assertEquals("", authMode.getBindTracker().getLastSuccessfulBindDN(br.getMessageID()));

		} finally {
			conn.close();
		}
	}


	@Test
	public void testBindAsAdmin() {
		// This test tests for anonymous bind, so run only in authentication mode DS_MANAGER.
		assumeTrue(authMode == AuthMode.DS_MANAGER);

		LdapConnection conn = new LdapConnection(settings);
		try {
			assertTrue(conn.connect());

			BindResult br = conn.bind();
			assertNotNull(br);
			assertEquals(ResultCode.SUCCESS, br.getResultCode());
			assertEquals(settings.getString(Keys.realm.ldap.username, "UNSET"), authMode.getBindTracker().getLastSuccessfulBindDN(br.getMessageID()));

		} finally {
			conn.close();
		}
	}


	@Test
	public void testBindToBindpattern() {
		LdapConnection conn = new LdapConnection(settings);
		try {
			assertTrue(conn.connect());

			String bindPattern = "CN=${username},OU=Canada," + ACCOUNT_BASE;

			BindResult br = conn.bind(bindPattern, "UserThree", "userThreePassword");
			assertNotNull(br);
			assertEquals(ResultCode.SUCCESS, br.getResultCode());
			assertEquals("CN=UserThree,OU=Canada," + ACCOUNT_BASE, authMode.getBindTracker().getLastSuccessfulBindDN(br.getMessageID()));

			br = conn.bind(bindPattern, "UserFour", "userThreePassword");
			assertNull(br);

			br = conn.bind(bindPattern, "UserTwo", "userTwoPassword");
			assertNull(br);

		} finally {
			conn.close();
		}
	}


	@Test
	public void testRebindAsUser() {
		LdapConnection conn = new LdapConnection(settings);
		try {
			assertTrue(conn.connect());

			assertFalse(conn.rebindAsUser());

			BindResult br = conn.bind();
			assertNotNull(br);
			assertFalse(conn.rebindAsUser());


			String bindPattern = "CN=${username},OU=Canada," + ACCOUNT_BASE;
			br = conn.bind(bindPattern, "UserThree", "userThreePassword");
			assertNotNull(br);
			assertFalse(conn.rebindAsUser());

			br = conn.bind();
			assertNotNull(br);
			assertTrue(conn.rebindAsUser());
			assertEquals(ResultCode.SUCCESS, br.getResultCode());
			assertEquals("CN=UserThree,OU=Canada," + ACCOUNT_BASE, authMode.getBindTracker().getLastSuccessfulBindDN());

		} finally {
			conn.close();
		}
	}



	@Test
	public void testSearchRequest() throws LDAPException {
		LdapConnection conn = new LdapConnection(settings);
		try {
			assertTrue(conn.connect());
			BindResult br = conn.bind();
			assertNotNull(br);

			SearchRequest req;
			SearchResult result;
			SearchResultEntry entry;

			req = new SearchRequest(ACCOUNT_BASE, SearchScope.BASE, "(CN=UserOne)");
			result = conn.search(req);
			assertNotNull(result);
			assertEquals(0, result.getEntryCount());

			req = new SearchRequest(ACCOUNT_BASE, SearchScope.ONE, "(CN=UserTwo)");
			result = conn.search(req);
			assertNotNull(result);
			assertEquals(0, result.getEntryCount());

			req = new SearchRequest(ACCOUNT_BASE, SearchScope.SUB, "(CN=UserThree)");
			result = conn.search(req);
			assertNotNull(result);
			assertEquals(1, result.getEntryCount());
			entry = result.getSearchEntries().get(0);
			assertEquals("CN=UserThree,OU=Canada," + ACCOUNT_BASE, entry.getDN());

			req = new SearchRequest(ACCOUNT_BASE, SearchScope.SUBORDINATE_SUBTREE, "(CN=UserFour)");
			result = conn.search(req);
			assertNotNull(result);
			assertEquals(1, result.getEntryCount());
			entry = result.getSearchEntries().get(0);
			assertEquals("CN=UserFour,OU=Canada," + ACCOUNT_BASE, entry.getDN());

		} finally {
			conn.close();
		}
	}


	@Test
	public void testSearch() throws LDAPException {
		LdapConnection conn = new LdapConnection(settings);
		try {
			assertTrue(conn.connect());
			BindResult br = conn.bind();
			assertNotNull(br);

			SearchResult result;
			SearchResultEntry entry;

			result = conn.search(ACCOUNT_BASE, false, "(CN=UserOne)", null);
			assertNotNull(result);
			assertEquals(1, result.getEntryCount());
			entry = result.getSearchEntries().get(0);
			assertEquals("CN=UserOne,OU=US," + ACCOUNT_BASE, entry.getDN());

			result = conn.search(ACCOUNT_BASE, true, "(&(CN=UserOne)(surname=One))", null);
			assertNotNull(result);
			assertEquals(1, result.getEntryCount());
			entry = result.getSearchEntries().get(0);
			assertEquals("CN=UserOne,OU=US," + ACCOUNT_BASE, entry.getDN());

			result = conn.search(ACCOUNT_BASE, true, "(&(CN=UserOne)(surname=Two))", null);
			assertNotNull(result);
			assertEquals(0, result.getEntryCount());

			result = conn.search(ACCOUNT_BASE, true, "(surname=Two)", Arrays.asList("givenName", "surname"));
			assertNotNull(result);
			assertEquals(1, result.getEntryCount());
			entry = result.getSearchEntries().get(0);
			assertEquals("CN=UserTwo,OU=US," + ACCOUNT_BASE, entry.getDN());
			assertEquals(2, entry.getAttributes().size());
			assertEquals("User", entry.getAttributeValue("givenName"));
			assertEquals("Two", entry.getAttributeValue("surname"));

			result = conn.search(ACCOUNT_BASE, true, "(personalTitle=Mr*)", null);
			assertNotNull(result);
			assertEquals(3, result.getEntryCount());
			ArrayList<String> names = new ArrayList<>(3);
			names.add(result.getSearchEntries().get(0).getAttributeValue("surname"));
			names.add(result.getSearchEntries().get(1).getAttributeValue("surname"));
			names.add(result.getSearchEntries().get(2).getAttributeValue("surname"));
			assertTrue(names.contains("One"));
			assertTrue(names.contains("Two"));
			assertTrue(names.contains("Three"));

		} finally {
			conn.close();
		}
	}

}