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 /*
  25  * @test
  26  * @bug 8200151
  27  * @summary Tests that when a DNS server is unreachable and an ICMP Destination
  28  *          Unreachable packet is received, we fail quickly and don't wait for
  29  *          the full timeout interval.  This could be caused, for example, by a
  30  *          dead DNS server or a flakey router.
  31  * @modules java.base/sun.security.util
  32  * @library ../lib/
  33  * @run main PortUnreachable
  34  */
  35 
  36 import javax.naming.CommunicationException;
  37 import javax.naming.Context;
  38 import javax.naming.directory.DirContext;
  39 import javax.naming.directory.InitialDirContext;
  40 import java.util.Hashtable;
  41 
  42 public class PortUnreachable {
  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     public static void main(String[] args) throws Exception {
  48         Hashtable<Object, Object> env;
  49 
  50         // initialize test
  51         env = DNSTestUtils.initEnv(false, PortUnreachable.class.getName(), args);
  52 
  53         String deadServerUrl = "dns://localhost:" + PORT;
  54 
  55         env.put(Context.PROVIDER_URL, deadServerUrl);
  56 
  57         DirContext ctx = null;
  58 
  59         try {
  60             ctx = new InitialDirContext(env);
  61 
  62             // Any request should fail quickly.
  63             long startTime = System.currentTimeMillis();
  64             try {
  65                 ctx.getAttributes("");
  66             } catch (CommunicationException e) {
  67 
  68                 long elapsedTime = System.currentTimeMillis() - startTime;
  69 
  70                 Throwable cause = e.getRootCause();
  71                 if (cause == null
  72                         || !(cause instanceof java.net.PortUnreachableException)) {
  73                     DNSTestUtils
  74                             .debug("Bug 7164518 can cause this failure on mac");
  75                     throw e;
  76                 }
  77 
  78                 DNSTestUtils.debug("Elapsed (ms):  " + elapsedTime);
  79 
  80                 // Check that elapsed time is under a second.
  81                 if (elapsedTime < 1000) {
  82                     return;
  83                 }
  84 
  85                 throw new RuntimeException(
  86                         "Failed: call took " + elapsedTime + " ms");
  87             }
  88 
  89             // You're running a DNS server on your SMTP port?
  90             throw new RuntimeException(
  91                     "Failed: getAttributes succeeded unexpectedly");
  92 
  93         } finally {
  94             DNSTestUtils.cleanup(ctx);
  95         }
  96     }
  97 }