diff options
Diffstat (limited to 'tests/net/java/com/android/server/ConnectivityServiceTest.java')
-rw-r--r-- | tests/net/java/com/android/server/ConnectivityServiceTest.java | 66 |
1 files changed, 35 insertions, 31 deletions
diff --git a/tests/net/java/com/android/server/ConnectivityServiceTest.java b/tests/net/java/com/android/server/ConnectivityServiceTest.java index dbf81d69e50c..e3db7e8a1354 100644 --- a/tests/net/java/com/android/server/ConnectivityServiceTest.java +++ b/tests/net/java/com/android/server/ConnectivityServiceTest.java @@ -62,9 +62,7 @@ import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; -import static org.mockito.Matchers.anyBoolean; import static org.mockito.Matchers.anyInt; -import static org.mockito.Matchers.anyString; import static org.mockito.Mockito.any; import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.eq; @@ -85,7 +83,6 @@ import android.content.ContentResolver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; -import android.content.pm.UserInfo; import android.content.res.Resources; import android.net.CaptivePortal; import android.net.ConnectivityManager; @@ -114,7 +111,6 @@ import android.net.NetworkUtils; import android.net.RouteInfo; import android.net.StringNetworkSpecifier; import android.net.UidRange; -import android.net.VpnService; import android.net.captiveportal.CaptivePortalProbeResult; import android.net.metrics.IpConnectivityLog; import android.net.util.MultinetworkPolicyTracker; @@ -135,7 +131,6 @@ import android.support.test.InstrumentationRegistry; import android.support.test.filters.SmallTest; import android.support.test.runner.AndroidJUnit4; import android.test.mock.MockContentResolver; -import android.text.TextUtils; import android.util.ArraySet; import android.util.Log; @@ -196,7 +191,16 @@ public class ConnectivityServiceTest { private static final String TAG = "ConnectivityServiceTest"; private static final int TIMEOUT_MS = 500; - private static final int TEST_LINGER_DELAY_MS = 120; + private static final int TEST_LINGER_DELAY_MS = 250; + // Chosen to be less than the linger timeout. This ensures that we can distinguish between a + // LOST callback that arrives immediately and a LOST callback that arrives after the linger + // timeout. For this, our assertions should run fast enough to leave less than + // (mService.mLingerDelayMs - TEST_CALLBACK_TIMEOUT_MS) between the time callbacks are + // supposedly fired, and the time we call expectCallback. + private final static int TEST_CALLBACK_TIMEOUT_MS = 200; + // Chosen to be less than TEST_CALLBACK_TIMEOUT_MS. This ensures that requests have time to + // complete before callbacks are verified. + private final static int TEST_REQUEST_TIMEOUT_MS = 150; private static final String CLAT_PREFIX = "v4-"; private static final String MOBILE_IFNAME = "test_rmnet_data0"; @@ -876,7 +880,7 @@ public class ConnectivityServiceTest { NetworkAgentInfo networkAgentInfo, NetworkRequest defaultRequest, IpConnectivityLog log) { super(context, handler, networkAgentInfo, defaultRequest, log, - NetworkMonitor.NetworkMonitorSettings.DEFAULT); + NetworkMonitor.Dependencies.DEFAULT); connectivityHandler = handler; } @@ -1465,11 +1469,6 @@ public class ConnectivityServiceTest { * received. assertNoCallback may be called at any time. */ private class TestNetworkCallback extends NetworkCallback { - // Chosen to be much less than the linger timeout. This ensures that we can distinguish - // between a LOST callback that arrives immediately and a LOST callback that arrives after - // the linger timeout. - private final static int TIMEOUT_MS = 100; - private final LinkedBlockingQueue<CallbackInfo> mCallbacks = new LinkedBlockingQueue<>(); private Network mLastAvailableNetwork; @@ -1545,20 +1544,20 @@ public class ConnectivityServiceTest { if (state == CallbackState.LOSING) { String msg = String.format( "Invalid linger time value %d, must be between %d and %d", - actual.arg, 0, TEST_LINGER_DELAY_MS); + actual.arg, 0, mService.mLingerDelayMs); int maxMsToLive = (Integer) actual.arg; - assertTrue(msg, 0 <= maxMsToLive && maxMsToLive <= TEST_LINGER_DELAY_MS); + assertTrue(msg, 0 <= maxMsToLive && maxMsToLive <= mService.mLingerDelayMs); } return actual; } CallbackInfo expectCallback(CallbackState state, MockNetworkAgent agent) { - return expectCallback(state, agent, TIMEOUT_MS); + return expectCallback(state, agent, TEST_CALLBACK_TIMEOUT_MS); } CallbackInfo expectCallbackLike(Predicate<CallbackInfo> fn) { - return expectCallbackLike(fn, TIMEOUT_MS); + return expectCallbackLike(fn, TEST_CALLBACK_TIMEOUT_MS); } CallbackInfo expectCallbackLike(Predicate<CallbackInfo> fn, int timeoutMs) { @@ -1601,15 +1600,15 @@ public class ConnectivityServiceTest { // Expects the available callbacks (validated), plus onSuspended. void expectAvailableAndSuspendedCallbacks(MockNetworkAgent agent, boolean expectValidated) { - expectAvailableCallbacks(agent, true, expectValidated, TIMEOUT_MS); + expectAvailableCallbacks(agent, true, expectValidated, TEST_CALLBACK_TIMEOUT_MS); } void expectAvailableCallbacksValidated(MockNetworkAgent agent) { - expectAvailableCallbacks(agent, false, true, TIMEOUT_MS); + expectAvailableCallbacks(agent, false, true, TEST_CALLBACK_TIMEOUT_MS); } void expectAvailableCallbacksUnvalidated(MockNetworkAgent agent) { - expectAvailableCallbacks(agent, false, false, TIMEOUT_MS); + expectAvailableCallbacks(agent, false, false, TEST_CALLBACK_TIMEOUT_MS); } // Expects the available callbacks (where the onCapabilitiesChanged must contain the @@ -1617,9 +1616,9 @@ public class ConnectivityServiceTest { // one we just sent. // TODO: this is likely a bug. Fix it and remove this method. void expectAvailableDoubleValidatedCallbacks(MockNetworkAgent agent) { - expectCallback(CallbackState.AVAILABLE, agent, TIMEOUT_MS); + expectCallback(CallbackState.AVAILABLE, agent, TEST_CALLBACK_TIMEOUT_MS); NetworkCapabilities nc1 = expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, agent); - expectCallback(CallbackState.LINK_PROPERTIES, agent, TIMEOUT_MS); + expectCallback(CallbackState.LINK_PROPERTIES, agent, TEST_CALLBACK_TIMEOUT_MS); NetworkCapabilities nc2 = expectCapabilitiesWith(NET_CAPABILITY_VALIDATED, agent); assertEquals(nc1, nc2); } @@ -1633,7 +1632,7 @@ public class ConnectivityServiceTest { } NetworkCapabilities expectCapabilitiesWith(int capability, MockNetworkAgent agent) { - return expectCapabilitiesWith(capability, agent, TIMEOUT_MS); + return expectCapabilitiesWith(capability, agent, TEST_CALLBACK_TIMEOUT_MS); } NetworkCapabilities expectCapabilitiesWith(int capability, MockNetworkAgent agent, @@ -1645,7 +1644,7 @@ public class ConnectivityServiceTest { } NetworkCapabilities expectCapabilitiesWithout(int capability, MockNetworkAgent agent) { - return expectCapabilitiesWithout(capability, agent, TIMEOUT_MS); + return expectCapabilitiesWithout(capability, agent, TEST_CALLBACK_TIMEOUT_MS); } NetworkCapabilities expectCapabilitiesWithout(int capability, MockNetworkAgent agent, @@ -1769,6 +1768,12 @@ public class ConnectivityServiceTest { @Test public void testMultipleLingering() { + // This test would be flaky with the default 120ms timer: that is short enough that + // lingered networks are torn down before assertions can be run. We don't want to mock the + // lingering timer to keep the WakeupMessage logic realistic: this has already proven useful + // in detecting races. + mService.mLingerDelayMs = 300; + NetworkRequest request = new NetworkRequest.Builder() .clearCapabilities().addCapability(NET_CAPABILITY_NOT_METERED) .build(); @@ -1986,7 +1991,7 @@ public class ConnectivityServiceTest { // Let linger run its course. callback.assertNoCallback(); - final int lingerTimeoutMs = TEST_LINGER_DELAY_MS + TEST_LINGER_DELAY_MS / 4; + final int lingerTimeoutMs = mService.mLingerDelayMs + mService.mLingerDelayMs / 4; callback.expectCallback(CallbackState.LOST, mCellNetworkAgent, lingerTimeoutMs); // Register a TRACK_DEFAULT request and check that it does not affect lingering. @@ -3210,12 +3215,12 @@ public class ConnectivityServiceTest { NetworkRequest nr = new NetworkRequest.Builder().addTransportType( NetworkCapabilities.TRANSPORT_WIFI).build(); final TestNetworkCallback networkCallback = new TestNetworkCallback(); - final int timeoutMs = 150; - mCm.requestNetwork(nr, networkCallback, timeoutMs); + mCm.requestNetwork(nr, networkCallback, TEST_REQUEST_TIMEOUT_MS); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent.connect(false); - networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, false, timeoutMs); + networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, false, + TEST_CALLBACK_TIMEOUT_MS); // pass timeout and validate that UNAVAILABLE is not called networkCallback.assertNoCallback(); @@ -3230,13 +3235,12 @@ public class ConnectivityServiceTest { NetworkRequest nr = new NetworkRequest.Builder().addTransportType( NetworkCapabilities.TRANSPORT_WIFI).build(); final TestNetworkCallback networkCallback = new TestNetworkCallback(); - final int requestTimeoutMs = 50; - mCm.requestNetwork(nr, networkCallback, requestTimeoutMs); + mCm.requestNetwork(nr, networkCallback, TEST_REQUEST_TIMEOUT_MS); mWiFiNetworkAgent = new MockNetworkAgent(TRANSPORT_WIFI); mWiFiNetworkAgent.connect(false); - final int assertTimeoutMs = 100; - networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, false, assertTimeoutMs); + networkCallback.expectAvailableCallbacks(mWiFiNetworkAgent, false, false, + TEST_CALLBACK_TIMEOUT_MS); mWiFiNetworkAgent.disconnect(); networkCallback.expectCallback(CallbackState.LOST, mWiFiNetworkAgent); |