Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/ServerSocketChannel/SocketOptionTests.java
38828 views
/*1* Copyright (c) 2007, 2018, 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*/2223/* @test24* @bug 464054425* @summary Unit test for ServerSocketChannel setOption/getOption/options26* methods.27*/2829import java.nio.*;30import java.nio.channels.*;31import java.net.*;32import java.io.IOException;33import java.util.*;34import static java.net.StandardSocketOptions.*;35import static jdk.net.ExtendedSocketOptions.*;3637public class SocketOptionTests {3839private static final int DEFAULT_KEEP_ALIVE_PROBES = 7;40private static final int DEFAULT_KEEP_ALIVE_TIME = 1973;41private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;4243static void checkOption(ServerSocketChannel ssc, SocketOption name, Object expectedValue)44throws IOException45{46Object value = ssc.getOption(name);47if (!value.equals(expectedValue))48throw new RuntimeException("value not as expected");49}5051public static void main(String[] args) throws IOException {52ServerSocketChannel ssc = ServerSocketChannel.open();5354// check supported options55Set<SocketOption<?>> options = ssc.supportedOptions();56if (!options.contains(SO_REUSEADDR))57throw new RuntimeException("SO_REUSEADDR should be supported");58if (!options.contains(SO_RCVBUF))59throw new RuntimeException("SO_RCVBUF should be supported");6061// allowed to change when not bound62ssc.setOption(SO_RCVBUF, 256*1024); // can't check63int before = ssc.getOption(SO_RCVBUF);64int after = ssc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);65if (after < before)66throw new RuntimeException("setOption caused SO_RCVBUF to decrease");67ssc.setOption(SO_REUSEADDR, true);68checkOption(ssc, SO_REUSEADDR, true);69ssc.setOption(SO_REUSEADDR, false);70checkOption(ssc, SO_REUSEADDR, false);7172// NullPointerException73try {74ssc.setOption(null, "value");75throw new RuntimeException("NullPointerException not thrown");76} catch (NullPointerException x) {77}78try {79ssc.getOption(null);80throw new RuntimeException("NullPointerException not thrown");81} catch (NullPointerException x) {82}83List<SocketOption<?>> keepAliveOpts = Arrays.asList(TCP_KEEPCOUNT, TCP_KEEPIDLE,84TCP_KEEPINTERVAL);85if (ssc.supportedOptions().containsAll(keepAliveOpts)) {86ssc.setOption(TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);87checkOption(ssc, TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);88ssc.setOption(TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);89checkOption(ssc, TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);90ssc.setOption(TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);91checkOption(ssc, TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);9293}9495// ClosedChannelException96ssc.close();97try {98ssc.setOption(SO_REUSEADDR, true);99throw new RuntimeException("ClosedChannelException not thrown");100} catch (ClosedChannelException x) {101}102}103}104105106