Path: blob/master/test/jdk/java/nio/channels/SocketChannel/SocketOptionTests.java
41154 views
/*1* Copyright (c) 2007, 2019, 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 to check SocketChannel setOption/getOption/options26* methods.27* @modules java.base/sun.net.ext28* jdk.net29* @requires !vm.graal.enabled30* @run main SocketOptionTests31* @run main/othervm --limit-modules=java.base SocketOptionTests32*/3334import java.io.IOException;35import java.net.InetSocketAddress;36import java.net.SocketOption;37import java.nio.channels.ClosedChannelException;38import java.nio.channels.SocketChannel;39import java.util.Set;40import sun.net.ext.ExtendedSocketOptions;41import static java.net.StandardSocketOptions.*;42import static jdk.net.ExtendedSocketOptions.*;4344public class SocketOptionTests {4546static void checkOption(SocketChannel sc, SocketOption name, Object expectedValue)47throws IOException48{49Object value = sc.getOption(name);50if (!value.equals(expectedValue))51throw new RuntimeException("value not as expected");52}5354public static void main(String[] args) throws IOException {55try (var channel = SocketChannel.open()) {56test(channel);57}58}5960static void test(SocketChannel sc) throws IOException {61Set<SocketOption<?>> extendedOptions = ExtendedSocketOptions.clientSocketOptions();62Set<SocketOption<?>> keepAliveOptions = Set.of(TCP_KEEPCOUNT, TCP_KEEPIDLE, TCP_KEEPINTERVAL);63boolean keepAliveOptionsSupported = extendedOptions.containsAll(keepAliveOptions);64Set<SocketOption<?>> expected;65if (keepAliveOptionsSupported) {66expected = Set.of(SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE,67SO_REUSEADDR, SO_LINGER, TCP_NODELAY, TCP_KEEPCOUNT,68TCP_KEEPIDLE, TCP_KEEPINTERVAL);69} else {70expected = Set.of(SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE,71SO_REUSEADDR, SO_LINGER, TCP_NODELAY);72}73for (SocketOption opt: expected) {74if (!sc.supportedOptions().contains(opt))75throw new RuntimeException(opt.name() + " should be supported");76}7778// check specified defaults79int linger = sc.<Integer>getOption(SO_LINGER);80if (linger >= 0)81throw new RuntimeException("initial value of SO_LINGER should be < 0");82checkOption(sc, SO_KEEPALIVE, false);83checkOption(sc, TCP_NODELAY, false);8485// allowed to change when not bound86sc.setOption(SO_KEEPALIVE, true);87checkOption(sc, SO_KEEPALIVE, true);88sc.setOption(SO_KEEPALIVE, false);89checkOption(sc, SO_KEEPALIVE, false);90sc.setOption(SO_SNDBUF, 128*1024); // can't check91sc.setOption(SO_RCVBUF, 256*1024); // can't check92int before, after;93before = sc.getOption(SO_SNDBUF);94after = sc.setOption(SO_SNDBUF, Integer.MAX_VALUE).getOption(SO_SNDBUF);95if (after < before)96throw new RuntimeException("setOption caused SO_SNDBUF to decrease");97before = sc.getOption(SO_RCVBUF);98after = sc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);99if (after < before)100throw new RuntimeException("setOption caused SO_RCVBUF to decrease");101sc.setOption(SO_REUSEADDR, true);102checkOption(sc, SO_REUSEADDR, true);103sc.setOption(SO_REUSEADDR, false);104checkOption(sc, SO_REUSEADDR, false);105sc.setOption(SO_LINGER, 10);106linger = sc.<Integer>getOption(SO_LINGER);107if (linger < 1)108throw new RuntimeException("expected linger to be enabled");109sc.setOption(SO_LINGER, -1);110linger = sc.<Integer>getOption(SO_LINGER);111if (linger >= 0)112throw new RuntimeException("expected linger to be disabled");113sc.setOption(TCP_NODELAY, true);114checkOption(sc, TCP_NODELAY, true);115sc.setOption(TCP_NODELAY, false); // can't check116117// bind socket118sc.bind(new InetSocketAddress(0));119120// allow to change when bound121sc.setOption(SO_KEEPALIVE, true);122checkOption(sc, SO_KEEPALIVE, true);123sc.setOption(SO_KEEPALIVE, false);124checkOption(sc, SO_KEEPALIVE, false);125126sc.setOption(SO_LINGER, 10);127linger = sc.<Integer>getOption(SO_LINGER);128if (linger < 1)129throw new RuntimeException("expected linger to be enabled");130sc.setOption(SO_LINGER, -1);131linger = sc.<Integer>getOption(SO_LINGER);132if (linger >= 0)133throw new RuntimeException("expected linger to be disabled");134sc.setOption(TCP_NODELAY, true); // can't check135sc.setOption(TCP_NODELAY, false); // can't check136if (keepAliveOptionsSupported) {137sc.setOption(TCP_KEEPIDLE, 1234);138checkOption(sc, TCP_KEEPIDLE, 1234);139sc.setOption(TCP_KEEPINTERVAL, 123);140checkOption(sc, TCP_KEEPINTERVAL, 123);141sc.setOption(TCP_KEEPCOUNT, 7);142checkOption(sc, TCP_KEEPCOUNT, 7);143}144// NullPointerException145try {146sc.setOption(null, "value");147throw new RuntimeException("NullPointerException not thrown");148} catch (NullPointerException x) {149}150try {151sc.getOption(null);152throw new RuntimeException("NullPointerException not thrown");153} catch (NullPointerException x) {154}155156// ClosedChannelException157sc.close();158try {159sc.setOption(TCP_NODELAY, true);160throw new RuntimeException("ClosedChannelException not thrown");161} catch (ClosedChannelException x) {162}163}164}165166