test/jdk/net/Sockets/Test.java

Print this page
rev 14282 : 8044773: Refactor jdk.net API so that it can be moved out of the base module
Reviewed-by:


   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 /*
  25  * @test
  26  * @bug 8032808
  27  * @run main/othervm -Xcheck:jni Test

  28  * @run main/othervm/policy=policy.fail -Xcheck:jni Test fail
  29  * @run main/othervm/policy=policy.success -Xcheck:jni Test success
  30  */
  31 
  32 import java.net.*;
  33 import java.io.IOException;
  34 import java.nio.channels.*;
  35 import java.util.concurrent.*;
  36 import java.util.Set;
  37 import jdk.net.*;

  38 
  39 public class Test {
  40 
  41     static boolean security;
  42     static boolean success;
  43 
  44     interface Runner {
  45         public void run() throws Exception;
  46     }
  47 
  48     public static void main(String[] args) throws Exception {
  49 
  50         // quick check to see if supportedOptions() working before
  51         // creating any sockets and libnet loaded
  52 
  53         Sockets.supportedOptions(Socket.class);
  54 
  55         security = System.getSecurityManager() != null;
  56         success = security && args[0].equals("success");
  57 
  58         // Main thing is to check for JNI problems
  59         // Doesn't matter if current system does not support the option
  60         // and currently setting the option with the loopback interface
  61         // doesn't work either
  62 
  63         System.out.println ("Security Manager enabled: " + security);
  64         if (security) {
  65             System.out.println ("Success expected: " + success);
  66         }
  67 
  68         final SocketFlow flowIn = SocketFlow.create()
  69             .bandwidth(1000)
  70             .priority(SocketFlow.HIGH_PRIORITY);
  71 
  72         ServerSocket ss = new ServerSocket(0);


  73         int tcp_port = ss.getLocalPort();
  74         final InetAddress loop = InetAddress.getByName("127.0.0.1");
  75         final InetSocketAddress loopad = new InetSocketAddress(loop, tcp_port);
  76 
  77         DatagramSocket dg = new DatagramSocket(0);
  78         final int udp_port = dg.getLocalPort();
  79 
  80         // If option not available, end test
  81         Set<SocketOption<?>> options = dg.supportedOptions();
  82         if (!options.contains(ExtendedSocketOptions.SO_FLOW_SLA)) {
  83             System.out.println("SO_FLOW_SLA not supported");
  84             return;
  85         }
  86 
  87         final Socket s = new Socket("127.0.0.1", tcp_port);
  88         final SocketChannel sc = SocketChannel.open();
  89         sc.connect (new InetSocketAddress("127.0.0.1", tcp_port));
  90 
  91         doTest(()->{

  92             Sockets.setOption(s, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);

  93         });
  94         doTest(()->{
  95             Sockets.getOption(s, ExtendedSocketOptions.SO_FLOW_SLA);

  96         });
  97         doTest(()->{
  98             sc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
  99         });
 100         doTest(()->{
 101             sc.getOption(ExtendedSocketOptions.SO_FLOW_SLA);
 102         });
 103         doTest(()->{
 104             DatagramSocket dg1 = new DatagramSocket(0);
 105             dg1.connect(loop, udp_port);
 106             Sockets.setOption(dg1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);

 107         });
 108         doTest(()->{
 109             DatagramChannel dg2 = DatagramChannel.open();
 110             dg2.bind(new InetSocketAddress(loop, 0));
 111             dg2.connect(new InetSocketAddress(loop, udp_port));
 112             dg2.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);

 113         });
 114         doTest(()->{
 115             MulticastSocket mc1 = new MulticastSocket(0);
 116             mc1.connect(loop, udp_port);
 117             Sockets.setOption(mc1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);

 118         });
 119         doTest(()->{
 120             AsynchronousSocketChannel asc = AsynchronousSocketChannel.open();
 121             Future<Void> f = asc.connect(loopad);
 122             f.get();
 123             asc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);

 124         });
 125     }

 126 
 127     static void doTest(Runner func) throws Exception {

 128         try {
 129             func.run();
 130             if (security && !success) {
 131                 throw new RuntimeException("Test failed");


 132             }
 133         } catch (SecurityException e) {
 134             if (success) {
 135                 throw new RuntimeException("Test failed");


 136             }
 137         } catch (UnsupportedOperationException e) {
 138             System.out.println (e);
 139         } catch (IOException e) {
 140             // Probably a permission error, but we're not
 141             // going to check unless a specific permission exception
 142             // is defined.
 143             System.out.println (e);
 144         }
 145     }
 146 }


   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 /*
  25  * @test
  26  * @bug 8032808 8044773
  27  * @modules jdk.net
  28  * @run main/othervm -Xcheck:jni Test success
  29  * @run main/othervm/policy=policy.fail -Xcheck:jni Test fail
  30  * @run main/othervm/policy=policy.success -Xcheck:jni Test success
  31  */
  32 
  33 import java.net.*;
  34 import java.io.IOException;
  35 import java.nio.channels.*;
  36 import java.util.concurrent.*;
  37 import java.util.Set;
  38 import jdk.net.*;
  39 import static java.lang.System.out;
  40 
  41 public class Test {
  42 
  43     interface Runner { void run() throws Exception; }

  44 
  45     static boolean expectSuccess;


  46 
  47     public static void main(String[] args) throws Exception {
  48 
  49         // quick check to see if supportedOptions() working before
  50         // creating any sockets and libnet loaded
  51 
  52         Sockets.supportedOptions(Socket.class);
  53 
  54         expectSuccess = args[0].equals("success");

  55 
  56         // Main thing is to check for JNI problems
  57         // Doesn't matter if current system does not support the option
  58         // and currently setting the option with the loopback interface
  59         // doesn't work either
  60 
  61         boolean sm = System.getSecurityManager() != null;
  62         out.println("Security Manager enabled: " + sm);
  63         out.println("Success expected: " + expectSuccess);

  64 
  65         SocketFlow flowIn = SocketFlow.create()
  66                                       .bandwidth(1000)
  67                                       .priority(SocketFlow.HIGH_PRIORITY);
  68 
  69         try (ServerSocket ss = new ServerSocket(0);
  70              DatagramSocket dg = new DatagramSocket(0)) {
  71 
  72             int tcp_port = ss.getLocalPort();
  73             final InetAddress loop = InetAddress.getByName("127.0.0.1");
  74             final InetSocketAddress loopad = new InetSocketAddress(loop, tcp_port);
  75 

  76             final int udp_port = dg.getLocalPort();
  77 
  78             // If option not available, end test
  79             Set<SocketOption<?>> options = dg.supportedOptions();
  80             if (!options.contains(ExtendedSocketOptions.SO_FLOW_SLA)) {
  81                 System.out.println("SO_FLOW_SLA not supported");
  82                 return;
  83             }
  84 
  85             final Socket s = new Socket("127.0.0.1", tcp_port);
  86             final SocketChannel sc = SocketChannel.open();
  87             sc.connect(new InetSocketAddress("127.0.0.1", tcp_port));
  88 
  89             doTest("Sockets.setOption Socket", () -> {
  90                 out.println(flowIn);
  91                 Sockets.setOption(s, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
  92                 out.println(flowIn);
  93             });
  94             doTest("Sockets.getOption Socket",() -> {
  95                 Sockets.getOption(s, ExtendedSocketOptions.SO_FLOW_SLA);
  96                 out.println(flowIn);
  97             });
  98             doTest("Sockets.setOption SocketChannel",() ->
  99                 sc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn)
 100             );
 101             doTest("Sockets.getOption SocketChannel",() ->
 102                 sc.getOption(ExtendedSocketOptions.SO_FLOW_SLA)
 103             );
 104             doTest("Sockets.setOption DatagramSocket",() -> {
 105                 try (DatagramSocket dg1 = new DatagramSocket(0)) {
 106                     dg1.connect(loop, udp_port);
 107                     Sockets.setOption(dg1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
 108                 }
 109             });
 110             doTest("Sockets.setOption DatagramSocket 2", () -> {
 111                 try (DatagramChannel dg2 = DatagramChannel.open()) {
 112                     dg2.bind(new InetSocketAddress(loop, 0));
 113                     dg2.connect(new InetSocketAddress(loop, udp_port));
 114                     dg2.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
 115                 }
 116             });
 117             doTest("Sockets.setOption MulticastSocket", () -> {
 118                 try (MulticastSocket mc1 = new MulticastSocket(0)) {
 119                     mc1.connect(loop, udp_port);
 120                     Sockets.setOption(mc1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
 121                 }
 122             });
 123             doTest("Sockets.setOption AsynchronousSocketChannel", () -> {
 124                 try (AsynchronousSocketChannel asc = AsynchronousSocketChannel.open()) {
 125                     Future<Void> f = asc.connect(loopad);
 126                     f.get();
 127                     asc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
 128                 }
 129             });
 130         }
 131     }
 132 
 133     static void doTest(String message, Runner func) throws Exception {
 134         out.println(message);
 135         try {
 136             func.run();
 137             if (expectSuccess) {
 138                 out.println("Completed as expected");
 139             } else {
 140                 throw new RuntimeException("Operation succeeded, but expected SecurityException");
 141             }
 142         } catch (SecurityException e) {
 143             if (expectSuccess) {
 144                 throw new RuntimeException("Unexpected SecurityException", e);
 145             } else {
 146                 out.println("Caught expected: " + e);
 147             }
 148         } catch (UnsupportedOperationException e) {
 149             System.out.println(e);
 150         } catch (IOException e) {
 151             // Probably a permission error, but we're not
 152             // going to check unless a specific permission exception
 153             // is defined.
 154             System.out.println(e);
 155         }
 156     }
 157 }