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

Commit cf1ce84f authored by Paul Hu's avatar Paul Hu
Browse files

Reset VpnConnectivityMetrics on VPN disconnect

Upon VPN disconnection, the associated NetworkAgent becomes
unregistered. To ensure that metrics collected for subsequent
VPN connections are accurate and not influenced by prior data,
the VpnConnectivityMetrics state should be reset at this point.

Bug: 306313287
Test: atest FrameworksVpnTests
Flag: android.net.platform.flags.collect_vpn_metrics
Change-Id: I95c190dc34b8b00aa46562b4cdd8dbffb8d2c961
parent 3df7f596
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -747,6 +747,8 @@ public class Vpn {
                    mNetworkAgent = null;
                    if (isVpnMetricsLoggable()) {
                        mVpnConnectivityMetrics.notifyVpnDisconnected();
                        // Clear the metrics since the NetworkAgent is disconnected.
                        mVpnConnectivityMetrics.resetMetrics();
                    }
                }
                break;
+16 −0
Original line number Diff line number Diff line
@@ -391,4 +391,20 @@ public class VpnConnectivityMetrics {
    public void notifyVpnDisconnected() {
        validateAndReportVpnConnectionEvent(false /* connected */);
    }

    /**
     * Resets all internal VPN metrics to their default states.
     * <p>
     * This method should be called to ensure a clean state.
     * </p>
     */
    public void resetMetrics() {
        mVpnType = VPN_TYPE_UNKNOWN;
        mVpnNetworkIpProtocol = IP_PROTOCOL_UNKNOWN;
        mServerIpProtocol = IP_PROTOCOL_UNKNOWN;
        mVpnProfileType = VPN_PROFILE_TYPE_UNKNOWN;
        mAllowedAlgorithms = 0;
        mMtu = 0;
        mUnderlyingNetworkTypes = new int[0];
    }
}
+45 −0
Original line number Diff line number Diff line
@@ -235,4 +235,49 @@ public class VpnConnectivityMetricsTest {
                    USER_ID);
        }, () -> Log.setWtfHandler(originalHandler));
    }

    @Test
    public void testResetMetrics() {
        final Network cellNetwork = new Network(1234);
        final NetworkCapabilities cellCap = new NetworkCapabilities();
        cellCap.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR);
        doReturn(cellCap).when(mCm).getNetworkCapabilities(cellNetwork);

        // Fill in metrics data
        mMetrics.setVpnType(VpnManager.TYPE_VPN_PLATFORM);
        mMetrics.setMtu(1327);
        mMetrics.setVpnProfileType(VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS);
        mMetrics.setAllowedAlgorithms(Ikev2VpnProfile.DEFAULT_ALGORITHMS);
        mMetrics.setUnderlyingNetwork(new Network[] { cellNetwork });
        mMetrics.setVpnNetworkIpProtocol(
                List.of(new LinkAddress(VPN_CLIENT_IP_V4), new LinkAddress(VPN_CLIENT_IP_V6)));
        mMetrics.setServerIpProtocol(InetAddresses.parseNumericAddress(VPN_SERVER_IP_V4));

        // Verify a vpn connected event with the filled in data.
        mMetrics.notifyVpnConnected();
        verify(mDeps).statsWrite(
                VpnManager.TYPE_VPN_PLATFORM,
                IP_PROTOCOL_IPv4v6,
                IP_PROTOCOL_IPv4,
                VpnProfile.TYPE_IKEV2_IPSEC_USER_PASS + 1,
                1999 /* allowedAlgorithms */,
                1327 /* mtu */,
                new int[] { NetworkCapabilities.TRANSPORT_CELLULAR },
                true /* connected */,
                USER_ID);

        // Reset all metrics and verify again. All data should be the default value.
        mMetrics.resetMetrics();
        mMetrics.notifyVpnConnected();
        verify(mDeps).statsWrite(
                VPN_TYPE_UNKNOWN,
                IP_PROTOCOL_UNKNOWN,
                IP_PROTOCOL_UNKNOWN,
                VPN_PROFILE_TYPE_UNKNOWN,
                0 /* allowedAlgorithms */,
                0 /* mtu */,
                new int[0],
                true /* connected */,
                USER_ID);
    }
}
+1 −0
Original line number Diff line number Diff line
@@ -3031,6 +3031,7 @@ public class VpnTest extends VpnTestBase {
        verify(mMockNetworkAgent, timeout(TEST_TIMEOUT_MS)).unregister();
        // Verify disconnection metrics notification.
        verify(mVpnConnectivityMetrics).notifyVpnDisconnected();
        verify(mVpnConnectivityMetrics).resetMetrics();
    }

    @Test