Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/InetAddress/nameservice/simple/CacheTest.java
41161 views
1
/*
2
* Copyright (c) 2002, 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 4292867
26
* @summary Check that InetAddress doesn't continue to throw UHE
27
* after the name service has recovered and the negative ttl
28
* on the initial lookup has expired.
29
* @run main/othervm/timeout=200 -Djdk.net.hosts.file=CacheTestHosts
30
* CacheTest
31
*/
32
import java.net.InetAddress;
33
import java.net.UnknownHostException;
34
import java.security.Security;
35
import java.io.PrintWriter;
36
import java.io.FileWriter;
37
import java.io.BufferedWriter;
38
39
public class CacheTest {
40
41
42
public static void main(String args[]) throws Exception {
43
44
/*
45
* First check the ttl on negative lookups is in the <15 second
46
* range. If the ttl is <=0 it means we cache forever or always
47
* consult the name service. For ttl > 15 the test would take
48
* too long so we skip it (need to coordinate jtreg timeout
49
* with negative ttl)
50
*/
51
String ttlProp = "networkaddress.cache.negative.ttl";
52
int ttl = 0;
53
String policy = Security.getProperty(ttlProp);
54
if (policy != null) {
55
ttl = Integer.parseInt(policy);
56
}
57
if (ttl <= 0 || ttl > 15) {
58
System.err.println("Security property " + ttlProp + " needs to " +
59
" in 1-15 second range to execute this test");
60
return;
61
62
}
63
String hostsFileName = System.getProperty("jdk.net.hosts.file");
64
65
/*
66
* The following outlines how the test works :-
67
*
68
* 1. Do a lookup via InetAddress.getByName that it guaranteed
69
* to succeed. This forces at least one entry into the cache
70
* that will not expire.
71
*
72
* 2. Do a lookup via InetAddress.getByName that is guarnateed
73
* to fail. This results in a negative lookup cached for
74
* for a short period to time.
75
*
76
* 3. Wait for the cache entry to expire.
77
*
78
* 4. Do a lookup (which should consult the name service) and
79
* the lookup should succeed.
80
*/
81
82
// name service needs to resolve this.
83
addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);
84
85
// this lookup will succeed
86
InetAddress.getByName("theclub");
87
88
// lookup "luster" - this should throw UHE as name service
89
// doesn't know anything about this host.
90
91
try {
92
InetAddress.getByName("luster");
93
throw new RuntimeException("Test internal error " +
94
" - luster is being resolved by name service");
95
} catch (UnknownHostException x) {
96
}
97
98
// name service now needs to know about luster
99
addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
100
101
// wait for the cache entry to expire and lookup should
102
// succeed.
103
Thread.currentThread().sleep(ttl*1000 + 1000);
104
InetAddress.getByName("luster");
105
}
106
107
private static void addMappingToHostsFile ( String host,
108
String addr,
109
String hostsFileName,
110
boolean append)
111
throws Exception {
112
String mapping = addr + " " + host;
113
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
114
new FileWriter(hostsFileName, append)))) {
115
hfPWriter.println(mapping);
116
}
117
}
118
119
}
120
121