Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/SocketOption/TcpKeepAliveTest.java
41149 views
1
/*
2
* Copyright (c) 2018, 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 8194298
27
* @summary Add support for per Socket configuration of TCP keepalive
28
* @modules jdk.net
29
* @run main TcpKeepAliveTest
30
*/
31
import java.io.IOException;
32
import java.net.DatagramSocket;
33
import java.net.InetAddress;
34
import java.net.InetSocketAddress;
35
import java.net.MulticastSocket;
36
import java.net.ServerSocket;
37
import java.net.Socket;
38
import jdk.net.ExtendedSocketOptions;
39
40
public class TcpKeepAliveTest {
41
42
private static final int DEFAULT_KEEP_ALIVE_PROBES = 7;
43
private static final int DEFAULT_KEEP_ALIVE_TIME = 1973;
44
private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;
45
46
public static void main(String args[]) throws IOException {
47
var loopback = InetAddress.getLoopbackAddress();
48
try (ServerSocket ss = boundServer(loopback);
49
Socket s = new Socket(loopback, ss.getLocalPort());
50
DatagramSocket ds = new DatagramSocket(0);
51
MulticastSocket mc = new MulticastSocket(0)) {
52
if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
53
ss.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);
54
if (ss.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) {
55
throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME);
56
}
57
}
58
if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
59
ss.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);
60
if (ss.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) {
61
throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES);
62
}
63
}
64
if (ss.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
65
ss.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);
66
if (ss.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) {
67
throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL);
68
}
69
}
70
if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
71
s.setOption(ExtendedSocketOptions.TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);
72
if (s.getOption(ExtendedSocketOptions.TCP_KEEPIDLE) != DEFAULT_KEEP_ALIVE_TIME) {
73
throw new RuntimeException("Test failed, TCP_KEEPIDLE should have been " + DEFAULT_KEEP_ALIVE_TIME);
74
}
75
}
76
if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
77
s.setOption(ExtendedSocketOptions.TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);
78
if (s.getOption(ExtendedSocketOptions.TCP_KEEPCOUNT) != DEFAULT_KEEP_ALIVE_PROBES) {
79
throw new RuntimeException("Test failed, TCP_KEEPCOUNT should have been " + DEFAULT_KEEP_ALIVE_PROBES);
80
}
81
}
82
if (s.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
83
s.setOption(ExtendedSocketOptions.TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);
84
if (s.getOption(ExtendedSocketOptions.TCP_KEEPINTERVAL) != DEFAULT_KEEP_ALIVE_INTVL) {
85
throw new RuntimeException("Test failed, TCP_KEEPINTERVAL should have been " + DEFAULT_KEEP_ALIVE_INTVL);
86
}
87
}
88
if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
89
throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable"
90
+ " for TCP Sockets only.");
91
}
92
if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
93
throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable"
94
+ " for TCP Sockets only.");
95
}
96
if (ds.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
97
throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable"
98
+ " for TCP Sockets only.");
99
}
100
if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPCOUNT)) {
101
throw new RuntimeException("Test failed, TCP_KEEPCOUNT is applicable"
102
+ " for TCP Sockets only");
103
}
104
if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPIDLE)) {
105
throw new RuntimeException("Test failed, TCP_KEEPIDLE is applicable"
106
+ " for TCP Sockets only");
107
}
108
if (mc.supportedOptions().contains(ExtendedSocketOptions.TCP_KEEPINTERVAL)) {
109
throw new RuntimeException("Test failed, TCP_KEEPINTERVAL is applicable"
110
+ " for TCP Sockets only");
111
}
112
}
113
}
114
115
private static ServerSocket boundServer(InetAddress address) throws IOException {
116
var socketAddress = new InetSocketAddress(address, 0);
117
var server = new ServerSocket();
118
server.bind(socketAddress);
119
return server;
120
}
121
}
122
123