1 /*
   2  * Copyright (c) 2014, 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 /*
  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.nio.channels.*;
  34 import java.util.concurrent.*;
  35 import jdk.net.*;
  36 
  37 public class Test {
  38 
  39     static boolean security;
  40     static boolean success;
  41 
  42     interface Runner {
  43         public void run() throws Exception;
  44     }
  45 
  46     public static void main(String[] args) throws Exception {
  47         
  48         // quick check to see if supportedOptions() working before
  49         // creating any sockets and libnet loaded
  50 
  51         Sockets.supportedOptions(Socket.class);
  52 
  53         security = System.getSecurityManager() != null;
  54         success = security && 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         System.out.println ("Security Manager enabled: " + security);
  62         if (security) {
  63             System.out.println ("Success expected: " + success);
  64         }
  65 
  66         final SocketFlow flowIn = SocketFlow.create()
  67             .bandwidth(1000)
  68             .priority(SocketFlow.HIGH_PRIORITY);
  69 
  70         ServerSocket ss = new ServerSocket(0);
  71         int tcp_port = ss.getLocalPort();
  72         final InetAddress loop = InetAddress.getByName("127.0.0.1");
  73         final InetSocketAddress loopad = new InetSocketAddress(loop, tcp_port);
  74 
  75         DatagramSocket dg = new DatagramSocket(0);
  76         final int udp_port = dg.getLocalPort();
  77 
  78         final Socket s = new Socket("127.0.0.1", tcp_port);
  79         final SocketChannel sc = SocketChannel.open();
  80         sc.connect (new InetSocketAddress("127.0.0.1", tcp_port));
  81 
  82         doTest(()->{
  83             Sockets.setOption(s, ExtendedSocketOptions.SO_FLOW_SLA, flowIn); 
  84         });
  85         doTest(()->{
  86             Sockets.getOption(s, ExtendedSocketOptions.SO_FLOW_SLA);
  87         });
  88         doTest(()->{
  89             sc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
  90         });
  91         doTest(()->{
  92             sc.getOption(ExtendedSocketOptions.SO_FLOW_SLA);
  93         });
  94         doTest(()->{
  95             DatagramSocket dg1 = new DatagramSocket(0);
  96             dg1.connect(loop, udp_port);
  97             Sockets.setOption(dg1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
  98         });
  99         doTest(()->{
 100             DatagramChannel dg2 = DatagramChannel.open();
 101             dg2.bind(new InetSocketAddress(loop, 0));
 102             dg2.connect(new InetSocketAddress(loop, udp_port));
 103             dg2.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
 104         });
 105         doTest(()->{
 106             MulticastSocket mc1 = new MulticastSocket(0);
 107             mc1.connect(loop, udp_port);
 108             Sockets.setOption(mc1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
 109         });
 110         doTest(()->{
 111             AsynchronousSocketChannel asc = AsynchronousSocketChannel.open();
 112             Future<Void> f = asc.connect(loopad);
 113             f.get();
 114             asc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
 115         });
 116     }
 117 
 118     static void doTest(Runner func) throws Exception {
 119         try {
 120             func.run();
 121             if (security && !success) {
 122                 throw new RuntimeException("Test failed");
 123             }
 124         } catch (SecurityException e) {
 125             if (success) {
 126                 throw new RuntimeException("Test failed");
 127             }
 128         } catch (UnsupportedOperationException e) {}
 129     }
 130 }