Merge "Explicitly check RouteInfo type in IpReachabityMonitor#isOnLink"
diff --git a/src/android/net/ip/IpReachabilityMonitor.java b/src/android/net/ip/IpReachabilityMonitor.java
index 076d73d..876610d 100644
--- a/src/android/net/ip/IpReachabilityMonitor.java
+++ b/src/android/net/ip/IpReachabilityMonitor.java
@@ -326,9 +326,11 @@
         return sb.toString();
     }
 
-    private static boolean isOnLink(List<RouteInfo> routes, InetAddress ip) {
+    @VisibleForTesting
+    static boolean isOnLink(List<RouteInfo> routes, InetAddress ip) {
         for (RouteInfo route : routes) {
-            if (!route.hasGateway() && route.matches(ip)) {
+            if (!route.hasGateway() && route.matches(ip)
+                    && route.getType() == RouteInfo.RTN_UNICAST) {
                 return true;
             }
         }
diff --git a/tests/unit/src/android/net/ip/IpReachabilityMonitorTest.kt b/tests/unit/src/android/net/ip/IpReachabilityMonitorTest.kt
index ea64ca2..cabd14a 100644
--- a/tests/unit/src/android/net/ip/IpReachabilityMonitorTest.kt
+++ b/tests/unit/src/android/net/ip/IpReachabilityMonitorTest.kt
@@ -83,6 +83,7 @@
 import java.util.concurrent.CompletableFuture
 import java.util.concurrent.ConcurrentLinkedQueue
 import java.util.concurrent.TimeUnit
+import kotlin.test.assertFalse
 import kotlin.test.assertTrue
 import kotlin.test.fail
 
@@ -601,4 +602,33 @@
 
         verifyNudMacAddrChangedType(TEST_IPV6_GATEWAY, NUD_ORGANIC_MAC_ADDRESS_CHANGED, IPV6)
     }
+
+    @SuppressLint("NewApi")
+    @Test
+    fun testIsOnLink() {
+        val routes: List<RouteInfo> = listOf(
+                RouteInfo(
+                        IpPrefix(parseNumericAddress("192.168.0.0"), 16),
+                        null /* gateway */,
+                        null /* iface */,
+                        RouteInfo.RTN_THROW),
+                RouteInfo(IpPrefix(parseNumericAddress("0.0.0.0"), 0), null /* gateway */)
+        )
+
+        assertTrue(IpReachabilityMonitor.isOnLink(routes, parseNumericAddress("192.168.0.1")))
+    }
+
+    @SuppressLint("NewApi")
+    @Test
+    fun testIsOnLink_withThrowRoutes() {
+        val routes: List<RouteInfo> = listOf(
+                RouteInfo(
+                        IpPrefix(parseNumericAddress("192.168.0.0"), 16),
+                        null /* gateway */,
+                        null /* iface */,
+                        RouteInfo.RTN_THROW)
+        )
+
+        assertFalse(IpReachabilityMonitor.isOnLink(routes, parseNumericAddress("192.168.0.1")))
+    }
 }