Path: blob/master/test/jdk/java/net/InetAddress/InternalNameServiceTest.java
41149 views
/*1* Copyright (c) 2016, 2021, 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*/2223/* @test24* @bug 813457725* @summary Test the internal NameService implementation which is enabled via26* the system property jdk.net.hosts.file. This property specifies27* a file name that contains address host mappings, similar to those in28* /etc/hosts file.29* @run main/othervm -Djdk.net.hosts.file=TestHosts -Dsun.net.inetaddr.ttl=030* InternalNameServiceTest31*/3233import java.io.BufferedWriter;34import java.io.FileWriter;35import java.io.PrintWriter;36import java.net.InetAddress;37import java.net.UnknownHostException;38import java.util.Arrays;3940public class InternalNameServiceTest {4142static final String HOSTS_FILE_NAME = System.getProperty("jdk.net.hosts.file");4344public static void main(String args[]) throws Exception {45testHostToIPAddressMappings(HOSTS_FILE_NAME);46testIpAddressToHostNameMappings(HOSTS_FILE_NAME);47}4849private static void testHostToIPAddressMappings(String hostsFileName)50throws Exception, UnknownHostException {51System.out.println(" TEST HOST TO IP ADDRESS MAPPINGS ");52InetAddress testAddress;53byte[] retrievedIpAddr;54byte[] expectedIpAddr1 = { 1, 2, 3, 4 };55byte[] expectedIpAddr2 = { 5, 6, 7, 8 };56byte[] expectedIpAddrIpv6_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};5758// hosts file with59// # test hosts file for internal NameService60// 1.2.3.4 host.sample-domain61// 5.6.7.8 host1.sample-domain62// 1.2.3.4 host2.sample-domain # this is a comment63// host3.sample-domain # no ip address64// host4.sample-domain # space as ip address65// host5.sample-domain # double space as ip address6667// add comment to hosts file68addMappingToHostsFile("test hosts file for internal NameService ", "#", hostsFileName,69false);70addMappingToHostsFile("host.sample-domain", "1.2.3.4", hostsFileName,71true);7273testAddress = InetAddress.getByName("host.sample-domain");74retrievedIpAddr = testAddress.getAddress();75if (!Arrays.equals(retrievedIpAddr, expectedIpAddr1)) {76throw new RuntimeException(77"retrievedIpAddr not equal to expectedipAddr");78}7980addMappingToHostsFile("host1.sample-domain", "5.6.7.8", hostsFileName,81true);82addMappingToHostsFile("host2.sample-domain", "1.2.3.4", hostsFileName,83true);8485testAddress = InetAddress.getByName("host1.sample-domain");86retrievedIpAddr = testAddress.getAddress();87if (!Arrays.equals(retrievedIpAddr, expectedIpAddr2)) {88throw new RuntimeException(89"retrievedIpAddr not equal to expectedIpAddr");90}9192testAddress = InetAddress.getByName("host2.sample-domain");93retrievedIpAddr = testAddress.getAddress();94if (!Arrays.equals(retrievedIpAddr, expectedIpAddr1)) {95throw new RuntimeException(96"retrievedIpAddr not equal to expectedIpAddr");97}9899try {100addMappingToHostsFile("host3.sample-domain", "", hostsFileName,101true);102testAddress = InetAddress.getByName("host3.sample-domain");103throw new RuntimeException(104"Expected UnknownHostException not thrown");105} catch (UnknownHostException uhEx) {106System.out.println("UnknownHostException as expected for host host3.sample-domain");107}108109try {110addMappingToHostsFile("host4.sample-domain", " ", hostsFileName,111true);112testAddress = InetAddress.getByName("host4.sample-domain");113throw new RuntimeException(114"Expected UnknownHostException not thrown");115} catch (UnknownHostException uhEx) {116System.out.println("UnknownHostException as expected for host host4.sample-domain");117}118119try {120addMappingToHostsFile("host5.sample-domain", " ", hostsFileName,121true);122testAddress = InetAddress.getByName("host4.sample-domain");123throw new RuntimeException(124"Expected UnknownHostException not thrown");125} catch (UnknownHostException uhEx) {126System.out.println("UnknownHostException as expected for host host5.sample-domain");127}128129// IPV6 tests130131// IPV6 tests132addMappingToHostsFile("host-ipv6.sample-domain", "::1", hostsFileName,133true);134testAddress = InetAddress.getByName("host-ipv6.sample-domain");135retrievedIpAddr = testAddress.getAddress();136if (!Arrays.equals(retrievedIpAddr, expectedIpAddrIpv6_1)) {137System.out.println("retrieved ipv6 addr == " + Arrays.toString(retrievedIpAddr));138System.out.println("expected ipv6 addr == " + Arrays.toString(expectedIpAddrIpv6_1));139throw new RuntimeException(140"retrieved IPV6 Addr not equal to expected IPV6 Addr");141}142}143144private static void testIpAddressToHostNameMappings(String hostsFileName)145throws Exception {146System.out.println(" TEST IP ADDRESS TO HOST MAPPINGS ");147InetAddress testAddress;148String retrievedHost;149String expectedHost = "testHost.testDomain";150151byte[] testHostIpAddr = { 10, 2, 3, 4 };152byte[] testHostIpAddr2 = { 10, 5, 6, 7 };153byte[] testHostIpAddr3 = { 10, 8, 9, 10 };154byte[] testHostIpAddr4 = { 10, 8, 9, 11 };155156// add comment to hosts file157addMappingToHostsFile("test hosts file for internal NameService ", "#", hostsFileName,158false);159addMappingToHostsFile("testHost.testDomain", "10.2.3.4", hostsFileName,160true);161162testAddress = InetAddress.getByAddress(testHostIpAddr);163System.out.println("******* testAddress == " + testAddress);164retrievedHost = testAddress.getHostName();165if (!expectedHost.equals(retrievedHost)) {166throw new RuntimeException(167"retrieved host name not equal to expected host name");168}169170addMappingToHostsFile("testHost.testDomain", "10.5.6.7", hostsFileName,171true);172173testAddress = InetAddress.getByAddress(testHostIpAddr2);174System.out.println("******* testAddress == " + testAddress);175retrievedHost = testAddress.getHostName();176System.out.println("******* retrievedHost == " + retrievedHost);177if (!expectedHost.equals(retrievedHost)) {178throw new RuntimeException("retrieved host name " + retrievedHost179+ " not equal to expected host name" + expectedHost);180}181182testAddress = InetAddress.getByAddress(testHostIpAddr4);183System.out.println("******* testAddress == " + testAddress);184if ("10.8.9.11".equalsIgnoreCase(testAddress.getCanonicalHostName())) {185System.out.println("addr = " + addrToString(testHostIpAddr4)186+ " resolve to a host address as expected");187} else {188System.out.println("addr = " + addrToString(testHostIpAddr4)189+ " does not resolve as expected, testAddress == " + testAddress.getCanonicalHostName());190throw new RuntimeException("problem with resolving "191+ addrToString(testHostIpAddr4));192}193194try {195addMappingToHostsFile("", "10.8.9.10", hostsFileName, true);196testAddress = InetAddress.getByAddress(testHostIpAddr3);197System.out.println("******* testAddress == " + testAddress);198retrievedHost = testAddress.getCanonicalHostName();199} catch (Throwable t) {200throw new RuntimeException("problem with resolving "201+ addrToString(testHostIpAddr3));202}203204}205206private static String addrToString(byte addr[]) {207return Byte.toString(addr[0]) + "." + Byte.toString(addr[1]) + "."208+ Byte.toString(addr[2]) + "." + Byte.toString(addr[3]);209}210211private static void addMappingToHostsFile( String host,212String addr,213String hostsFileName,214boolean append)215throws Exception {216String mapping = addr + " " + host;217try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(218new FileWriter(hostsFileName, append)))) {219hfPWriter.println(mapping);220}221}222223}224225226