1 /* 2 * Copyright (c) 2016, 2021, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* @test 25 * @bug 8134577 26 * @summary Test the internal NameService implementation which is enabled via 27 * the system property jdk.net.hosts.file. This property specifies 28 * a file name that contains address host mappings, similar to those in 29 * /etc/hosts file. TestHosts-III file exist, with a set of ipv4 and ipv6 30 * mappings 31 * @run main/othervm -Djdk.net.hosts.file=${test.src}/TestHosts-III -Dsun.net.inetaddr.ttl=0 32 * InternalNameServiceWithHostsFileTest 33 */ 34 35 import java.net.InetAddress; 36 import java.net.UnknownHostException; 37 import java.util.Arrays; 38 39 public class InternalNameServiceWithHostsFileTest { main(String args[])40 public static void main(String args[]) throws Exception { 41 // fe80::1 42 byte[] expectedIpv6Address = { (byte) 0xfe, (byte) 0x80, 0, 0, 0, 0, 0, 43 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 44 // fe00::0 45 byte[] expectedIpv6LocalAddress = { (byte) 0xfe, (byte) 0x00, 0, 0, 0, 46 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; 47 // 10.2.3.4 48 byte[] expectedIpv4Address = { 10, 2, 3, 4 }; 49 // 50 byte[] expectedIpv6LocalhostAddress = { 0, 0, 0, 0, 0, 0, 0, 51 0, 0, 0, 0, 0, 0, 0, 0, 1 }; 52 53 // 10.2.3.4 testHost.testDomain 54 testHostsMapping(expectedIpv4Address, "testHost.testDomain"); 55 // ::1 ip6-localhost ip6-loopback 56 testHostsMapping(expectedIpv6LocalhostAddress, "ip6-localhost"); 57 // fe00::0 ip6-localnet 58 testHostsMapping(expectedIpv6LocalAddress, "ip6-localnet"); 59 // fe80::1 link-local-host 60 testHostsMapping(expectedIpv6Address, "link-local-host"); 61 62 testReverseLookup("10.2.3.4", "testHost.testDomain"); 63 64 testReverseLookup("::1", "ip6-localhost"); 65 testReverseLookup("0:0:0:0:0:0:0:1", "ip6-localhost"); 66 testReverseLookup("0000:0000:0000:0000:0000:0000:0000:0001", "ip6-localhost"); 67 68 testReverseLookup("fe00::0", "ip6-localnet"); 69 testReverseLookup("fe00:0:0:0:0:0:0:0", "ip6-localnet"); 70 testReverseLookup("fe00:0000:0000:0000:0000:0000:0000:0000", "ip6-localnet"); 71 72 testReverseLookup("fe80::1", "link-local-host"); 73 testReverseLookup("fe80:000:0:00:0:000:00:1", "link-local-host"); 74 testReverseLookup("fe80:0000:0000:0000:0000:0000:0000:0001", "link-local-host"); 75 } 76 testHostsMapping(byte[] expectedIpAddress, String hostName)77 private static void testHostsMapping(byte[] expectedIpAddress, String hostName) 78 throws UnknownHostException { 79 InetAddress testAddress; 80 byte[] rawIpAddress; 81 testAddress = InetAddress.getByName(hostName); 82 System.out 83 .println("############################ InetAddress == " 84 + testAddress); 85 86 rawIpAddress = testAddress.getAddress(); 87 if (!Arrays.equals(rawIpAddress, expectedIpAddress)) { 88 System.out.println("retrieved address == " 89 + Arrays.toString(rawIpAddress) 90 + " not equal to expected address == " 91 + Arrays.toString(expectedIpAddress)); 92 throw new RuntimeException( 93 "retrieved address not equal to expected address"); 94 } 95 System.out.println("retrieved address == " 96 + Arrays.toString(rawIpAddress) 97 + " equal to expected address == " 98 + Arrays.toString(expectedIpAddress)); 99 } 100 testReverseLookup(String numericHost, String expectedName)101 private static void testReverseLookup(String numericHost, String expectedName) 102 throws UnknownHostException { 103 String lookupResult = InetAddress.getByName(numericHost).getHostName(); 104 if (!expectedName.equals(lookupResult)) { 105 throw new RuntimeException( 106 String.format( 107 "reverse lookup of \"%s\" is \"%s\", should be \"%s\"\n", 108 numericHost, lookupResult, expectedName)); 109 } 110 } 111 } 112