Merge "Limit the number of routes for performance" am: 7f5163f453

am: 554f03dc04

Change-Id: I1fcf34b553b4a7cbadd0a2a7bf697140e948243e
This commit is contained in:
Chalard Jean
2018-03-30 08:58:44 +00:00
committed by android-build-merger

View File

@@ -70,6 +70,7 @@ import android.os.Build.VERSION_CODES;
import android.os.Bundle; import android.os.Bundle;
import android.os.INetworkManagementService; import android.os.INetworkManagementService;
import android.os.Looper; import android.os.Looper;
import android.os.SystemClock;
import android.os.UserHandle; import android.os.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.support.test.filters.SmallTest; import android.support.test.filters.SmallTest;
@@ -88,6 +89,8 @@ import org.mockito.InOrder;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.MockitoAnnotations; import org.mockito.MockitoAnnotations;
import java.net.Inet4Address;
import java.net.UnknownHostException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
@@ -639,4 +642,32 @@ public class VpnTest {
lp.addRoute(new RouteInfo(new IpPrefix("::/1"))); lp.addRoute(new RouteInfo(new IpPrefix("::/1")));
assertTrue(Vpn.providesRoutesToMostDestinations(lp)); 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);
}
} }