jdk/test/com/sun/jndi/dns/IPv6NameserverPlatformParsingTest.java
author kshefov
Thu, 06 Aug 2015 19:01:40 +0300
changeset 32036 f7693603b12c
parent 29979 4f003e691877
permissions -rw-r--r--
8080108: [TEST_BUG] ERROR: No IPv6 address returned from platform Reviewed-by: chegar, rriggs Contributed-by: alexander.fomin@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29979
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     1
/*
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     4
 *
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     7
 * published by the Free Software Foundation.
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     8
 *
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    13
 * accompanied this code).
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    14
 *
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    18
 *
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    21
 * questions.
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    22
 */
4f003e691877 8078276: add copyright header to IPv6NameserverPlatformParsingTest
michaelm
parents: 29978
diff changeset
    23
29978
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    24
import java.lang.reflect.Field;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    25
import java.util.Hashtable;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    26
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    27
import javax.naming.Context;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    28
import javax.naming.NamingException;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    29
import javax.naming.spi.NamingManager;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    30
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    31
import com.sun.jndi.dns.DnsContext;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    32
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    33
/**
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    34
 * @bug 6991580
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    35
 * @summary IPv6 Nameservers in resolv.conf throws NumberFormatException
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    36
 *
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    37
 * @author Severin Gehwolf
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    38
 */
32036
f7693603b12c 8080108: [TEST_BUG] ERROR: No IPv6 address returned from platform
kshefov
parents: 29979
diff changeset
    39
29978
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    40
public class IPv6NameserverPlatformParsingTest {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    41
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    42
    private static boolean foundIPv6 = false;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    43
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    44
    public static void main(String[] args) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    45
        Hashtable<String, String> env = new Hashtable<>();
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    46
        env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.dns.DnsContextFactory.class.getName());
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    47
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    48
        String[] servers;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    49
        try {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    50
            Context ctx = NamingManager.getInitialContext(env);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    51
            if (!com.sun.jndi.dns.DnsContextFactory.platformServersAvailable()) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    52
                throw new RuntimeException("FAIL: no platform servers available, test does not make sense");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    53
            }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    54
            DnsContext context = (DnsContext)ctx;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    55
            servers = getServersFromContext(context);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    56
        } catch (NamingException e) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    57
            throw new RuntimeException(e);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    58
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    59
        for (String server: servers) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    60
            System.out.println("DEBUG: 'nameserver = " + server + "'");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    61
            if (server.indexOf(':') >= 0 && server.indexOf('.') < 0) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    62
                System.out.println("DEBUG: ==> Found IPv6 address in servers list: " + server);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    63
                foundIPv6 = true;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    64
            }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    65
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    66
        try {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    67
            new com.sun.jndi.dns.DnsClient(servers, 100, 1);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    68
        } catch (NumberFormatException e) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    69
            throw new RuntimeException("FAIL: Tried to parse non-[]-encapsulated IPv6 address.", e);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    70
        } catch (Exception e) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    71
            throw new RuntimeException("ERROR: Something unexpected happened.");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    72
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    73
        if (!foundIPv6) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    74
            // This is a manual test, since it requires changing /etc/resolv.conf on Linux/Unix
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    75
            // platforms. See comment as to how to run this test.
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    76
            throw new RuntimeException("ERROR: No IPv6 address returned from platform.");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    77
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    78
        System.out.println("PASS: Found IPv6 address and DnsClient parsed it correctly.");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    79
    }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    80
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    81
    private static String[] getServersFromContext(DnsContext context) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    82
        try {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    83
            Field serversField = DnsContext.class.getDeclaredField("servers");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    84
            serversField.setAccessible(true);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    85
            return (String[])serversField.get(context);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    86
        } catch (Exception e) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    87
            throw new RuntimeException(e);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    88
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    89
    }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    90
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    91
}