duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 1 | /* |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 2 | * Copyright (c) 2001, 2002, Oracle and/or its affiliates. All rights reserved. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify it |
| 6 | * under the terms of the GNU General Public License version 2 only, as |
| 7 | * published by the Free Software Foundation. |
| 8 | * |
| 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | * accompanied this code). |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License version |
| 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | * |
ohair | 2283b9d | 2010-05-25 15:58:33 -0700 | [diff] [blame^] | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | * or visit www.oracle.com if you need additional information or have any |
| 21 | * questions. |
duke | 6e45e10 | 2007-12-01 00:00:00 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
| 24 | /* |
| 25 | * @test |
| 26 | * @bug 4686717 |
| 27 | * @summary Test MulticastSocket.setLoopbackMode |
| 28 | */ |
| 29 | import java.net.*; |
| 30 | import java.io.IOException; |
| 31 | |
| 32 | public class SetLoopbackMode { |
| 33 | |
| 34 | static final boolean FAILED = true; |
| 35 | static final boolean PASSED = false; |
| 36 | |
| 37 | static boolean test(MulticastSocket mc, InetAddress grp) throws IOException { |
| 38 | |
| 39 | boolean disabled = mc.getLoopbackMode(); |
| 40 | |
| 41 | if (disabled) { |
| 42 | System.out.println("Loopback mode is disabled."); |
| 43 | } else { |
| 44 | System.out.println("Loopback mode is enabled."); |
| 45 | } |
| 46 | |
| 47 | byte b[] = "hello".getBytes(); |
| 48 | DatagramPacket p = new DatagramPacket(b, b.length, grp, |
| 49 | mc.getLocalPort()); |
| 50 | mc.send(p); |
| 51 | |
| 52 | boolean gotPacket = false; |
| 53 | |
| 54 | mc.setSoTimeout(1000); |
| 55 | try { |
| 56 | b = new byte[16]; |
| 57 | p = new DatagramPacket(b, b.length); |
| 58 | mc.receive(p); |
| 59 | gotPacket = true; |
| 60 | |
| 61 | /* purge any additional copies of the packet */ |
| 62 | for (;;) { |
| 63 | mc.receive(p); |
| 64 | } |
| 65 | |
| 66 | } catch (SocketTimeoutException x) { |
| 67 | } |
| 68 | |
| 69 | if (gotPacket && disabled) { |
| 70 | System.out.println("Packet received (unexpected as loopback is disabled)"); |
| 71 | return FAILED; |
| 72 | } |
| 73 | if (!gotPacket && !disabled) { |
| 74 | System.out.println |
| 75 | ("Packet not received (packet excepted as loopback is enabled)"); |
| 76 | return FAILED; |
| 77 | } |
| 78 | |
| 79 | if (gotPacket && !disabled) { |
| 80 | System.out.println("Packet received - correct."); |
| 81 | } else { |
| 82 | System.out.println("Packet not received - correct."); |
| 83 | } |
| 84 | |
| 85 | return PASSED; |
| 86 | } |
| 87 | |
| 88 | public static void main (String args[]) throws Exception { |
| 89 | int failures = 0; |
| 90 | |
| 91 | MulticastSocket mc = new MulticastSocket(); |
| 92 | InetAddress grp = InetAddress.getByName("224.80.80.80"); |
| 93 | |
| 94 | |
| 95 | /* |
| 96 | * If IPv6 is available then use IPv6 multicast group - needed |
| 97 | * to workaround Linux IPv6 bug whereby !IPV6_MULTICAST_LOOP |
| 98 | * doesn't prevent loopback of IPv4 multicast packets. |
| 99 | */ |
| 100 | InetAddress lb = InetAddress.getByName("::1"); |
| 101 | if (NetworkInterface.getByInetAddress(lb) != null) { |
| 102 | grp = InetAddress.getByName("ff01::1"); |
| 103 | } |
| 104 | |
| 105 | System.out.println("\nTest will use multicast group: " + grp); |
| 106 | mc.joinGroup(grp); |
| 107 | |
| 108 | System.out.println("\n******************\n"); |
| 109 | |
| 110 | mc.setLoopbackMode(true); |
| 111 | if (test(mc, grp) == FAILED) failures++; |
| 112 | |
| 113 | System.out.println("\n******************\n"); |
| 114 | |
| 115 | mc.setLoopbackMode(false); |
| 116 | if (test(mc, grp) == FAILED) failures++; |
| 117 | |
| 118 | System.out.println("\n******************\n"); |
| 119 | |
| 120 | if (failures > 0) { |
| 121 | throw new RuntimeException("Test failed"); |
| 122 | } |
| 123 | } |
| 124 | } |