Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b4af1302 authored by Remi NGUYEN VAN's avatar Remi NGUYEN VAN
Browse files

Support beta 4 system images.

Beta 4 system images only understand the NETWORK_TEST_RESULT_*
constants and not the new NETWORK_VALIDATION_RESULT_* constants.
When talking to such a build, use the older constants.

Bug: 134923144
Test: atest NetorkStackTests
Test: beta 4 OS build validates
(patched from Icf28c5dcd622c89c411a87b63d2ce4aa7488d082 using:
git -C ~/other-branch-repo/frameworks/base/ show -p | patch -p3)
Change-Id: I749a228711b1b7ea9b5f87be5da3b026aaed0807
parent 67be1d71
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -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;
@@ -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;
@@ -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;
@@ -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,
@@ -376,6 +392,7 @@ public class NetworkMonitor extends StateMachine {
        mMetricsLog = logger;
        mValidationLogs = validationLogs;
        mCallback = cb;
        mCallbackVersion = getCallbackVersion(cb);
        mDependencies = deps;
        mDetectionStatsUtils = detectionStatsUtils;
        mNetwork = network;
@@ -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;
        }
    }
+23 −10
Original line number Diff line number Diff line
@@ -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);
@@ -355,6 +355,8 @@ public class NetworkMonitorTest {
            return null;
        }).when(mContext).unregisterReceiver(any());

        resetCallbacks();

        setMinDataStallEvaluateInterval(500);
        setDataStallEvaluationType(DATA_STALL_EVALUATION_TYPE_DNS);
        setValidDataStallDnsTimeThreshold(500);
@@ -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);
@@ -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
@@ -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());
@@ -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
@@ -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]));
@@ -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())
@@ -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(
@@ -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);
@@ -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);
@@ -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.