Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/nio/channels/SocketChannel/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 to check SocketChannel setOption/getOption/options26* methods.27*/2829import java.nio.*;30import java.nio.channels.*;31import java.net.*;32import java.io.IOException;33import java.util.*;34import sun.net.ExtendedOptionsHelper;35import static java.net.StandardSocketOptions.*;36import static jdk.net.ExtendedSocketOptions.*;3738public class SocketOptionTests {3940static void checkOption(SocketChannel sc, SocketOption name, Object expectedValue)41throws IOException42{43Object value = sc.getOption(name);44if (!value.equals(expectedValue))45throw new RuntimeException("value not as expected");46}4748public static void main(String[] args) throws IOException {49SocketChannel sc = SocketChannel.open();5051// check supported options52Set<SocketOption<?>> options = sc.supportedOptions();5354List<SocketOption<?>> extOptions = Arrays.asList(TCP_KEEPCOUNT, TCP_KEEPIDLE,55TCP_KEEPINTERVAL);56List<? extends SocketOption> expected;57boolean keepAliveOptsupported;58if (keepAliveOptsupported=ExtendedOptionsHelper.keepAliveOptions()59.containsAll(extOptions)) {60expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE,61SO_REUSEADDR, SO_LINGER, TCP_NODELAY, TCP_KEEPCOUNT,62TCP_KEEPIDLE, TCP_KEEPINTERVAL);63} else {64expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, SO_KEEPALIVE,65SO_REUSEADDR, SO_LINGER, TCP_NODELAY);66}67for (SocketOption opt: expected) {68if (!options.contains(opt))69throw new RuntimeException(opt.name() + " should be supported");70}7172// check specified defaults73int linger = sc.<Integer>getOption(SO_LINGER);74if (linger >= 0)75throw new RuntimeException("initial value of SO_LINGER should be < 0");76checkOption(sc, SO_KEEPALIVE, false);77checkOption(sc, TCP_NODELAY, false);7879// allowed to change when not bound80sc.setOption(SO_KEEPALIVE, true);81checkOption(sc, SO_KEEPALIVE, true);82sc.setOption(SO_KEEPALIVE, false);83checkOption(sc, SO_KEEPALIVE, false);84sc.setOption(SO_SNDBUF, 128*1024); // can't check85sc.setOption(SO_RCVBUF, 256*1024); // can't check86int before, after;87before = sc.getOption(SO_SNDBUF);88after = sc.setOption(SO_SNDBUF, Integer.MAX_VALUE).getOption(SO_SNDBUF);89if (after < before)90throw new RuntimeException("setOption caused SO_SNDBUF to decrease");91before = sc.getOption(SO_RCVBUF);92after = sc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);93if (after < before)94throw new RuntimeException("setOption caused SO_RCVBUF to decrease");95sc.setOption(SO_REUSEADDR, true);96checkOption(sc, SO_REUSEADDR, true);97sc.setOption(SO_REUSEADDR, false);98checkOption(sc, SO_REUSEADDR, false);99sc.setOption(SO_LINGER, 10);100linger = sc.<Integer>getOption(SO_LINGER);101if (linger < 1)102throw new RuntimeException("expected linger to be enabled");103sc.setOption(SO_LINGER, -1);104linger = sc.<Integer>getOption(SO_LINGER);105if (linger >= 0)106throw new RuntimeException("expected linger to be disabled");107sc.setOption(TCP_NODELAY, true);108checkOption(sc, TCP_NODELAY, true);109sc.setOption(TCP_NODELAY, false); // can't check110111// bind socket112sc.bind(new InetSocketAddress(0));113114// allow to change when bound115sc.setOption(SO_KEEPALIVE, true);116checkOption(sc, SO_KEEPALIVE, true);117sc.setOption(SO_KEEPALIVE, false);118checkOption(sc, SO_KEEPALIVE, false);119120sc.setOption(SO_LINGER, 10);121linger = sc.<Integer>getOption(SO_LINGER);122if (linger < 1)123throw new RuntimeException("expected linger to be enabled");124sc.setOption(SO_LINGER, -1);125linger = sc.<Integer>getOption(SO_LINGER);126if (linger >= 0)127throw new RuntimeException("expected linger to be disabled");128sc.setOption(TCP_NODELAY, true); // can't check129sc.setOption(TCP_NODELAY, false); // can't check130if (keepAliveOptsupported) {131sc.setOption(TCP_KEEPIDLE, 1234);132checkOption(sc, TCP_KEEPIDLE, 1234);133sc.setOption(TCP_KEEPINTERVAL, 123);134checkOption(sc, TCP_KEEPINTERVAL, 123);135sc.setOption(TCP_KEEPCOUNT, 7);136checkOption(sc, TCP_KEEPCOUNT, 7);137}138// NullPointerException139try {140sc.setOption(null, "value");141throw new RuntimeException("NullPointerException not thrown");142} catch (NullPointerException x) {143}144try {145sc.getOption(null);146throw new RuntimeException("NullPointerException not thrown");147} catch (NullPointerException x) {148}149150// ClosedChannelException151sc.close();152try {153sc.setOption(TCP_NODELAY, true);154throw new RuntimeException("ClosedChannelException not thrown");155} catch (ClosedChannelException x) {156}157}158}159160161