jdk/test/javax/naming/dns/IPv6NameserverPlatformParsingTest.java
author michaelm
Tue, 21 Apr 2015 14:36:57 +0100
changeset 29978 2a735486f812
permissions -rw-r--r--
6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException Reviewed-by: michaelm, andrew Contributed-by: sgehwolf@redhat.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29978
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     1
import java.lang.reflect.Field;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     2
import java.util.Hashtable;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     3
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     4
import javax.naming.Context;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     5
import javax.naming.NamingException;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     6
import javax.naming.spi.NamingManager;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     7
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     8
import com.sun.jndi.dns.DnsContext;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
     9
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    10
/**
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    11
 * @test
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    12
 * @bug 6991580
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    13
 * @summary IPv6 Nameservers in resolv.conf throws NumberFormatException
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    14
 * @run main/manual IPv6NameserverPlatformParsingTest
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    15
 *
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    16
 * In order to run this test be sure to place, for example, the following
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    17
 * snippet into your platform's {@code /etc/resolv.conf}:
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    18
 * <pre>
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    19
 * nameserver 127.0.0.1
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    20
 * nameserver 2001:4860:4860::8888
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    21
 * nameserver [::1]:5353
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    22
 * nameserver 127.0.0.1:5353
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    23
 * </pre>
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    24
 *
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    25
 * Then, run this test as manual jtreg test.
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
 * @author Severin Gehwolf
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    28
 *
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    29
 */
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    30
public class IPv6NameserverPlatformParsingTest {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    31
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    32
    private static boolean foundIPv6 = false;
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
    public static void main(String[] args) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    35
        Hashtable<String, String> env = new Hashtable<>();
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    36
        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
    37
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    38
        String[] servers;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    39
        try {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    40
            Context ctx = NamingManager.getInitialContext(env);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    41
            if (!com.sun.jndi.dns.DnsContextFactory.platformServersAvailable()) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    42
                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
    43
            }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    44
            DnsContext context = (DnsContext)ctx;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    45
            servers = getServersFromContext(context);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    46
        } catch (NamingException e) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    47
            throw new RuntimeException(e);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    48
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    49
        for (String server: servers) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    50
            System.out.println("DEBUG: 'nameserver = " + server + "'");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    51
            if (server.indexOf(':') >= 0 && server.indexOf('.') < 0) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    52
                System.out.println("DEBUG: ==> Found IPv6 address in servers list: " + server);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    53
                foundIPv6 = true;
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    54
            }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    55
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    56
        try {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    57
            new com.sun.jndi.dns.DnsClient(servers, 100, 1);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    58
        } catch (NumberFormatException e) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    59
            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
    60
        } catch (Exception e) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    61
            throw new RuntimeException("ERROR: Something unexpected happened.");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    62
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    63
        if (!foundIPv6) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    64
            // 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
    65
            // platforms. See comment as to how to run this test.
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    66
            throw new RuntimeException("ERROR: No IPv6 address returned from platform.");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    67
        }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    68
        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
    69
    }
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    70
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    71
    private static String[] getServersFromContext(DnsContext context) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    72
        try {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    73
            Field serversField = DnsContext.class.getDeclaredField("servers");
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    74
            serversField.setAccessible(true);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    75
            return (String[])serversField.get(context);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    76
        } catch (Exception e) {
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    77
            throw new RuntimeException(e);
2a735486f812 6991580: IPv6 Nameservers in resolv.conf throws NumberFormatException
michaelm
parents:
diff changeset
    78
        }
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
}