/* * 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. */ import javax.naming.Context; import javax.naming.NamingException; import javax.naming.directory.Attributes; import java.io.Closeable; import java.io.PrintStream; import java.net.DatagramSocket; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.Hashtable; public class DNSTestUtils { public static final String TEST_DNS_SERVER_THREAD = "test.dns.server.thread"; public static final String TEST_DNS_ROOT_URL = "test.dns.root.url"; public static final int HOSTS_LOOKUP_MAX_DEPTH = 3; protected static boolean debug = true; /* * Check that attrs contains the mandatory attributes and the right * objectclass attribute */ public static boolean checkSchema(Attributes attrs, String[] mandatory, String[] optional) { // Check mandatory attributes for (String mandatoryAttr : mandatory) { if (attrs.get(mandatoryAttr) == null) { debug("missing mandatory attribute: " + mandatoryAttr); return false; } } // 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); return false; } return true; } /* * Process command line arguments and init env */ public static Hashtable initEnv(boolean localServer, String testname, String[] args) { Hashtable 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"); boolean traceEnable = false; boolean loopPlayback = false; for (int i = 0; i < args.length; i++) { if ((args[i].equals("-D")) && (args.length > i + 1)) { extractProperty(args[++i], env); } else if (args[i].startsWith("-D")) { extractProperty(args[i].substring(2), env); } else if (args[i].equalsIgnoreCase("-trace")) { traceEnable = true; } else if (args[i].equalsIgnoreCase("-loop")) { loopPlayback = true; } } debug = Boolean.valueOf(System.getProperty("debug", "true")); if (env.get("DNS_SERVER") != null) { String port = (String) env.get("DNS_PORT"); String portSuffix = (port == null) ? "" : ":" + port; String url = "dns://" + env.get("DNS_SERVER") + portSuffix; env.put(Context.PROVIDER_URL, url); env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN")); } Thread inst = null; if (traceEnable) { inst = createDNSTracer(testname, env); } else { if (localServer) { inst = createDNSServer(testname, loopPlayback); } else { // for tests which run against remote server // or no server required debug("Skip local DNS Server creation "); } } if (inst != null) { env.put(TEST_DNS_SERVER_THREAD, inst); inst.start(); String url = "dns://localhost:" + ((Server) inst).getPort(); env.put(TEST_DNS_ROOT_URL, url); env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN")); } return env; } /* * Clean-up the directory context. */ public static void cleanup(Context ctx) { if (ctx != null) { try { ctx.close(); } catch (NamingException e) { // ignore } } } /* * Clean up given closable resource */ public static void cleanupClosableRes(Closeable res) { if (res != null) { try { res.close(); } catch (Exception e) { // ignore } } } private static void extractProperty(String propString, Hashtable env) { int index; if ((index = propString.indexOf('=')) > 0) { env.put(propString.substring(0, index), propString.substring(index + 1)); } else { throw new RuntimeException( "Failed to extract test args property from " + propString); } } public static DNSTracer createDNSTracer(String testname, Hashtable env) { try { PrintStream outStream = new PrintStream(getCaptureFile(testname)); return new DNSTracer(outStream, (String) env.get("DNS_SERVER"), Integer.parseInt((String) env.get("DNS_PORT"))); } catch (Exception e) { throw new RuntimeException( "Error: failed to create DNSTracer : " + e.getMessage(), e); } } public static DNSServer createDNSServer(String testname, boolean loop) { String path = getCaptureFile(testname); if (Files.exists(Paths.get(path))) { try { return new DNSServer(path, loop); } catch (Exception e) { throw new RuntimeException( "Error: failed to create DNSServer : " + e.getMessage(), e); } } else { throw new RuntimeException( "Error: failed to create DNSServer, not found dns " + "cache file " + path); } } public static String getCaptureFile(String testname) { return Paths.get(System.getProperty("test.src")) .resolve(testname + ".dns").toString(); } public static void enableHostsFile(String hostsFile) { System.out.println("Enable jdk.net.hosts.file = " + hostsFile); System.setProperty("jdk.net.hosts.file", hostsFile); } public static void enableHostsFile(int depth) { Path path = Paths.get(System.getProperty("test.src", ".")) .toAbsolutePath(); for (int i = depth; i >= 0; i--) { Path filePath = path.resolve("hosts"); if (Files.exists(filePath) && !Files.isDirectory(filePath)) { enableHostsFile(filePath.toString()); break; } path = path.getParent(); if (path == null) { break; } } } public static void debug(Object object) { if (debug) { System.out.println(object); } } public static void verifySchema(Attributes attrs, String[] mandatory, String[] optional) { debug(attrs); if (!checkSchema(attrs, mandatory, optional)) { throw new RuntimeException("Check schema failed."); } } public static String getRootUrl(Hashtable env) { return (String) env.get(TEST_DNS_ROOT_URL); } /* * Assemble a fully-qualified domain name from the base component and the * domain name. */ public static String buildFqdn(String base, Hashtable env, boolean primary) { String domain = (String) (primary ? env.get("DNS_DOMAIN") : env.get("FOREIGN_DOMAIN")); return base + "." + domain; } }