Path: blob/master/test/jdk/java/net/MulticastSocket/SetLoopbackMode.java
41149 views
/*1* Copyright (c) 2001, 2020, 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* @library /test/lib28* @modules java.base/java.net:+open29* @build jdk.test.lib.NetworkConfiguration30* jdk.test.lib.Platform31* @run main/othervm SetLoopbackMode32* @run main/othervm -Djdk.net.usePlainDatagramSocketImpl SetLoopbackMode33*/3435import java.lang.reflect.Method;36import java.lang.reflect.UndeclaredThrowableException;37import java.net.*;38import java.io.IOException;39import jdk.test.lib.NetworkConfiguration;4041public class SetLoopbackMode {4243static final boolean FAILED = true;44static final boolean PASSED = false;4546static boolean test(MulticastSocket mc, InetAddress grp) throws IOException {4748boolean disabled = mc.getLoopbackMode();4950if (disabled) {51System.out.println("Loopback mode is disabled.");52} else {53System.out.println("Loopback mode is enabled.");54}5556System.out.println(mc.getLocalSocketAddress());5758byte b[] = "hello".getBytes();59DatagramPacket p = new DatagramPacket(b, b.length, grp,60mc.getLocalPort());61mc.send(p);6263boolean gotPacket = false;6465mc.setSoTimeout(1000);66try {67b = new byte[16];68p = new DatagramPacket(b, b.length);69mc.receive(p);70gotPacket = true;7172/* purge any additional copies of the packet */73for (;;) {74mc.receive(p);75}7677} catch (SocketTimeoutException x) {78}7980if (gotPacket && disabled) {81System.out.println("Packet received (unexpected as loopback is disabled)");82return FAILED;83}84if (!gotPacket && !disabled) {85System.out.println86("Packet not received (packet excepted as loopback is enabled)");87return FAILED;88}8990if (gotPacket && !disabled) {91System.out.println("Packet received - correct.");92} else {93System.out.println("Packet not received - correct.");94}9596return PASSED;97}9899private static boolean canUseIPv6(NetworkConfiguration nc) {100return nc.ip6MulticastInterfaces().toArray().length > 0;101}102103public static void main (String args[]) throws Exception {104int failures = 0;105NetworkConfiguration nc = NetworkConfiguration.probe();106107try (MulticastSocket mc = new MulticastSocket()) {108InetAddress grp = InetAddress.getByName("224.80.80.80");109110111/*112* If IPv6 is available then use IPv6 multicast group - needed113* to workaround Linux IPv6 bug whereby !IPV6_MULTICAST_LOOP114* doesn't prevent loopback of IPv4 multicast packets.115*/116117if (canUseIPv6(nc)) {118System.out.println("IPv6 can be used");119grp = InetAddress.getByName("ff01::1");120} else {121System.out.println("IPv6 cannot be used: using IPv4");122}123System.out.println("Default network interface: " + DefaultInterface.getDefaultName());124System.out.println("\nTest will use multicast group: " + grp);125try {126System.out.println("NetworkInterface.getByInetAddress(grp): "127+ getName(NetworkInterface.getByInetAddress(grp)));128} catch (Exception x) {129// OK130}131mc.joinGroup(grp);132133System.out.println("\n******************\n");134135mc.setLoopbackMode(true);136if (test(mc, grp) == FAILED) failures++;137138System.out.println("\n******************\n");139140mc.setLoopbackMode(false);141if (test(mc, grp) == FAILED) failures++;142143System.out.println("\n******************\n");144145if (failures > 0) {146throw new RuntimeException("Test failed");147}148}149}150151static String getName(NetworkInterface nif) {152return nif == null ? null : nif.getName();153}154155static class DefaultInterface {156static final Method GET_DEFAULT;157static {158try {159GET_DEFAULT = Class.forName("java.net.DefaultInterface")160.getDeclaredMethod("getDefault");161GET_DEFAULT.setAccessible(true);162} catch (Exception x) {163throw new ExceptionInInitializerError(x);164}165}166static NetworkInterface getDefault() {167try {168return (NetworkInterface) GET_DEFAULT169.invoke(null);170} catch (Exception x) {171throw new UndeclaredThrowableException(x);172}173}174static String getDefaultName() {175return getName(getDefault());176}177}178}179180181