Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/net/MulticastSocket/SetLoopbackMode.java
38811 views
/*1* Copyright (c) 2001, 2002, 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 468671726* @summary Test MulticastSocket.setLoopbackMode27*/28import java.net.*;29import java.io.IOException;3031public class SetLoopbackMode {3233static final boolean FAILED = true;34static final boolean PASSED = false;3536static boolean test(MulticastSocket mc, InetAddress grp) throws IOException {3738boolean disabled = mc.getLoopbackMode();3940if (disabled) {41System.out.println("Loopback mode is disabled.");42} else {43System.out.println("Loopback mode is enabled.");44}4546byte b[] = "hello".getBytes();47DatagramPacket p = new DatagramPacket(b, b.length, grp,48mc.getLocalPort());49mc.send(p);5051boolean gotPacket = false;5253mc.setSoTimeout(1000);54try {55b = new byte[16];56p = new DatagramPacket(b, b.length);57mc.receive(p);58gotPacket = true;5960/* purge any additional copies of the packet */61for (;;) {62mc.receive(p);63}6465} catch (SocketTimeoutException x) {66}6768if (gotPacket && disabled) {69System.out.println("Packet received (unexpected as loopback is disabled)");70return FAILED;71}72if (!gotPacket && !disabled) {73System.out.println74("Packet not received (packet excepted as loopback is enabled)");75return FAILED;76}7778if (gotPacket && !disabled) {79System.out.println("Packet received - correct.");80} else {81System.out.println("Packet not received - correct.");82}8384return PASSED;85}8687public static void main (String args[]) throws Exception {88int failures = 0;8990MulticastSocket mc = new MulticastSocket();91InetAddress grp = InetAddress.getByName("224.80.80.80");929394/*95* If IPv6 is available then use IPv6 multicast group - needed96* to workaround Linux IPv6 bug whereby !IPV6_MULTICAST_LOOP97* doesn't prevent loopback of IPv4 multicast packets.98*/99InetAddress lb = InetAddress.getByName("::1");100if (NetworkInterface.getByInetAddress(lb) != null) {101grp = InetAddress.getByName("ff01::1");102}103104System.out.println("\nTest will use multicast group: " + grp);105mc.joinGroup(grp);106107System.out.println("\n******************\n");108109mc.setLoopbackMode(true);110if (test(mc, grp) == FAILED) failures++;111112System.out.println("\n******************\n");113114mc.setLoopbackMode(false);115if (test(mc, grp) == FAILED) failures++;116117System.out.println("\n******************\n");118119if (failures > 0) {120throw new RuntimeException("Test failed");121}122}123}124125126