1 /*
   2  * Copyright (c) 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 import java.io.IOException;
  25 import java.lang.reflect.Field;
  26 import java.net.*;
  27 import java.util.ArrayList;
  28 import java.util.List;
  29 
  30 /*
  31  * @test
  32  * @bug 8143554 8044773
  33  * @summary Test checks that UnsupportedOperationException for unsupported
  34  * SOCKET_OPTIONS is thrown by both getOption() and setOption() methods.
  35  * @run main UnsupportedOptionsTest
  36  * @run main/othervm --limit-modules=java.base UnsupportedOptionsTest
  37  */
  38 
  39 public class UnsupportedOptionsTest {
  40 
  41     private static final List<SocketOption<?>> socketOptions = new ArrayList<>();
  42 
  43     static {
  44         socketOptions.add(StandardSocketOptions.IP_MULTICAST_IF);
  45         socketOptions.add(StandardSocketOptions.IP_MULTICAST_LOOP);
  46         socketOptions.add(StandardSocketOptions.IP_MULTICAST_TTL);
  47         socketOptions.add(StandardSocketOptions.IP_TOS);
  48         socketOptions.add(StandardSocketOptions.SO_BROADCAST);
  49         socketOptions.add(StandardSocketOptions.SO_KEEPALIVE);
  50         socketOptions.add(StandardSocketOptions.SO_LINGER);
  51         socketOptions.add(StandardSocketOptions.SO_RCVBUF);
  52         socketOptions.add(StandardSocketOptions.SO_REUSEADDR);
  53         socketOptions.add(StandardSocketOptions.SO_SNDBUF);
  54         socketOptions.add(StandardSocketOptions.TCP_NODELAY);
  55 
  56         try {
  57             Class<?> c = Class.forName("jdk.net.ExtendedSocketOptions");
  58             Field field = c.getField("SO_FLOW_SLA");
  59             socketOptions.add((SocketOption<?>)field.get(null));
  60         } catch (ClassNotFoundException e) {
  61             // ignore, jdk.net module not present
  62         } catch (ReflectiveOperationException e) {
  63             throw new AssertionError(e);
  64         }
  65     }
  66 
  67     public static void main(String[] args) throws IOException {
  68         Socket s = new Socket();
  69         ServerSocket ss = new ServerSocket();
  70         DatagramSocket ds = new DatagramSocket();
  71         MulticastSocket ms = new MulticastSocket();
  72 
  73         for (SocketOption option : socketOptions) {
  74             if (!s.supportedOptions().contains(option)) {
  75                 testUnsupportedSocketOption(s, option);
  76             }
  77 
  78             if (!ss.supportedOptions().contains(option)) {
  79                 testUnsupportedSocketOption(ss, option);
  80             }
  81 
  82             if (!ms.supportedOptions().contains(option)) {
  83                 testUnsupportedSocketOption(ms, option);
  84             }
  85 
  86             if (!ds.supportedOptions().contains(option)) {
  87                 testUnsupportedSocketOption(ds, option);
  88             }
  89         }
  90     }
  91 
  92     /*
  93      * Check that UnsupportedOperationException for unsupported option is
  94      * thrown from both getOption() and setOption() methods.
  95      */
  96     private static void testUnsupportedSocketOption(Object socket,
  97                                                     SocketOption option) {
  98         testSet(socket, option);
  99         testGet(socket, option);
 100     }
 101 
 102     private static void testSet(Object socket, SocketOption option) {
 103         try {
 104             setOption(socket, option);
 105         } catch (UnsupportedOperationException e) {
 106             System.out.println("UnsupportedOperationException was throw " +
 107                     "as expected. Socket: " + socket + " Option: " + option);
 108             return;
 109         } catch (Exception e) {
 110             throw new RuntimeException("FAIL. Unexpected exception.", e);
 111         }
 112         throw new RuntimeException("FAIL. UnsupportedOperationException " +
 113                 "hasn't been thrown. Socket: " + socket + " Option: " + option);
 114     }
 115 
 116     private static void testGet(Object socket, SocketOption option) {
 117         try {
 118             getOption(socket, option);
 119         } catch (UnsupportedOperationException e) {
 120             System.out.println("UnsupportedOperationException was throw " +
 121                     "as expected. Socket: " + socket + " Option: " + option);
 122             return;
 123         } catch (Exception e) {
 124             throw new RuntimeException("FAIL. Unexpected exception.", e);
 125         }
 126         throw new RuntimeException("FAIL. UnsupportedOperationException " +
 127                 "hasn't been thrown. Socket: " + socket + " Option: " + option);
 128     }
 129 
 130     private static void getOption(Object socket,
 131                                   SocketOption option) throws IOException {
 132         if (socket instanceof Socket) {
 133             ((Socket) socket).getOption(option);
 134         } else if (socket instanceof ServerSocket) {
 135             ((ServerSocket) socket).getOption(option);
 136         } else if (socket instanceof DatagramSocket) {
 137             ((DatagramSocket) socket).getOption(option);
 138         } else {
 139             throw new RuntimeException("Unsupported socket type");
 140         }
 141     }
 142 
 143     private static void setOption(Object socket,
 144                                   SocketOption option) throws IOException {
 145         if (socket instanceof Socket) {
 146             ((Socket) socket).setOption(option, null);
 147         } else if (socket instanceof ServerSocket) {
 148             ((ServerSocket) socket).setOption(option, null);
 149         } else if (socket instanceof DatagramSocket) {
 150             ((DatagramSocket) socket).setOption(option, null);
 151         } else {
 152             throw new RuntimeException("Unsupported socket type");
 153         }
 154     }
 155 }