Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/com/sun/jndi/dns/IPv6NameserverPlatformParsingTest.java
38855 views
/*1* Copyright (c) 2015, Red Hat, Inc.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* @test34* @bug 699158035* @summary IPv6 Nameservers in resolv.conf throws NumberFormatException36* @run main/manual IPv6NameserverPlatformParsingTest37*38* In order to run this test be sure to place, for example, the following39* snippet into your platform's {@code /etc/resolv.conf}:40* <pre>41* nameserver 127.0.0.142* nameserver 2001:4860:4860::888843* nameserver [::1]:535344* nameserver 127.0.0.1:535345* </pre>46*47* Then, run this test as manual jtreg test.48*49* @author Severin Gehwolf50*51*/52public class IPv6NameserverPlatformParsingTest {5354private static boolean foundIPv6 = false;5556public static void main(String[] args) {57Hashtable<String, String> env = new Hashtable<>();58env.put(Context.INITIAL_CONTEXT_FACTORY, com.sun.jndi.dns.DnsContextFactory.class.getName());5960String[] servers;61try {62Context ctx = NamingManager.getInitialContext(env);63if (!com.sun.jndi.dns.DnsContextFactory.platformServersAvailable()) {64throw new RuntimeException("FAIL: no platform servers available, test does not make sense");65}66DnsContext context = (DnsContext)ctx;67servers = getServersFromContext(context);68} catch (NamingException e) {69throw new RuntimeException(e);70}71for (String server: servers) {72System.out.println("DEBUG: 'nameserver = " + server + "'");73if (server.indexOf(':') >= 0 && server.indexOf('.') < 0) {74System.out.println("DEBUG: ==> Found IPv6 address in servers list: " + server);75foundIPv6 = true;76}77}78try {79new com.sun.jndi.dns.DnsClient(servers, 100, 1);80} catch (NumberFormatException e) {81throw new RuntimeException("FAIL: Tried to parse non-[]-encapsulated IPv6 address.", e);82} catch (Exception e) {83throw new RuntimeException("ERROR: Something unexpected happened.");84}85if (!foundIPv6) {86// This is a manual test, since it requires changing /etc/resolv.conf on Linux/Unix87// platforms. See comment as to how to run this test.88throw new RuntimeException("ERROR: No IPv6 address returned from platform.");89}90System.out.println("PASS: Found IPv6 address and DnsClient parsed it correctly.");91}9293private static String[] getServersFromContext(DnsContext context) {94try {95Field serversField = DnsContext.class.getDeclaredField("servers");96serversField.setAccessible(true);97return (String[])serversField.get(context);98} catch (Exception e) {99throw new RuntimeException(e);100}101}102103}104105106