test/jdk/java/net/DatagramSocket/LocalSocketAddress.java
author pconcannon
Wed, 27 Nov 2019 16:01:34 +0000
changeset 59299 17d242844fc9
parent 47216 71c04702a3d5
permissions -rw-r--r--
8233018: Add a new test to verify that DatagramSocket is not interruptible Summary: Test added to check the interruptability of DatagramSocket, MulticastSocket and DatagramSocketAdaptor. Reviewed-by: chegar, dfuchs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5456
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5456
diff changeset
     2
 * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
5456
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
     4
 *
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
     8
 *
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    13
 * accompanied this code).
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    14
 *
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5456
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5456
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 5456
diff changeset
    21
 * questions.
5456
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    22
 */
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    23
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    24
/* @test
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    25
 * @bug 6718504
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    26
 * @summary IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    27
 */
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    28
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    29
import java.net.DatagramSocket;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    30
import java.net.InetAddress;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    31
import java.net.Inet6Address;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    32
import java.net.NetworkInterface;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    33
import java.net.SocketException;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    34
import java.util.*;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    35
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    36
public class LocalSocketAddress  {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    37
    public static void main(String[] args) throws SocketException {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    38
        InetAddress IPv6LoopbackAddr = null;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    39
        DatagramSocket soc = null;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    40
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    41
        try {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    42
            List<NetworkInterface> nics = Collections.list(NetworkInterface.getNetworkInterfaces());
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    43
            for (NetworkInterface nic : nics) {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    44
                if (!nic.isLoopback())
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    45
                    continue;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    46
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    47
                List<InetAddress> addrs = Collections.list(nic.getInetAddresses());
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    48
                for (InetAddress addr : addrs) {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    49
                    if (addr instanceof Inet6Address) {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    50
                        IPv6LoopbackAddr = addr;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    51
                        break;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    52
                    }
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    53
                }
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    54
            }
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    55
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    56
            if (IPv6LoopbackAddr == null) {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    57
                System.out.println("IPv6 is not available, exiting test.");
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    58
                return;
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    59
            }
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    60
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    61
            soc = new DatagramSocket(0, IPv6LoopbackAddr);
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    62
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    63
            if (!IPv6LoopbackAddr.equals(soc.getLocalAddress())) {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    64
                throw new RuntimeException("Bound address is " + soc.getLocalAddress() +
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    65
                                   ", but should be " + IPv6LoopbackAddr);
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    66
            }
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    67
        }  finally {
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    68
            if (soc != null) { soc.close(); }
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    69
        }
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    70
    }
40ff273941a2 6718504: IN6_IS_ADDR_ANY tests only 12 bytes of 16-byte address
chegar
parents:
diff changeset
    71
}