From 459c9109ca7d2be7995ced6ced9cfec2240621f2 Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Thu, 29 Mar 2018 14:10:44 +0900 Subject: [PATCH] Limit the number of routes for performance In evaluating whether "most" of the addressing space is covered, the list of routes are obtained from a third-party app, so it's possbile the system service stalls unless some limit is enforced on how much work it has to do. This change limits the number of routes to 400, as determined by time measurement on various devices. Bug: 74176086 Test: runtest framework-net Change-Id: Ie4a96098bc044ade87b188839586f14dd101c100 --- .../android/server/connectivity/VpnTest.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/tests/net/java/com/android/server/connectivity/VpnTest.java b/tests/net/java/com/android/server/connectivity/VpnTest.java index f59850d45a..e377a47253 100644 --- a/tests/net/java/com/android/server/connectivity/VpnTest.java +++ b/tests/net/java/com/android/server/connectivity/VpnTest.java @@ -70,6 +70,7 @@ import android.os.Build.VERSION_CODES; import android.os.Bundle; import android.os.INetworkManagementService; import android.os.Looper; +import android.os.SystemClock; import android.os.UserHandle; import android.os.UserManager; import android.support.test.filters.SmallTest; @@ -88,6 +89,8 @@ import org.mockito.InOrder; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import java.net.Inet4Address; +import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -639,4 +642,32 @@ public class VpnTest { lp.addRoute(new RouteInfo(new IpPrefix("::/1"))); assertTrue(Vpn.providesRoutesToMostDestinations(lp)); } + + @Test + public void testDoesNotLockUpWithTooManyRoutes() { + final LinkProperties lp = new LinkProperties(); + final byte[] ad = new byte[4]; + // Actually evaluating this many routes under 1500ms is impossible on + // current hardware and for some time, as the algorithm is O(n²). + // Make sure the system has a safeguard against this and does not + // lock up. + final int MAX_ROUTES = 4000; + final long MAX_ALLOWED_TIME_MS = 1500; + for (int i = 0; i < MAX_ROUTES; ++i) { + ad[0] = (byte)((i >> 24) & 0xFF); + ad[1] = (byte)((i >> 16) & 0xFF); + ad[2] = (byte)((i >> 8) & 0xFF); + ad[3] = (byte)(i & 0xFF); + try { + lp.addRoute(new RouteInfo(new IpPrefix(Inet4Address.getByAddress(ad), 32))); + } catch (UnknownHostException e) { + // UnknownHostException is only thrown for an address of illegal length, + // which can't happen in the case above. + } + } + final long start = SystemClock.currentThreadTimeMillis(); + assertTrue(Vpn.providesRoutesToMostDestinations(lp)); + final long end = SystemClock.currentThreadTimeMillis(); + assertTrue(end - start < MAX_ALLOWED_TIME_MS); + } }