1/*
2 * Copyright (c) 2016, 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.
30 * @run main/othervm  InternalNameServiceTest
31 */
32
33import java.io.BufferedWriter;
34import java.io.FileWriter;
35import java.io.PrintWriter;
36import java.net.InetAddress;
37import java.net.UnknownHostException;
38import java.util.Arrays;
39
40public class InternalNameServiceTest {
41
42    public static void main(String args[]) throws Exception {
43
44        String hostsFileName = System.getProperty("test.src", ".") + "/TestHosts";
45        System.setProperty("jdk.net.hosts.file", hostsFileName);
46        System.setProperty("sun.net.inetaddr.ttl", "0");
47
48        testHostToIPAddressMappings(hostsFileName);
49        testIpAddressToHostNameMappings(hostsFileName);
50    }
51
52    private static void testHostToIPAddressMappings(String hostsFileName)
53            throws Exception, UnknownHostException {
54        System.out.println(" TEST HOST TO  IP ADDRESS MAPPINGS ");
55        InetAddress testAddress;
56        byte[] retrievedIpAddr;
57        byte[] expectedIpAddr1 = { 1, 2, 3, 4 };
58        byte[] expectedIpAddr2 = { 5, 6, 7, 8 };
59        byte[] expectedIpAddrIpv6_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1};
60
61        // hosts file with
62        // # test hosts file for internal NameService
63        // 1.2.3.4 host.sample-domain
64        // 5.6.7.8 host1.sample-domain
65        // 1.2.3.4 host2.sample-domain  # this is a comment
66        // host3.sample-domain # no ip address
67        //  host4.sample-domain # space as ip address
68        //   host5.sample-domain # double space as ip address
69
70        // add comment to hosts file
71        addMappingToHostsFile("test hosts file for internal NameService ", "#", hostsFileName,
72                false);
73        addMappingToHostsFile("host.sample-domain", "1.2.3.4", hostsFileName,
74                true);
75
76        testAddress = InetAddress.getByName("host.sample-domain");
77        retrievedIpAddr = testAddress.getAddress();
78        if (!Arrays.equals(retrievedIpAddr, expectedIpAddr1)) {
79            throw new RuntimeException(
80                    "retrievedIpAddr not equal to expectedipAddr");
81        }
82
83        addMappingToHostsFile("host1.sample-domain", "5.6.7.8", hostsFileName,
84                true);
85        addMappingToHostsFile("host2.sample-domain", "1.2.3.4", hostsFileName,
86                true);
87
88        testAddress = InetAddress.getByName("host1.sample-domain");
89        retrievedIpAddr = testAddress.getAddress();
90        if (!Arrays.equals(retrievedIpAddr, expectedIpAddr2)) {
91            throw new RuntimeException(
92                    "retrievedIpAddr not equal to expectedIpAddr");
93        }
94
95        testAddress = InetAddress.getByName("host2.sample-domain");
96        retrievedIpAddr = testAddress.getAddress();
97        if (!Arrays.equals(retrievedIpAddr, expectedIpAddr1)) {
98            throw new RuntimeException(
99                    "retrievedIpAddr not equal to expectedIpAddr");
100        }
101
102        try {
103            addMappingToHostsFile("host3.sample-domain", "", hostsFileName,
104                    true);
105            testAddress = InetAddress.getByName("host3.sample-domain");
106            throw new RuntimeException(
107                    "Expected UnknownHostException not thrown");
108        } catch (UnknownHostException uhEx) {
109            System.out.println("UnknownHostException as expected for host host3.sample-domain");
110        }
111
112        try {
113            addMappingToHostsFile("host4.sample-domain", " ", hostsFileName,
114                    true);
115            testAddress = InetAddress.getByName("host4.sample-domain");
116            throw new RuntimeException(
117                    "Expected UnknownHostException not thrown");
118        } catch (UnknownHostException uhEx) {
119            System.out.println("UnknownHostException as expected for host host4.sample-domain");
120        }
121
122        try {
123            addMappingToHostsFile("host5.sample-domain", "  ", hostsFileName,
124                    true);
125            testAddress = InetAddress.getByName("host4.sample-domain");
126            throw new RuntimeException(
127                    "Expected UnknownHostException not thrown");
128        } catch (UnknownHostException uhEx) {
129            System.out.println("UnknownHostException as expected for host host5.sample-domain");
130        }
131
132        // IPV6 tests
133
134        // IPV6 tests
135        addMappingToHostsFile("host-ipv6.sample-domain", "::1", hostsFileName,
136                true);
137        testAddress = InetAddress.getByName("host-ipv6.sample-domain");
138        retrievedIpAddr = testAddress.getAddress();
139        if (!Arrays.equals(retrievedIpAddr, expectedIpAddrIpv6_1)) {
140            System.out.println("retrieved ipv6 addr == " + Arrays.toString(retrievedIpAddr));
141            System.out.println("expected ipv6 addr == " + Arrays.toString(expectedIpAddrIpv6_1));
142            throw new RuntimeException(
143                    "retrieved IPV6 Addr not equal to expected IPV6 Addr");
144        }
145    }
146
147    private static void testIpAddressToHostNameMappings(String hostsFileName)
148            throws Exception {
149        System.out.println(" TEST IP ADDRESS TO HOST MAPPINGS ");
150        InetAddress testAddress;
151        String retrievedHost;
152        String expectedHost = "testHost.testDomain";
153
154        byte[] testHostIpAddr = { 10, 2, 3, 4 };
155        byte[] testHostIpAddr2 = { 10, 5, 6, 7 };
156        byte[] testHostIpAddr3 = { 10, 8, 9, 10 };
157        byte[] testHostIpAddr4 = { 10, 8, 9, 11 };
158
159        // add comment to hosts file
160        addMappingToHostsFile("test hosts file for internal NameService ", "#", hostsFileName,
161                false);
162        addMappingToHostsFile("testHost.testDomain", "10.2.3.4", hostsFileName,
163                true);
164
165        testAddress = InetAddress.getByAddress(testHostIpAddr);
166        System.out.println("*******   testAddress == " + testAddress);
167        retrievedHost = testAddress.getHostName();
168        if (!expectedHost.equals(retrievedHost)) {
169            throw new RuntimeException(
170                    "retrieved host name not equal to expected host name");
171        }
172
173        addMappingToHostsFile("testHost.testDomain", "10.5.6.7", hostsFileName,
174                true);
175
176        testAddress = InetAddress.getByAddress(testHostIpAddr2);
177        System.out.println("*******   testAddress == " + testAddress);
178        retrievedHost = testAddress.getHostName();
179        System.out.println("*******   retrievedHost == " + retrievedHost);
180        if (!expectedHost.equals(retrievedHost)) {
181            throw new RuntimeException("retrieved host name " + retrievedHost
182                    + " not equal to expected host name" + expectedHost);
183        }
184
185        testAddress = InetAddress.getByAddress(testHostIpAddr4);
186        System.out.println("*******   testAddress == " + testAddress);
187        if ("10.8.9.11".equalsIgnoreCase(testAddress.getCanonicalHostName())) {
188            System.out.println("addr = " + addrToString(testHostIpAddr4)
189                    + "  resolve to a host address as expected");
190        } else {
191            System.out.println("addr = " + addrToString(testHostIpAddr4)
192                    + " does not resolve as expected, testAddress == " + testAddress.getCanonicalHostName());
193            throw new RuntimeException("problem with resolving "
194                    + addrToString(testHostIpAddr4));
195        }
196
197        try {
198            addMappingToHostsFile("", "10.8.9.10", hostsFileName, true);
199            testAddress = InetAddress.getByAddress(testHostIpAddr3);
200            System.out.println("*******   testAddress == " + testAddress);
201            retrievedHost = testAddress.getCanonicalHostName();
202        } catch (Throwable t) {
203            throw new RuntimeException("problem with resolving "
204                    + addrToString(testHostIpAddr3));
205        }
206
207    }
208
209    private static String addrToString(byte addr[]) {
210        return Byte.toString(addr[0]) + "." + Byte.toString(addr[1]) + "."
211                + Byte.toString(addr[2]) + "." + Byte.toString(addr[3]);
212    }
213
214    private static void addMappingToHostsFile( String host,
215                                               String addr,
216                                               String hostsFileName,
217                                               boolean append)
218                                               throws Exception {
219        String mapping = addr + " " + host;
220        try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
221                new FileWriter(hostsFileName, append)))) {
222            hfPWriter.println(mapping);
223        }
224    }
225
226}
227