Path: blob/master/test/jdk/java/net/SocketOption/UnsupportedOptionsTest.java
41149 views
/*1* Copyright (c) 2016, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.IOException;24import java.lang.reflect.Field;25import java.net.*;26import java.util.ArrayList;27import java.util.List;2829import jdk.test.lib.net.IPSupport;3031/*32* @test33* @bug 8143554 804477334* @library /test/lib35* @summary Test checks that UnsupportedOperationException for unsupported36* SOCKET_OPTIONS is thrown by both getOption() and setOption() methods.37* @requires !vm.graal.enabled38* @run main UnsupportedOptionsTest39* @run main/othervm -Djava.net.preferIPv4Stack=true UnsupportedOptionsTest40* @run main/othervm --limit-modules=java.base UnsupportedOptionsTest41*/4243public class UnsupportedOptionsTest {4445private static final List<SocketOption<?>> socketOptions = new ArrayList<>();4647static {48socketOptions.add(StandardSocketOptions.IP_MULTICAST_IF);49socketOptions.add(StandardSocketOptions.IP_MULTICAST_LOOP);50socketOptions.add(StandardSocketOptions.IP_MULTICAST_TTL);51socketOptions.add(StandardSocketOptions.IP_TOS);52socketOptions.add(StandardSocketOptions.SO_BROADCAST);53socketOptions.add(StandardSocketOptions.SO_KEEPALIVE);54socketOptions.add(StandardSocketOptions.SO_LINGER);55socketOptions.add(StandardSocketOptions.SO_RCVBUF);56socketOptions.add(StandardSocketOptions.SO_REUSEADDR);57socketOptions.add(StandardSocketOptions.SO_SNDBUF);58socketOptions.add(StandardSocketOptions.TCP_NODELAY);5960try {61Class<?> c = Class.forName("jdk.net.ExtendedSocketOptions");62Field field = c.getField("TCP_QUICKACK");63socketOptions.add((SocketOption<?>)field.get(null));64field = c.getField("TCP_KEEPIDLE");65socketOptions.add((SocketOption<?>)field.get(null));66field = c.getField("TCP_KEEPINTERVAL");67socketOptions.add((SocketOption<?>)field.get(null));68field = c.getField("TCP_KEEPCOUNT");69socketOptions.add((SocketOption<?>)field.get(null));7071} catch (ClassNotFoundException e) {72// ignore, jdk.net module not present73} catch (ReflectiveOperationException e) {74throw new AssertionError(e);75}76}7778public static void main(String[] args) throws IOException {79IPSupport.throwSkippedExceptionIfNonOperational();8081Socket s = new Socket();82ServerSocket ss = new ServerSocket();83DatagramSocket ds = new DatagramSocket();84MulticastSocket ms = new MulticastSocket();8586for (SocketOption option : socketOptions) {87if (!s.supportedOptions().contains(option)) {88testUnsupportedSocketOption(s, option);89}9091if (!ss.supportedOptions().contains(option)) {92testUnsupportedSocketOption(ss, option);93}9495if (!ms.supportedOptions().contains(option)) {96testUnsupportedSocketOption(ms, option);97}9899if (!ds.supportedOptions().contains(option)) {100testUnsupportedSocketOption(ds, option);101}102}103}104105/*106* Check that UnsupportedOperationException for unsupported option is107* thrown from both getOption() and setOption() methods.108*/109private static void testUnsupportedSocketOption(Object socket,110SocketOption option) {111testSet(socket, option);112testGet(socket, option);113}114115private static void testSet(Object socket, SocketOption option) {116try {117setOption(socket, option);118} catch (UnsupportedOperationException e) {119System.out.println("UnsupportedOperationException was throw " +120"as expected. Socket: " + socket + " Option: " + option);121return;122} catch (Exception e) {123throw new RuntimeException("FAIL. Unexpected exception.", e);124}125throw new RuntimeException("FAIL. UnsupportedOperationException " +126"hasn't been thrown. Socket: " + socket + " Option: " + option);127}128129private static void testGet(Object socket, SocketOption option) {130try {131getOption(socket, option);132} catch (UnsupportedOperationException e) {133System.out.println("UnsupportedOperationException was throw " +134"as expected. Socket: " + socket + " Option: " + option);135return;136} catch (Exception e) {137throw new RuntimeException("FAIL. Unexpected exception.", e);138}139throw new RuntimeException("FAIL. UnsupportedOperationException " +140"hasn't been thrown. Socket: " + socket + " Option: " + option);141}142143private static void getOption(Object socket,144SocketOption option) throws IOException {145if (socket instanceof Socket) {146((Socket) socket).getOption(option);147} else if (socket instanceof ServerSocket) {148((ServerSocket) socket).getOption(option);149} else if (socket instanceof DatagramSocket) {150((DatagramSocket) socket).getOption(option);151} else {152throw new RuntimeException("Unsupported socket type");153}154}155156private static void setOption(Object socket,157SocketOption option) throws IOException {158if (socket instanceof Socket) {159((Socket) socket).setOption(option, null);160} else if (socket instanceof ServerSocket) {161((ServerSocket) socket).setOption(option, null);162} else if (socket instanceof DatagramSocket) {163((DatagramSocket) socket).setOption(option, null);164} else {165throw new RuntimeException("Unsupported socket type");166}167}168}169170171