Add a test for IP_MULTICAST_ALL for DatagramSocket

This option defaults to 1 for AF_INET dgram sockets and 0 for AF_INET6.
Android uses AF_INET6 sockets and don't need to explicitly set the
option, but it is better to have a test to assert this behaviour.

Test: org.apache.harmony.tests.java.net.DatagramSocketTest
Change-Id: I367be7f2a1bc7fd4836a132f16ecfd3ca65b930a
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java
index 1cb6808..7523752 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/DatagramSocketTest.java
@@ -31,11 +31,15 @@
 import java.net.SocketException;
 import java.net.UnknownHostException;
 import java.nio.channels.DatagramChannel;
+import libcore.io.Libcore;
 import libcore.junit.junit3.TestCaseWithRules;
 import libcore.junit.util.ResourceLeakageDetector;
 import org.junit.Rule;
 import org.junit.rules.TestRule;
 
+import static android.system.OsConstants.IPPROTO_IP;
+import static android.system.OsConstants.IP_MULTICAST_ALL;
+
 public class DatagramSocketTest extends TestCaseWithRules {
     @Rule
     public TestRule guardRule = ResourceLeakageDetector.getRule();
@@ -97,8 +101,14 @@
     /**
      * java.net.DatagramSocket#DatagramSocket()
      */
-    public void test_Constructor() throws SocketException {
-        new DatagramSocket().close();
+    public void test_Constructor() throws Exception {
+        try (DatagramSocket ds = new DatagramSocket()) {
+            // Datagram sockets bound to the wildcard INADDR_ANY address should by default only
+            // receive messages from groups they explicitly joined.
+            boolean multicastAllEnabled = Libcore.os.getsockoptInt(ds.getFileDescriptor$(),
+                    IPPROTO_IP, IP_MULTICAST_ALL) == 1;
+            assertFalse(multicastAllEnabled);
+        }
     }
 
     /**
diff --git a/luni/src/main/java/android/system/OsConstants.java b/luni/src/main/java/android/system/OsConstants.java
index 28b2523..96846ca 100644
--- a/luni/src/main/java/android/system/OsConstants.java
+++ b/luni/src/main/java/android/system/OsConstants.java
@@ -313,6 +313,7 @@
     public static final int IPV6_TCLASS = placeholder();
     public static final int IPV6_UNICAST_HOPS = placeholder();
     public static final int IPV6_V6ONLY = placeholder();
+    /** @hide */ public static final int IP_MULTICAST_ALL = placeholder();
     public static final int IP_MULTICAST_IF = placeholder();
     public static final int IP_MULTICAST_LOOP = placeholder();
     public static final int IP_MULTICAST_TTL = placeholder();
diff --git a/luni/src/main/native/android_system_OsConstants.cpp b/luni/src/main/native/android_system_OsConstants.cpp
index 7334420..a8b2c2f 100644
--- a/luni/src/main/native/android_system_OsConstants.cpp
+++ b/luni/src/main/native/android_system_OsConstants.cpp
@@ -335,6 +335,7 @@
 #endif
     initConstant(env, c, "IPV6_UNICAST_HOPS", IPV6_UNICAST_HOPS);
     initConstant(env, c, "IPV6_V6ONLY", IPV6_V6ONLY);
+    initConstant(env, c, "IP_MULTICAST_ALL", IP_MULTICAST_ALL);
     initConstant(env, c, "IP_MULTICAST_IF", IP_MULTICAST_IF);
     initConstant(env, c, "IP_MULTICAST_LOOP", IP_MULTICAST_LOOP);
     initConstant(env, c, "IP_MULTICAST_TTL", IP_MULTICAST_TTL);