Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/InetAddress/nameservice/simple/DefaultCaching.java
41161 views
1
/*
2
* Copyright (c) 2006, 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 6442088
26
* @summary Change default DNS caching behavior for code not running under
27
* security manager.
28
* @run main/othervm/timeout=200 -Djdk.net.hosts.file=DefaultCachingHosts
29
* -Dsun.net.inetaddr.ttl=20 DefaultCaching
30
*/
31
import java.net.InetAddress;
32
import java.net.UnknownHostException;
33
import java.io.FileWriter;
34
import java.io.PrintWriter;
35
import java.io.BufferedWriter;
36
37
public class DefaultCaching {
38
39
public static void main(String args[]) throws Exception {
40
41
String hostsFileName = System.getProperty("jdk.net.hosts.file");
42
addMappingToHostsFile("theclub", "129.156.220.219", hostsFileName, false);
43
44
test("theclub", "129.156.220.219", true); // lk: 1
45
test("luster", "1.16.20.2", false); // lk: 2
46
47
// name service now needs to know about luster
48
addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
49
50
test("luster", "1.16.20.2", false); // lk: 2
51
sleep(10+1);
52
test("luster", "10.5.18.21", true, 3); // lk: 3
53
sleep(5);
54
55
// new mapping for theclub and rewrite existing foo and luster mappings
56
addMappingToHostsFile("theclub", "129.156.220.1", hostsFileName, false);
57
addMappingToHostsFile("foo", "10.5.18.22", hostsFileName, true);
58
addMappingToHostsFile("luster", "10.5.18.21", hostsFileName, true);
59
60
test("theclub", "129.156.220.219", true, 3);
61
test("luster", "10.5.18.21", true, 3);
62
test("bar", "10.5.18.22", false, 4);
63
test("foo", "10.5.18.22", true, 5);
64
65
// now delay to see if theclub has expired
66
sleep(5);
67
68
test("foo", "10.5.18.22", true, 5);
69
test("theclub", "129.156.220.1", true, 6);
70
71
sleep(11);
72
// now see if luster has expired
73
test("luster", "10.5.18.21", true, 7);
74
test("theclub", "129.156.220.1", true, 7);
75
76
// now delay to see if 3rd has expired
77
sleep(10+6);
78
79
test("theclub", "129.156.220.1", true, 8);
80
test("luster", "10.5.18.21", true, 8);
81
test("foo", "10.5.18.22", true, 9);
82
}
83
84
/* throws RuntimeException if it fails */
85
86
static void test(String host, String address,
87
boolean shouldSucceed, int count) {
88
test(host, address, shouldSucceed);
89
}
90
91
static void sleep(int seconds) {
92
try {
93
sleepms(seconds * 1000);
94
} catch (InterruptedException e) {
95
e.printStackTrace();
96
}
97
}
98
99
static long sleepms(long millis) throws InterruptedException {
100
long start = System.nanoTime();
101
long ms = millis;
102
while (ms > 0) {
103
assert ms < Long.MAX_VALUE/1000_000L;
104
Thread.sleep(ms);
105
long elapsedms = (System.nanoTime() - start)/1000_000L;
106
ms = millis - elapsedms;
107
}
108
return millis - ms;
109
}
110
111
static void test(String host, String address, boolean shouldSucceed) {
112
InetAddress addr = null;
113
try {
114
addr = InetAddress.getByName(host);
115
if (!shouldSucceed) {
116
throw new RuntimeException(host+":"+address+": should fail (got "
117
+ addr + ")");
118
}
119
if (!address.equals(addr.getHostAddress())) {
120
throw new RuntimeException(host+"/"+address+": compare failed (found "
121
+ addr + ")");
122
}
123
System.out.println("test: " + host + "/" + address
124
+ " succeeded - got " + addr);
125
} catch (UnknownHostException e) {
126
if (shouldSucceed) {
127
throw new RuntimeException(host+":"+address+": should succeed");
128
} else {
129
System.out.println("test: " + host + "/" + address
130
+ " succeeded - got expected " + e);
131
}
132
}
133
}
134
135
136
private static void addMappingToHostsFile(String host,
137
String addr,
138
String hostsFileName,
139
boolean append)
140
throws Exception {
141
String mapping = addr + " " + host;
142
try (PrintWriter hfPWriter = new PrintWriter(new BufferedWriter(
143
new FileWriter(hostsFileName, append)))) {
144
hfPWriter.println(mapping);
145
}
146
}
147
}
148
149