test/jdk/java/net/MulticastSocket/MulticastAddresses.java
author aefimov
Thu, 22 Aug 2019 18:54:56 +0100
changeset 57846 01d9a1cff83a
parent 47216 71c04702a3d5
permissions -rw-r--r--
8078219: Verify lack of @test tag in files in java/net test directory Reviewed-by: alanb Contributed-by: Patrick Concannon <patrick.concannon@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
     2
 * Copyright (c) 2001, 2019, 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
/*
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    25
 * @test
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 4488458
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    27
 * @library /test/lib
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @summary Test that MutlicastSocket.joinGroup is working for
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          various multicast and non-multicast addresses.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 */
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    31
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    32
import jdk.test.lib.NetworkConfiguration;
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    33
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.IOException;
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    36
import java.util.stream.Collectors;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
public class MulticastAddresses {
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    39
    public static void runTest(NetworkInterface ni,
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    40
                               String[] multicasts,
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    41
                               String[] nonMulticasts) throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        int failures = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        MulticastSocket s = new MulticastSocket();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        /* test valid multicast addresses */
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    47
        for (int i = 0; i < multicasts.length; i++) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            InetAddress ia = InetAddress.getByName(multicasts[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    50
            System.out.println("Test: " + ia + " " + " ni: " + ni);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
                System.out.print("    joinGroup(InetAddress) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                s.joinGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
                s.leaveGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                System.out.println("    Passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                System.out.print("    joinGroup(InetAddress,NetworkInterface) ");
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    59
                s.joinGroup(new InetSocketAddress(ia, 0), ni);
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    60
                s.leaveGroup(new InetSocketAddress(ia, 0), ni);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                System.out.println("    Passed.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                System.out.println("Failed: " + e.getMessage());
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        /* test non-multicast addresses */
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    70
        for (int i = 0; i < nonMulticasts.length; i++) {
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    71
            InetAddress ia = InetAddress.getByName(nonMulticasts[i]);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            boolean failed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            System.out.println("Test: " + ia + " ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                System.out.println("    joinGroup(InetAddress) ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
                s.joinGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                System.out.println("Failed!! -- incorrectly joined group");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
                failed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            } catch (IOException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                System.out.println("    Passed: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            if (failed) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                s.leaveGroup(ia);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                failures++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        s.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        if (failures > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            throw new Exception(failures + " test(s) failed - see log file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
57846
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    95
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    96
    public static void main(String args[]) throws Exception {
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    97
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    98
        String[] multicastIPv4 = {
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
    99
                "224.80.80.80",
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   100
        };
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   101
        String[] multicastIPv6 = {
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   102
                "ff01::1",
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   103
                "ff02::1234",
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   104
                "ff05::a",
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   105
                "ff0e::1234:a"};
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   106
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   107
        String[] nonMulticastIPv4 = {
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   108
                "129.1.1.1"
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   109
        };
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   110
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   111
        String[] nonMulticastIPv6 = {
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   112
                "::1",
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   113
                "::129.1.1.1",
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   114
                "fe80::a00:20ff:fee5:bc02"};
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   115
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   116
        /*
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   117
         * Examine the network interfaces and determine :-
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   118
         *
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   119
         * 1. If host has IPv6 support
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   120
         * 2. Get reference to a non-loopback interface
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   121
         */
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   122
        NetworkConfiguration nc = NetworkConfiguration.probe();
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   123
        var ipv6List = nc.ip6MulticastInterfaces(false)
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   124
                .collect(Collectors.toList());
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   125
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   126
        var ipv4List = nc.ip4MulticastInterfaces(false)
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   127
                .collect(Collectors.toList());
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   128
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   129
        if (ipv6List.retainAll(ipv4List)) {
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   130
            runTest(ipv6List.get(0), multicastIPv4, nonMulticastIPv4);
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   131
            runTest(ipv6List.get(0), multicastIPv6, nonMulticastIPv6);
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   132
        } else {
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   133
            if (!ipv4List.isEmpty())
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   134
                runTest(ipv4List.get(0), multicastIPv4, nonMulticastIPv4);
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   135
            if (!ipv6List.isEmpty())
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   136
                runTest(ipv6List.get(0), multicastIPv6, nonMulticastIPv6);
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   137
        }
01d9a1cff83a 8078219: Verify lack of @test tag in files in java/net test directory
aefimov
parents: 47216
diff changeset
   138
    }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
}