jdk/test/jdk/net/Sockets/Test.java
author michaelm
Sat, 12 Apr 2014 20:21:09 +0100
changeset 23879 284802a2d355
child 24962 a6927b2a7315
permissions -rw-r--r--
8036979: Support java.net.SocketOption<> in java.net socket types Reviewed-by: alanb, chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
23879
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     1
/*
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     4
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     7
 * published by the Free Software Foundation.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     8
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    13
 * accompanied this code).
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    14
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    18
 *
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    21
 * questions.
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    22
 */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    23
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    24
/*
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    25
 * @test
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    26
 * @bug 8032808
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    27
 * @run main/othervm -Xcheck:jni Test
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    28
 * @run main/othervm/policy=policy.fail -Xcheck:jni Test fail
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    29
 * @run main/othervm/policy=policy.success -Xcheck:jni Test success
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    30
 */
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    31
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    32
import java.net.*;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    33
import java.nio.channels.*;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    34
import java.util.concurrent.*;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    35
import jdk.net.*;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    36
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    37
public class Test {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    38
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    39
    static boolean security;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    40
    static boolean success;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    41
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    42
    interface Runner {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    43
        public void run() throws Exception;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    44
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    45
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    46
    public static void main(String[] args) throws Exception {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    47
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    48
        // quick check to see if supportedOptions() working before
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    49
        // creating any sockets and libnet loaded
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    50
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    51
        Sockets.supportedOptions(Socket.class);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    52
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    53
        security = System.getSecurityManager() != null;
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    54
        success = security && args[0].equals("success");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    55
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    56
        // Main thing is to check for JNI problems
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    57
        // Doesn't matter if current system does not support the option
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    58
        // and currently setting the option with the loopback interface
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    59
        // doesn't work either
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    60
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    61
        System.out.println ("Security Manager enabled: " + security);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    62
        if (security) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    63
            System.out.println ("Success expected: " + success);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    64
        }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    65
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    66
        final SocketFlow flowIn = SocketFlow.create()
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    67
            .bandwidth(1000)
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    68
            .priority(SocketFlow.HIGH_PRIORITY);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    69
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    70
        ServerSocket ss = new ServerSocket(0);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    71
        int tcp_port = ss.getLocalPort();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    72
        final InetAddress loop = InetAddress.getByName("127.0.0.1");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    73
        final InetSocketAddress loopad = new InetSocketAddress(loop, tcp_port);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    74
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    75
        DatagramSocket dg = new DatagramSocket(0);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    76
        final int udp_port = dg.getLocalPort();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    77
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    78
        final Socket s = new Socket("127.0.0.1", tcp_port);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    79
        final SocketChannel sc = SocketChannel.open();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    80
        sc.connect (new InetSocketAddress("127.0.0.1", tcp_port));
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    81
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    82
        doTest(()->{
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    83
            Sockets.setOption(s, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    84
        });
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    85
        doTest(()->{
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    86
            Sockets.getOption(s, ExtendedSocketOptions.SO_FLOW_SLA);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    87
        });
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    88
        doTest(()->{
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    89
            sc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    90
        });
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    91
        doTest(()->{
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    92
            sc.getOption(ExtendedSocketOptions.SO_FLOW_SLA);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    93
        });
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    94
        doTest(()->{
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    95
            DatagramSocket dg1 = new DatagramSocket(0);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    96
            dg1.connect(loop, udp_port);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    97
            Sockets.setOption(dg1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    98
        });
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
    99
        doTest(()->{
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   100
            DatagramChannel dg2 = DatagramChannel.open();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   101
            dg2.bind(new InetSocketAddress(loop, 0));
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   102
            dg2.connect(new InetSocketAddress(loop, udp_port));
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   103
            dg2.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   104
        });
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   105
        doTest(()->{
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   106
            MulticastSocket mc1 = new MulticastSocket(0);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   107
            mc1.connect(loop, udp_port);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   108
            Sockets.setOption(mc1, ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   109
        });
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   110
        doTest(()->{
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   111
            AsynchronousSocketChannel asc = AsynchronousSocketChannel.open();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   112
            Future<Void> f = asc.connect(loopad);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   113
            f.get();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   114
            asc.setOption(ExtendedSocketOptions.SO_FLOW_SLA, flowIn);
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   115
        });
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   116
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   117
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   118
    static void doTest(Runner func) throws Exception {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   119
        try {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   120
            func.run();
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   121
            if (security && !success) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   122
                throw new RuntimeException("Test failed");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   123
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   124
        } catch (SecurityException e) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   125
            if (success) {
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   126
                throw new RuntimeException("Test failed");
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   127
            }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   128
        } catch (UnsupportedOperationException e) {}
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   129
    }
284802a2d355 8036979: Support java.net.SocketOption<> in java.net socket types
michaelm
parents:
diff changeset
   130
}