1 /*
   2  * Copyright (c) 2007, 2016, 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 
  29 import java.nio.*;
  30 import java.nio.channels.*;
  31 import java.net.*;
  32 import java.io.IOException;
  33 import java.util.*;
  34 import static java.net.StandardSocketOptions.*;
  35 
  36 public 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         boolean reuseport = options.contains(SO_REUSEPORT);
  54         List<? extends SocketOption<?>> expected;
  55         if (reuseport) {
  56            expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF,
  57                       SO_REUSEADDR, SO_REUSEPORT, SO_BROADCAST, IP_TOS, IP_MULTICAST_IF,
  58                       IP_MULTICAST_TTL, IP_MULTICAST_LOOP);
  59         } else {
  60            expected = Arrays.asList(SO_SNDBUF, SO_RCVBUF,
  61                       SO_REUSEADDR, SO_BROADCAST, IP_TOS, IP_MULTICAST_IF, IP_MULTICAST_TTL,
  62                       IP_MULTICAST_LOOP);
  63         }
  64         for (SocketOption opt: expected) {
  65             if (!options.contains(opt))
  66                 throw new RuntimeException(opt.name() + " should be supported");
  67         }
  68 
  69         // check specified defaults
  70         checkOption(dc, SO_BROADCAST, false);
  71         checkOption(dc, IP_MULTICAST_TTL, 1);           // true on supported platforms
  72         checkOption(dc, IP_MULTICAST_LOOP, true);       // true on supported platforms
  73 
  74         // allowed to change when not bound
  75         dc.setOption(SO_BROADCAST, true);
  76         checkOption(dc, SO_BROADCAST, true);
  77         dc.setOption(SO_BROADCAST, false);
  78         checkOption(dc, SO_BROADCAST, false);
  79         dc.setOption(SO_SNDBUF, 128*1024);       // can't check
  80         dc.setOption(SO_RCVBUF, 128*1024);       // can't check
  81         int before, after;
  82         before = dc.getOption(SO_SNDBUF);
  83         after = dc.setOption(SO_SNDBUF, Integer.MAX_VALUE).getOption(SO_SNDBUF);
  84         if (after < before)
  85             throw new RuntimeException("setOption caused SO_SNDBUF to decrease");
  86         before = dc.getOption(SO_RCVBUF);
  87         after = dc.setOption(SO_RCVBUF, Integer.MAX_VALUE).getOption(SO_RCVBUF);
  88         if (after < before)
  89             throw new RuntimeException("setOption caused SO_RCVBUF to decrease");
  90         dc.setOption(SO_REUSEADDR, true);
  91         checkOption(dc, SO_REUSEADDR, true);
  92         dc.setOption(SO_REUSEADDR, false);
  93         checkOption(dc, SO_REUSEADDR, false);
  94         if (reuseport) {
  95             dc.setOption(SO_REUSEPORT, true);
  96             checkOption(dc, SO_REUSEPORT, true);
  97             dc.setOption(SO_REUSEPORT, false);
  98             checkOption(dc, SO_REUSEPORT, false);
  99         }
 100         // bind socket
 101         dc.bind(new InetSocketAddress(0));
 102 
 103         // allow to change when bound
 104         dc.setOption(SO_BROADCAST, true);
 105         checkOption(dc, SO_BROADCAST, true);
 106         dc.setOption(SO_BROADCAST, false);
 107         checkOption(dc, SO_BROADCAST, false);
 108         dc.setOption(IP_TOS, 0x08);     // can't check
 109         dc.setOption(IP_MULTICAST_TTL, 2);
 110         checkOption(dc, IP_MULTICAST_TTL, 2);
 111         dc.setOption(IP_MULTICAST_LOOP, false);
 112         checkOption(dc, IP_MULTICAST_LOOP, false);
 113         dc.setOption(IP_MULTICAST_LOOP, true);
 114         checkOption(dc, IP_MULTICAST_LOOP, true);
 115 
 116 
 117         // NullPointerException
 118         try {
 119             dc.setOption(null, "value");
 120             throw new RuntimeException("NullPointerException not thrown");
 121         } catch (NullPointerException x) {
 122         }
 123         try {
 124             dc.getOption(null);
 125             throw new RuntimeException("NullPointerException not thrown");
 126         } catch (NullPointerException x) {
 127         }
 128 
 129         // ClosedChannelException
 130         dc.close();
 131         try {
 132             dc.setOption(IP_MULTICAST_LOOP, true);
 133             throw new RuntimeException("ClosedChannelException not thrown");
 134         } catch (ClosedChannelException x) {
 135         }
 136     }
 137 }