From ef97a43c64f9b80714177ae16b5432fab7bdffde Mon Sep 17 00:00:00 2001 From: Neil Fuller Date: Tue, 17 Mar 2015 11:54:29 +0000 Subject: Fix for devices with multiple multicast-capable interfaces test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface was wrong: 1) The sending socket was joining a group for no obvious reason except, perhaps, to test null NetworkInterface handling. 2) The sending socket was always using the default interface to send multicast packets, but receiving on a specified interface. For devices with several interfaces we have reports of the test failing. It is assumed to be because of (2) when the specified interface and the default interface differ in the presence of multiple interfaces. To explicitly test null handling for joinGroup() more tests have been added. Thanks to swl77wade for helping to track this down. Bug: 159740 Change-Id: Ibb180f6d93770a95f70a6bfa34bfadd93aa3187e --- .../tests/java/net/MulticastSocketTest.java | 36 +++++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/MulticastSocketTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/MulticastSocketTest.java index d531301..9c77e3f 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/MulticastSocketTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/MulticastSocketTest.java @@ -49,7 +49,6 @@ public class MulticastSocketTest extends junit.framework.TestCase { private static InetAddress GOOD_IPv4 = lookup("224.0.0.3"); private static InetAddress BAD_IPv4 = lookup("224.0.0.4"); - private static InetAddress GOOD_IPv6 = lookup("ff05::7:7"); private static InetAddress BAD_IPv6 = lookup("ff05::7:8"); @@ -87,14 +86,15 @@ public class MulticastSocketTest extends junit.framework.TestCase { final InetAddress nextAddress = addresses.nextElement(); if (nextAddress instanceof Inet6Address && ipv6NetworkInterface == null) { ipv6NetworkInterface = nextInterface; - } else if (nextAddress instanceof Inet4Address && ipv4NetworkInterface == null) { + } else if (nextAddress instanceof Inet4Address + && ipv4NetworkInterface == null) { ipv4NetworkInterface = nextInterface; } } } } assertTrue("Test environment must have at least one interface capable of multicast for IPv4" - + " and IPv6", + + " and IPv6", ipv4NetworkInterface != null && ipv6NetworkInterface != null); } @@ -309,18 +309,31 @@ public class MulticastSocketTest extends junit.framework.TestCase { ipv6NetworkInterface, GOOD_IPv6, BAD_IPv6); } + public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_IPv4_nullInterface() + throws Exception { + test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface(null, GOOD_IPv4, BAD_IPv4); + } + + public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_IPv6_nullInterface() + throws Exception { + test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface(null, GOOD_IPv6, BAD_IPv6); + } + private void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface( NetworkInterface networkInterface, InetAddress group, InetAddress group2) throws Exception { - // Check that we can join a group using a null network interface. + // Create the sending socket and specify the interface to use as needed (otherwise use the + // default). MulticastSocket sendingSocket = new MulticastSocket(0); - SocketAddress groupSockAddr = new InetSocketAddress(group, sendingSocket.getLocalPort()); - sendingSocket.joinGroup(groupSockAddr, null); + if (networkInterface != null) { + sendingSocket.setNetworkInterface(networkInterface); + } sendingSocket.setTimeToLive(2); MulticastSocket receivingSocket = createReceivingSocket(0); InetSocketAddress groupAddress = new InetSocketAddress(group, receivingSocket.getLocalPort()); + // Join the group. A null network interface is valid and means "use default". receivingSocket.joinGroup(groupAddress, networkInterface); String msg = "Hello World"; @@ -334,12 +347,19 @@ public class MulticastSocketTest extends junit.framework.TestCase { receivingSocket.close(); sendingSocket.close(); + // Create the sending socket and specify the interface to use as needed (otherwise use the + // default). + sendingSocket = new MulticastSocket(0); + if (networkInterface != null) { + sendingSocket.setNetworkInterface(networkInterface); + } + sendingSocket.setTimeToLive(10); + receivingSocket = createReceivingSocket(0); groupAddress = new InetSocketAddress(group, receivingSocket.getLocalPort()); + // Join the group. A null network interface is valid and means "use default". receivingSocket.joinGroup(groupAddress, networkInterface); - sendingSocket = new MulticastSocket(0); - sendingSocket.setTimeToLive(10); msg = "Hello World - Different Group"; InetSocketAddress group2Address = new InetSocketAddress(group2, receivingSocket.getLocalPort()); -- cgit v1.1