Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/SocketOptionTests.java
38828 views
1
/*
2
* Copyright (c) 2007, 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
* @bug 4640544
26
* @summary Unit test to check SocketChannel setOption/getOption/options
27
* methods.
28
*/
29
30
import java.nio.*;
31
import java.nio.channels.*;
32
import java.net.*;
33
import java.io.IOException;
34
import java.util.*;
35
import sun.net.ExtendedOptionsHelper;
36
import static java.net.StandardSocketOptions.*;
37
import static jdk.net.ExtendedSocketOptions.*;
38
39
public class SocketOptionTests {
40
41
static void checkOption(SocketChannel sc, SocketOption name, Object expectedValue)
42
throws IOException
43
{
44
Object value = sc.getOption(name);
45
if (!value.equals(expectedValue))
46
throw new RuntimeException("value not as expected");
47
}
48
49
public static void main(String[] args) throws IOException {
50
SocketChannel sc = SocketChannel.open();
51
52
// check supported options
53
Set<SocketOption<?>> options = sc.supportedOptions();
54
55
List<SocketOption<?>> extOptions = Arrays.asList(TCP_KEEPCOUNT, TCP_KEEPIDLE,
56
TCP_KEEPINTERVAL);
57
List<? extends SocketOption> expected;
58
boolean keepAliveOptsupported;
59
if (keepAliveOptsupported=ExtendedOptionsHelper.keepAliveOptions()
60
.containsAll(extOptions)) {
61
expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE,
62
SO_REUSEADDR, SO_LINGER, TCP_NODELAY, TCP_KEEPCOUNT,
63
TCP_KEEPIDLE, TCP_KEEPINTERVAL);
64
} else {
65
expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE,
66
SO_REUSEADDR, SO_LINGER, TCP_NODELAY);
67
}
68
for (SocketOption opt: expected) {
69
if (!options.contains(opt))
70
throw new RuntimeException(opt.name() + " should be supported");
71
}
72
73
// check specified defaults
74
int linger = sc.<Integer>getOption(SO_LINGER);
75
if (linger >= 0)
76
throw new RuntimeException("initial value of SO_LINGER should be < 0");
77
checkOption(sc, SO_KEEPALIVE, false);
78
checkOption(sc, TCP_NODELAY, false);
79
80
// allowed to change when not bound
81
sc.setOption(SO_KEEPALIVE, true);
82
checkOption(sc, SO_KEEPALIVE, true);
83
sc.setOption(SO_KEEPALIVE, false);
84
checkOption(sc, SO_KEEPALIVE, false);
85
sc.setOption(SO_SNDBUF, 128*1024); // can't check
86
sc.setOption(SO_RCVBUF, 256*1024); // can't check
87
int before, after;
88
before = sc.getOption(SO_SNDBUF);
89
after = sc.setOption(SO_SNDBUF, Integer.MAX_VALUE).getOption(SO_SNDBUF);
90
if (after < before)
91
throw new RuntimeException("setOption caused SO_SNDBUF to decrease");
92
before = sc.getOption(SO_RCVBUF);
93
after = sc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);
94
if (after < before)
95
throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
96
sc.setOption(SO_REUSEADDR, true);
97
checkOption(sc, SO_REUSEADDR, true);
98
sc.setOption(SO_REUSEADDR, false);
99
checkOption(sc, SO_REUSEADDR, false);
100
sc.setOption(SO_LINGER, 10);
101
linger = sc.<Integer>getOption(SO_LINGER);
102
if (linger < 1)
103
throw new RuntimeException("expected linger to be enabled");
104
sc.setOption(SO_LINGER, -1);
105
linger = sc.<Integer>getOption(SO_LINGER);
106
if (linger >= 0)
107
throw new RuntimeException("expected linger to be disabled");
108
sc.setOption(TCP_NODELAY, true);
109
checkOption(sc, TCP_NODELAY, true);
110
sc.setOption(TCP_NODELAY, false); // can't check
111
112
// bind socket
113
sc.bind(new InetSocketAddress(0));
114
115
// allow to change when bound
116
sc.setOption(SO_KEEPALIVE, true);
117
checkOption(sc, SO_KEEPALIVE, true);
118
sc.setOption(SO_KEEPALIVE, false);
119
checkOption(sc, SO_KEEPALIVE, false);
120
121
sc.setOption(SO_LINGER, 10);
122
linger = sc.<Integer>getOption(SO_LINGER);
123
if (linger < 1)
124
throw new RuntimeException("expected linger to be enabled");
125
sc.setOption(SO_LINGER, -1);
126
linger = sc.<Integer>getOption(SO_LINGER);
127
if (linger >= 0)
128
throw new RuntimeException("expected linger to be disabled");
129
sc.setOption(TCP_NODELAY, true); // can't check
130
sc.setOption(TCP_NODELAY, false); // can't check
131
if (keepAliveOptsupported) {
132
sc.setOption(TCP_KEEPIDLE, 1234);
133
checkOption(sc, TCP_KEEPIDLE, 1234);
134
sc.setOption(TCP_KEEPINTERVAL, 123);
135
checkOption(sc, TCP_KEEPINTERVAL, 123);
136
sc.setOption(TCP_KEEPCOUNT, 7);
137
checkOption(sc, TCP_KEEPCOUNT, 7);
138
}
139
// NullPointerException
140
try {
141
sc.setOption(null, "value");
142
throw new RuntimeException("NullPointerException not thrown");
143
} catch (NullPointerException x) {
144
}
145
try {
146
sc.getOption(null);
147
throw new RuntimeException("NullPointerException not thrown");
148
} catch (NullPointerException x) {
149
}
150
151
// ClosedChannelException
152
sc.close();
153
try {
154
sc.setOption(TCP_NODELAY, true);
155
throw new RuntimeException("ClosedChannelException not thrown");
156
} catch (ClosedChannelException x) {
157
}
158
}
159
}
160
161