Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/Test.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* @test25* @bug 448845826* @summary IPv4 and IPv6 multicasting broken on Linux27*/28import java.net.*;29import java.io.IOException;30import java.util.Enumeration;3132public class Test {3334static int count = 0;35static int failures = 0;3637void doTest(String address) throws Exception {38boolean failed = false;3940InetAddress ia = InetAddress.getByName(address);4142count++;43System.out.println("**********************");44System.out.println("Test " + count + ": " + ia);4546MulticastSocket mc = new MulticastSocket();47int port = mc.getLocalPort();48DatagramSocket s1 = new DatagramSocket();4950byte msg[] = "Hello".getBytes();51DatagramPacket p = new DatagramPacket(msg, msg.length);5253mc.setSoTimeout(2000);5455try {56for (int i=0; i<2; i++) {5758System.out.println("Join: " + ia);59mc.joinGroup(ia);6061/* packets should be received */6263for (int j=0; j<2; j++) {64p.setAddress(ia);65p.setPort(port);6667System.out.println("Send packet to: " + ia);68s1.send(p);6970try {71mc.receive(p);72System.out.println("Got packet! - Good.");73} catch (SocketTimeoutException e) {74failed = true;75System.out.println("Failed: No packet received within timeout!!!");76}77}7879System.out.println("Leave: " + ia);80mc.leaveGroup(ia);8182/*83* If there are multiple interface we might be a couple of84* copies still in our queue85*/86try {87while (true) {88mc.receive(p);89}90} catch (SocketTimeoutException e) { }9192/* packets should not be received */9394p.setAddress(ia);95p.setPort(port);9697s1.send(p);9899try {100mc.receive(p);101System.out.println("Failed: Got packet after leaving group!!!");102failed = true;103} catch (SocketTimeoutException e) {104System.out.println("No packet received within timeout! - Good.");105}106}107108} catch (IOException ioe) {109System.out.println("Failed: Unexpected exception thrown: ");110ioe.printStackTrace();111failed = true;112}113114mc.close();115s1.close();116117if (failed) {118failures++;119System.out.println("Test failed!!");120} else {121System.out.println("Test passed.");122}123}124125void allTests() throws Exception {126127/*128* Assume machine has IPv4 address129*/130doTest("224.80.80.80");131132/*133* Check if IPv6 is enabled and the scope of the addresses134*/135boolean has_ipv6 = false;136boolean has_siteaddress = false;137boolean has_linklocaladdress = false;138boolean has_globaladdress = false;139140Enumeration nifs = NetworkInterface.getNetworkInterfaces();141while (nifs.hasMoreElements()) {142NetworkInterface ni = (NetworkInterface)nifs.nextElement();143Enumeration addrs = ni.getInetAddresses();144145while (addrs.hasMoreElements()) {146InetAddress ia = (InetAddress)addrs.nextElement();147148if (ia instanceof Inet6Address) {149has_ipv6 = true;150if (ia.isLinkLocalAddress()) has_linklocaladdress = true;151if (ia.isSiteLocalAddress()) has_siteaddress = true;152153if (!ia.isLinkLocalAddress() &&154!ia.isSiteLocalAddress() &&155!ia.isLoopbackAddress()) {156has_globaladdress = true;157}158}159}160}161162/*163* If IPv6 is enabled perform multicast tests with various scopes164*/165if (has_ipv6) {166doTest("ff01::a");167}168169if (has_linklocaladdress) {170doTest("ff02::a");171}172173if (has_siteaddress) {174doTest("ff05::a");175}176177if (has_globaladdress) {178doTest("ff0e::a");179}180}181182public static void main(String args[]) throws Exception {183Test t = new Test();184185if (args.length == 0) {186t.allTests();187} else {188for (int i=0; i<args.length; i++) {189t.doTest(args[i]);190}191}192193System.out.println("**********************");194System.out.println(count + " test(s) executed. " + failures +195" test(s) failed.");196197if (failures > 0) {198throw new Exception("Test failed - see log file for details");199}200}201}202203204