SocketOptionTests.java revision 6073:cea72c2bf071
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 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.StandardSocketOptions.*;
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, 128*1024);       // can't check
72        dc.setOption(SO_RCVBUF, 128*1024);       // can't check
73        int before, after;
74        before = dc.getOption(SO_SNDBUF);
75        after = dc.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 = dc.getOption(SO_RCVBUF);
79        after = dc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);
80        if (after < before)
81            throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
82        dc.setOption(SO_REUSEADDR, true);
83        checkOption(dc, SO_REUSEADDR, true);
84        dc.setOption(SO_REUSEADDR, false);
85        checkOption(dc, SO_REUSEADDR, false);
86
87        // bind socket
88        dc.bind(new InetSocketAddress(0));
89
90        // allow to change when bound
91        dc.setOption(SO_BROADCAST, true);
92        checkOption(dc, SO_BROADCAST, true);
93        dc.setOption(SO_BROADCAST, false);
94        checkOption(dc, SO_BROADCAST, false);
95        dc.setOption(IP_TOS, 0x08);     // can't check
96        dc.setOption(IP_MULTICAST_TTL, 2);
97        checkOption(dc, IP_MULTICAST_TTL, 2);
98        dc.setOption(IP_MULTICAST_LOOP, false);
99        checkOption(dc, IP_MULTICAST_LOOP, false);
100        dc.setOption(IP_MULTICAST_LOOP, true);
101        checkOption(dc, IP_MULTICAST_LOOP, true);
102
103
104        // NullPointerException
105        try {
106            dc.setOption(null, "value");
107            throw new RuntimeException("NullPointerException not thrown");
108        } catch (NullPointerException x) {
109        }
110        try {
111            dc.getOption(null);
112            throw new RuntimeException("NullPointerException not thrown");
113        } catch (NullPointerException x) {
114        }
115
116        // ClosedChannelException
117        dc.close();
118        try {
119            dc.setOption(IP_MULTICAST_LOOP, true);
120            throw new RuntimeException("ClosedChannelException not thrown");
121        } catch (ClosedChannelException x) {
122        }
123    }
124}
125