Path: blob/master/test/jdk/java/net/InetAddress/InternalNameServiceWithHostsFileTest.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. TestHosts-III file exist, with a set of ipv4 and ipv629* mappings30* @run main/othervm -Djdk.net.hosts.file=${test.src}/TestHosts-III -Dsun.net.inetaddr.ttl=031* InternalNameServiceWithHostsFileTest32*/3334import java.net.InetAddress;35import java.net.UnknownHostException;36import java.util.Arrays;3738public class InternalNameServiceWithHostsFileTest {39public static void main(String args[]) throws Exception {40// fe80::141byte[] expectedIpv6Address = { (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0,420, 0, 0, 0, 0, 0, 0, 0, 1 };43// fe00::044byte[] expectedIpv6LocalAddress = { (byte) 0xfe, (byte) 0x00, 0, 0, 0,450, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };46// 10.2.3.447byte[] expectedIpv4Address = { 10, 2, 3, 4 };48//49byte[] expectedIpv6LocalhostAddress = { 0, 0, 0, 0, 0, 0, 0,500, 0, 0, 0, 0, 0, 0, 0, 1 };5152// 10.2.3.4 testHost.testDomain53testHostsMapping(expectedIpv4Address, "testHost.testDomain");54// ::1 ip6-localhost ip6-loopback55testHostsMapping(expectedIpv6LocalhostAddress, "ip6-localhost");56// fe00::0 ip6-localnet57testHostsMapping(expectedIpv6LocalAddress, "ip6-localnet");58// fe80::1 link-local-host59testHostsMapping(expectedIpv6Address, "link-local-host");6061testReverseLookup("10.2.3.4", "testHost.testDomain");6263testReverseLookup("::1", "ip6-localhost");64testReverseLookup("0:0:0:0:0:0:0:1", "ip6-localhost");65testReverseLookup("0000:0000:0000:0000:0000:0000:0000:0001", "ip6-localhost");6667testReverseLookup("fe00::0", "ip6-localnet");68testReverseLookup("fe00:0:0:0:0:0:0:0", "ip6-localnet");69testReverseLookup("fe00:0000:0000:0000:0000:0000:0000:0000", "ip6-localnet");7071testReverseLookup("fe80::1", "link-local-host");72testReverseLookup("fe80:000:0:00:0:000:00:1", "link-local-host");73testReverseLookup("fe80:0000:0000:0000:0000:0000:0000:0001", "link-local-host");74}7576private static void testHostsMapping(byte[] expectedIpAddress, String hostName)77throws UnknownHostException {78InetAddress testAddress;79byte[] rawIpAddress;80testAddress = InetAddress.getByName(hostName);81System.out82.println("############################ InetAddress == "83+ testAddress);8485rawIpAddress = testAddress.getAddress();86if (!Arrays.equals(rawIpAddress, expectedIpAddress)) {87System.out.println("retrieved address == "88+ Arrays.toString(rawIpAddress)89+ " not equal to expected address == "90+ Arrays.toString(expectedIpAddress));91throw new RuntimeException(92"retrieved address not equal to expected address");93}94System.out.println("retrieved address == "95+ Arrays.toString(rawIpAddress)96+ " equal to expected address == "97+ Arrays.toString(expectedIpAddress));98}99100private static void testReverseLookup(String numericHost, String expectedName)101throws UnknownHostException {102String lookupResult = InetAddress.getByName(numericHost).getHostName();103if (!expectedName.equals(lookupResult)) {104throw new RuntimeException(105String.format(106"reverse lookup of \"%s\" is \"%s\", should be \"%s\"\n",107numericHost, lookupResult, expectedName));108}109}110}111112113