Path: blob/master/test/jdk/java/net/MulticastSocket/Test.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*/2223import java.io.IOException;24import java.net.DatagramPacket;25import java.net.DatagramSocket;26import java.net.InetAddress;27import java.net.MulticastSocket;28import java.net.SocketTimeoutException;2930import jdk.test.lib.NetworkConfiguration;31import jdk.test.lib.net.IPSupport;3233/**34* @test35* @bug 448845836* @summary IPv4 and IPv6 multicasting broken on Linux37* @library /test/lib38* @build jdk.test.lib.NetworkConfiguration39* jdk.test.lib.Platform40* @run main Test41* @run main/othervm -Djava.net.preferIPv4Stack=true Test42*/43public class Test {4445static int count = 0;46static int failures = 0;4748void doTest(String address) throws IOException {49boolean failed = false;5051InetAddress ia = InetAddress.getByName(address);5253count++;54System.out.println("**********************");55System.out.println("Test " + count + ": " + ia);5657MulticastSocket mc = new MulticastSocket();58int port = mc.getLocalPort();59DatagramSocket s1 = new DatagramSocket();6061byte msg[] = "Hello".getBytes();62DatagramPacket p = new DatagramPacket(msg, msg.length);6364mc.setSoTimeout(2000);6566try {67for (int i=0; i<2; i++) {6869System.out.println("Join: " + ia);70mc.joinGroup(ia);7172/* packets should be received */7374for (int j = 0; j < 2; j++) {75p.setAddress(ia);76p.setPort(port);7778System.out.println("Send packet to: " + ia);79s1.send(p);8081try {82mc.receive(p);83System.out.println("Got packet! - Good.");84} catch (SocketTimeoutException e) {85failed = true;86System.out.println("Failed: No packet received within timeout!!!");87}88}8990System.out.println("Leave: " + ia);91mc.leaveGroup(ia);9293/*94* If there are multiple interface we might be a couple of95* copies still in our queue96*/97try {98while (true) {99mc.receive(p);100}101} catch (SocketTimeoutException e) { }102103/* packets should not be received */104105p.setAddress(ia);106p.setPort(port);107108s1.send(p);109110try {111mc.receive(p);112System.out.println("Failed: Got packet after leaving group!!!");113failed = true;114} catch (SocketTimeoutException e) {115System.out.println("No packet received within timeout! - Good.");116}117}118119} catch (IOException ioe) {120System.out.println("Failed: Unexpected exception thrown: ");121ioe.printStackTrace();122failed = true;123}124125mc.close();126s1.close();127128if (failed) {129failures++;130System.out.println("Test failed!!");131} else {132System.out.println("Test passed.");133}134}135136void allTests() throws IOException {137NetworkConfiguration nc = NetworkConfiguration.probe();138139// unconditionally test IPv4 address140doTest("224.80.80.80");141142// If IPv6 is enabled perform multicast tests with various scopes143if (nc.hasTestableIPv6Address()) {144doTest("ff01::a");145}146147if (nc.hasLinkLocalAddress()) {148doTest("ff02::a");149}150151if (nc.hasSiteLocalAddress()) {152doTest("ff05::a");153}154155if (nc.has_globaladdress()) {156doTest("ff0e::a");157}158}159160public static void main(String args[]) throws Exception {161IPSupport.throwSkippedExceptionIfNonOperational();162163Test t = new Test();164165if (args.length == 0) {166t.allTests();167} else {168for (int i = 0; i < args.length; i++) {169t.doTest(args[i]);170}171}172173System.out.println("**********************");174System.out.println(count + " test(s) executed. " + failures +175" test(s) failed.");176177if (failures > 0) {178throw new Exception("Test failed - see log file for details");179}180}181}182183184