Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/channels/SocketChannel/ReadWriteAfterClose.java
41154 views
1
/*
2
* Copyright (c) 2020, 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
import org.testng.annotations.AfterTest;
25
import org.testng.annotations.BeforeTest;
26
import org.testng.annotations.Test;
27
28
import java.io.IOException;
29
import java.net.InetAddress;
30
import java.net.InetSocketAddress;
31
import java.net.SocketAddress;
32
import java.nio.ByteBuffer;
33
import java.nio.channels.ClosedChannelException;
34
import java.nio.channels.ServerSocketChannel;
35
import java.nio.channels.SocketChannel;
36
37
import static org.testng.Assert.*;
38
39
/*
40
* @test
41
* @bug 8246707
42
* @library /test/lib
43
* @summary Reading or Writing to a closed SocketChannel should throw a ClosedChannelException
44
* @run testng/othervm ReadWriteAfterClose
45
*/
46
47
public class ReadWriteAfterClose {
48
49
private ServerSocketChannel listener;
50
private SocketAddress saddr;
51
private static final int bufCapacity = 4;
52
private static final int bufArraySize = 4;
53
private static final Class<ClosedChannelException> CCE = ClosedChannelException.class;
54
55
@BeforeTest
56
public void setUp() throws IOException {
57
listener = ServerSocketChannel.open();
58
listener.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
59
saddr = listener.getLocalAddress();
60
}
61
62
@Test
63
public void testWriteAfterClose1() throws IOException {
64
SocketChannel sc = SocketChannel.open(saddr);
65
sc.close();
66
ByteBuffer bufWrite = ByteBuffer.allocate(bufCapacity);
67
Throwable ex = expectThrows(CCE, () -> sc.write(bufWrite));
68
assertEquals(ex.getClass(), CCE);
69
}
70
71
@Test
72
public void testWriteAfterClose2() throws IOException {
73
SocketChannel sc = SocketChannel.open(saddr);
74
sc.close();
75
ByteBuffer[] bufArrayWrite = allocateBufArray();
76
Throwable ex = expectThrows(CCE, () -> sc.write(bufArrayWrite));
77
assertEquals(ex.getClass(), CCE);
78
}
79
80
@Test
81
public void testWriteAfterClose3() throws IOException {
82
SocketChannel sc = SocketChannel.open(saddr);
83
sc.close();
84
ByteBuffer[] bufArrayWrite = allocateBufArray();
85
Throwable ex = expectThrows(CCE, () -> sc.write(bufArrayWrite, 0, bufArraySize));
86
assertEquals(ex.getClass(), CCE);
87
}
88
89
@Test
90
public void testReadAfterClose1() throws IOException {
91
SocketChannel sc = SocketChannel.open(saddr);
92
sc.close();
93
ByteBuffer dst = ByteBuffer.allocate(bufCapacity);
94
Throwable ex = expectThrows(CCE, () -> sc.read(dst));
95
assertEquals(ex.getClass(), CCE);
96
}
97
98
@Test
99
public void testReadAfterClose2() throws IOException {
100
SocketChannel sc = SocketChannel.open(saddr);
101
sc.close();
102
ByteBuffer[] dstArray = allocateBufArray();
103
Throwable ex = expectThrows(CCE, () -> sc.read(dstArray));
104
assertEquals(ex.getClass(), CCE);
105
}
106
107
@Test
108
public void testReadAfterClose3() throws IOException {
109
SocketChannel sc = SocketChannel.open(saddr);
110
sc.close();
111
ByteBuffer[] dstArray = allocateBufArray();
112
Throwable ex = expectThrows(CCE, () -> sc.read(dstArray, 0, bufArraySize));
113
assertEquals(ex.getClass(), CCE);
114
}
115
116
public ByteBuffer[] allocateBufArray() {
117
ByteBuffer[] bufArr = new ByteBuffer[bufArraySize];
118
for (int i = 0; i < bufArraySize; i++)
119
bufArr[i] = ByteBuffer.allocate(bufCapacity);
120
return bufArr;
121
}
122
123
@AfterTest
124
public void tearDown() throws IOException {
125
listener.close();
126
}
127
}
128