Path: blob/master/test/jdk/com/sun/jndi/dns/ConfigTests/PortUnreachable.java
41155 views
/*1* Copyright (c) 2002, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import javax.naming.CommunicationException;24import javax.naming.Context;25import javax.naming.directory.InitialDirContext;2627/*28* @test29* @bug 820015130* @summary Tests that when a DNS server is unreachable and an ICMP Destination31* Unreachable packet is received, we fail quickly and don't wait for32* the full timeout interval. This could be caused, for example, by a33* dead DNS server or a flakey router.34* On AIX, no ICMP Destination Unreachable is received, so skip test.35* @requires os.family != "aix"36* @library ../lib/37* @modules java.base/sun.security.util38* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl=false PortUnreachable39*/4041public class PortUnreachable extends DNSTestBase {4243// Port 25 is the SMTP port, used here to simulate a dead DNS server.44private static final int PORT = 25;4546// Threshold in ms for elapsed time of request failed. Normally, it should47// be very quick, but consider to different platform and test machine48// performance, here we define 3000 ms as threshold which acceptable for49// this test.50private static final int THRESHOLD = 3000;5152private long startTime;5354public PortUnreachable() {55setLocalServer(false);56}5758public static void main(String[] args) throws Exception {59new PortUnreachable().run(args);60}6162/*63* Tests that when a DNS server is unreachable and an ICMP Destination64* Unreachable packet is received, we fail quickly and don't wait for65* the full timeout interval.66*/67@Override68public void runTest() throws Exception {69String deadServerUrl = "dns://localhost:" + PORT;70env().put(Context.PROVIDER_URL, deadServerUrl);71setContext(new InitialDirContext(env()));7273// Any request should fail quickly.74startTime = System.currentTimeMillis();75context().getAttributes("");7677// You're running a DNS server on your SMTP port?78throw new RuntimeException(79"Failed: getAttributes succeeded unexpectedly");80}8182@Override83public boolean handleException(Exception e) {84if (e instanceof CommunicationException) {85long elapsedTime = System.currentTimeMillis() - startTime;8687Throwable cause = ((CommunicationException) e).getRootCause();88if (!(cause instanceof java.net.PortUnreachableException)) {89DNSTestUtils.debug("Bug 7164518 can cause this failure on mac");90return false;91}9293DNSTestUtils.debug("Elapsed (ms): " + elapsedTime);9495// Check that elapsed time is less than defined threshold.96if (elapsedTime < THRESHOLD) {97return true;98}99100throw new RuntimeException("Failed: call took " + elapsedTime101+ " ms, expected less than " + THRESHOLD + " ms");102}103104return super.handleException(e);105}106}107108109