jdk/test/com/oracle/net/Sanity.java
author weijun
Fri, 28 Feb 2014 08:40:27 +0800
changeset 23030 51178fbafe9c
parent 6525 56be41b86ef8
permissions -rw-r--r--
8035759: [parfait] JNI exception pending in jdk/src/windows/native/sun/security/krb5/NativeCreds.c Reviewed-by: valeriep
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6525
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     1
/*
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     4
 *
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     8
 *
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    13
 * accompanied this code).
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    14
 *
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    18
 *
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    21
 * questions.
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    22
 */
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    23
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    24
import com.oracle.net.Sdp;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    25
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    26
import java.net.*;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    27
import java.io.*;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    28
import java.nio.channels.*;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    29
import java.util.*;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    30
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    31
/**
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    32
 * Exercise com.oracle.net.Sdp with each IP address plumbed to InfiniBand
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    33
 * interfaces listed in a given file.
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    34
 */
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    35
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    36
public class Sanity {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    37
    public static void main(String[] args) throws Exception {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    38
        // The file is a list of interfaces to test.
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    39
        Scanner s = new Scanner(new File(args[0]));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    40
        try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    41
            while (s.hasNextLine()) {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    42
                String link = s.nextLine();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    43
                NetworkInterface ni = NetworkInterface.getByName(link);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    44
                if (ni != null) {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    45
                    Enumeration<InetAddress> addrs = ni.getInetAddresses();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    46
                    while (addrs.hasMoreElements()) {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    47
                        InetAddress addr = addrs.nextElement();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    48
                        System.out.format("Testing %s: %s\n", link, addr.getHostAddress());
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    49
                        test(addr);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    50
                    }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    51
                }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    52
            }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    53
        } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    54
            s.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    55
        }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    56
    }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    57
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    58
    static void test(InetAddress addr) throws Exception {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    59
        // Test SocketChannel and ServerSocketChannel
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    60
        ServerSocketChannel ssc = Sdp.openServerSocketChannel();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    61
        try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    62
            ssc.socket().bind(new InetSocketAddress(addr, 0));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    63
            int port = ssc.socket().getLocalPort();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    64
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    65
            // SocketChannel.connect (implicit bind)
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    66
            SocketChannel client = Sdp.openSocketChannel();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    67
            try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    68
                client.connect(new InetSocketAddress(addr, port));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    69
                SocketChannel peer = ssc.accept();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    70
                try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    71
                    testConnection(Channels.newOutputStream(client),
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    72
                                   Channels.newInputStream(peer));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    73
                } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    74
                    peer.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    75
                }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    76
            } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    77
                client.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    78
            }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    79
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    80
            // SocketChannel.connect (explicit bind)
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    81
            client = Sdp.openSocketChannel();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    82
            try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    83
                client.socket().bind(new InetSocketAddress(addr, 0));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    84
                client.connect(new InetSocketAddress(addr, port));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    85
                ssc.accept().close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    86
            } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    87
                client.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    88
            }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    89
        } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    90
            ssc.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    91
        }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    92
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    93
        // Test Socket and ServerSocket
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    94
        ServerSocket ss = Sdp.openServerSocket();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    95
        try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    96
            ss.bind(new InetSocketAddress(addr, 0));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    97
            int port = ss.getLocalPort();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    98
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
    99
            // Socket.connect (implicit bind)
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   100
            Socket s = Sdp.openSocket();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   101
            try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   102
                s.connect(new InetSocketAddress(addr, port));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   103
                Socket peer = ss.accept();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   104
                try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   105
                    testConnection(s.getOutputStream(), peer.getInputStream());
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   106
                } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   107
                    peer.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   108
                }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   109
            } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   110
                s.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   111
            }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   112
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   113
            // Socket.connect (explicit bind)
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   114
            s = Sdp.openSocket();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   115
            try {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   116
                s.bind(new InetSocketAddress(addr, 0));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   117
                s.connect(new InetSocketAddress(addr, port));
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   118
                ss.accept().close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   119
            } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   120
                s.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   121
            }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   122
        } finally {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   123
            ss.close();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   124
        }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   125
    }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   126
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   127
    static void testConnection(OutputStream out, InputStream in)
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   128
        throws IOException
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   129
    {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   130
        byte[] msg = "hello".getBytes();
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   131
        out.write(msg);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   132
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   133
        byte[] ba = new byte[100];
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   134
        int nread = 0;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   135
        while (nread < msg.length) {
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   136
            int n = in.read(ba);
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   137
            if (n < 0)
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   138
                throw new IOException("EOF not expected!");
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   139
            nread += n;
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   140
        }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   141
    }
56be41b86ef8 6965072: Need API to create SDP sockets
alanb
parents:
diff changeset
   142
}