jdk/test/sun/net/sdp/Sanity.java
author ihse
Mon, 09 Mar 2015 10:39:16 +0100
changeset 29304 81a723f8d33c
parent 5506 202f599c92aa
permissions -rw-r--r--
8074096: Disable (most) native warnings in JDK on a per-library basis Reviewed-by: erikj, tbell
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
     2
 * Copyright (c) 2009, Oracle and/or its affiliates. All rights reserved.
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
     4
 *
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
     8
 *
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    13
 * accompanied this code).
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    14
 *
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2446
diff changeset
    21
 * questions.
2446
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    22
 */
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    23
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    24
import java.net.*;
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    25
import java.io.*;
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    26
import java.nio.channels.*;
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    27
import java.util.Enumeration;
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    28
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    29
/**
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    30
 * Sanity check Socket/ServerSocket and each of the stream-oriented channels
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    31
 * on each IP address plumbed to the network adapters.
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    32
 */
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    33
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    34
public class Sanity {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    35
    public static void main(String[] args) throws Exception {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    36
        Enumeration<NetworkInterface> nifs = NetworkInterface.getNetworkInterfaces();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    37
        while (nifs.hasMoreElements()) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    38
            NetworkInterface ni = nifs.nextElement();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    39
            Enumeration<InetAddress> addrs = ni.getInetAddresses();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    40
            while (addrs.hasMoreElements()) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    41
                InetAddress addr = addrs.nextElement();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    42
                test(addr);
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    43
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    44
        }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    45
    }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    46
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    47
    static void test(InetAddress addr) throws Exception {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    48
        System.out.println(addr.getHostAddress());
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    49
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    50
        // ServerSocketChannel.bind
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    51
        ServerSocketChannel ssc = ServerSocketChannel.open();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    52
        try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    53
            ssc.bind(new InetSocketAddress(addr, 0));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    54
            int port = ((InetSocketAddress)(ssc.getLocalAddress())).getPort();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    55
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    56
            // SocketChannel.connect (implicit bind)
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    57
            SocketChannel client = SocketChannel.open();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    58
            try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    59
                client.connect(new InetSocketAddress(addr, port));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    60
                SocketChannel peer = ssc.accept();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    61
                try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    62
                    testConnection(Channels.newOutputStream(client),
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    63
                                   Channels.newInputStream(peer));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    64
                } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    65
                    peer.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    66
                }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    67
            } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    68
                client.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    69
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    70
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    71
            // SocketChannel.connect (explicit bind)
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    72
            client = SocketChannel.open();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    73
            try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    74
                client.bind(new InetSocketAddress(addr, 0))
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    75
                  .connect(new InetSocketAddress(addr, port));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    76
                ssc.accept().close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    77
            } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    78
                client.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    79
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    80
        } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    81
            ssc.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    82
        }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    83
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    84
        // AsynchronousServerSocketChannel.bind
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    85
        AsynchronousServerSocketChannel server =
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    86
            AsynchronousServerSocketChannel.open();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    87
        try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    88
            server.bind(new InetSocketAddress(addr, 0));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    89
            int port = ((InetSocketAddress)(server.getLocalAddress())).getPort();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    90
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    91
            // AsynchronousSocketChannel.connect (implicit bind)
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    92
            AsynchronousSocketChannel client = AsynchronousSocketChannel.open();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    93
            try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    94
                client.connect(new InetSocketAddress(addr, port)).get();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    95
                AsynchronousSocketChannel peer = server.accept().get();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    96
                try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    97
                    testConnection(Channels.newOutputStream(client),
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    98
                                   Channels.newInputStream(peer));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
    99
                } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   100
                    peer.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   101
                }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   102
            } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   103
                client.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   104
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   105
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   106
            // AsynchronousSocketChannel.connect (explicit bind)
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   107
            client = AsynchronousSocketChannel.open();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   108
            try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   109
                client.bind(new InetSocketAddress(addr, 0))
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   110
                  .connect(new InetSocketAddress(addr, port)).get();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   111
                server.accept().get().close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   112
            } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   113
                client.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   114
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   115
        } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   116
            server.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   117
        }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   118
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   119
        // ServerSocket.bind
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   120
        ServerSocket ss = new ServerSocket();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   121
        try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   122
            ss.bind(new InetSocketAddress(addr, 0));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   123
            int port = ss.getLocalPort();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   124
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   125
            // Socket.connect (implicit bind)
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   126
            Socket s = new Socket();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   127
            try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   128
                s.connect(new InetSocketAddress(addr, port));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   129
                Socket peer = ss.accept();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   130
                try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   131
                    testConnection(s.getOutputStream(), peer.getInputStream());
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   132
                } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   133
                    peer.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   134
                }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   135
            } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   136
                s.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   137
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   138
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   139
            // Socket.connect (explicit bind)
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   140
            s = new Socket();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   141
            try {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   142
                s.bind(new InetSocketAddress(addr, 0));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   143
                s.connect(new InetSocketAddress(addr, port));
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   144
                ss.accept().close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   145
            } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   146
                s.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   147
            }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   148
        } finally {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   149
            ss.close();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   150
        }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   151
    }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   152
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   153
    static void testConnection(OutputStream out, InputStream in)
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   154
        throws IOException
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   155
    {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   156
        byte[] msg = "hello".getBytes();
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   157
        out.write(msg);
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   158
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   159
        byte[] ba = new byte[100];
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   160
        int nread = 0;
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   161
        while (nread < msg.length) {
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   162
            int n = in.read(ba);
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   163
            if (n < 0)
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   164
                throw new IOException("EOF not expected!");
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   165
            nread += n;
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   166
        }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   167
    }
07047237e4d4 4890703: Support SDP (sol)
alanb
parents:
diff changeset
   168
}