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