Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/MulticastAddresses.java
38812 views
/*1* Copyright (c) 2001, 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*25* @bug 448845826* @summary Test that MutlicastSocket.joinGroup is working for27* various multicast and non-multicast addresses.28*/29import java.net.*;30import java.util.Enumeration;31import java.io.IOException;3233public class MulticastAddresses {3435public static void main(String args[]) throws Exception {3637boolean ipv6_available = false;38NetworkInterface ni = null;3940/*41* Examine the network interfaces and determine :-42*43* 1. If host has IPv6 support44* 2. Get reference to a non-loopback interface45*/46Enumeration nifs = NetworkInterface.getNetworkInterfaces();47while (nifs.hasMoreElements()) {48NetworkInterface this_ni = (NetworkInterface)nifs.nextElement();4950Enumeration addrs = this_ni.getInetAddresses();51while (addrs.hasMoreElements()) {52InetAddress addr = (InetAddress)addrs.nextElement();53if (addr instanceof Inet6Address) {54ipv6_available = true;55}5657if (!addr.isLoopbackAddress() && ni == null) {58ni = this_ni;59}60}6162if (ipv6_available) {63break;64}65}6667int failures = 0;6869String multicasts[] = {70"224.80.80.80",71"ff01::1",72"ff02::1234",73"ff05::a",74"ff0e::1234:a" };7576String non_multicasts[] = {77"129.1.1.1",78"::1",79"::129.1.1.1",80"fe80::a00:20ff:fee5:bc02" };8182MulticastSocket s = new MulticastSocket();8384/* test valid multicast addresses */8586for (int i=0; i<multicasts.length; i++) {87InetAddress ia = InetAddress.getByName(multicasts[i]);88if (ia instanceof Inet6Address && !ipv6_available) {89continue;90}9192System.out.println("Test: " + ia);9394try {9596System.out.print(" joinGroup(InetAddress) ");97s.joinGroup(ia);98s.leaveGroup(ia);99System.out.println(" Passed.");100101System.out.print(" joinGroup(InetAddress,NetworkInterface) ");102s.joinGroup(new InetSocketAddress(ia,0), ni);103s.leaveGroup(new InetSocketAddress(ia,0), ni);104System.out.println(" Passed.");105} catch (IOException e) {106failures++;107System.out.println("Failed: " + e.getMessage());108}109110}111112/* test non-multicast addresses */113114for (int i=0; i<non_multicasts.length; i++) {115InetAddress ia = InetAddress.getByName(non_multicasts[i]);116if (ia instanceof Inet6Address && !ipv6_available) {117continue;118}119120boolean failed = false;121122System.out.println("Test: " + ia + " ");123try {124System.out.println(" joinGroup(InetAddress) ");125s.joinGroup(ia);126127System.out.println("Failed!! -- incorrectly joined group");128failed = true;129} catch (IOException e) {130System.out.println(" Passed: " + e.getMessage());131}132133if (failed) {134s.leaveGroup(ia);135failures++;136}137}138139/* done */140141s.close();142143if (failures > 0) {144throw new Exception(failures + " test(s) failed - see log file.");145}146}147148}149150151