Path: blob/master/test/jdk/java/nio/channels/ServerSocketChannel/SocketOptionTests.java
41154 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 4640544 804477325* @summary Unit test for ServerSocketChannel setOption/getOption/options26* methods.27* @modules jdk.net28* @requires !vm.graal.enabled29* @run main SocketOptionTests30* @run main/othervm --limit-modules=java.base SocketOptionTests31*/3233import java.nio.channels.*;34import java.net.*;35import java.io.IOException;36import java.util.*;37import static java.net.StandardSocketOptions.*;38import static jdk.net.ExtendedSocketOptions.*;3940public class SocketOptionTests {4142private static final int DEFAULT_KEEP_ALIVE_PROBES = 7;43private static final int DEFAULT_KEEP_ALIVE_TIME = 1973;44private static final int DEFAULT_KEEP_ALIVE_INTVL = 53;4546static void checkOption(ServerSocketChannel ssc, SocketOption name, Object expectedValue)47throws IOException48{49Object value = ssc.getOption(name);50if (!value.equals(expectedValue))51throw new RuntimeException("value not as expected");52}5354public static void main(String[] args) throws IOException {55ServerSocketChannel ssc = ServerSocketChannel.open();5657// check supported options58Set<SocketOption<?>> options = ssc.supportedOptions();59boolean reuseport = options.contains(SO_REUSEPORT);60if (!options.contains(SO_REUSEADDR))61throw new RuntimeException("SO_REUSEADDR should be supported");62if (!options.contains(SO_REUSEPORT) && reuseport)63throw new RuntimeException("SO_REUSEPORT should be supported");64if (!options.contains(SO_RCVBUF))65throw new RuntimeException("SO_RCVBUF should be supported");6667// allowed to change when not bound68ssc.setOption(SO_RCVBUF, 256*1024); // can't check69int before = ssc.getOption(SO_RCVBUF);70int after = ssc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);71if (after < before)72throw new RuntimeException("setOption caused SO_RCVBUF to decrease");73ssc.setOption(SO_REUSEADDR, true);74checkOption(ssc, SO_REUSEADDR, true);75ssc.setOption(SO_REUSEADDR, false);76checkOption(ssc, SO_REUSEADDR, false);77if (reuseport) {78ssc.setOption(SO_REUSEPORT, true);79checkOption(ssc, SO_REUSEPORT, true);80ssc.setOption(SO_REUSEPORT, false);81checkOption(ssc, SO_REUSEPORT, false);82}83if (ssc.supportedOptions().containsAll(List.of(TCP_KEEPCOUNT,84TCP_KEEPIDLE, TCP_KEEPINTERVAL))) {85ssc.setOption(TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);86checkOption(ssc, TCP_KEEPCOUNT, DEFAULT_KEEP_ALIVE_PROBES);87ssc.setOption(TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);88checkOption(ssc, TCP_KEEPIDLE, DEFAULT_KEEP_ALIVE_TIME);89ssc.setOption(TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);90checkOption(ssc, TCP_KEEPINTERVAL, DEFAULT_KEEP_ALIVE_INTVL);9192}9394// NullPointerException95try {96ssc.setOption(null, "value");97throw new RuntimeException("NullPointerException not thrown");98} catch (NullPointerException x) {99}100try {101ssc.getOption(null);102throw new RuntimeException("NullPointerException not thrown");103} catch (NullPointerException x) {104}105106// ClosedChannelException107ssc.close();108try {109ssc.setOption(SO_REUSEADDR, true);110throw new RuntimeException("ClosedChannelException not thrown");111} catch (ClosedChannelException x) {112}113}114}115116117