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