1 /* 2 * Copyright (c) 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 * @library /test/lib 27 * @build jdk.test.lib.Utils 28 * @bug 8204233 29 * @summary Add configurable option for enhanced socket IOException messages 30 * @run main/othervm ExceptionText 31 * @run main/othervm -Djdk.net.includeInExceptions= ExceptionText 32 * @run main/othervm -Djdk.net.includeInExceptions=hostInfo ExceptionText 33 * @run main/othervm -Djdk.net.includeInExceptions=somethingElse ExceptionText 34 */ 35 36 import java.net.*; 37 import java.io.IOException; 38 import java.nio.channels.ClosedChannelException; 39 import java.nio.channels.*; 40 import java.util.concurrent.ExecutionException; 41 import jdk.test.lib.Utils; 42 43 public class ExceptionText { 44 45 enum TestTarget {SOCKET, CHANNEL, ASYNC_CHANNEL}; 46 47 static boolean propEnabled() { 48 String val = System.getProperty("jdk.net.includeInExceptions"); 49 if ("hostinfo".equalsIgnoreCase(val)) 50 return true; 51 return false; 52 } 53 54 public static void main(String args[]) throws Exception { 55 boolean prop = propEnabled(); 56 test(prop); 57 } 58 59 static final InetSocketAddress dest = Utils.refusingEndpoint(); 60 static final String PORT = ":" + Integer.toString(dest.getPort()); 61 static final String HOST = dest.getHostString(); 62 63 static void test(boolean withProperty) { 64 // Socket 65 IOException e = getException(TestTarget.SOCKET); 66 checkResult(e, withProperty); 67 // SocketChannel 68 e = getException(TestTarget.CHANNEL); 69 checkResult(e, withProperty); 70 // AsyncSocketChannel 71 e = getException(TestTarget.ASYNC_CHANNEL); 72 checkResult(e, withProperty); 73 } 74 75 static void checkResult(IOException e, boolean withProperty) { 76 String msg = e.getMessage(); 77 if (!withProperty) { 78 if (msg.contains(HOST) || msg.contains(PORT)) { 79 System.err.println("msg = " + msg); 80 throw new RuntimeException("Test failed: exception contains address info"); 81 } 82 } else { 83 if (!msg.contains(HOST) || !msg.contains(PORT)) { 84 if (e instanceof ClosedChannelException) 85 return; // has no detail msg 86 System.err.println("msg = " + msg); 87 throw new RuntimeException("Test failed: exception does not contain address info"); 88 } 89 } 90 } 91 92 static IOException getException(TestTarget target) { 93 try { 94 if (target == TestTarget.SOCKET) { 95 Socket s = new Socket(); 96 s.connect(dest); 97 } else if (target == TestTarget.CHANNEL) { 98 SocketChannel c = SocketChannel.open(dest); 99 } else if (target == TestTarget.ASYNC_CHANNEL) { 100 AsynchronousSocketChannel c = AsynchronousSocketChannel.open(); 101 try { 102 c.connect(dest).get(); 103 } catch (InterruptedException | ExecutionException ee) { 104 if (ee.getCause() instanceof IOException) 105 throw (IOException)ee.getCause(); 106 throw new RuntimeException(ee.getCause()); 107 } 108 } 109 return null; 110 } catch (IOException e) { 111 e.printStackTrace(); 112 return e; 113 } 114 } 115 }