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