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.Closeable;
  28 import java.io.PrintStream;
  29 import java.net.DatagramSocket;
  30 import java.nio.file.Files;
  31 import java.nio.file.Path;
  32 import java.nio.file.Paths;
  33 import java.util.Hashtable;
  34 
  35 public class DNSTestUtils {
  36     public static final String TEST_DNS_SERVER_THREAD = "test.dns.server.thread";
  37     public static final String TEST_DNS_ROOT_URL = "test.dns.root.url";
  38     public static final int HOSTS_LOOKUP_MAX_DEPTH = 3;
  39 
  40     protected static boolean debug = true;
  41 
  42     /*
  43      * Check that attrs contains the mandatory attributes and the right
  44      * objectclass attribute
  45      */
  46     public static boolean checkSchema(Attributes attrs, String[] mandatory,
  47             String[] optional) {
  48         // Check mandatory attributes
  49         for (String mandatoryAttr : mandatory) {
  50             if (attrs.get(mandatoryAttr) == null) {
  51                 debug("missing mandatory attribute: " + mandatoryAttr);
  52                 return false;
  53             }
  54         }
  55 
  56         // Check optional attributes
  57         int optMissing = 0;
  58         for (String optionalAttr : optional) {
  59             if (attrs.get(optionalAttr) == null) {
  60                 debug("warning: missing optional attribute: " + optionalAttr);
  61                 ++optMissing;
  62             }
  63         }
  64 
  65         if (attrs.size() > (mandatory.length + (optional.length
  66                 - optMissing))) {
  67             debug("too many attributes: " + attrs);
  68             return false;
  69         }
  70 
  71         return true;
  72     }
  73 
  74     /*
  75      * Process command line arguments and init env
  76      */
  77     public static Hashtable<Object, Object> initEnv(DatagramSocket socket,
  78             String testname, String[] args) {
  79 
  80         Hashtable<Object, Object> env = new Hashtable<>();
  81 
  82         // set some default parameters if no additional specified
  83         env.put("DNS_DOMAIN", "domain1.com.");
  84         env.put("FOREIGN_DOMAIN", "Central.Sun.COM.");
  85         env.put("FOREIGN_LEAF", "sunweb");
  86 
  87         // set defaults for some JNDI properties
  88         env.put(Context.INITIAL_CONTEXT_FACTORY,
  89                 "com.sun.jndi.dns.DnsContextFactory");
  90 
  91         boolean traceEnable = false;
  92         boolean loopPlayback = false;
  93         for (int i = 0; i < args.length; i++) {
  94             if ((args[i].equals("-D")) && (args.length > i + 1)) {
  95                 extractProperty(args[++i], env);
  96             } else if (args[i].startsWith("-D")) {
  97                 extractProperty(args[i].substring(2), env);
  98             } else if (args[i].equalsIgnoreCase("-trace")) {
  99                 traceEnable = true;
 100             } else if (args[i].equalsIgnoreCase("-loop")) {
 101                 loopPlayback = true;
 102             }
 103         }
 104 
 105         debug = Boolean.valueOf(System.getProperty("debug", "true"));
 106 
 107         if (env.get("DNS_SERVER") != null) {
 108             String port = (String) env.get("DNS_PORT");
 109             String portSuffix = (port == null) ? "" : ":" + port;
 110             String url = "dns://" + env.get("DNS_SERVER") + portSuffix;
 111             env.put(Context.PROVIDER_URL, url);
 112             env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
 113         }
 114 
 115         Runnable inst = null;
 116         if (traceEnable) {
 117             inst = createDNSTracer(socket, testname, env);
 118         } else {
 119             if (socket != null) {
 120                 inst = createDNSServer(socket, testname, loopPlayback);
 121             } else {
 122                 // for tests which run against remote server
 123                 // or no server required
 124                 debug("Skip local DNS Server creation "
 125                         + "since DatagramSocket is null");
 126             }
 127         }
 128 
 129         if (inst != null) {
 130             env.put(TEST_DNS_SERVER_THREAD, startServer(inst));
 131             String url = "dns://localhost:" + socket.getLocalPort();
 132 
 133             env.put(TEST_DNS_ROOT_URL, url);
 134             env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
 135         }
 136 
 137         return env;
 138     }
 139 
 140     /*
 141      * Clean-up the directory context.
 142      */
 143     public static void cleanup(Context ctx) {
 144         if (ctx != null) {
 145             try {
 146                 ctx.close();
 147             } catch (NamingException e) {
 148                 // ignore
 149             }
 150         }
 151     }
 152 
 153     /*
 154      * Clean up given closable resource
 155      */
 156     public static void cleanupClosableRes(Closeable res) {
 157         if (res != null) {
 158             try {
 159                 res.close();
 160             } catch (Exception e) {
 161                 // ignore
 162             }
 163         }
 164     }
 165 
 166     private static void extractProperty(String propString,
 167             Hashtable<Object, Object> env) {
 168         int index;
 169 
 170         if ((index = propString.indexOf('=')) > 0) {
 171             env.put(propString.substring(0, index),
 172                     propString.substring(index + 1));
 173         } else {
 174             throw new RuntimeException(
 175                     "Failed to extract test args property from " + propString);
 176         }
 177     }
 178 
 179     public static DNSTracer createDNSTracer(DatagramSocket socket,
 180             String testname, Hashtable<Object, Object> env) {
 181         if (socket == null) {
 182             throw new RuntimeException("Error: failed to create DNSTracer "
 183                     + "since DatagramSocket is null");
 184         }
 185 
 186         try {
 187             PrintStream outStream = new PrintStream(getCaptureFile(testname));
 188             return new DNSTracer(socket, outStream,
 189                     (String) env.get("DNS_SERVER"),
 190                     Integer.parseInt((String) env.get("DNS_PORT")));
 191         } catch (Exception e) {
 192             throw new RuntimeException(
 193                     "Error: failed to create DNSTracer : " + e.getMessage(), e);
 194         }
 195     }
 196 
 197     public static DNSServer createDNSServer(DatagramSocket socket,
 198             String testname, boolean loop) {
 199         if (socket == null) {
 200             throw new RuntimeException("Error: failed to create DNSServer "
 201                     + "since DatagramSocket is null");
 202         }
 203 
 204         String path = getCaptureFile(testname);
 205         if (Files.exists(Paths.get(path))) {
 206             return new DNSServer(socket, path, loop);
 207         } else {
 208             throw new RuntimeException(
 209                     "Error: failed to create DNSServer, not found dns "
 210                             + "cache file " + path);
 211         }
 212     }
 213 
 214     public static Thread startServer(Runnable runnable) {
 215         Thread thread = new Thread(runnable);
 216         thread.start();
 217         return thread;
 218     }
 219 
 220     public static String getCaptureFile(String testname) {
 221         return Paths.get(System.getProperty("test.src"))
 222                 .resolve(testname + ".dns").toString();
 223     }
 224 
 225     public static void enableHostsFile(String hostsFile) {
 226         System.out.println("Enable jdk.net.hosts.file = " + hostsFile);
 227         System.setProperty("jdk.net.hosts.file", hostsFile);
 228     }
 229 
 230     public static void enableHostsFile(int depth) {
 231         Path path = Paths.get(System.getProperty("test.src", "."))
 232                 .toAbsolutePath();
 233         for (int i = depth; i >= 0; i--) {
 234             Path filePath = path.resolve("hosts");
 235             if (Files.exists(filePath) && !Files.isDirectory(filePath)) {
 236                 enableHostsFile(filePath.toString());
 237                 break;
 238             }
 239 
 240             path = path.getParent();
 241             if (path == null) {
 242                 break;
 243             }
 244         }
 245     }
 246 
 247     public static void debug(Object object) {
 248         if (debug) {
 249             System.out.println(object);
 250         }
 251     }
 252 
 253     public static void verifySchema(Attributes attrs, String[] mandatory,
 254             String[] optional) {
 255         debug(attrs);
 256         if (!checkSchema(attrs, mandatory, optional)) {
 257             throw new RuntimeException("Check schema failed.");
 258         }
 259     }
 260 
 261     public static String getRootUrl(Hashtable<Object, Object> env) {
 262         return (String) env.get(TEST_DNS_ROOT_URL);
 263     }
 264 
 265     /*
 266      * Assemble a fully-qualified domain name from the base component and the
 267      * domain name.
 268      */
 269     public static String buildFqdn(String base, Hashtable<Object, Object> env,
 270             boolean primary) {
 271         String domain = (String) (primary ?
 272                 env.get("DNS_DOMAIN") :
 273                 env.get("FOREIGN_DOMAIN"));
 274 
 275         return base + "." + domain;
 276     }
 277 }