Path: blob/master/test/jdk/java/net/MulticastSocket/MulticastAddresses.java
41149 views
/*1* Copyright (c) 2001, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 448845826* @library /test/lib27* @summary Test that MutlicastSocket.joinGroup is working for28* various multicast and non-multicast addresses.29* @run main MulticastAddresses30* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl MulticastAddresses31*/3233import jdk.test.lib.NetworkConfiguration;3435import java.net.*;36import java.io.IOException;37import java.util.stream.Collectors;3839public class MulticastAddresses {40public static void runTest(NetworkInterface ni,41String[] multicasts,42String[] nonMulticasts) throws Exception {43int failures = 0;4445MulticastSocket s = new MulticastSocket();4647/* test valid multicast addresses */48for (int i = 0; i < multicasts.length; i++) {49InetAddress ia = InetAddress.getByName(multicasts[i]);5051System.out.println("Test: " + ia + " " + " ni: " + ni);52try {5354System.out.print(" joinGroup(InetAddress) ");55s.joinGroup(ia);56s.leaveGroup(ia);57System.out.println(" Passed.");5859System.out.print(" joinGroup(InetAddress,NetworkInterface) ");60s.joinGroup(new InetSocketAddress(ia, 0), ni);61s.leaveGroup(new InetSocketAddress(ia, 0), ni);62System.out.println(" Passed.");63} catch (IOException e) {64failures++;65System.out.println("Failed: " + e.getMessage());66}6768}6970/* test non-multicast addresses */71for (int i = 0; i < nonMulticasts.length; i++) {72InetAddress ia = InetAddress.getByName(nonMulticasts[i]);73boolean failed = false;7475System.out.println("Test: " + ia + " ");76try {77System.out.println(" joinGroup(InetAddress) ");78s.joinGroup(ia);7980System.out.println("Failed!! -- incorrectly joined group");81failed = true;82} catch (IOException e) {83System.out.println(" Passed: " + e.getMessage());84}85if (failed) {86s.leaveGroup(ia);87failures++;88}89}90s.close();91if (failures > 0) {92throw new Exception(failures + " test(s) failed - see log file.");93}94}959697public static void main(String args[]) throws Exception {9899String[] multicastIPv4 = {100"224.80.80.80",101};102String[] multicastIPv6 = {103"ff01::1",104"ff02::1234",105"ff05::a",106"ff0e::1234:a"};107108String[] nonMulticastIPv4 = {109"129.1.1.1"110};111112String[] nonMulticastIPv6 = {113"::1",114"::129.1.1.1",115"fe80::a00:20ff:fee5:bc02"};116117/*118* Examine the network interfaces and determine :-119*120* 1. If host has IPv6 support121* 2. Get reference to a non-loopback interface122*/123NetworkConfiguration nc = NetworkConfiguration.probe();124var ipv6List = nc.ip6MulticastInterfaces(false)125.collect(Collectors.toList());126127var ipv4List = nc.ip4MulticastInterfaces(false)128.collect(Collectors.toList());129130if (ipv6List.retainAll(ipv4List)) {131runTest(ipv6List.get(0), multicastIPv4, nonMulticastIPv4);132runTest(ipv6List.get(0), multicastIPv6, nonMulticastIPv6);133} else {134if (!ipv4List.isEmpty())135runTest(ipv4List.get(0), multicastIPv4, nonMulticastIPv4);136if (!ipv6List.isEmpty())137runTest(ipv6List.get(0), multicastIPv6, nonMulticastIPv6);138}139}140}141142143