/* * Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
staticclass Test<T> { final SocketOption<T> option; final T value;
Test(SocketOption<T> option, T value) { this.option = option; this.value = value;
} static <T> Test<T> create(SocketOption<T> option, T value) { returnnew Test<T>(option, value);
}
}
// The tests set the option using the new API, read back the set value // which could be different, and then use the legacy get API to check // these values are the same
// Tests default and negative values of SO_LINGER. All negative values should // retrieve as -1. staticvoid testSoLingerValues() throws Exception { try (Socket s = new Socket()) { // retrieve without set int defaultValue = s.getOption(StandardSocketOptions.SO_LINGER);
testEqual(StandardSocketOptions.SO_LINGER, -1, defaultValue);
for (int v : List.of(-1, -2, -100, -65534, -65535, -65536, -100000)) {
System.out.println("Testing SO_LINGER with:" + v);
s.setOption(StandardSocketOptions.SO_LINGER, v); int value = s.getOption(StandardSocketOptions.SO_LINGER);
testEqual(StandardSocketOptions.SO_LINGER, -1, value);
}
}
}
@SuppressWarnings("try") staticvoid doSocketTests() throws Exception { // unconnected socket try (Socket s = new Socket()) { for (Test<?> test : socketTests) { if (okayToTest(s, test.option)) {
test(s, test);
}
}
}
// connected socket try (ServerSocket ss = new ServerSocket()) { var loopback = InetAddress.getLoopbackAddress();
ss.bind(new InetSocketAddress(loopback, 0)); try (Socket s1 = new Socket()) {
s1.connect(ss.getLocalSocketAddress()); try (Socket s2 = ss.accept()) { for (Test<?> test : socketTests) { if (okayToTest(s1, test.option)) {
test(s1, test);
}
}
}
}
}
testSoLingerValues();
}
staticvoid doServerSocketTests() throws Exception { try (ServerSocket ss = new ServerSocket(0)) {
Set<SocketOption<?>> options = ss.supportedOptions(); boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT); for (Test<?> test : serverSocketTests) { if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
test(ss, test);
}
}
}
}
staticvoid doDatagramSocketTests() throws Exception { try (DatagramSocket ds = new DatagramSocket(0)) {
Set<SocketOption<?>> options = ds.supportedOptions(); boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT); for (Test<?> test : datagramSocketTests) { if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
test(ds, test);
}
}
}
}
staticvoid doMulticastSocketTests() throws Exception { try (MulticastSocket ms = new MulticastSocket(0)) { for (Test<?> test : multicastSocketTests) {
test(ms, test);
}
}
}
// Reflectively access jdk.net.Sockets.getOption so that the test can run // without the jdk.net module. static Object getServerSocketTrafficClass(ServerSocket ss) throws Exception { try { Class<?> c = Class.forName("jdk.net.Sockets");
Method m = c.getMethod("getOption", ServerSocket.class, SocketOption.class); return m.invoke(null, ss, StandardSocketOptions.IP_TOS);
} catch (ClassNotFoundException e) { // Ok, jdk.net module not present, just fall back
System.out.println("jdk.net module not present, falling back."); return Integer.valueOf(ss.getOption(StandardSocketOptions.IP_TOS));
} catch (ReflectiveOperationException e) { thrownew AssertionError(e);
}
}
}
¤ Dauer der Verarbeitung: 0.12 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.