jdk/test/java/net/MulticastSocket/MulticastAddresses.java
author dsamersoff
Tue, 13 Jul 2010 15:32:36 +0400
changeset 6108 d5bfa0bc6123
parent 5506 202f599c92aa
permissions -rw-r--r--
6964714: NetworkInterface getInetAddresses enumerates IPv6 addresses if java.net.preferIPvStack property set Summary: User can disable ipv6 explicitly, have to check it Reviewed-by: chegar, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4488458
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary Test that MutlicastSocket.joinGroup is working for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          various multicast and non-multicast addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.Enumeration;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class MulticastAddresses {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void main(String args[]) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        boolean ipv6_available = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        NetworkInterface ni = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        /*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
         * Examine the network interfaces and determine :-
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
         *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
         * 1. If host has IPv6 support
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
         * 2. Get reference to a non-loopback interface
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
         */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        Enumeration nifs = NetworkInterface.getNetworkInterfaces();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        while (nifs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            NetworkInterface this_ni = (NetworkInterface)nifs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            Enumeration addrs = this_ni.getInetAddresses();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            while (addrs.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                InetAddress addr = (InetAddress)addrs.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                if (addr instanceof Inet6Address) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                    ipv6_available = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                if (!addr.isLoopbackAddress() && ni == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                    ni = this_ni;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            if (ipv6_available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        String multicasts[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                "224.80.80.80",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                "ff01::1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                "ff02::1234",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                "ff05::a",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                "ff0e::1234:a" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String non_multicasts[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                "129.1.1.1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                "::1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                "::129.1.1.1",
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                "fe80::a00:20ff:fee5:bc02" };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        MulticastSocket s = new MulticastSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        /* test valid multicast addresses */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        for (int i=0; i<multicasts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            InetAddress ia = InetAddress.getByName(multicasts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            if (ia instanceof Inet6Address && !ipv6_available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            System.out.println("Test: " + ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                System.out.print("    joinGroup(InetAddress) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                s.joinGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                s.leaveGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                System.out.println("    Passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
                System.out.print("    joinGroup(InetAddress,NetworkInterface) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                s.joinGroup(new InetSocketAddress(ia,0), ni);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
                s.leaveGroup(new InetSocketAddress(ia,0), ni);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                System.out.println("    Passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
                System.out.println("Failed: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        /* test non-multicast addresses */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        for (int i=0; i<non_multicasts.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            InetAddress ia = InetAddress.getByName(non_multicasts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            if (ia instanceof Inet6Address && !ipv6_available) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
                continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            System.out.println("Test: " + ia + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
                System.out.println("    joinGroup(InetAddress) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                s.joinGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                System.out.println("Failed!! -- incorrectly joined group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                System.out.println("    Passed: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
                s.leaveGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        /* done */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new Exception(failures + " test(s) failed - see log file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}