1 /* 2 * Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /* 25 * @test 26 * @bug 8224477 27 * @summary Basic test for java.net.DatagramSocketImpl implSpec 28 * @run testng TestImplSpec 29 */ 30 31 import java.io.IOException; 32 import java.net.DatagramPacket; 33 import java.net.DatagramSocketImpl; 34 import java.net.InetAddress; 35 import java.net.NetworkInterface; 36 import java.net.SocketAddress; 37 import java.net.SocketOption; 38 import java.util.Set; 39 import org.testng.annotations.Test; 40 import static java.lang.Boolean.*; 41 import static java.net.StandardSocketOptions.*; 42 import static org.testng.Assert.assertEquals; 43 import static org.testng.Assert.expectThrows; 44 45 public class TestImplSpec { 46 47 static final Class<UnsupportedOperationException> UOE = UnsupportedOperationException.class; 48 49 @Test 50 public void socketImpl() { 51 DelegatingDatagramSocketImpl dsi = new DelegatingDatagramSocketImpl(); 52 53 assertEquals(dsi.supportedOptions().size(), 0); 54 55 expectThrows(UOE, () -> dsi.setOption(null, null)); 56 expectThrows(UOE, () -> dsi.setOption(null, 1)); 57 expectThrows(UOE, () -> dsi.setOption(SO_RCVBUF, 100)); 58 expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE)); 59 expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, FALSE)); 60 expectThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, TRUE)); 61 expectThrows(UOE, () -> dsi.setOption(FAKE_SOCK_OPT, FALSE)); 62 expectThrows(UOE, () -> dsi.setOption(SO_KEEPALIVE, TRUE)); 63 64 expectThrows(UOE, () -> dsi.getOption(null)); 65 expectThrows(UOE, () -> dsi.getOption(SO_RCVBUF)); 66 expectThrows(UOE, () -> dsi.getOption(SO_KEEPALIVE)); 67 expectThrows(UOE, () -> dsi.getOption(FAKE_SOCK_OPT)); 68 } 69 70 static final SocketOption<Boolean> FAKE_SOCK_OPT = new SocketOption<>() { 71 @Override public String name() { return "FAKE_SOCK_OPT"; } 72 @Override public Class<Boolean> type() { return Boolean.class; } 73 }; 74 75 // A DatagramSocketImpl that delegates the three new-style socket option 76 // methods to the default java.net.DatagramSocketImpl implementation. 77 static class DelegatingDatagramSocketImpl extends DatagramSocketImpl { 78 79 @Override 80 public <T> void setOption(SocketOption<T> name, T value) throws IOException { 81 super.setOption(name, value); 82 } 83 84 @Override 85 public Set<SocketOption<?>> supportedOptions() { 86 return super.supportedOptions(); 87 } 88 89 @Override 90 public <T> T getOption(SocketOption<T> name) throws IOException { 91 return super.getOption(name); 92 } 93 94 // -- 95 @Override protected void create() { } 96 @Override protected void bind(int lport, InetAddress laddr) { } 97 @Override protected void send(DatagramPacket p) { } 98 @Override protected int peek(InetAddress i) { return 0; } 99 @Override protected int peekData(DatagramPacket p) { return 0; } 100 @Override protected void receive(DatagramPacket p) { } 101 @Override protected void setTTL(byte ttl) { } 102 @Override protected byte getTTL() { return 0; } 103 @Override protected void setTimeToLive(int ttl) { } 104 @Override protected int getTimeToLive() { return 0; } 105 @Override protected void join(InetAddress inetaddr) { } 106 @Override protected void leave(InetAddress inetaddr) { } 107 @Override protected void joinGroup(SocketAddress mcastaddr, NetworkInterface netIf) { } 108 @Override protected void leaveGroup(SocketAddress mcastaddr, NetworkInterface netIf) { } 109 @Override protected void close() { } 110 @Override public void setOption(int optID, Object value) { } 111 @Override public Object getOption(int optID) { return null; } 112 } 113 }