/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * This is utility class for DNS test, it contains many helper methods to * support test construction. * * For basic test sequence see TestBase and DNSTestBase.
*/ publicclass DNSTestUtils { publicstaticfinal String TEST_DNS_SERVER_THREAD = "test.dns.server.thread"; publicstaticfinal String TEST_DNS_ROOT_URL = "test.dns.root.url"; publicstaticfinalint HOSTS_LOOKUP_MAX_DEPTH = 3;
protectedstaticboolean debug = true;
/** * Check that attributes contains the mandatory attributes and the right * objectclass attribute. * * @param attrs given attributes to verify * @param mandatory given mandatory for verification * @param optional given optional for verification * @return <tt>true</tt> if check ok
*/ publicstaticboolean checkSchema(Attributes attrs, String[] mandatory,
String[] optional) { // Check mandatory attributes for (String mandatoryAttr : mandatory) { if (attrs.get(mandatoryAttr) == null) {
debug("missing mandatory attribute: " + mandatoryAttr); returnfalse;
}
}
// Check optional attributes int optMissing = 0; for (String optionalAttr : optional) { if (attrs.get(optionalAttr) == null) {
debug("warning: missing optional attribute: " + optionalAttr);
++optMissing;
}
}
if (attrs.size() > (mandatory.length + (optional.length
- optMissing))) {
debug("too many attributes: " + attrs); returnfalse;
}
returntrue;
}
/** * Process command line arguments and init env. * This method will prepare default environments which to be used to initial * DirContext. * * @param localServer <tt>true</tt> if this test will run against with local * server against dump message playback * @param testname test case name to identify playback file * @param args additional arguments for env preparation * @return prepared env which will be used later to initial DirContext
*/ publicstatic Hashtable<Object, Object> initEnv(boolean localServer,
String testname, String[] args) {
Hashtable<Object, Object> env = new Hashtable<>();
// set some default parameters if no additional specified
env.put("DNS_DOMAIN", "domain1.com.");
env.put("FOREIGN_DOMAIN", "Central.Sun.COM.");
env.put("FOREIGN_LEAF", "sunweb");
// set defaults for some JNDI properties
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.dns.DnsContextFactory");
Thread inst = null; if (traceEnable) { // if trace is enabled, create DNSTracer to dump those message
inst = createDNSTracer(testname, env);
} else { if (localServer) { // if use local server, create local DNSServer for playback
inst = createDNSServer(testname, loopPlayback);
} else { // for tests which run against remote server // or no server required
debug("Skip local DNS Server creation ");
}
}
/** * Clean-up the given directory context. * * @param ctx given context object
*/ publicstaticvoid cleanup(Context ctx) { if (ctx != null) { try {
ctx.close();
} catch (NamingException e) { // ignore
}
}
}
privatestaticvoid extractProperty(String propString,
Hashtable<Object, Object> env) { int index;
if ((index = propString.indexOf('=')) > 0) {
env.put(propString.substring(0, index),
propString.substring(index + 1));
} else { thrownew RuntimeException( "Failed to extract test args property from " + propString);
}
}
/** * Return new created DNS tracer. * * @param testname test case name to identify playback file * @param env given env for initialization * @return created DNS tracer * @see DNSTracer
*/ publicstatic DNSTracer createDNSTracer(String testname,
Hashtable<Object, Object> env) { try {
PrintStream outStream = new PrintStream(getCaptureFile(testname)); returnnew DNSTracer(outStream, (String) env.get("DNS_SERVER"),
Integer.parseInt((String) env.get("DNS_PORT")));
} catch (Exception e) { thrownew RuntimeException( "Error: failed to create DNSTracer : " + e.getMessage(), e);
}
}
/** * Return new created local DNS Server. * * @param testname test case name to identify playback file * @param loop <tt>true</tt> if DNS server required playback message in loop * @return created local DNS Server * @see DNSServer
*/ publicstatic DNSServer createDNSServer(String testname, boolean loop) {
String path = getCaptureFile(testname); if (Files.exists(Paths.get(path))) { try { returnnew DNSServer(path, loop);
} catch (Exception e) { thrownew RuntimeException( "Error: failed to create DNSServer : " + e.getMessage(),
e);
}
} else { thrownew RuntimeException( "Error: failed to create DNSServer, not found dns "
+ "cache file " + path);
}
}
/** * Return dns message capture file path. * * @param testname test case name to identify playback file * @return capture file path
*/ publicstatic String getCaptureFile(String testname) { return Paths.get(System.getProperty("test.src"))
.resolve(testname + ".dns").toString();
}
/** * Print given object if debug enabled. * * @param object given object to print
*/ publicstaticvoid debug(Object object) { if (debug) {
System.out.println(object);
}
}
/** * Verify attributes contains the mandatory attributes and the right * objectclass attribute, will throw RuntimeException if verify failed. * * @param attrs given attributes to verify * @param mandatory given mandatory for verification * @param optional given optional for verification
*/ publicstaticvoid verifySchema(Attributes attrs, String[] mandatory,
String[] optional) {
debug(attrs); if (!checkSchema(attrs, mandatory, optional)) { thrownew RuntimeException("Check schema failed.");
}
}
/** * Return dns root url. * * @param env given env * @return dns root url
*/ publicstatic String getRootUrl(Hashtable<Object, Object> env) { return (String) env.get(TEST_DNS_ROOT_URL);
}
/** * Assemble a fully-qualified domain name from the base component and the * domain name. * * @param base given base component * @param env given env * @param primary <tt>true</tt> if primary domain * @return assembled fully-qualified domain name
*/ publicstatic String buildFqdn(String base, Hashtable<Object, Object> env, boolean primary) {
String domain = (String) (primary ?
env.get("DNS_DOMAIN") :
env.get("FOREIGN_DOMAIN"));
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.