Path: blob/master/test/jdk/java/nio/channels/SocketChannel/ReadWriteAfterClose.java
41154 views
/*1* Copyright (c) 2020, 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*/2223import org.testng.annotations.AfterTest;24import org.testng.annotations.BeforeTest;25import org.testng.annotations.Test;2627import java.io.IOException;28import java.net.InetAddress;29import java.net.InetSocketAddress;30import java.net.SocketAddress;31import java.nio.ByteBuffer;32import java.nio.channels.ClosedChannelException;33import java.nio.channels.ServerSocketChannel;34import java.nio.channels.SocketChannel;3536import static org.testng.Assert.*;3738/*39* @test40* @bug 824670741* @library /test/lib42* @summary Reading or Writing to a closed SocketChannel should throw a ClosedChannelException43* @run testng/othervm ReadWriteAfterClose44*/4546public class ReadWriteAfterClose {4748private ServerSocketChannel listener;49private SocketAddress saddr;50private static final int bufCapacity = 4;51private static final int bufArraySize = 4;52private static final Class<ClosedChannelException> CCE = ClosedChannelException.class;5354@BeforeTest55public void setUp() throws IOException {56listener = ServerSocketChannel.open();57listener.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));58saddr = listener.getLocalAddress();59}6061@Test62public void testWriteAfterClose1() throws IOException {63SocketChannel sc = SocketChannel.open(saddr);64sc.close();65ByteBuffer bufWrite = ByteBuffer.allocate(bufCapacity);66Throwable ex = expectThrows(CCE, () -> sc.write(bufWrite));67assertEquals(ex.getClass(), CCE);68}6970@Test71public void testWriteAfterClose2() throws IOException {72SocketChannel sc = SocketChannel.open(saddr);73sc.close();74ByteBuffer[] bufArrayWrite = allocateBufArray();75Throwable ex = expectThrows(CCE, () -> sc.write(bufArrayWrite));76assertEquals(ex.getClass(), CCE);77}7879@Test80public void testWriteAfterClose3() throws IOException {81SocketChannel sc = SocketChannel.open(saddr);82sc.close();83ByteBuffer[] bufArrayWrite = allocateBufArray();84Throwable ex = expectThrows(CCE, () -> sc.write(bufArrayWrite, 0, bufArraySize));85assertEquals(ex.getClass(), CCE);86}8788@Test89public void testReadAfterClose1() throws IOException {90SocketChannel sc = SocketChannel.open(saddr);91sc.close();92ByteBuffer dst = ByteBuffer.allocate(bufCapacity);93Throwable ex = expectThrows(CCE, () -> sc.read(dst));94assertEquals(ex.getClass(), CCE);95}9697@Test98public void testReadAfterClose2() throws IOException {99SocketChannel sc = SocketChannel.open(saddr);100sc.close();101ByteBuffer[] dstArray = allocateBufArray();102Throwable ex = expectThrows(CCE, () -> sc.read(dstArray));103assertEquals(ex.getClass(), CCE);104}105106@Test107public void testReadAfterClose3() throws IOException {108SocketChannel sc = SocketChannel.open(saddr);109sc.close();110ByteBuffer[] dstArray = allocateBufArray();111Throwable ex = expectThrows(CCE, () -> sc.read(dstArray, 0, bufArraySize));112assertEquals(ex.getClass(), CCE);113}114115public ByteBuffer[] allocateBufArray() {116ByteBuffer[] bufArr = new ByteBuffer[bufArraySize];117for (int i = 0; i < bufArraySize; i++)118bufArr[i] = ByteBuffer.allocate(bufCapacity);119return bufArr;120}121122@AfterTest123public void tearDown() throws IOException {124listener.close();125}126}127128