SocketOptionTests.java revision 893:f06f30b29f36
1/* 2 * Copyright 2007-2009 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 20 * CA 95054 USA or visit www.sun.com if you need additional information or 21 * have any questions. 22 */ 23 24/* @test 25 * @bug 4640544 26 * @summary Unit test for setOption/getOption/options methods 27 */ 28 29import java.nio.*; 30import java.nio.channels.*; 31import java.net.*; 32import java.io.IOException; 33import java.util.*; 34import static java.net.StandardSocketOption.*; 35 36public class SocketOptionTests { 37 38 static <T> void checkOption(DatagramChannel dc, 39 SocketOption<T> name, 40 T expectedValue) 41 throws IOException 42 { 43 T value = dc.getOption(name); 44 if (!value.equals(expectedValue)) 45 throw new RuntimeException("value not as expected"); 46 } 47 48 public static void main(String[] args) throws IOException { 49 DatagramChannel dc = DatagramChannel.open(); 50 51 // check supported options 52 Set<SocketOption<?>> options = dc.supportedOptions(); 53 List<? extends SocketOption<?>> expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF, 54 SO_REUSEADDR, SO_BROADCAST, IP_TOS, IP_MULTICAST_IF, IP_MULTICAST_TTL, 55 IP_MULTICAST_LOOP); 56 for (SocketOption opt: expected) { 57 if (!options.contains(opt)) 58 throw new RuntimeException(opt.name() + " should be supported"); 59 } 60 61 // check specified defaults 62 checkOption(dc, SO_BROADCAST, false); 63 checkOption(dc, IP_MULTICAST_TTL, 1); // true on supported platforms 64 checkOption(dc, IP_MULTICAST_LOOP, true); // true on supported platforms 65 66 // allowed to change when not bound 67 dc.setOption(SO_BROADCAST, true); 68 checkOption(dc, SO_BROADCAST, true); 69 dc.setOption(SO_BROADCAST, false); 70 checkOption(dc, SO_BROADCAST, false); 71 dc.setOption(SO_SNDBUF, 16*1024); // can't check 72 dc.setOption(SO_RCVBUF, 16*1024); // can't check 73 dc.setOption(SO_REUSEADDR, true); 74 checkOption(dc, SO_REUSEADDR, true); 75 dc.setOption(SO_REUSEADDR, false); 76 checkOption(dc, SO_REUSEADDR, false); 77 78 // bind socket 79 dc.bind(new InetSocketAddress(0)); 80 81 // allow to change when bound 82 dc.setOption(SO_BROADCAST, true); 83 checkOption(dc, SO_BROADCAST, true); 84 dc.setOption(SO_BROADCAST, false); 85 checkOption(dc, SO_BROADCAST, false); 86 dc.setOption(IP_TOS, 0x08); // can't check 87 dc.setOption(IP_MULTICAST_TTL, 2); 88 checkOption(dc, IP_MULTICAST_TTL, 2); 89 dc.setOption(IP_MULTICAST_LOOP, false); 90 checkOption(dc, IP_MULTICAST_LOOP, false); 91 dc.setOption(IP_MULTICAST_LOOP, true); 92 checkOption(dc, IP_MULTICAST_LOOP, true); 93 94 95 // NullPointerException 96 try { 97 dc.setOption(null, "value"); 98 throw new RuntimeException("NullPointerException not thrown"); 99 } catch (NullPointerException x) { 100 } 101 try { 102 dc.getOption(null); 103 throw new RuntimeException("NullPointerException not thrown"); 104 } catch (NullPointerException x) { 105 } 106 107 // ClosedChannelException 108 dc.close(); 109 try { 110 dc.setOption(IP_MULTICAST_LOOP, true); 111 throw new RuntimeException("ClosedChannelException not thrown"); 112 } catch (ClosedChannelException x) { 113 } 114 } 115} 116