Merge "Fix CTS multicast IPv4 related cases failure" am: d6077e7d41

Original change: https://android-review.googlesource.com/c/platform/libcore/+/1375695

MUST ONLY BE SUBMITTED BY AUTOMERGER

Change-Id: I8bffbcdba71885f6e9ca01058cea532cd70c54e7
diff --git a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
index 1f71a24..a151da3 100644
--- a/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
+++ b/luni/src/test/java/libcore/java/nio/channels/DatagramChannelMulticastTest.java
@@ -36,6 +36,8 @@
 import java.util.ArrayList;
 import java.util.Enumeration;
 import java.util.concurrent.TimeUnit;
+import java.net.ProtocolFamily;
+import java.net.StandardProtocolFamily;
 
 import libcore.io.IoBridge;
 
@@ -219,25 +221,25 @@
     }
 
     public void test_joinAnySource_IPv4() throws Exception {
-        check_joinAnySource(GOOD_MULTICAST_IPv4, BAD_MULTICAST_IPv4, ipv4NetworkInterface);
+        check_joinAnySource(GOOD_MULTICAST_IPv4, BAD_MULTICAST_IPv4, ipv4NetworkInterface, StandardProtocolFamily.INET);
     }
 
     public void test_joinAnySource_IPv6() throws Exception {
-        check_joinAnySource(GOOD_MULTICAST_IPv6, BAD_MULTICAST_IPv6, ipv6NetworkInterface);
+        check_joinAnySource(GOOD_MULTICAST_IPv6, BAD_MULTICAST_IPv6, ipv6NetworkInterface, StandardProtocolFamily.INET6);
     }
 
     private void check_joinAnySource(InetAddress group, InetAddress group2,
-            NetworkInterface networkInterface) throws Exception {
+            NetworkInterface networkInterface, ProtocolFamily protocolFamily) throws Exception {
         if (!supportsMulticast) {
             return;
         }
         // Set up a receiver join the group on ipv4NetworkInterface
-        DatagramChannel receiverChannel = createReceiverChannel();
+        DatagramChannel receiverChannel = createReceiverChannel(protocolFamily);
         InetSocketAddress localAddress = (InetSocketAddress) receiverChannel.getLocalAddress();
         receiverChannel.join(group, networkInterface);
 
         String msg = "Hello World";
-        createChannelAndSendMulticastMessage(group, localAddress.getPort(), msg, networkInterface);
+        createChannelAndSendMulticastMessage(group, localAddress.getPort(), msg, networkInterface, protocolFamily);
 
         // now verify that we received the data as expected
         ByteBuffer recvBuffer = ByteBuffer.allocate(100);
@@ -247,7 +249,7 @@
         // now verify that we didn't receive the second message
         String msg2 = "Hello World - Different Group";
         createChannelAndSendMulticastMessage(
-                group2, localAddress.getPort(), msg2, networkInterface);
+                group2, localAddress.getPort(), msg2, networkInterface, protocolFamily);
         recvBuffer.position(0);
         checkNoDatagramReceived(receiverChannel);
 
@@ -396,6 +398,13 @@
         return dc;
     }
 
+    private DatagramChannel createReceiverChannel(ProtocolFamily protocolFamily) throws Exception {
+        DatagramChannel dc = DatagramChannel.open(protocolFamily);
+        dc.bind(null /* leave the OS to determine the port, and use the wildcard address */);
+        configureChannelForReceiving(dc);
+        return dc;
+    }
+
     public void test_joinAnySource_multiple_joins_IPv4()
             throws Exception {
         check_joinAnySource_multiple_joins(GOOD_MULTICAST_IPv4, ipv4NetworkInterface);
@@ -425,21 +434,21 @@
     }
 
     public void test_joinAnySource_multicastLoopOption_IPv4() throws Exception {
-        check_joinAnySource_multicastLoopOption(GOOD_MULTICAST_IPv4, ipv4NetworkInterface);
+        check_joinAnySource_multicastLoopOption(GOOD_MULTICAST_IPv4, ipv4NetworkInterface, StandardProtocolFamily.INET);
     }
 
     public void test_multicastLoopOption_IPv6() throws Exception {
-        check_joinAnySource_multicastLoopOption(GOOD_MULTICAST_IPv6, ipv6NetworkInterface);
+        check_joinAnySource_multicastLoopOption(GOOD_MULTICAST_IPv6, ipv6NetworkInterface, StandardProtocolFamily.INET6);
     }
 
     private void check_joinAnySource_multicastLoopOption(InetAddress group,
-            NetworkInterface networkInterface) throws Exception {
+            NetworkInterface networkInterface, ProtocolFamily protocolFamily) throws Exception {
         if (!supportsMulticast) {
             return;
         }
         final String message = "Hello, world!";
 
-        DatagramChannel dc = createReceiverChannel();
+        DatagramChannel dc = createReceiverChannel(protocolFamily);
         dc.setOption(StandardSocketOptions.IP_MULTICAST_LOOP, true /* enable loop */);
         dc.setOption(StandardSocketOptions.IP_MULTICAST_IF, networkInterface);
         configureChannelForReceiving(dc);
@@ -1290,6 +1299,17 @@
         dc.close();
     }
 
+
+    private static void createChannelAndSendMulticastMessage(
+            InetAddress group, int port, String msg, NetworkInterface sendingInterface, ProtocolFamily protocolFamily)
+            throws IOException {
+        // Any datagram socket can send to a group. It does not need to have joined the group.
+        DatagramChannel dc = DatagramChannel.open(protocolFamily);
+        BindableChannel channel = new BindableChannel(dc, sendingInterface);
+        channel.sendMulticastMessage(msg, new InetSocketAddress(group, port));
+        dc.close();
+    }
+
     /**
      * A {@link DatagramChannel} which is optionally bound to a {@link NetworkInterface}.
      */