Path: blob/master/test/jdk/java/net/Socket/ShutdownInput.java
41149 views
/*1* Copyright (c) 2011, 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* @bug 701486026* @library /test/lib27* @summary Socket.getInputStream().available() not clear for28* case that connection is shutdown for reading29* @run main ShutdownInput30* @run main/othervm -Djava.net.preferIPv4Stack=true ShutdownInput31*/3233import java.io.InputStream;34import java.io.OutputStream;35import java.net.InetAddress;36import java.net.InetSocketAddress;37import java.net.ServerSocket;38import java.net.Socket;39import java.nio.channels.ServerSocketChannel;40import java.nio.channels.SocketChannel;41import jdk.test.lib.net.IPSupport;4243public class ShutdownInput {44static boolean failed = false;4546public static void main(String args[]) throws Exception {47IPSupport.throwSkippedExceptionIfNonOperational();4849InetAddress iaddr = InetAddress.getLoopbackAddress();5051try (ServerSocket ss = new ServerSocket(0, 0, iaddr);52Socket s1 = new Socket(iaddr, ss.getLocalPort());53Socket s2 = ss.accept() ) {5455test(s1, s2, "Testing NET");56}5758// check the NIO socket adapter59InetSocketAddress socketAddress = new InetSocketAddress(iaddr, 0);60try (ServerSocketChannel sc = ServerSocketChannel.open().bind(socketAddress);61SocketChannel s1 = SocketChannel.open(62new InetSocketAddress(iaddr, sc.socket().getLocalPort()));63SocketChannel s2 = sc.accept() ) {6465test(s1.socket(), s2.socket(), "Testing NIO");66}6768if (failed) {69throw new RuntimeException("Failed: check output");70}71}7273public static void test(Socket s1, Socket s2, String mesg) throws Exception {74OutputStream os = s1.getOutputStream();75os.write("This is a message".getBytes("US-ASCII"));7677InputStream in = s2.getInputStream();78s2.shutdownInput();7980if (in.available() != 0) {81failed = true;82System.out.println(mesg + ":" + s2 + " in.available() should be 0, " +83"but returns "+ in.available());84}8586byte[] ba = new byte[2];87if (in.read() != -1 ||88in.read(ba) != -1 ||89in.read(ba, 0, ba.length) != -1) {9091failed = true;92System.out.append(mesg + ":" + s2 + " in.read() should be -1");93}94}95}969798