1 /* 2 * Copyright (c) 2007, 2017, 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 /* @test 25 * @bug 4640544 8044773 26 * @summary Unit test for setOption/getOption/options methods 27 * @requires !vm.graal.enabled 28 * @run main SocketOptionTests 29 * @run main/othervm --limit-modules=java.base SocketOptionTests 30 */ 31 32 import java.nio.*; 33 import java.nio.channels.*; 34 import java.net.*; 35 import java.io.IOException; 36 import java.util.*; 37 import static java.net.StandardSocketOptions.*; 38 39 public class SocketOptionTests { 40 41 static <T> void checkOption(DatagramChannel dc, 42 SocketOption<T> name, 43 T expectedValue) 44 throws IOException 45 { 46 T value = dc.getOption(name); 47 if (!value.equals(expectedValue)) 48 throw new RuntimeException("value not as expected"); 49 } 50 51 public static void main(String[] args) throws IOException { 52 DatagramChannel dc = DatagramChannel.open(); 53 54 // check supported options 55 Set<SocketOption<?>> options = dc.supportedOptions(); 56 boolean reuseport = options.contains(SO_REUSEPORT); 57 List<? extends SocketOption<?>> expected; 58 if (reuseport) { 59 expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, 60 SO_REUSEADDR, SO_REUSEPORT, SO_BROADCAST, IP_TOS, IP_MULTICAST_IF, 61 IP_MULTICAST_TTL, IP_MULTICAST_LOOP); 62 } else { 63 expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, 64 SO_REUSEADDR, SO_BROADCAST, IP_TOS, IP_MULTICAST_IF, IP_MULTICAST_TTL, 65 IP_MULTICAST_LOOP); 66 } 67 for (SocketOption opt: expected) { 68 if (!options.contains(opt)) 69 throw new RuntimeException(opt.name() + " should be supported"); 70 } 71 72 // check specified defaults 73 checkOption(dc, SO_BROADCAST, false); 74 checkOption(dc, IP_MULTICAST_TTL, 1); // true on supported platforms 75 checkOption(dc, IP_MULTICAST_LOOP, true); // true on supported platforms 76 77 // allowed to change when not bound 78 dc.setOption(SO_BROADCAST, true); 79 checkOption(dc, SO_BROADCAST, true); 80 dc.setOption(SO_BROADCAST, false); 81 checkOption(dc, SO_BROADCAST, false); 82 dc.setOption(SO_SNDBUF, 128*1024); // can't check 83 dc.setOption(SO_RCVBUF, 128*1024); // can't check 84 int before, after; 85 before = dc.getOption(SO_SNDBUF); 86 after = dc.setOption(SO_SNDBUF, Integer.MAX_VALUE).getOption(SO_SNDBUF); 87 if (after < before) 88 throw new RuntimeException("setOption caused SO_SNDBUF to decrease"); 89 before = dc.getOption(SO_RCVBUF); 90 after = dc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF); 91 if (after < before) 92 throw new RuntimeException("setOption caused SO_RCVBUF to decrease"); 93 dc.setOption(SO_REUSEADDR, true); 94 checkOption(dc, SO_REUSEADDR, true); 95 dc.setOption(SO_REUSEADDR, false); 96 checkOption(dc, SO_REUSEADDR, false); 97 if (reuseport) { 98 dc.setOption(SO_REUSEPORT, true); 99 checkOption(dc, SO_REUSEPORT, true); 100 dc.setOption(SO_REUSEPORT, false); 101 checkOption(dc, SO_REUSEPORT, false); 102 } 103 // bind socket 104 dc.bind(new InetSocketAddress(0)); 105 106 // allow to change when bound 107 dc.setOption(SO_BROADCAST, true); 108 checkOption(dc, SO_BROADCAST, true); 109 dc.setOption(SO_BROADCAST, false); 110 checkOption(dc, SO_BROADCAST, false); 111 dc.setOption(IP_TOS, 0x08); // can't check 112 dc.setOption(IP_MULTICAST_TTL, 2); 113 checkOption(dc, IP_MULTICAST_TTL, 2); 114 dc.setOption(IP_MULTICAST_LOOP, false); 115 checkOption(dc, IP_MULTICAST_LOOP, false); 116 dc.setOption(IP_MULTICAST_LOOP, true); 117 checkOption(dc, IP_MULTICAST_LOOP, true); 118 119 120 // NullPointerException 121 try { 122 dc.setOption(null, "value"); 123 throw new RuntimeException("NullPointerException not thrown"); 124 } catch (NullPointerException x) { 125 } 126 try { 127 dc.getOption(null); 128 throw new RuntimeException("NullPointerException not thrown"); 129 } catch (NullPointerException x) { 130 } 131 132 // ClosedChannelException 133 dc.close(); 134 try { 135 dc.setOption(IP_MULTICAST_LOOP, true); 136 throw new RuntimeException("ClosedChannelException not thrown"); 137 } catch (ClosedChannelException x) { 138 } 139 } 140 }