1 /*
   2  * Copyright (c) 2014, 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 /* @test
  25  * @bug 8195160
  26  * @summary Unit test for SocketOption
  27  * @requires (os.family == "linux")
  28  * @requires !vm.graal.enabled
  29  * @library .. /test/lib
  30  * @build RsocketTest
  31  * @run main/othervm -Djava.net.preferIPv4Stack=true OptionsTest
  32  */
  33 import java.lang.reflect.Method;
  34 import java.net.*;
  35 import java.util.*;
  36 import jdk.net.Sockets;
  37 import jdk.net.RdmaSocketOptions;
  38 
  39 public class OptionsTest {
  40 
  41     static class Test {
  42         Test(SocketOption<?> option, Object testValue) {
  43             this.option = option;
  44             this.testValue = testValue;
  45         }
  46         static Test create (SocketOption<?> option, Object testValue) {
  47             return new Test(option, testValue);
  48         }
  49         Object option;
  50         Object testValue;
  51     }
  52 
  53     // The tests set the option using the new API, read back the set value
  54     // which could be diferent, and then use the legacy get API to check
  55     // these values are the same
  56 
  57     static Test[] socketTests = new Test[] {
  58         Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
  59         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
  60         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(10)),
  61         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
  62     };
  63 
  64     static Test[] serverSocketTests = new Test[] {
  65         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
  66         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
  67     };
  68 
  69     static Test[] rdmaSocketOptionTests = new Test[] {
  70         Test.create(RdmaSocketOptions.RDMA_SQSIZE, Integer.valueOf(8 * 100)),
  71         Test.create(RdmaSocketOptions.RDMA_RQSIZE, Integer.valueOf(10 * 100)),
  72         Test.create(RdmaSocketOptions.RDMA_INLINE, Integer.valueOf(20 * 100)),
  73     };
  74 
  75     static void doSocketTests() throws Exception {
  76         try {
  77             Socket c = Sockets.openRdmaSocket();
  78 
  79             for (int i = 0; i < socketTests.length; i++) {
  80                 Test test = socketTests[i];
  81                 c.setOption((SocketOption)test.option, test.testValue);
  82                 Object getval = c.getOption((SocketOption)test.option);
  83                 Object legacyget = legacyGetOption(Socket.class, c, test.option);
  84                 if (!getval.equals(legacyget)) {
  85                     Formatter f = new Formatter();
  86                     f.format("S Err %d: %s/%s", i, getval, legacyget);
  87                     throw new RuntimeException(f.toString());
  88                 }
  89             }
  90 
  91             for (int i = 0; i < rdmaSocketOptionTests.length; i++) {
  92                 Test test = rdmaSocketOptionTests[i];
  93                 c.setOption((SocketOption)test.option, test.testValue);
  94                 Object getval = c.getOption((SocketOption)test.option);
  95                 if (((Integer)getval).intValue() !=
  96                         ((Integer)test.testValue).intValue())
  97                     throw new RuntimeException("Test Failed");
  98             }
  99         } catch (Exception e) {
 100             e.printStackTrace();
 101             throw new RuntimeException("Test Failed");
 102         }
 103     }
 104 
 105     static void doServerSocketTests() throws Exception {
 106         try {
 107             ServerSocket srv = Sockets.openRdmaServerSocket();
 108             for (int i = 0; i < serverSocketTests.length; i++) {
 109                 Test test = serverSocketTests[i];
 110                 srv.setOption((SocketOption)test.option, test.testValue);
 111                 Object getval = srv.getOption((SocketOption)test.option);
 112                 Object legacyget = legacyGetOption(
 113                     ServerSocket.class, srv, test.option
 114                 );
 115                 if (!getval.equals(legacyget)) {
 116                     Formatter f = new Formatter();
 117                     f.format("SS Err %d: %s/%s", i, getval, legacyget);
 118                     throw new RuntimeException(f.toString());
 119                 }
 120             }
 121 
 122             for (int i = 0; i < rdmaSocketOptionTests.length; i++) {
 123                 Test test = rdmaSocketOptionTests[i];
 124                 srv.setOption((SocketOption)test.option, test.testValue);
 125                 Object getval = srv.getOption((SocketOption)test.option);
 126                 if (((Integer)getval).intValue() != 
 127                         ((Integer)test.testValue).intValue())
 128                     throw new RuntimeException("Test Failed");
 129             }
 130         } catch (Exception e) {
 131             e.printStackTrace();
 132             throw new RuntimeException("Test Failed");
 133         }
 134     }
 135 
 136     static Object legacyGetOption(
 137         Class<?> type, Object s, Object option)
 138 
 139         throws Exception
 140     {
 141         if (type.equals(Socket.class)) {
 142             Socket socket = (Socket)s;
 143             Set<SocketOption<?>> options = socket.supportedOptions();
 144 
 145             if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
 146                 return Integer.valueOf(socket.getSendBufferSize());
 147             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
 148                 return Integer.valueOf(socket.getReceiveBufferSize());
 149             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
 150                 return Boolean.valueOf(socket.getReuseAddress());
 151             } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
 152                 return Boolean.valueOf(socket.getTcpNoDelay());
 153             } else if (option.equals(StandardSocketOptions.SO_LINGER)) {
 154                 return Integer.valueOf(socket.getSoLinger());
 155             } else {
 156                 throw new RuntimeException("unexecpted socket option");
 157             }
 158         } else if  (type.equals(ServerSocket.class)) {
 159             ServerSocket socket = (ServerSocket)s;
 160             Set<SocketOption<?>> options = socket.supportedOptions();
 161 
 162             if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
 163                 return Integer.valueOf(socket.getReceiveBufferSize());
 164             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
 165                 return Boolean.valueOf(socket.getReuseAddress());
 166             } else {
 167                 throw new RuntimeException("unexecpted socket option");
 168             }
 169         }
 170         throw new RuntimeException("unexecpted socket type");
 171     }
 172 
 173     public static void main(String args[]) throws Exception {
 174         if (!RsocketTest.isRsocketAvailable())
 175             return;
 176 
 177         doSocketTests();
 178         doServerSocketTests();
 179     }
 180 }