Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jndi/dns/lib/DNSTestUtils.java
41155 views
1
/*
2
* Copyright (c) 2018, 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 javax.naming.Context;
25
import javax.naming.NamingException;
26
import javax.naming.directory.Attributes;
27
import java.io.PrintStream;
28
import java.nio.file.Files;
29
import java.nio.file.Path;
30
import java.nio.file.Paths;
31
import java.util.Hashtable;
32
33
/**
34
* This is utility class for DNS test, it contains many helper methods to
35
* support test construction.
36
*
37
* For basic test sequence see TestBase and DNSTestBase.
38
*/
39
public class DNSTestUtils {
40
public static final String TEST_DNS_SERVER_THREAD = "test.dns.server.thread";
41
public static final String TEST_DNS_ROOT_URL = "test.dns.root.url";
42
public static final int HOSTS_LOOKUP_MAX_DEPTH = 3;
43
44
protected static boolean debug = true;
45
46
/**
47
* Check that attributes contains the mandatory attributes and the right
48
* objectclass attribute.
49
*
50
* @param attrs given attributes to verify
51
* @param mandatory given mandatory for verification
52
* @param optional given optional for verification
53
* @return <tt>true</tt> if check ok
54
*/
55
public static boolean checkSchema(Attributes attrs, String[] mandatory,
56
String[] optional) {
57
// Check mandatory attributes
58
for (String mandatoryAttr : mandatory) {
59
if (attrs.get(mandatoryAttr) == null) {
60
debug("missing mandatory attribute: " + mandatoryAttr);
61
return false;
62
}
63
}
64
65
// Check optional attributes
66
int optMissing = 0;
67
for (String optionalAttr : optional) {
68
if (attrs.get(optionalAttr) == null) {
69
debug("warning: missing optional attribute: " + optionalAttr);
70
++optMissing;
71
}
72
}
73
74
if (attrs.size() > (mandatory.length + (optional.length
75
- optMissing))) {
76
debug("too many attributes: " + attrs);
77
return false;
78
}
79
80
return true;
81
}
82
83
/**
84
* Process command line arguments and init env.
85
* This method will prepare default environments which to be used to initial
86
* DirContext.
87
*
88
* @param localServer <tt>true</tt> if this test will run against with local
89
* server against dump message playback
90
* @param testname test case name to identify playback file
91
* @param args additional arguments for env preparation
92
* @return prepared env which will be used later to initial DirContext
93
*/
94
public static Hashtable<Object, Object> initEnv(boolean localServer,
95
String testname, String[] args) {
96
97
Hashtable<Object, Object> env = new Hashtable<>();
98
99
// set some default parameters if no additional specified
100
env.put("DNS_DOMAIN", "domain1.com.");
101
env.put("FOREIGN_DOMAIN", "Central.Sun.COM.");
102
env.put("FOREIGN_LEAF", "sunweb");
103
104
// set defaults for some JNDI properties
105
env.put(Context.INITIAL_CONTEXT_FACTORY,
106
"com.sun.jndi.dns.DnsContextFactory");
107
108
boolean traceEnable = false;
109
boolean loopPlayback = false;
110
for (String arg : args) {
111
if (arg.startsWith("-D")) {
112
extractProperty(arg.substring(2), env);
113
} else if (arg.equalsIgnoreCase("-trace")) {
114
traceEnable = true;
115
} else if (arg.equalsIgnoreCase("-loop")) {
116
loopPlayback = true;
117
}
118
}
119
120
debug = Boolean.valueOf(System.getProperty("debug", "true"));
121
122
// override testname here if it's been specified
123
String newTestName = (String) env.get("testname");
124
if (newTestName != null && !newTestName.isEmpty()) {
125
testname = newTestName;
126
}
127
128
if (env.get("DNS_SERVER") != null) {
129
String port = (String) env.get("DNS_PORT");
130
String portSuffix = (port == null) ? "" : ":" + port;
131
String url = "dns://" + env.get("DNS_SERVER") + portSuffix;
132
env.put(Context.PROVIDER_URL, url);
133
env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
134
}
135
136
Thread inst = null;
137
if (traceEnable) {
138
// if trace is enabled, create DNSTracer to dump those message
139
inst = createDNSTracer(testname, env);
140
} else {
141
if (localServer) {
142
// if use local server, create local DNSServer for playback
143
inst = createDNSServer(testname, loopPlayback);
144
} else {
145
// for tests which run against remote server
146
// or no server required
147
debug("Skip local DNS Server creation ");
148
}
149
}
150
151
if (inst != null) {
152
inst.start();
153
env.put(TEST_DNS_SERVER_THREAD, inst);
154
String url = "dns://localhost:" + ((Server) inst).getPort();
155
156
env.put(TEST_DNS_ROOT_URL, url);
157
env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
158
}
159
160
return env;
161
}
162
163
/**
164
* Clean-up the given directory context.
165
*
166
* @param ctx given context object
167
*/
168
public static void cleanup(Context ctx) {
169
if (ctx != null) {
170
try {
171
ctx.close();
172
} catch (NamingException e) {
173
// ignore
174
}
175
}
176
}
177
178
private static void extractProperty(String propString,
179
Hashtable<Object, Object> env) {
180
int index;
181
182
if ((index = propString.indexOf('=')) > 0) {
183
env.put(propString.substring(0, index),
184
propString.substring(index + 1));
185
} else {
186
throw new RuntimeException(
187
"Failed to extract test args property from " + propString);
188
}
189
}
190
191
/**
192
* Return new created DNS tracer.
193
*
194
* @param testname test case name to identify playback file
195
* @param env given env for initialization
196
* @return created DNS tracer
197
* @see DNSTracer
198
*/
199
public static DNSTracer createDNSTracer(String testname,
200
Hashtable<Object, Object> env) {
201
try {
202
PrintStream outStream = new PrintStream(getCaptureFile(testname));
203
return new DNSTracer(outStream, (String) env.get("DNS_SERVER"),
204
Integer.parseInt((String) env.get("DNS_PORT")));
205
} catch (Exception e) {
206
throw new RuntimeException(
207
"Error: failed to create DNSTracer : " + e.getMessage(), e);
208
}
209
}
210
211
/**
212
* Return new created local DNS Server.
213
*
214
* @param testname test case name to identify playback file
215
* @param loop <tt>true</tt> if DNS server required playback message in loop
216
* @return created local DNS Server
217
* @see DNSServer
218
*/
219
public static DNSServer createDNSServer(String testname, boolean loop) {
220
String path = getCaptureFile(testname);
221
if (Files.exists(Paths.get(path))) {
222
try {
223
return new DNSServer(path, loop);
224
} catch (Exception e) {
225
throw new RuntimeException(
226
"Error: failed to create DNSServer : " + e.getMessage(),
227
e);
228
}
229
} else {
230
throw new RuntimeException(
231
"Error: failed to create DNSServer, not found dns "
232
+ "cache file " + path);
233
}
234
}
235
236
/**
237
* Return dns message capture file path.
238
*
239
* @param testname test case name to identify playback file
240
* @return capture file path
241
*/
242
public static String getCaptureFile(String testname) {
243
return Paths.get(System.getProperty("test.src"))
244
.resolve(testname + ".dns").toString();
245
}
246
247
/**
248
* Enable hosts file.
249
*
250
* @param hostsFile given hosts file
251
*/
252
public static void enableHostsFile(String hostsFile) {
253
System.out.println("Enable jdk.net.hosts.file = " + hostsFile);
254
System.setProperty("jdk.net.hosts.file", hostsFile);
255
}
256
257
/**
258
* Enable hosts file by given searching depth.
259
*
260
* @param depth given depth for searching hosts file
261
*/
262
public static void enableHostsFile(int depth) {
263
Path path = Paths.get(System.getProperty("test.src", "."))
264
.toAbsolutePath();
265
for (int i = depth; i >= 0; i--) {
266
Path filePath = path.resolve("hosts");
267
if (Files.exists(filePath) && !Files.isDirectory(filePath)) {
268
enableHostsFile(filePath.toString());
269
break;
270
}
271
272
path = path.getParent();
273
if (path == null) {
274
break;
275
}
276
}
277
}
278
279
/**
280
* Print given object if debug enabled.
281
*
282
* @param object given object to print
283
*/
284
public static void debug(Object object) {
285
if (debug) {
286
System.out.println(object);
287
}
288
}
289
290
/**
291
* Verify attributes contains the mandatory attributes and the right
292
* objectclass attribute, will throw RuntimeException if verify failed.
293
*
294
* @param attrs given attributes to verify
295
* @param mandatory given mandatory for verification
296
* @param optional given optional for verification
297
*/
298
public static void verifySchema(Attributes attrs, String[] mandatory,
299
String[] optional) {
300
debug(attrs);
301
if (!checkSchema(attrs, mandatory, optional)) {
302
throw new RuntimeException("Check schema failed.");
303
}
304
}
305
306
/**
307
* Return dns root url.
308
*
309
* @param env given env
310
* @return dns root url
311
*/
312
public static String getRootUrl(Hashtable<Object, Object> env) {
313
return (String) env.get(TEST_DNS_ROOT_URL);
314
}
315
316
/**
317
* Assemble a fully-qualified domain name from the base component and the
318
* domain name.
319
*
320
* @param base given base component
321
* @param env given env
322
* @param primary <tt>true</tt> if primary domain
323
* @return assembled fully-qualified domain name
324
*/
325
public static String buildFqdn(String base, Hashtable<Object, Object> env,
326
boolean primary) {
327
String domain = (String) (primary ?
328
env.get("DNS_DOMAIN") :
329
env.get("FOREIGN_DOMAIN"));
330
331
return base + "." + domain;
332
}
333
}
334
335