Path: blob/master/test/jdk/com/sun/jndi/dns/IPv6NameserverPlatformParsingTest.java
41153 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.lang.reflect.Field;24import java.util.Hashtable;2526import javax.naming.Context;27import javax.naming.NamingException;28import javax.naming.spi.NamingManager;2930import com.sun.jndi.dns.DnsContext;3132/**33* @bug 699158034* @summary IPv6 Nameservers in resolv.conf throws NumberFormatException35*36* @author Severin Gehwolf37*/3839public class IPv6NameserverPlatformParsingTest {4041private static boolean foundIPv6 = false;4243public static void main(String[] args) {44Hashtable<String, String> env = new Hashtable<>();45env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.dns.DnsContextFactory.class.getName());4647String[] servers;48try {49Context ctx = NamingManager.getInitialContext(env);50if (!com.sun.jndi.dns.DnsContextFactory.platformServersAvailable()) {51throw new RuntimeException("FAIL: no platform servers available, test does not make sense");52}53DnsContext context = (DnsContext)ctx;54servers = getServersFromContext(context);55} catch (NamingException e) {56throw new RuntimeException(e);57}58for (String server: servers) {59System.out.println("DEBUG: 'nameserver = " + server + "'");60if (server.indexOf(':') >= 0 && server.indexOf('.') < 0) {61System.out.println("DEBUG: ==> Found IPv6 address in servers list: " + server);62foundIPv6 = true;63}64}65try {66new com.sun.jndi.dns.DnsClient(servers, 100, 1);67} catch (NumberFormatException e) {68throw new RuntimeException("FAIL: Tried to parse non-[]-encapsulated IPv6 address.", e);69} catch (Exception e) {70throw new RuntimeException("ERROR: Something unexpected happened.");71}72if (!foundIPv6) {73// This is a manual test, since it requires changing /etc/resolv.conf on Linux/Unix74// platforms. See comment as to how to run this test.75throw new RuntimeException("ERROR: No IPv6 address returned from platform.");76}77System.out.println("PASS: Found IPv6 address and DnsClient parsed it correctly.");78}7980private static String[] getServersFromContext(DnsContext context) {81try {82Field serversField = DnsContext.class.getDeclaredField("servers");83serversField.setAccessible(true);84return (String[])serversField.get(context);85} catch (Exception e) {86throw new RuntimeException(e);87}88}8990}919293