Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/ReadAfterReset.java
41152 views
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
/* @test
25
* @requires (os.family == "linux" | os.family == "mac")
26
* @bug 8203937
27
* @summary Test reading bytes from a socket after the connection has been
28
* reset by the peer
29
*/
30
31
import java.io.IOException;
32
import java.io.PrintStream;
33
import java.net.InetAddress;
34
import java.net.InetSocketAddress;
35
import java.net.ServerSocket;
36
import java.net.Socket;
37
38
/**
39
* This test exercises platform specific and unspecified behavior. It exists
40
* only to ensure that the behavior doesn't change between JDK releases.
41
*/
42
43
public class ReadAfterReset {
44
private static final PrintStream out = System.out;
45
46
// number of bytes to write before the connection reset
47
private static final int NUM_BYTES_TO_WRITE = 1000;
48
49
public static void main(String[] args) throws IOException {
50
try (ServerSocket ss = new ServerSocket()) {
51
ss.bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0));
52
53
/**
54
* Connect to the server which will write some bytes and reset the
55
* connection. The client then attempts to read the bytes sent by
56
* the server before it closed the connection.
57
*/
58
out.println("Test connection ...");
59
try (Socket s = new Socket()) {
60
s.connect(ss.getLocalSocketAddress());
61
int nwrote = acceptAndResetConnection(ss);
62
int nread = readUntilIOException(s);
63
if (nread != nwrote) {
64
throw new RuntimeException("Client read " + nread + ", expected " + nwrote);
65
}
66
}
67
68
/**
69
* Connect to the server which will write some bytes and reset the
70
* connection. The client then writes to its end of the connection,
71
* failing as the connection is reset. It then attempts to read the
72
* bytes sent by the server before it closed the connection.
73
*/
74
out.println();
75
out.println("Test connection ...");
76
try (Socket s = new Socket()) {
77
s.connect(ss.getLocalSocketAddress());
78
int nwrote = acceptAndResetConnection(ss);
79
writeUntilIOException(s);
80
int nread = readUntilIOException(s);
81
if (nread != nwrote) {
82
throw new RuntimeException("Client read " + nread + ", expected " + nwrote);
83
}
84
}
85
}
86
}
87
88
/**
89
* Accept a connection, write bytes, and then reset the connection
90
*/
91
static int acceptAndResetConnection(ServerSocket ss) throws IOException {
92
int count = NUM_BYTES_TO_WRITE;
93
try (Socket peer = ss.accept()) {
94
peer.getOutputStream().write(new byte[count]);
95
peer.setSoLinger(true, 0);
96
out.format("Server wrote %d bytes and reset connection%n", count);
97
}
98
return count;
99
}
100
101
/**
102
* Write bytes to a socket until I/O exception is thrown
103
*/
104
static void writeUntilIOException(Socket s) {
105
try {
106
byte[] bytes = new byte[100];
107
while (true) {
108
s.getOutputStream().write(bytes);
109
out.format("Client wrote %d bytes%n", bytes.length);
110
}
111
} catch (IOException ioe) {
112
out.format("Client write failed: %s (expected)%n", ioe);
113
}
114
}
115
116
/**
117
* Read bytes from a socket until I/O exception is thrown.
118
*
119
* @return the number of bytes read before the I/O exception was thrown
120
*/
121
static int readUntilIOException(Socket s) {
122
int nread = 0;
123
try {
124
byte[] bytes = new byte[100];
125
while (true) {
126
int n = s.getInputStream().read(bytes);
127
if (n < 0) {
128
out.println("Client read EOF");
129
break;
130
} else {
131
out.format("Client read %s bytes%n", n);
132
nread += n;
133
}
134
}
135
} catch (IOException ioe) {
136
out.format("Client read failed: %s (expected)%n", ioe);
137
}
138
return nread;
139
}
140
}
141
142