1 /* 2 * Copyright (c) 2012, 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 7162687 8015595 8194486 27 * @summary enhance KDC server availability detection 28 * @library /test/lib 29 * @compile -XDignore.symbol.file Unreachable.java 30 * @run main jdk.test.lib.FileInstaller TestHosts TestHosts 31 * @run main/othervm -Djdk.net.hosts.file=TestHosts Unreachable 32 */ 33 import java.net.PortUnreachableException; 34 import java.net.SocketTimeoutException; 35 import java.net.DatagramPacket; 36 import java.net.DatagramSocket; 37 import java.net.InetSocketAddress; 38 import java.util.concurrent.Callable; 39 import java.util.concurrent.ExecutorService; 40 import java.util.concurrent.Future; 41 import java.util.concurrent.TimeUnit; 42 import java.util.concurrent.TimeoutException; 43 import java.util.concurrent.Executors; 44 import javax.security.auth.login.LoginException; 45 import sun.security.krb5.Config; 46 47 public class Unreachable { 48 49 // Wait for 20 second until unreachable KDC throws PortUnreachableException. 50 private static final int TIMEOUT = 20; 51 private static final String REALM = "RABBIT.HOLE"; 52 private static final String HOST = "127.0.0.1"; 53 private static final int PORT = 13434; 54 private static final String KRB_CONF = "unreachable.krb5.conf"; 55 main(String[] args)56 public static void main(String[] args) throws Exception { 57 58 // - Only PortUnreachableException will allow to continue execution. 59 // - SocketTimeoutException may occur on Mac because it will not throw 60 // PortUnreachableException for unreachable port in which case the Test 61 // execution will be skipped. 62 // - For Reachable port, the Test execution will get skipped. 63 // - Any other Exception will be treated as Test failure. 64 if (!findPortUnreachableExc()) { 65 System.out.println(String.format("WARNING: Either a reachable " 66 + "connection found to %s:%s or SocketTimeoutException " 67 + "occured which means PortUnreachableException not thrown" 68 + " by the platform.", HOST, PORT)); 69 return; 70 } 71 KDC kdc = KDC.existing(REALM, HOST, PORT); 72 KDC.saveConfig(KRB_CONF, kdc); 73 ExecutorService executor = Executors.newSingleThreadExecutor(); 74 Future<Exception> future = executor.submit(new Callable<Exception>() { 75 @Override 76 public Exception call() { 77 System.setProperty("java.security.krb5.conf", KRB_CONF); 78 try { 79 Config.refresh(); 80 // If PortUnreachableException is not received, the login 81 // will consume about 3*3*30 seconds and the test will 82 // timeout. 83 try { 84 Context.fromUserPass("name", "pass".toCharArray(), true); 85 } catch (LoginException le) { 86 // This is OK 87 } 88 System.out.println("Execution successful."); 89 } catch (Exception e) { 90 return e; 91 } 92 return null; 93 } 94 }); 95 try { 96 Exception ex = null; 97 if ((ex = future.get(TIMEOUT, TimeUnit.SECONDS)) != null) { 98 throw new RuntimeException(ex); 99 } 100 } catch (TimeoutException e) { 101 future.cancel(true); 102 throw new RuntimeException("PortUnreachableException not thrown."); 103 } finally { 104 executor.shutdownNow(); 105 } 106 } 107 108 /** 109 * If the remote destination to which the socket is connected does not 110 * exist, or is otherwise unreachable, and if an ICMP destination unreachable 111 * packet has been received for that address, then a subsequent call to 112 * send or receive may throw a PortUnreachableException. Note, there is no 113 * guarantee that the exception will be thrown. 114 */ findPortUnreachableExc()115 private static boolean findPortUnreachableExc() throws Exception { 116 try { 117 InetSocketAddress iaddr = new InetSocketAddress(HOST, PORT); 118 DatagramSocket dgSocket = new DatagramSocket(); 119 dgSocket.setSoTimeout(5000); 120 dgSocket.connect(iaddr); 121 byte[] data = new byte[]{}; 122 dgSocket.send(new DatagramPacket(data, data.length, iaddr)); 123 dgSocket.receive(new DatagramPacket(data, data.length)); 124 } catch (PortUnreachableException e) { 125 return true; 126 } catch (SocketTimeoutException e) { 127 return false; 128 } 129 return false; 130 } 131 } 132