test/jdk/java/net/MulticastSocket/MulticastAddresses.java
changeset 57846 01d9a1cff83a
parent 47216 71c04702a3d5
equal deleted inserted replaced
57845:db6829c1cc3a 57846:01d9a1cff83a
     1 /*
     1 /*
     2  * Copyright (c) 2001, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    20  * or visit www.oracle.com if you need additional information or have any
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  *
    25  * @test
    26  * @bug 4488458
    26  * @bug 4488458
       
    27  * @library /test/lib
    27  * @summary Test that MutlicastSocket.joinGroup is working for
    28  * @summary Test that MutlicastSocket.joinGroup is working for
    28  *          various multicast and non-multicast addresses.
    29  *          various multicast and non-multicast addresses.
    29  */
    30  */
       
    31 
       
    32 import jdk.test.lib.NetworkConfiguration;
       
    33 
    30 import java.net.*;
    34 import java.net.*;
    31 import java.util.Enumeration;
       
    32 import java.io.IOException;
    35 import java.io.IOException;
       
    36 import java.util.stream.Collectors;
    33 
    37 
    34 public class MulticastAddresses {
    38 public class MulticastAddresses {
    35 
    39     public static void runTest(NetworkInterface ni,
    36     public static void main(String args[]) throws Exception {
    40                                String[] multicasts,
    37 
    41                                String[] nonMulticasts) throws Exception {
    38         boolean ipv6_available = false;
       
    39         NetworkInterface ni = null;
       
    40 
       
    41         /*
       
    42          * Examine the network interfaces and determine :-
       
    43          *
       
    44          * 1. If host has IPv6 support
       
    45          * 2. Get reference to a non-loopback interface
       
    46          */
       
    47         Enumeration nifs = NetworkInterface.getNetworkInterfaces();
       
    48         while (nifs.hasMoreElements()) {
       
    49             NetworkInterface this_ni = (NetworkInterface)nifs.nextElement();
       
    50 
       
    51             Enumeration addrs = this_ni.getInetAddresses();
       
    52             while (addrs.hasMoreElements()) {
       
    53                 InetAddress addr = (InetAddress)addrs.nextElement();
       
    54                 if (addr instanceof Inet6Address) {
       
    55                     ipv6_available = true;
       
    56                 }
       
    57 
       
    58                 if (!addr.isLoopbackAddress() && ni == null) {
       
    59                     ni = this_ni;
       
    60                 }
       
    61             }
       
    62 
       
    63             if (ipv6_available) {
       
    64                 break;
       
    65             }
       
    66         }
       
    67 
       
    68         int failures = 0;
    42         int failures = 0;
    69 
       
    70         String multicasts[] = {
       
    71                 "224.80.80.80",
       
    72                 "ff01::1",
       
    73                 "ff02::1234",
       
    74                 "ff05::a",
       
    75                 "ff0e::1234:a" };
       
    76 
       
    77         String non_multicasts[] = {
       
    78                 "129.1.1.1",
       
    79                 "::1",
       
    80                 "::129.1.1.1",
       
    81                 "fe80::a00:20ff:fee5:bc02" };
       
    82 
    43 
    83         MulticastSocket s = new MulticastSocket();
    44         MulticastSocket s = new MulticastSocket();
    84 
    45 
    85         /* test valid multicast addresses */
    46         /* test valid multicast addresses */
       
    47         for (int i = 0; i < multicasts.length; i++) {
       
    48             InetAddress ia = InetAddress.getByName(multicasts[i]);
    86 
    49 
    87         for (int i=0; i<multicasts.length; i++) {
    50             System.out.println("Test: " + ia + " " + " ni: " + ni);
    88             InetAddress ia = InetAddress.getByName(multicasts[i]);
       
    89             if (ia instanceof Inet6Address && !ipv6_available) {
       
    90                 continue;
       
    91             }
       
    92 
       
    93             System.out.println("Test: " + ia);
       
    94 
       
    95             try {
    51             try {
    96 
    52 
    97                 System.out.print("    joinGroup(InetAddress) ");
    53                 System.out.print("    joinGroup(InetAddress) ");
    98                 s.joinGroup(ia);
    54                 s.joinGroup(ia);
    99                 s.leaveGroup(ia);
    55                 s.leaveGroup(ia);
   100                 System.out.println("    Passed.");
    56                 System.out.println("    Passed.");
   101 
    57 
   102                 System.out.print("    joinGroup(InetAddress,NetworkInterface) ");
    58                 System.out.print("    joinGroup(InetAddress,NetworkInterface) ");
   103                 s.joinGroup(new InetSocketAddress(ia,0), ni);
    59                 s.joinGroup(new InetSocketAddress(ia, 0), ni);
   104                 s.leaveGroup(new InetSocketAddress(ia,0), ni);
    60                 s.leaveGroup(new InetSocketAddress(ia, 0), ni);
   105                 System.out.println("    Passed.");
    61                 System.out.println("    Passed.");
   106             } catch (IOException e) {
    62             } catch (IOException e) {
   107                 failures++;
    63                 failures++;
   108                 System.out.println("Failed: " + e.getMessage());
    64                 System.out.println("Failed: " + e.getMessage());
   109             }
    65             }
   110 
    66 
   111         }
    67         }
   112 
    68 
   113         /* test non-multicast addresses */
    69         /* test non-multicast addresses */
   114 
    70         for (int i = 0; i < nonMulticasts.length; i++) {
   115         for (int i=0; i<non_multicasts.length; i++) {
    71             InetAddress ia = InetAddress.getByName(nonMulticasts[i]);
   116             InetAddress ia = InetAddress.getByName(non_multicasts[i]);
       
   117             if (ia instanceof Inet6Address && !ipv6_available) {
       
   118                 continue;
       
   119             }
       
   120 
       
   121             boolean failed = false;
    72             boolean failed = false;
   122 
    73 
   123             System.out.println("Test: " + ia + " ");
    74             System.out.println("Test: " + ia + " ");
   124             try {
    75             try {
   125                 System.out.println("    joinGroup(InetAddress) ");
    76                 System.out.println("    joinGroup(InetAddress) ");
   128                 System.out.println("Failed!! -- incorrectly joined group");
    79                 System.out.println("Failed!! -- incorrectly joined group");
   129                 failed = true;
    80                 failed = true;
   130             } catch (IOException e) {
    81             } catch (IOException e) {
   131                 System.out.println("    Passed: " + e.getMessage());
    82                 System.out.println("    Passed: " + e.getMessage());
   132             }
    83             }
   133 
       
   134             if (failed) {
    84             if (failed) {
   135                 s.leaveGroup(ia);
    85                 s.leaveGroup(ia);
   136                 failures++;
    86                 failures++;
   137             }
    87             }
   138         }
    88         }
   139 
       
   140         /* done */
       
   141 
       
   142         s.close();
    89         s.close();
   143 
       
   144         if (failures > 0) {
    90         if (failures > 0) {
   145             throw new Exception(failures + " test(s) failed - see log file.");
    91             throw new Exception(failures + " test(s) failed - see log file.");
   146         }
    92         }
   147     }
    93     }
   148 
    94 
       
    95 
       
    96     public static void main(String args[]) throws Exception {
       
    97 
       
    98         String[] multicastIPv4 = {
       
    99                 "224.80.80.80",
       
   100         };
       
   101         String[] multicastIPv6 = {
       
   102                 "ff01::1",
       
   103                 "ff02::1234",
       
   104                 "ff05::a",
       
   105                 "ff0e::1234:a"};
       
   106 
       
   107         String[] nonMulticastIPv4 = {
       
   108                 "129.1.1.1"
       
   109         };
       
   110 
       
   111         String[] nonMulticastIPv6 = {
       
   112                 "::1",
       
   113                 "::129.1.1.1",
       
   114                 "fe80::a00:20ff:fee5:bc02"};
       
   115 
       
   116         /*
       
   117          * Examine the network interfaces and determine :-
       
   118          *
       
   119          * 1. If host has IPv6 support
       
   120          * 2. Get reference to a non-loopback interface
       
   121          */
       
   122         NetworkConfiguration nc = NetworkConfiguration.probe();
       
   123         var ipv6List = nc.ip6MulticastInterfaces(false)
       
   124                 .collect(Collectors.toList());
       
   125 
       
   126         var ipv4List = nc.ip4MulticastInterfaces(false)
       
   127                 .collect(Collectors.toList());
       
   128 
       
   129         if (ipv6List.retainAll(ipv4List)) {
       
   130             runTest(ipv6List.get(0), multicastIPv4, nonMulticastIPv4);
       
   131             runTest(ipv6List.get(0), multicastIPv6, nonMulticastIPv6);
       
   132         } else {
       
   133             if (!ipv4List.isEmpty())
       
   134                 runTest(ipv4List.get(0), multicastIPv4, nonMulticastIPv4);
       
   135             if (!ipv6List.isEmpty())
       
   136                 runTest(ipv6List.get(0), multicastIPv6, nonMulticastIPv6);
       
   137         }
       
   138     }
   149 }
   139 }