Path: blob/master/test/jdk/java/net/SocketOption/ImmutableOptions.java
41152 views
/*1* Copyright (c) 2016, 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/*24* @test25* @bug 814860926* @library /test/lib27* @summary Assert that the set of socket options are immutable28* @run testng/othervm ImmutableOptions29* @run testng/othervm -Djava.net.preferIPv4Stack=true ImmutableOptions30*/31import java.io.IOException;32import java.io.InputStream;33import java.io.OutputStream;34import java.net.*;35import java.util.Set;3637import jdk.test.lib.net.IPSupport;3839import org.testng.annotations.BeforeTest;40import org.testng.annotations.Test;4142public class ImmutableOptions {4344@BeforeTest45void setupServerSocketFactory() throws IOException {46IPSupport.throwSkippedExceptionIfNonOperational();47ServerSocket.setSocketFactory(new ServerSocketImplFactory());48}4950@Test(expectedExceptions = UnsupportedOperationException.class)51public void socketThrows() throws IOException {52CustomSocketImpl impl = new CustomSocketImpl();53Socket socket = new CustomSocket(impl);54socket.supportedOptions().clear();55}5657@Test(expectedExceptions = UnsupportedOperationException.class)58public void socketImplThrows() throws IOException {59CustomSocketImpl impl = new CustomSocketImpl();60impl.supportedOptions().clear();61}6263@Test(expectedExceptions = UnsupportedOperationException.class)64public void serverSocketThrows() throws IOException {65ServerSocket ss = new ServerSocket();66ss.supportedOptions().clear();67}6869@Test(expectedExceptions = UnsupportedOperationException.class)70public void serverSocketImplThrows() throws IOException {71ServerSocket ss = new ServerSocket();72ServerSocketImplFactory.mostRecentlyCreated.supportedOptions().clear();73}7475@Test(expectedExceptions = UnsupportedOperationException.class)76public void datagramSocketThrows() throws IOException {77CustomDatagramSocketImpl impl = new CustomDatagramSocketImpl();78DatagramSocket socket = new CustomDatagramSocket(impl);79socket.supportedOptions().clear();80}8182@Test(expectedExceptions = UnsupportedOperationException.class)83public void datagramSocketImplThrows() throws IOException {84CustomDatagramSocketImpl impl = new CustomDatagramSocketImpl();85impl.supportedOptions().clear();86}878889// Socket descendants90static class CustomSocket extends Socket {91public CustomSocket(SocketImpl impl) throws IOException {92super(impl);93}94}9596static class CustomDatagramSocket extends DatagramSocket {97public CustomDatagramSocket(DatagramSocketImpl impl) {98super(impl);99}100}101102static class ServerSocketImplFactory implements SocketImplFactory {103static volatile CustomSocketImpl mostRecentlyCreated;104105@Override public SocketImpl createSocketImpl() {106return mostRecentlyCreated = new CustomSocketImpl();107}108}109110// Custom impl's111static class CustomSocketImpl extends SocketImpl {112// The only method interesting to this test.113@Override public Set<SocketOption<?>> supportedOptions() {114return super.supportedOptions();115}116117public void create(boolean stream) throws IOException { }118119public void connect(String host, int port) throws IOException { }120121public void connect(InetAddress addr, int port) throws IOException { }122123public void connect(SocketAddress addr, int timeout) throws IOException { }124125public void bind(InetAddress host, int port) throws IOException { }126127public void listen(int backlog) throws IOException { }128129public void accept(SocketImpl s) throws IOException { }130131public InputStream getInputStream() throws IOException { return null; }132133public OutputStream getOutputStream() throws IOException { return null; }134135public int available() throws IOException { return 0; }136137public void close() throws IOException { }138139public void sendUrgentData(int data) throws IOException { }140141public Object getOption(int i) throws SocketException { return null; }142143public void setOption(int i, Object o) throws SocketException { }144}145146static class CustomDatagramSocketImpl extends DatagramSocketImpl {147// The only method interesting to this test.148@Override public Set<SocketOption<?>> supportedOptions() {149return super.supportedOptions();150}151152protected void create() throws SocketException { }153154protected void bind(int lport, InetAddress laddr) throws SocketException { }155156protected void send(DatagramPacket p) throws IOException { }157158protected int peek(InetAddress i) throws IOException { return 0; }159160protected int peekData(DatagramPacket p) throws IOException { return 0; }161162protected void receive(DatagramPacket p) throws IOException { }163164protected void setTTL(byte ttl) throws IOException { }165166protected byte getTTL() throws IOException { return 0; }167168protected void setTimeToLive(int ttl) throws IOException { }169170protected int getTimeToLive() throws IOException { return 0; }171172protected void join(InetAddress inetaddr) throws IOException { }173174protected void leave(InetAddress inetaddr) throws IOException { }175176protected void joinGroup(SocketAddress x, NetworkInterface y)177throws IOException { }178179protected void leaveGroup(SocketAddress x, NetworkInterface y)180throws IOException { }181182protected void close() { }183184public void setOption(int optID, Object value) throws SocketException { }185186public Object getOption(int optID) throws SocketException { return null; }187}188}189190191