Add conversion util methods for MeteredOverride

This patch adds util methods to convert MeteredOverride to and from
its proto equivalent, and also adds unit tests to test the conversion.

Test: atest ConnectivityCoverageTests
Bug: 397536853
Flag: EXEMPT mainline
Change-Id: Ie19a19c1124e87b8e23a9b4e216e170e1cfb22b2
This commit is contained in:
Chuwei He 2025-09-03 13:16:00 +09:00
commit b5ad6f83b2
2 changed files with 134 additions and 0 deletions

View file

@ -0,0 +1,72 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.net;
import static java.util.Objects.requireNonNull;
import android.net.EthernetConfiguration;
import android.net.EthernetConfiguration.MeteredOverride;
import android.util.Log;
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.MeteredOverrideProto;
/**
* Static util methods for {@link ProtoConfig}.
*/
public class ProtoConfigUtils {
private static final String TAG = "ProtoConfigUtils";
/**
* Converts an {@link EthernetConfiguration.MeteredOverride} value to a corresponding value of
* {@link MeteredOverrideProto} type.
*/
public static MeteredOverrideProto convertMeteredOverrideToProto(
@MeteredOverride int override) {
return switch (override) {
case EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED ->
MeteredOverrideProto.METERED_OVERRIDE_FORCE_METERED;
case EthernetConfiguration.METERED_OVERRIDE_FORCE_UNMETERED ->
MeteredOverrideProto.METERED_OVERRIDE_FORCE_UNMETERED;
case EthernetConfiguration.METERED_OVERRIDE_NONE ->
MeteredOverrideProto.METERED_OVERRIDE_NONE;
default -> {
Log.e(TAG, "Ignore invalid metered override: " + override);
yield MeteredOverrideProto.METERED_OVERRIDE_NONE;
}
};
}
/**
* Converts a {@link MeteredOverrideProto} value to a corresponding value of
* {@link EthernetConfiguration.MeteredOverride} type.
*/
public static @MeteredOverride int convertMeteredOverrideFromProto(
MeteredOverrideProto protoOverride) {
requireNonNull(protoOverride, "MeteredOverrideProto must not be null");
return switch (protoOverride) {
case METERED_OVERRIDE_FORCE_METERED ->
EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED;
case METERED_OVERRIDE_FORCE_UNMETERED ->
EthernetConfiguration.METERED_OVERRIDE_FORCE_UNMETERED;
case METERED_OVERRIDE_NONE ->
EthernetConfiguration.METERED_OVERRIDE_NONE;
default -> {
Log.e(TAG, "Ignore invalid metered override: " + protoOverride);
yield EthernetConfiguration.METERED_OVERRIDE_NONE;
}
};
}
}

View file

@ -0,0 +1,62 @@
/*
* Copyright (C) 2025 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.server.net;
import static org.junit.Assert.assertEquals;
import android.net.EthernetConfiguration;
import android.os.Build;
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.MeteredOverrideProto;
import com.android.testutils.DevSdkIgnoreRule;
import com.android.testutils.DevSdkIgnoreRunner;
import org.junit.Test;
import org.junit.runner.RunWith;
/**
* Unit tests for {@link ProtoConfigUtils}
*/
@RunWith(DevSdkIgnoreRunner.class)
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
public class ProtoConfigUtilsTest {
@Test
public void testconvertMeteredOverrideToProto() {
assertEquals(MeteredOverrideProto.METERED_OVERRIDE_FORCE_METERED,
ProtoConfigUtils.convertMeteredOverrideToProto(
EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED));
assertEquals(MeteredOverrideProto.METERED_OVERRIDE_FORCE_UNMETERED,
ProtoConfigUtils.convertMeteredOverrideToProto(
EthernetConfiguration.METERED_OVERRIDE_FORCE_UNMETERED));
assertEquals(MeteredOverrideProto.METERED_OVERRIDE_NONE,
ProtoConfigUtils.convertMeteredOverrideToProto(
EthernetConfiguration.METERED_OVERRIDE_NONE));
}
@Test
public void testConvertMeteredOverrideFromProto() {
assertEquals(EthernetConfiguration.METERED_OVERRIDE_FORCE_METERED,
ProtoConfigUtils.convertMeteredOverrideFromProto(
MeteredOverrideProto.METERED_OVERRIDE_FORCE_METERED));
assertEquals(EthernetConfiguration.METERED_OVERRIDE_FORCE_UNMETERED,
ProtoConfigUtils.convertMeteredOverrideFromProto(
MeteredOverrideProto.METERED_OVERRIDE_FORCE_UNMETERED));
assertEquals(EthernetConfiguration.METERED_OVERRIDE_NONE,
ProtoConfigUtils.convertMeteredOverrideFromProto(
MeteredOverrideProto.METERED_OVERRIDE_NONE));
}
}