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