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.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 }