jdk/test/java/net/SocketOption/OptionsTest.java
changeset 36115 0676e37a0b9c
parent 34889 9aac81c72779
child 37676 24ef455da1b0
equal deleted inserted replaced
36114:a5ed9456c9be 36115:0676e37a0b9c
    52     static Test[] socketTests = new Test[] {
    52     static Test[] socketTests = new Test[] {
    53         Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
    53         Test.create(StandardSocketOptions.SO_KEEPALIVE, Boolean.TRUE),
    54         Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
    54         Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
    55         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
    55         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
    56         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
    56         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
       
    57         Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
    57         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
    58         Test.create(StandardSocketOptions.SO_LINGER, Integer.valueOf(80)),
    58         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))
    59         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))
    59     };
    60     };
    60 
    61 
    61     static Test[] serverSocketTests = new Test[] {
    62     static Test[] serverSocketTests = new Test[] {
    62         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
    63         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
    63         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
    64         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
       
    65         Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
    64         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))
    66         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))
    65     };
    67     };
    66 
    68 
    67     static Test[] dgSocketTests = new Test[] {
    69     static Test[] dgSocketTests = new Test[] {
    68         Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
    70         Test.create(StandardSocketOptions.SO_SNDBUF, Integer.valueOf(10 * 100)),
    69         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
    71         Test.create(StandardSocketOptions.SO_RCVBUF, Integer.valueOf(8 * 100)),
    70         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
    72         Test.create(StandardSocketOptions.SO_REUSEADDR, Boolean.FALSE),
       
    73         Test.create(StandardSocketOptions.SO_REUSEPORT, Boolean.FALSE),
    71         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))
    74         Test.create(StandardSocketOptions.IP_TOS, Integer.valueOf(100))
    72     };
    75     };
    73 
    76 
    74     static Test[] mcSocketTests = new Test[] {
    77     static Test[] mcSocketTests = new Test[] {
    75         Test.create(StandardSocketOptions.IP_MULTICAST_IF, getNetworkInterface()),
    78         Test.create(StandardSocketOptions.IP_MULTICAST_IF, getNetworkInterface()),
    95         try (
    98         try (
    96             ServerSocket srv = new ServerSocket(0);
    99             ServerSocket srv = new ServerSocket(0);
    97             Socket c = new Socket("127.0.0.1", srv.getLocalPort());
   100             Socket c = new Socket("127.0.0.1", srv.getLocalPort());
    98             Socket s = srv.accept();
   101             Socket s = srv.accept();
    99         ) {
   102         ) {
       
   103             Set<SocketOption<?>> options = c.supportedOptions();
       
   104             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   100             for (int i=0; i<socketTests.length; i++) {
   105             for (int i=0; i<socketTests.length; i++) {
   101                 Test test = socketTests[i];
   106                 Test test = socketTests[i];
   102                 c.setOption((SocketOption)test.option, test.testValue);
   107                 if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
   103                 Object getval = c.getOption((SocketOption)test.option);
   108                     c.setOption((SocketOption)test.option, test.testValue);
   104                 Object legacyget = legacyGetOption(Socket.class, c,test.option);
   109                     Object getval = c.getOption((SocketOption)test.option);
   105                 if (!getval.equals(legacyget)) {
   110                     Object legacyget = legacyGetOption(Socket.class, c,test.option);
   106                     Formatter f = new Formatter();
   111                     if (!getval.equals(legacyget)) {
   107                     f.format("S Err %d: %s/%s", i, getval, legacyget);
   112                         Formatter f = new Formatter();
   108                     throw new RuntimeException(f.toString());
   113                         f.format("S Err %d: %s/%s", i, getval, legacyget);
       
   114                         throw new RuntimeException(f.toString());
       
   115                     }
   109                 }
   116                 }
   110             }
   117             }
   111         }
   118         }
   112     }
   119     }
   113 
   120 
   114     static void doDgSocketTests() throws Exception {
   121     static void doDgSocketTests() throws Exception {
   115         try (
   122         try (
   116             DatagramSocket c = new DatagramSocket(0);
   123             DatagramSocket c = new DatagramSocket(0);
   117         ) {
   124         ) {
       
   125             Set<SocketOption<?>> options = c.supportedOptions();
       
   126             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   118             for (int i=0; i<dgSocketTests.length; i++) {
   127             for (int i=0; i<dgSocketTests.length; i++) {
   119                 Test test = dgSocketTests[i];
   128                 Test test = dgSocketTests[i];
   120                 c.setOption((SocketOption)test.option, test.testValue);
   129                 if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
   121                 Object getval = c.getOption((SocketOption)test.option);
   130                     c.setOption((SocketOption)test.option, test.testValue);
   122                 Object legacyget = legacyGetOption(DatagramSocket.class, c,test.option);
   131                     Object getval = c.getOption((SocketOption)test.option);
   123                 if (!getval.equals(legacyget)) {
   132                     Object legacyget = legacyGetOption(DatagramSocket.class, c,test.option);
   124                     Formatter f = new Formatter();
   133                     if (!getval.equals(legacyget)) {
   125                     f.format("DG Err %d: %s/%s", i, getval, legacyget);
   134                         Formatter f = new Formatter();
   126                     throw new RuntimeException(f.toString());
   135                         f.format("DG Err %d: %s/%s", i, getval, legacyget);
       
   136                         throw new RuntimeException(f.toString());
       
   137                     }
   127                 }
   138                 }
   128             }
   139             }
   129         }
   140         }
   130     }
   141     }
   131 
   142 
   149 
   160 
   150     static void doServerSocketTests() throws Exception {
   161     static void doServerSocketTests() throws Exception {
   151         try (
   162         try (
   152             ServerSocket c = new ServerSocket(0);
   163             ServerSocket c = new ServerSocket(0);
   153         ) {
   164         ) {
       
   165             Set<SocketOption<?>> options = c.supportedOptions();
       
   166             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   154             for (int i=0; i<serverSocketTests.length; i++) {
   167             for (int i=0; i<serverSocketTests.length; i++) {
   155                 Test test = serverSocketTests[i];
   168                 Test test = serverSocketTests[i];
   156                 c.setOption((SocketOption)test.option, test.testValue);
   169                 if (!(test.option == StandardSocketOptions.SO_REUSEPORT && !reuseport)) {
   157                 Object getval = c.getOption((SocketOption)test.option);
   170                     c.setOption((SocketOption)test.option, test.testValue);
   158                 Object legacyget = legacyGetOption(
   171                     Object getval = c.getOption((SocketOption)test.option);
   159                     ServerSocket.class, c, test.option
   172                     Object legacyget = legacyGetOption(
   160                 );
   173                         ServerSocket.class, c, test.option
   161                 if (!getval.equals(legacyget)) {
   174                     );
   162                     Formatter f = new Formatter();
   175                     if (!getval.equals(legacyget)) {
   163                     f.format("SS Err %d: %s/%s", i, getval, legacyget);
   176                         Formatter f = new Formatter();
   164                     throw new RuntimeException(f.toString());
   177                         f.format("SS Err %d: %s/%s", i, getval, legacyget);
       
   178                         throw new RuntimeException(f.toString());
       
   179                     }
   165                 }
   180                 }
   166             }
   181             }
   167         }
   182         }
   168     }
   183     }
   169 
   184 
   172 
   187 
   173         throws Exception
   188         throws Exception
   174     {
   189     {
   175         if (type.equals(Socket.class)) {
   190         if (type.equals(Socket.class)) {
   176             Socket socket = (Socket)s;
   191             Socket socket = (Socket)s;
       
   192             Set<SocketOption<?>> options = socket.supportedOptions();
       
   193             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   177 
   194 
   178             if (option.equals(StandardSocketOptions.SO_KEEPALIVE)) {
   195             if (option.equals(StandardSocketOptions.SO_KEEPALIVE)) {
   179                 return Boolean.valueOf(socket.getKeepAlive());
   196                 return Boolean.valueOf(socket.getKeepAlive());
   180             } else if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
   197             } else if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
   181                 return Integer.valueOf(socket.getSendBufferSize());
   198                 return Integer.valueOf(socket.getSendBufferSize());
   182             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
   199             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
   183                 return Integer.valueOf(socket.getReceiveBufferSize());
   200                 return Integer.valueOf(socket.getReceiveBufferSize());
   184             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
   201             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
   185                 return Boolean.valueOf(socket.getReuseAddress());
   202                 return Boolean.valueOf(socket.getReuseAddress());
       
   203             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
       
   204                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
   186             } else if (option.equals(StandardSocketOptions.SO_LINGER)) {
   205             } else if (option.equals(StandardSocketOptions.SO_LINGER)) {
   187                 return Integer.valueOf(socket.getSoLinger());
   206                 return Integer.valueOf(socket.getSoLinger());
   188             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   207             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   189                 return Integer.valueOf(socket.getTrafficClass());
   208                 return Integer.valueOf(socket.getTrafficClass());
   190             } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
   209             } else if (option.equals(StandardSocketOptions.TCP_NODELAY)) {
   192             } else {
   211             } else {
   193                 throw new RuntimeException("unexecpted socket option");
   212                 throw new RuntimeException("unexecpted socket option");
   194             }
   213             }
   195         } else if  (type.equals(ServerSocket.class)) {
   214         } else if  (type.equals(ServerSocket.class)) {
   196             ServerSocket socket = (ServerSocket)s;
   215             ServerSocket socket = (ServerSocket)s;
       
   216             Set<SocketOption<?>> options = socket.supportedOptions();
       
   217             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
       
   218 
   197             if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
   219             if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
   198                 return Integer.valueOf(socket.getReceiveBufferSize());
   220                 return Integer.valueOf(socket.getReceiveBufferSize());
   199             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
   221             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
   200                 return Boolean.valueOf(socket.getReuseAddress());
   222                 return Boolean.valueOf(socket.getReuseAddress());
       
   223             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
       
   224                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
   201             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   225             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   202                 return Integer.valueOf(jdk.net.Sockets.getOption(
   226                 return Integer.valueOf(jdk.net.Sockets.getOption(
   203                     socket, StandardSocketOptions.IP_TOS));
   227                     socket, StandardSocketOptions.IP_TOS));
   204             } else {
   228             } else {
   205                 throw new RuntimeException("unexecpted socket option");
   229                 throw new RuntimeException("unexecpted socket option");
   206             }
   230             }
   207         } else if  (type.equals(DatagramSocket.class)) {
   231         } else if  (type.equals(DatagramSocket.class)) {
   208             DatagramSocket socket = (DatagramSocket)s;
   232             DatagramSocket socket = (DatagramSocket)s;
       
   233             Set<SocketOption<?>> options = socket.supportedOptions();
       
   234             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   209 
   235 
   210             if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
   236             if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
   211                 return Integer.valueOf(socket.getSendBufferSize());
   237                 return Integer.valueOf(socket.getSendBufferSize());
   212             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
   238             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
   213                 return Integer.valueOf(socket.getReceiveBufferSize());
   239                 return Integer.valueOf(socket.getReceiveBufferSize());
   214             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
   240             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
   215                 return Boolean.valueOf(socket.getReuseAddress());
   241                 return Boolean.valueOf(socket.getReuseAddress());
       
   242             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
       
   243                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
   216             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   244             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   217                 return Integer.valueOf(socket.getTrafficClass());
   245                 return Integer.valueOf(socket.getTrafficClass());
   218             } else {
   246             } else {
   219                 throw new RuntimeException("unexecpted socket option");
   247                 throw new RuntimeException("unexecpted socket option");
   220             }
   248             }
   221 
   249 
   222         } else if  (type.equals(MulticastSocket.class)) {
   250         } else if  (type.equals(MulticastSocket.class)) {
   223             MulticastSocket socket = (MulticastSocket)s;
   251             MulticastSocket socket = (MulticastSocket)s;
       
   252             Set<SocketOption<?>> options = socket.supportedOptions();
       
   253             boolean reuseport = options.contains(StandardSocketOptions.SO_REUSEPORT);
   224 
   254 
   225             if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
   255             if (option.equals(StandardSocketOptions.SO_SNDBUF)) {
   226                 return Integer.valueOf(socket.getSendBufferSize());
   256                 return Integer.valueOf(socket.getSendBufferSize());
   227             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
   257             } else if (option.equals(StandardSocketOptions.SO_RCVBUF)) {
   228                 return Integer.valueOf(socket.getReceiveBufferSize());
   258                 return Integer.valueOf(socket.getReceiveBufferSize());
   229             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
   259             } else if (option.equals(StandardSocketOptions.SO_REUSEADDR)) {
   230                 return Boolean.valueOf(socket.getReuseAddress());
   260                 return Boolean.valueOf(socket.getReuseAddress());
       
   261             } else if (option.equals(StandardSocketOptions.SO_REUSEPORT) && reuseport) {
       
   262                 return Boolean.valueOf(socket.getOption(StandardSocketOptions.SO_REUSEPORT));
   231             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   263             } else if (option.equals(StandardSocketOptions.IP_TOS)) {
   232                 return Integer.valueOf(socket.getTrafficClass());
   264                 return Integer.valueOf(socket.getTrafficClass());
   233             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_IF)) {
   265             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_IF)) {
   234                 return socket.getNetworkInterface();
   266                 return socket.getNetworkInterface();
   235             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {
   267             } else if (option.equals(StandardSocketOptions.IP_MULTICAST_TTL)) {