Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/com/sun/jndi/dns/ConfigTests/PortUnreachable.java
41155 views
1
/*
2
* Copyright (c) 2002, 2020, 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
import javax.naming.CommunicationException;
25
import javax.naming.Context;
26
import javax.naming.directory.InitialDirContext;
27
28
/*
29
* @test
30
* @bug 8200151
31
* @summary Tests that when a DNS server is unreachable and an ICMP Destination
32
* Unreachable packet is received, we fail quickly and don't wait for
33
* the full timeout interval. This could be caused, for example, by a
34
* dead DNS server or a flakey router.
35
* On AIX, no ICMP Destination Unreachable is received, so skip test.
36
* @requires os.family != "aix"
37
* @library ../lib/
38
* @modules java.base/sun.security.util
39
* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl=false PortUnreachable
40
*/
41
42
public class PortUnreachable extends DNSTestBase {
43
44
// Port 25 is the SMTP port, used here to simulate a dead DNS server.
45
private static final int PORT = 25;
46
47
// Threshold in ms for elapsed time of request failed. Normally, it should
48
// be very quick, but consider to different platform and test machine
49
// performance, here we define 3000 ms as threshold which acceptable for
50
// this test.
51
private static final int THRESHOLD = 3000;
52
53
private long startTime;
54
55
public PortUnreachable() {
56
setLocalServer(false);
57
}
58
59
public static void main(String[] args) throws Exception {
60
new PortUnreachable().run(args);
61
}
62
63
/*
64
* Tests that when a DNS server is unreachable and an ICMP Destination
65
* Unreachable packet is received, we fail quickly and don't wait for
66
* the full timeout interval.
67
*/
68
@Override
69
public void runTest() throws Exception {
70
String deadServerUrl = "dns://localhost:" + PORT;
71
env().put(Context.PROVIDER_URL, deadServerUrl);
72
setContext(new InitialDirContext(env()));
73
74
// Any request should fail quickly.
75
startTime = System.currentTimeMillis();
76
context().getAttributes("");
77
78
// You're running a DNS server on your SMTP port?
79
throw new RuntimeException(
80
"Failed: getAttributes succeeded unexpectedly");
81
}
82
83
@Override
84
public boolean handleException(Exception e) {
85
if (e instanceof CommunicationException) {
86
long elapsedTime = System.currentTimeMillis() - startTime;
87
88
Throwable cause = ((CommunicationException) e).getRootCause();
89
if (!(cause instanceof java.net.PortUnreachableException)) {
90
DNSTestUtils.debug("Bug 7164518 can cause this failure on mac");
91
return false;
92
}
93
94
DNSTestUtils.debug("Elapsed (ms): " + elapsedTime);
95
96
// Check that elapsed time is less than defined threshold.
97
if (elapsedTime < THRESHOLD) {
98
return true;
99
}
100
101
throw new RuntimeException("Failed: call took " + elapsedTime
102
+ " ms, expected less than " + THRESHOLD + " ms");
103
}
104
105
return super.handleException(e);
106
}
107
}
108
109