Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jndi/ldap/lib/LDAPTestUtils.java
41155 views
1
/*
2
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import com.sun.jndi.ldap.LdapURL;
25
26
import javax.naming.Context;
27
import javax.naming.NamingEnumeration;
28
import javax.naming.NamingException;
29
import javax.naming.directory.Attribute;
30
import javax.naming.directory.Attributes;
31
import javax.naming.directory.DirContext;
32
import javax.naming.directory.SearchResult;
33
import java.io.FileNotFoundException;
34
import java.io.PrintStream;
35
import java.net.ServerSocket;
36
import java.nio.file.Files;
37
import java.nio.file.Path;
38
import java.nio.file.Paths;
39
import java.util.Enumeration;
40
import java.util.Hashtable;
41
import java.util.Vector;
42
43
public class LDAPTestUtils {
44
public static final String TEST_LDAP_SERVER_THREAD = "test.ldap.server.thread";
45
public static final int CERTS_LOOKUP_MAX_DEPTH = 4;
46
47
protected static boolean debug = true;
48
49
/*
50
* Process command line arguments and return properties in a Hashtable.
51
*/
52
public static Hashtable<Object, Object> initEnv(String testname,
53
String[] args) {
54
return initEnv(null, testname, args, false);
55
}
56
57
public static Hashtable<Object, Object> initEnv(ServerSocket socket,
58
String testname, String[] args, boolean authInfo) {
59
return initEnv(socket, null, testname, args, authInfo);
60
}
61
62
public static Hashtable<Object, Object> initEnv(ServerSocket socket, String providerUrl,
63
String testname, String[] args, boolean authInfo) {
64
65
Hashtable<Object, Object> env = new Hashtable<>();
66
String root = "o=IMC,c=US";
67
String vendor = "Vendor1";
68
String client = "Client1";
69
String realm = "";
70
Vector<String> refs = new Vector<>();
71
72
// set defaults for some JNDI properties
73
env.put(Context.INITIAL_CONTEXT_FACTORY,
74
"com.sun.jndi.ldap.LdapCtxFactory");
75
76
if (authInfo) {
77
env.put(Context.SECURITY_AUTHENTICATION, "simple");
78
env.put(Context.SECURITY_PRINCIPAL, "cn=admin,o=IMC,c=US");
79
env.put(Context.SECURITY_CREDENTIALS, "secret99");
80
}
81
82
env.put("root", root);
83
env.put("vendor", vendor);
84
env.put("client", client);
85
86
boolean traceEnable = false;
87
for (int i = 0; i < args.length; i++) {
88
if (args[i].equals("-D") && (args.length > i + 1)) {
89
extractProperty(args[++i], env);
90
} else if (args[i].startsWith("-D")) {
91
extractProperty(args[i].substring(2), env);
92
} else if (args[i].equals("-referral") && (args.length > i + 1)) {
93
refs.addElement(args[++i]);
94
} else if (args[i].equals("-trace")) {
95
traceEnable = true;
96
}
97
}
98
99
env.put("disabled.realm", realm);
100
101
if (refs.size() > 0) {
102
env.put("referrals", refs);
103
}
104
105
if (traceEnable) {
106
enableLDAPTrace(env, testname);
107
} else {
108
if (socket != null) {
109
env.put(TEST_LDAP_SERVER_THREAD,
110
startLDAPServer(socket, getCaptureFile(testname)));
111
String url = providerUrl != null ? providerUrl :
112
"ldap://localhost:" + socket.getLocalPort();
113
env.put("java.naming.provider.url", url);
114
} else {
115
// for tests which run against remote server or no server
116
// required
117
debug("Skip local LDAP Server creation "
118
+ "since ServerSocket is null");
119
}
120
}
121
122
return env;
123
}
124
125
/*
126
* Clean-up the directory context.
127
*/
128
public static void cleanup(DirContext ctx) {
129
if (ctx != null) {
130
try {
131
ctx.close();
132
} catch (NamingException e) {
133
// ignore
134
}
135
}
136
}
137
138
/*
139
* Clean-up the sub context.
140
*/
141
public static void cleanupSubcontext(DirContext ctx, String name) {
142
if (ctx != null) {
143
try {
144
ctx.destroySubcontext(name);
145
} catch (NamingException ne) {
146
// ignore
147
}
148
}
149
}
150
151
/*
152
* Assemble a distinguished name from the base components and the
153
* namespace root.
154
*
155
* The components are prefixed with 'dc=' if the root is a DC-style name.
156
* Otherwise they are prefixed with 'ou='.
157
*/
158
public static String buildDN(String[] bases, String root) {
159
160
StringBuilder dn = new StringBuilder();
161
String prefix;
162
163
if (!root.contains("dc=")) {
164
prefix = "ou=";
165
} else {
166
prefix = "dc=";
167
}
168
169
for (String base : bases) {
170
dn.append(prefix).append(base).append(",");
171
}
172
173
return dn.append(root).toString();
174
}
175
176
/*
177
* Scan the results to confirm that the expected name is present.
178
*/
179
public static int checkResult(NamingEnumeration results, String name)
180
throws NamingException {
181
182
return checkResult(results, new String[] { name }, null);
183
}
184
185
/*
186
* Scan the results to confirm that the expected names and attributes
187
* are present.
188
*/
189
public static int checkResult(NamingEnumeration results, String[] names,
190
Attributes attrs) throws NamingException {
191
192
int found = 0;
193
194
while (results != null && results.hasMore()) {
195
196
SearchResult entry = (SearchResult) results.next();
197
String entryDN = entry.getName();
198
199
debug(">>> received: " + entryDN);
200
201
if (entry.isRelative()) {
202
entryDN = entryDN.toLowerCase(); // normalize
203
} else {
204
LdapURL url = new LdapURL(entryDN); // extract DN
205
entryDN = url.getDN().toLowerCase(); // normalize
206
}
207
208
for (String name : names) {
209
if ((entryDN.contains(name.toLowerCase())) || (entryDN
210
.equalsIgnoreCase(name))) {
211
212
debug(">>> checked results: found '" + name + "'");
213
214
if (attrs == null || foundAttributes(entry, attrs)) {
215
found++;
216
break;
217
}
218
}
219
}
220
}
221
222
debug(">>> checked results: found " + found
223
+ " entries that meet the criteria.");
224
225
return found;
226
}
227
228
/*
229
* Confirm that the attributes are present in the entry.
230
*/
231
public static boolean foundAttributes(SearchResult entry, Attributes attrs)
232
throws NamingException {
233
234
Attributes eattrs = entry.getAttributes();
235
int found = 0;
236
237
if ((eattrs == null) || (attrs == null)) {
238
return false;
239
}
240
241
for (NamingEnumeration ne = attrs.getAll(); ne.hasMoreElements(); ) {
242
243
Attribute attr = (Attribute) ne.next();
244
245
if (equalsIgnoreCase(eattrs.get(attr.getID()), attr)) {
246
found++;
247
} else {
248
debug(">>> foundAttributes: no match for " + attr.getID());
249
}
250
}
251
debug(">>> foundAttributes: found " + found + " attributes");
252
return (found == attrs.size());
253
}
254
255
public static Thread startLDAPServer(ServerSocket serverSocket,
256
String fileName) {
257
if (serverSocket == null) {
258
throw new RuntimeException("Error: failed to create LDAPServer "
259
+ "since ServerSocket is null");
260
}
261
262
if (!Files.exists(Paths.get(fileName))) {
263
throw new RuntimeException(
264
"Error: failed to create LDAPServer, not found ldap "
265
+ "cache file " + fileName);
266
}
267
268
Thread thread = new Thread(() -> {
269
try {
270
new test.LDAPServer(serverSocket, fileName);
271
} catch (Exception e) {
272
System.out.println("Warning: LDAP server running with issue");
273
e.printStackTrace();
274
}
275
});
276
277
thread.start();
278
return thread;
279
}
280
281
private static boolean equalsIgnoreCase(Attribute received,
282
Attribute expected) {
283
284
if (received == null || !received.getID()
285
.equalsIgnoreCase(expected.getID())) {
286
return false;
287
}
288
289
try {
290
291
Enumeration expectedVals = expected.getAll();
292
Object obj;
293
while (expectedVals.hasMoreElements()) {
294
obj = expectedVals.nextElement();
295
if (!received.contains(obj)) {
296
if (!(obj instanceof String)) {
297
return false;
298
}
299
if (!received.contains(((String) obj).toLowerCase())) {
300
return false;
301
}
302
}
303
}
304
305
} catch (NamingException e) {
306
return false;
307
}
308
309
return true;
310
}
311
312
private static void extractProperty(String propString,
313
Hashtable<Object, Object> env) {
314
int index;
315
316
if ((index = propString.indexOf('=')) > 0) {
317
env.put(propString.substring(0, index),
318
propString.substring(index + 1));
319
} else {
320
throw new RuntimeException(
321
"Failed to extract test args property from " + propString);
322
}
323
}
324
325
private static void enableLDAPTrace(Hashtable<Object, Object> env,
326
String testname) {
327
try {
328
PrintStream outStream = new PrintStream(getCaptureFile(testname));
329
env.put("com.sun.jndi.ldap.trace.ber", outStream);
330
} catch (FileNotFoundException e) {
331
throw new RuntimeException(
332
"Error: failed to enable ldap trace: " + e.getMessage(), e);
333
}
334
}
335
336
private static String getCaptureFile(String testname) {
337
return Paths.get(System.getProperty("test.src"))
338
.resolve(testname + ".ldap").toString();
339
}
340
341
public static void debug(Object object) {
342
if (debug) {
343
System.out.println(object);
344
}
345
}
346
347
public static String findCertsHome(int depth) {
348
Path path = Paths.get(System.getProperty("test.src", "."))
349
.toAbsolutePath();
350
for (int i = depth; i >= 0; i--) {
351
Path homePath = path.resolve("certs");
352
if (Files.exists(homePath) && Files.isDirectory(homePath)) {
353
return homePath.toString();
354
}
355
356
path = path.getParent();
357
if (path == null) {
358
break;
359
}
360
}
361
362
return System.getProperty("test.src", ".");
363
}
364
}
365
366