|
1 /* |
|
2 * Copyright (c) 2002, 2018, 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 * @library ../lib/ |
|
36 * @modules java.base/sun.security.util |
|
37 * @run main PortUnreachable |
|
38 */ |
|
39 |
|
40 public class PortUnreachable extends DNSTestBase { |
|
41 |
|
42 // Port 25 is the SMTP port, used here to simulate a dead DNS server. |
|
43 private static final int PORT = 25; |
|
44 |
|
45 // Threshold in ms for elapsed time of request failed. Normally, it should |
|
46 // be very quick, but consider to different platform and test machine |
|
47 // performance, here we define 1000 ms as threshold which acceptable for |
|
48 // this test. |
|
49 private static final int THRESHOLD = 1000; |
|
50 |
|
51 private long startTime; |
|
52 |
|
53 public PortUnreachable() { |
|
54 setLocalServer(false); |
|
55 } |
|
56 |
|
57 public static void main(String[] args) throws Exception { |
|
58 new PortUnreachable().run(args); |
|
59 } |
|
60 |
|
61 /* |
|
62 * Tests that when a DNS server is unreachable and an ICMP Destination |
|
63 * Unreachable packet is received, we fail quickly and don't wait for |
|
64 * the full timeout interval. |
|
65 */ |
|
66 @Override |
|
67 public void runTest() throws Exception { |
|
68 String deadServerUrl = "dns://localhost:" + PORT; |
|
69 env().put(Context.PROVIDER_URL, deadServerUrl); |
|
70 setContext(new InitialDirContext(env())); |
|
71 |
|
72 // Any request should fail quickly. |
|
73 startTime = System.currentTimeMillis(); |
|
74 context().getAttributes(""); |
|
75 |
|
76 // You're running a DNS server on your SMTP port? |
|
77 throw new RuntimeException( |
|
78 "Failed: getAttributes succeeded unexpectedly"); |
|
79 } |
|
80 |
|
81 @Override |
|
82 public boolean handleException(Exception e) { |
|
83 if (e instanceof CommunicationException) { |
|
84 long elapsedTime = System.currentTimeMillis() - startTime; |
|
85 |
|
86 Throwable cause = ((CommunicationException) e).getRootCause(); |
|
87 if (!(cause instanceof java.net.PortUnreachableException)) { |
|
88 DNSTestUtils.debug("Bug 7164518 can cause this failure on mac"); |
|
89 return false; |
|
90 } |
|
91 |
|
92 DNSTestUtils.debug("Elapsed (ms): " + elapsedTime); |
|
93 |
|
94 // Check that elapsed time is less than defined threshold. |
|
95 if (elapsedTime < THRESHOLD) { |
|
96 return true; |
|
97 } |
|
98 |
|
99 throw new RuntimeException("Failed: call took " + elapsedTime |
|
100 + " ms, expected less than " + THRESHOLD + " ms"); |
|
101 } |
|
102 |
|
103 return super.handleException(e); |
|
104 } |
|
105 } |