Loading src/com/android/server/connectivity/NetworkMonitor.java +26 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,9 @@ import static android.net.ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL; import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.DnsResolver.FLAG_EMPTY; import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_INVALID; import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY; import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_VALID; import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_DNS; import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_FALLBACK; import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_HTTP; Loading Loading @@ -93,6 +96,7 @@ import android.net.util.SharedLog; import android.net.util.Stopwatch; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.os.Message; import android.os.RemoteException; Loading Loading @@ -286,6 +290,7 @@ public class NetworkMonitor extends StateMachine { private final Context mContext; private final INetworkMonitorCallbacks mCallback; private final int mCallbackVersion; private final Network mCleartextDnsNetwork; private final Network mNetwork; private final TelephonyManager mTelephonyManager; Loading Loading @@ -355,6 +360,17 @@ public class NetworkMonitor extends StateMachine { private boolean mAcceptPartialConnectivity = false; private final EvaluationState mEvaluationState = new EvaluationState(); private int getCallbackVersion(INetworkMonitorCallbacks cb) { int version; try { version = cb.getInterfaceVersion(); } catch (RemoteException e) { version = 0; } if (version == Build.VERSION_CODES.CUR_DEVELOPMENT) version = 0; return version; } public NetworkMonitor(Context context, INetworkMonitorCallbacks cb, Network network, SharedLog validationLog) { this(context, cb, network, new IpConnectivityLog(), validationLog, Loading @@ -376,6 +392,7 @@ public class NetworkMonitor extends StateMachine { mMetricsLog = logger; mValidationLogs = validationLogs; mCallback = cb; mCallbackVersion = getCallbackVersion(cb); mDependencies = deps; mDetectionStatsUtils = detectionStatsUtils; mNetwork = network; Loading Loading @@ -2113,6 +2130,15 @@ public class NetworkMonitor extends StateMachine { } protected int getNetworkTestResult() { if (mCallbackVersion < 3) { if ((mEvaluationResult & NETWORK_VALIDATION_RESULT_VALID) != 0) { return NETWORK_TEST_RESULT_VALID; } if ((mEvaluationResult & NETWORK_VALIDATION_RESULT_PARTIAL) != 0) { return NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY; } return NETWORK_TEST_RESULT_INVALID; } return mEvaluationResult | mProbeResults; } } Loading tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java +23 −10 Original line number Diff line number Diff line Loading @@ -287,7 +287,7 @@ public class NetworkMonitorTest { private FakeDns mFakeDns; @Before public void setUp() throws IOException { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(mDependencies.getPrivateDnsBypassNetwork(any())).thenReturn(mCleartextDnsNetwork); when(mDependencies.getDnsResolver()).thenReturn(mDnsResolver); Loading Loading @@ -355,6 +355,8 @@ public class NetworkMonitorTest { return null; }).when(mContext).unregisterReceiver(any()); resetCallbacks(); setMinDataStallEvaluateInterval(500); setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS); setValidDataStallDnsTimeThreshold(500); Loading Loading @@ -382,6 +384,17 @@ public class NetworkMonitorTest { 0, mRegisteredReceivers.size()); } private void resetCallbacks() { reset(mCallbacks); // TODO: make this a parameterized test. try { when(mCallbacks.getInterfaceVersion()).thenReturn(3); } catch (RemoteException e) { // Can't happen as mCallbacks is a mock fail("Error mocking getInterfaceVersion" + e); } } private class WrappedNetworkMonitor extends NetworkMonitor { private long mProbeTime = 0; private final ConditionVariable mQuitCv = new ConditionVariable(false); Loading Loading @@ -720,7 +733,7 @@ public class NetworkMonitorTest { setStatus(mHttpsConnection, 204); setStatus(mHttpConnection, 204); reset(mCallbacks); resetCallbacks(); nm.notifyCaptivePortalAppFinished(APP_RETURN_DISMISSED); verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) .notifyNetworkTested(eq(NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP Loading Loading @@ -758,7 +771,7 @@ public class NetworkMonitorTest { eq(null)); // Fix DNS and retry, expect validation to succeed. reset(mCallbacks); resetCallbacks(); mFakeDns.setAnswer("dns.google", new String[]{"2001:db8::1"}); wnm.forceReevaluation(Process.myUid()); Loading @@ -767,7 +780,7 @@ public class NetworkMonitorTest { eq(null)); // Change configuration to an invalid DNS name, expect validation to fail. reset(mCallbacks); resetCallbacks(); mFakeDns.setAnswer("dns.bad", new String[0]); wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.bad", new InetAddress[0])); // Strict mode hostname resolve fail. Expect only notification for evaluation fail. No probe Loading @@ -778,7 +791,7 @@ public class NetworkMonitorTest { // Change configuration back to working again, but make private DNS not work. // Expect validation to fail. reset(mCallbacks); resetCallbacks(); mFakeDns.setNonBypassPrivateDnsWorking(false); wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", new InetAddress[0])); Loading @@ -788,7 +801,7 @@ public class NetworkMonitorTest { eq(null)); // Make private DNS work again. Expect validation to succeed. reset(mCallbacks); resetCallbacks(); mFakeDns.setNonBypassPrivateDnsWorking(true); wnm.forceReevaluation(Process.myUid()); verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) Loading Loading @@ -856,7 +869,7 @@ public class NetworkMonitorTest { // Expect to send HTTP, HTTPS, FALLBACK probe and evaluation result notifications to CS. final NetworkMonitor nm = runNetworkTest(VALIDATION_RESULT_PARTIAL); reset(mCallbacks); resetCallbacks(); nm.setAcceptPartialConnectivity(); // Expect to update evaluation result notifications to CS. verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyNetworkTested( Loading @@ -870,7 +883,7 @@ public class NetworkMonitorTest { setStatus(mFallbackConnection, 500); runPartialConnectivityNetworkTest(VALIDATION_RESULT_PARTIAL); reset(mCallbacks); resetCallbacks(); setStatus(mHttpsConnection, 500); setStatus(mHttpConnection, 500); setStatus(mFallbackConnection, 204); Loading Loading @@ -923,7 +936,7 @@ public class NetworkMonitorTest { final NetworkMonitor nm = runValidatedNetworkTest(); // Verify forceReevalution will not reset the validation result but only probe result until // getting the validation result. reset(mCallbacks); resetCallbacks(); setSslException(mHttpsConnection); setStatus(mHttpConnection, 500); setStatus(mFallbackConnection, 204); Loading @@ -948,7 +961,7 @@ public class NetworkMonitorTest { public void testEvaluationState_reportProbeResult() throws Exception { final NetworkMonitor nm = runValidatedNetworkTest(); reset(mCallbacks); resetCallbacks(); nm.reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, CaptivePortalProbeResult.SUCCESS); // Verify result should be appended and notifyNetworkTested callback is triggered once. Loading Loading
src/com/android/server/connectivity/NetworkMonitor.java +26 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,9 @@ import static android.net.ConnectivityManager.EXTRA_CAPTIVE_PORTAL_URL; import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.DnsResolver.FLAG_EMPTY; import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_INVALID; import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY; import static android.net.INetworkMonitor.NETWORK_TEST_RESULT_VALID; import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_DNS; import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_FALLBACK; import static android.net.INetworkMonitor.NETWORK_VALIDATION_PROBE_HTTP; Loading Loading @@ -93,6 +96,7 @@ import android.net.util.SharedLog; import android.net.util.Stopwatch; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.os.Message; import android.os.RemoteException; Loading Loading @@ -286,6 +290,7 @@ public class NetworkMonitor extends StateMachine { private final Context mContext; private final INetworkMonitorCallbacks mCallback; private final int mCallbackVersion; private final Network mCleartextDnsNetwork; private final Network mNetwork; private final TelephonyManager mTelephonyManager; Loading Loading @@ -355,6 +360,17 @@ public class NetworkMonitor extends StateMachine { private boolean mAcceptPartialConnectivity = false; private final EvaluationState mEvaluationState = new EvaluationState(); private int getCallbackVersion(INetworkMonitorCallbacks cb) { int version; try { version = cb.getInterfaceVersion(); } catch (RemoteException e) { version = 0; } if (version == Build.VERSION_CODES.CUR_DEVELOPMENT) version = 0; return version; } public NetworkMonitor(Context context, INetworkMonitorCallbacks cb, Network network, SharedLog validationLog) { this(context, cb, network, new IpConnectivityLog(), validationLog, Loading @@ -376,6 +392,7 @@ public class NetworkMonitor extends StateMachine { mMetricsLog = logger; mValidationLogs = validationLogs; mCallback = cb; mCallbackVersion = getCallbackVersion(cb); mDependencies = deps; mDetectionStatsUtils = detectionStatsUtils; mNetwork = network; Loading Loading @@ -2113,6 +2130,15 @@ public class NetworkMonitor extends StateMachine { } protected int getNetworkTestResult() { if (mCallbackVersion < 3) { if ((mEvaluationResult & NETWORK_VALIDATION_RESULT_VALID) != 0) { return NETWORK_TEST_RESULT_VALID; } if ((mEvaluationResult & NETWORK_VALIDATION_RESULT_PARTIAL) != 0) { return NETWORK_TEST_RESULT_PARTIAL_CONNECTIVITY; } return NETWORK_TEST_RESULT_INVALID; } return mEvaluationResult | mProbeResults; } } Loading
tests/unit/src/com/android/server/connectivity/NetworkMonitorTest.java +23 −10 Original line number Diff line number Diff line Loading @@ -287,7 +287,7 @@ public class NetworkMonitorTest { private FakeDns mFakeDns; @Before public void setUp() throws IOException { public void setUp() throws Exception { MockitoAnnotations.initMocks(this); when(mDependencies.getPrivateDnsBypassNetwork(any())).thenReturn(mCleartextDnsNetwork); when(mDependencies.getDnsResolver()).thenReturn(mDnsResolver); Loading Loading @@ -355,6 +355,8 @@ public class NetworkMonitorTest { return null; }).when(mContext).unregisterReceiver(any()); resetCallbacks(); setMinDataStallEvaluateInterval(500); setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS); setValidDataStallDnsTimeThreshold(500); Loading Loading @@ -382,6 +384,17 @@ public class NetworkMonitorTest { 0, mRegisteredReceivers.size()); } private void resetCallbacks() { reset(mCallbacks); // TODO: make this a parameterized test. try { when(mCallbacks.getInterfaceVersion()).thenReturn(3); } catch (RemoteException e) { // Can't happen as mCallbacks is a mock fail("Error mocking getInterfaceVersion" + e); } } private class WrappedNetworkMonitor extends NetworkMonitor { private long mProbeTime = 0; private final ConditionVariable mQuitCv = new ConditionVariable(false); Loading Loading @@ -720,7 +733,7 @@ public class NetworkMonitorTest { setStatus(mHttpsConnection, 204); setStatus(mHttpConnection, 204); reset(mCallbacks); resetCallbacks(); nm.notifyCaptivePortalAppFinished(APP_RETURN_DISMISSED); verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) .notifyNetworkTested(eq(NETWORK_VALIDATION_PROBE_DNS | NETWORK_VALIDATION_PROBE_HTTP Loading Loading @@ -758,7 +771,7 @@ public class NetworkMonitorTest { eq(null)); // Fix DNS and retry, expect validation to succeed. reset(mCallbacks); resetCallbacks(); mFakeDns.setAnswer("dns.google", new String[]{"2001:db8::1"}); wnm.forceReevaluation(Process.myUid()); Loading @@ -767,7 +780,7 @@ public class NetworkMonitorTest { eq(null)); // Change configuration to an invalid DNS name, expect validation to fail. reset(mCallbacks); resetCallbacks(); mFakeDns.setAnswer("dns.bad", new String[0]); wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.bad", new InetAddress[0])); // Strict mode hostname resolve fail. Expect only notification for evaluation fail. No probe Loading @@ -778,7 +791,7 @@ public class NetworkMonitorTest { // Change configuration back to working again, but make private DNS not work. // Expect validation to fail. reset(mCallbacks); resetCallbacks(); mFakeDns.setNonBypassPrivateDnsWorking(false); wnm.notifyPrivateDnsSettingsChanged(new PrivateDnsConfig("dns.google", new InetAddress[0])); Loading @@ -788,7 +801,7 @@ public class NetworkMonitorTest { eq(null)); // Make private DNS work again. Expect validation to succeed. reset(mCallbacks); resetCallbacks(); mFakeDns.setNonBypassPrivateDnsWorking(true); wnm.forceReevaluation(Process.myUid()); verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).atLeastOnce()) Loading Loading @@ -856,7 +869,7 @@ public class NetworkMonitorTest { // Expect to send HTTP, HTTPS, FALLBACK probe and evaluation result notifications to CS. final NetworkMonitor nm = runNetworkTest(VALIDATION_RESULT_PARTIAL); reset(mCallbacks); resetCallbacks(); nm.setAcceptPartialConnectivity(); // Expect to update evaluation result notifications to CS. verify(mCallbacks, timeout(HANDLER_TIMEOUT_MS).times(1)).notifyNetworkTested( Loading @@ -870,7 +883,7 @@ public class NetworkMonitorTest { setStatus(mFallbackConnection, 500); runPartialConnectivityNetworkTest(VALIDATION_RESULT_PARTIAL); reset(mCallbacks); resetCallbacks(); setStatus(mHttpsConnection, 500); setStatus(mHttpConnection, 500); setStatus(mFallbackConnection, 204); Loading Loading @@ -923,7 +936,7 @@ public class NetworkMonitorTest { final NetworkMonitor nm = runValidatedNetworkTest(); // Verify forceReevalution will not reset the validation result but only probe result until // getting the validation result. reset(mCallbacks); resetCallbacks(); setSslException(mHttpsConnection); setStatus(mHttpConnection, 500); setStatus(mFallbackConnection, 204); Loading @@ -948,7 +961,7 @@ public class NetworkMonitorTest { public void testEvaluationState_reportProbeResult() throws Exception { final NetworkMonitor nm = runValidatedNetworkTest(); reset(mCallbacks); resetCallbacks(); nm.reportHttpProbeResult(NETWORK_VALIDATION_PROBE_HTTP, CaptivePortalProbeResult.SUCCESS); // Verify result should be appended and notifyNetworkTested callback is triggered once. Loading