Path: blob/master/test/jdk/java/net/Socket/ExceptionText.java
41149 views
/*1* Copyright (c) 2018, 2019, 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*/2223/*24* @test25* @library /test/lib26* @build jdk.test.lib.Utils27* @bug 820423328* @summary Add configurable option for enhanced socket IOException messages29* @run main/othervm30* ExceptionText31* WITHOUT_Enhanced_Text32* @run main/othervm33* -Djdk.includeInExceptions=34* ExceptionText35* WITHOUT_Enhanced_Text36* @run main/othervm37* -Djdk.includeInExceptions=somethingElse38* ExceptionText39* WITHOUT_Enhanced_Text40* @run main/othervm41* -Djdk.includeInExceptions=blah,blah,blah,42* ExceptionText43* WITHOUT_Enhanced_Text44* @run main/othervm45* -Djdk.includeInExceptions=hostInfo46* ExceptionText47* expectEnhancedText48* @run main/othervm49* -Djdk.includeInExceptions=foo,hostinfo,bar50* ExceptionText51* expectEnhancedText52* @run main/othervm53* -Djdk.includeInExceptions=",HOSTINFO,"54* ExceptionText55* expectEnhancedText56*/5758import java.io.IOException;59import java.net.InetSocketAddress;60import java.net.Socket;61import java.nio.channels.AsynchronousSocketChannel;62import java.nio.channels.ClosedChannelException;63import java.nio.channels.SocketChannel;64import java.util.concurrent.ExecutionException;65import jdk.test.lib.Utils;6667public class ExceptionText {6869enum TestTarget {SOCKET, CHANNEL, ASYNC_CHANNEL};7071public static void main(String args[]) throws Exception {72String passOrFail = args[0];73boolean expectEnhancedText;74if (passOrFail.equals("expectEnhancedText")) {75expectEnhancedText = true;76} else {77expectEnhancedText = false;78}79test(expectEnhancedText);80}8182static final InetSocketAddress dest = Utils.refusingEndpoint();83static final String PORT = ":" + Integer.toString(dest.getPort());84static final String HOST = dest.getHostString();8586static void test(boolean withProperty) {87// Socket88IOException e = getException(TestTarget.SOCKET);89checkResult(e, withProperty);90// SocketChannel91e = getException(TestTarget.CHANNEL);92checkResult(e, withProperty);93// AsyncSocketChannel94e = getException(TestTarget.ASYNC_CHANNEL);95checkResult(e, withProperty);96}9798static void checkResult(IOException e, boolean withProperty) {99String msg = e.getMessage();100if (!withProperty) {101if (msg.contains(HOST) || msg.contains(PORT)) {102System.err.println("msg = " + msg);103throw new RuntimeException("Test failed: exception contains address info");104}105} else {106if (!msg.contains(HOST) || !msg.contains(PORT)) {107if (e instanceof ClosedChannelException)108return; // has no detail msg109System.err.println("msg = " + msg);110throw new RuntimeException("Test failed: exception does not contain address info");111}112}113}114115static IOException getException(TestTarget target) {116try {117if (target == TestTarget.SOCKET) {118try (Socket s = new Socket()) {119s.connect(dest);120}121} else if (target == TestTarget.CHANNEL) {122SocketChannel.open(dest);123} else if (target == TestTarget.ASYNC_CHANNEL) {124AsynchronousSocketChannel c = AsynchronousSocketChannel.open();125try {126c.connect(dest).get();127} catch (InterruptedException | ExecutionException ee) {128if (ee.getCause() instanceof IOException)129throw (IOException)ee.getCause();130throw new RuntimeException(ee.getCause());131}132}133return null;134} catch (IOException e) {135e.printStackTrace();136return e;137}138}139}140141142