test/jdk/com/sun/jndi/dns/lib/DNSTestUtils.java
author xyin
Tue, 13 Feb 2018 12:19:37 +0800
changeset 48851 1d8f882f2b2f
child 51192 499b873761d8
permissions -rw-r--r--
8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java Reviewed-by: vtewari, rriggs, alanb, jjiang, xiaofeya
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48851
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     1
/*
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     4
 *
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     8
 *
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    13
 * accompanied this code).
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    14
 *
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    18
 *
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    21
 * questions.
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    22
 */
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    23
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    24
import javax.naming.Context;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    25
import javax.naming.NamingException;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    26
import javax.naming.directory.Attributes;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    27
import java.io.PrintStream;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    28
import java.net.DatagramSocket;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    29
import java.nio.file.Files;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    30
import java.nio.file.Path;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    31
import java.nio.file.Paths;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    32
import java.util.Hashtable;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    33
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    34
public class DNSTestUtils {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    35
    public static final String TEST_DNS_SERVER_THREAD = "test.dns.server.thread";
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    36
    public static final String TEST_DNS_ROOT_URL = "test.dns.root.url";
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    37
    public static final int HOSTS_LOOKUP_MAX_DEPTH = 3;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    38
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    39
    protected static boolean debug = true;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    40
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    41
    /*
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    42
     * Check that attrs contains the mandatory attributes and the right
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    43
     * objectclass attribute
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    44
     */
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    45
    public static boolean checkSchema(Attributes attrs, String[] mandatory,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    46
            String[] optional) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    47
        // Check mandatory attributes
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    48
        for (String mandatoryAttr : mandatory) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    49
            if (attrs.get(mandatoryAttr) == null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    50
                debug("missing mandatory attribute: " + mandatoryAttr);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    51
                return false;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    52
            }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    53
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    54
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    55
        // Check optional attributes
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    56
        int optMissing = 0;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    57
        for (String optionalAttr : optional) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    58
            if (attrs.get(optionalAttr) == null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    59
                debug("warning: missing optional attribute: " + optionalAttr);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    60
                ++optMissing;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    61
            }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    62
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    63
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    64
        if (attrs.size() > (mandatory.length + (optional.length
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    65
                - optMissing))) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    66
            debug("too many attributes: " + attrs);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    67
            return false;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    68
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    69
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    70
        return true;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    71
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    72
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    73
    /*
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    74
     * Process command line arguments and init env
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    75
     */
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    76
    public static Hashtable<Object, Object> initEnv(DatagramSocket socket,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    77
            String testname, String[] args) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    78
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    79
        Hashtable<Object, Object> env = new Hashtable<>();
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    80
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    81
        // set some default parameters if no additional specified
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    82
        env.put("DNS_DOMAIN", "domain1.com.");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    83
        env.put("FOREIGN_DOMAIN", "Central.Sun.COM.");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    84
        env.put("FOREIGN_LEAF", "sunweb");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    85
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    86
        // set defaults for some JNDI properties
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    87
        env.put(Context.INITIAL_CONTEXT_FACTORY,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    88
                "com.sun.jndi.dns.DnsContextFactory");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    89
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    90
        boolean traceEnable = false;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    91
        boolean loopPlayback = false;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    92
        for (int i = 0; i < args.length; i++) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    93
            if ((args[i].equals("-D")) && (args.length > i + 1)) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    94
                extractProperty(args[++i], env);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    95
            } else if (args[i].startsWith("-D")) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    96
                extractProperty(args[i].substring(2), env);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    97
            } else if (args[i].equalsIgnoreCase("-trace")) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    98
                traceEnable = true;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
    99
            } else if (args[i].equalsIgnoreCase("-loop")) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   100
                loopPlayback = true;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   101
            }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   102
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   103
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   104
        debug = Boolean.valueOf(System.getProperty("debug", "true"));
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   105
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   106
        if (env.get("DNS_SERVER") != null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   107
            String port = (String) env.get("DNS_PORT");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   108
            String portSuffix = (port == null) ? "" : ":" + port;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   109
            String url = "dns://" + env.get("DNS_SERVER") + portSuffix;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   110
            env.put(Context.PROVIDER_URL, url);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   111
            env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   112
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   113
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   114
        Runnable inst = null;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   115
        if (traceEnable) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   116
            inst = createDNSTracer(socket, testname, env);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   117
        } else {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   118
            if (socket != null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   119
                inst = createDNSServer(socket, testname, loopPlayback);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   120
            } else {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   121
                // for tests which run against remote server
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   122
                // or no server required
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   123
                debug("Skip local DNS Server creation "
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   124
                        + "since DatagramSocket is null");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   125
            }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   126
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   127
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   128
        if (inst != null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   129
            env.put(TEST_DNS_SERVER_THREAD, startServer(inst));
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   130
            String url = "dns://localhost:" + socket.getLocalPort();
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   131
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   132
            env.put(TEST_DNS_ROOT_URL, url);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   133
            env.put(Context.PROVIDER_URL, url + "/" + env.get("DNS_DOMAIN"));
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   134
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   135
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   136
        return env;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   137
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   138
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   139
    /*
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   140
     * Clean-up the directory context.
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   141
     */
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   142
    public static void cleanup(Context ctx) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   143
        if (ctx != null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   144
            try {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   145
                ctx.close();
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   146
            } catch (NamingException e) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   147
                // ignore
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   148
            }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   149
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   150
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   151
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   152
    private static void extractProperty(String propString,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   153
            Hashtable<Object, Object> env) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   154
        int index;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   155
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   156
        if ((index = propString.indexOf('=')) > 0) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   157
            env.put(propString.substring(0, index),
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   158
                    propString.substring(index + 1));
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   159
        } else {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   160
            throw new RuntimeException(
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   161
                    "Failed to extract test args property from " + propString);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   162
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   163
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   164
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   165
    public static DNSTracer createDNSTracer(DatagramSocket socket,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   166
            String testname, Hashtable<Object, Object> env) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   167
        if (socket == null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   168
            throw new RuntimeException("Error: failed to create DNSTracer "
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   169
                    + "since DatagramSocket is null");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   170
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   171
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   172
        try {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   173
            PrintStream outStream = new PrintStream(getCaptureFile(testname));
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   174
            return new DNSTracer(socket, outStream,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   175
                    (String) env.get("DNS_SERVER"),
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   176
                    Integer.parseInt((String) env.get("DNS_PORT")));
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   177
        } catch (Exception e) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   178
            throw new RuntimeException(
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   179
                    "Error: failed to create DNSTracer : " + e.getMessage(), e);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   180
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   181
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   182
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   183
    public static DNSServer createDNSServer(DatagramSocket socket,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   184
            String testname, boolean loop) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   185
        if (socket == null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   186
            throw new RuntimeException("Error: failed to create DNSServer "
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   187
                    + "since DatagramSocket is null");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   188
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   189
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   190
        String path = getCaptureFile(testname);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   191
        if (Files.exists(Paths.get(path))) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   192
            return new DNSServer(socket, path, loop);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   193
        } else {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   194
            throw new RuntimeException(
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   195
                    "Error: failed to create DNSServer, not found dns "
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   196
                            + "cache file " + path);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   197
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   198
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   199
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   200
    public static Thread startServer(Runnable runnable) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   201
        Thread thread = new Thread(runnable);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   202
        thread.start();
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   203
        return thread;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   204
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   205
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   206
    public static String getCaptureFile(String testname) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   207
        return Paths.get(System.getProperty("test.src"))
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   208
                .resolve(testname + ".dns").toString();
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   209
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   210
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   211
    public static void enableHostsFile(String hostsFile) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   212
        System.out.println("Enable jdk.net.hosts.file = " + hostsFile);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   213
        System.setProperty("jdk.net.hosts.file", hostsFile);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   214
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   215
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   216
    public static void enableHostsFile(int depth) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   217
        Path path = Paths.get(System.getProperty("test.src", "."))
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   218
                .toAbsolutePath();
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   219
        for (int i = depth; i >= 0; i--) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   220
            Path filePath = path.resolve("hosts");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   221
            if (Files.exists(filePath) && !Files.isDirectory(filePath)) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   222
                enableHostsFile(filePath.toString());
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   223
                break;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   224
            }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   225
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   226
            path = path.getParent();
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   227
            if (path == null) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   228
                break;
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   229
            }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   230
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   231
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   232
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   233
    public static void debug(Object object) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   234
        if (debug) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   235
            System.out.println(object);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   236
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   237
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   238
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   239
    public static void verifySchema(Attributes attrs, String[] mandatory,
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   240
            String[] optional) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   241
        debug(attrs);
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   242
        if (!checkSchema(attrs, mandatory, optional)) {
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   243
            throw new RuntimeException("Check schema failed.");
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   244
        }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   245
    }
1d8f882f2b2f 8195976: Add JNDI test com/sun/jndi/dns/AttributeTests/GetAny.java
xyin
parents:
diff changeset
   246
}