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