Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/Socket/TestAfterClose.java
41152 views
1
/*
2
* Copyright (c) 2007, 2019, 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
/*
25
* @test
26
* @bug 6505016
27
* @library /test/lib
28
* @summary Socket spec should clarify what getInetAddress/getPort/etc return
29
* after the Socket is closed
30
* @run main TestAfterClose
31
* @run main/othervm -Djava.net.preferIPv4Stack=true TestAfterClose
32
*/
33
34
import java.net.*;
35
import java.io.*;
36
import jdk.test.lib.net.IPSupport;
37
38
public class TestAfterClose
39
{
40
static int failCount;
41
42
public static void main(String[] args) {
43
IPSupport.throwSkippedExceptionIfNonOperational();
44
45
try {
46
InetAddress loopback = InetAddress.getLoopbackAddress();
47
ServerSocket ss = new ServerSocket(0, 0, loopback);
48
Socket socket = new Socket(loopback, ss.getLocalPort());
49
ss.accept();
50
ss.close();
51
test(socket);
52
} catch (IOException ioe) {
53
ioe.printStackTrace();
54
}
55
56
if (failCount > 0)
57
throw new RuntimeException("Failed: failcount = " + failCount);
58
59
}
60
61
static void test(Socket socket) throws IOException {
62
//Before Close
63
int socketPort = socket.getPort();
64
InetAddress socketInetAddress = socket.getInetAddress();
65
SocketAddress socketRemoteSocketAddress = socket.getRemoteSocketAddress();
66
int socketLocalPort = socket.getLocalPort();
67
68
//After Close
69
socket.close();
70
71
if (socketPort != socket.getPort()) {
72
System.out.println("Socket.getPort failed");
73
failCount++;
74
}
75
76
if (!socket.getInetAddress().equals(socketInetAddress)) {
77
System.out.println("Socket.getInetAddress failed");
78
failCount++;
79
}
80
81
if (!socket.getRemoteSocketAddress().equals(socketRemoteSocketAddress)) {
82
System.out.println("Socket.getRemoteSocketAddresss failed");
83
failCount++;
84
}
85
86
if (socketLocalPort != socket.getLocalPort()) {
87
System.out.println("Socket.getLocalPort failed");
88
failCount++;
89
}
90
91
InetAddress anyAddr = null;
92
try {
93
anyAddr = InetAddress.getByAddress("",new byte[] {0,0,0,0});
94
} catch (UnknownHostException uhe) {
95
}
96
97
if (anyAddr != null && !socket.getLocalAddress().equals(anyAddr)) {
98
System.out.println("Socket.getLocalAddress failed");
99
failCount++;
100
}
101
102
InetSocketAddress addr = new InetSocketAddress(socket.getLocalPort());
103
if (!socket.getLocalSocketAddress().equals(addr)) {
104
System.out.println("Socket.getLocalSocketAddress failed");
105
failCount++;
106
}
107
108
if (!socket.isConnected()) {
109
System.out.println("Socket.isConnected failed");
110
failCount++;
111
}
112
113
if (!socket.isBound()) {
114
System.out.println("Socket.isBound failed");
115
failCount++;
116
}
117
}
118
}
119
120