Merge "Add conversion util methods for EthernetPortSelector" into main

This commit is contained in:
Chuwei He 2025-09-07 18:37:16 -07:00 • committed by Android (Google) Code Review
commit 54b93bb3c5
2 changed files with 111 additions and 0 deletions

View file

@ -19,10 +19,14 @@ import static java.util.Objects.requireNonNull;
import android.net.EthernetConfiguration;
import android.net.EthernetConfiguration.MeteredOverride;
import android.net.EthernetPortSelector;
import android.net.InetAddresses;
import android.net.LinkAddress;
import android.net.MacAddress;
import android.text.TextUtils;
import android.util.Log;
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.EthernetPortSelectorProto;
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.LinkAddressProto;
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.MeteredOverrideProto;
@ -96,4 +100,47 @@ public class ProtoConfigUtils {
return new LinkAddress(InetAddresses.parseNumericAddress(linkAddress.getAddress()),
linkAddress.getPrefixLength());
}
/**
* Converts an {@link EthernetPortSelector} object to a corresponding
* {@link EthernetPortSelectorProto} object.
*
* @throws IllegalArgumentException if both MAC address and interface name in
* {@code portSelector} are invalid.
*/
public static EthernetPortSelectorProto convertPortSelectorToProto(
EthernetPortSelector portSelector) {
requireNonNull(portSelector, "EthernetPortSelector must not be null");
EthernetPortSelectorProto.Builder selectorBuilder =
EthernetPortSelectorProto.newBuilder();
if (portSelector.getMacAddress() != null) {
selectorBuilder.setMacAddr(portSelector.getMacAddress().toString());
} else if (!TextUtils.isEmpty(portSelector.getInterfaceName())) {
selectorBuilder.setIfaceName(portSelector.getInterfaceName());
} else {
// This branch should not be reached theoretically. Added for safety reason.
throw new IllegalArgumentException("Both MAC address and interface name are invalid in "
+ "port selector: " + portSelector);
}
return selectorBuilder.build();
}
/**
* Converts an {@link EthernetPortSelectorProto} object to a corresponding value of
* {@link EthernetPortSelector} type.
*
* @throws IllegalArgumentException if both MAC address and interface in {@code portSelector}
* are invalid.
*/
public static EthernetPortSelector convertPortSelectorFromProto(
EthernetPortSelectorProto portSelector) {
requireNonNull(portSelector, "EthernetPortSelectorProto must not be null");
if (portSelector.hasMacAddr()) {
return new EthernetPortSelector(MacAddress.fromString(portSelector.getMacAddr()));
} else if (portSelector.hasIfaceName()) {
return new EthernetPortSelector(portSelector.getIfaceName());
} else {
throw new IllegalArgumentException("No MAC address or interface is found.");
}
}
}

View file

@ -17,11 +17,18 @@
package com.android.server.net;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertThrows;
import static org.junit.Assert.assertTrue;
import android.net.EthernetConfiguration;
import android.net.EthernetPortSelector;
import android.net.LinkAddress;
import android.net.MacAddress;
import android.os.Build;
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.EthernetPortSelectorProto;
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.LinkAddressProto;
import com.android.server.network.configstore.proto.NetworkConfigStoreProto.MeteredOverrideProto;
import com.android.testutils.DevSdkIgnoreRule;
@ -39,6 +46,8 @@ public class ProtoConfigUtilsTest {
private static final String LINK_ADDRESS_STRING = "192.168.1.10/24";
private static final String IP_ADDRESS_STRING = "192.168.1.10";
private static final int PREFIX_LENGTH = 24;
private static final String IFACE_NAME = "eth0";
private static final MacAddress MAC_ADDR = MacAddress.fromString("aa:bb:cc:dd:ee:11");
@Test
public void testconvertMeteredOverrideToProto() {
@ -86,4 +95,59 @@ public class ProtoConfigUtilsTest {
LinkAddress target = new LinkAddress(LINK_ADDRESS_STRING);
assertEquals(actual, target);
}
@Test
public void testConvertPortSelectorToProto_withMacAddress_setsMacAddrField() {
final EthernetPortSelector portSelector = new EthernetPortSelector(MAC_ADDR);
final EthernetPortSelectorProto proto =
ProtoConfigUtils.convertPortSelectorToProto(portSelector);
assertNotNull(proto);
assertTrue(proto.hasMacAddr());
assertFalse(proto.hasIfaceName());
assertEquals(MAC_ADDR.toString(), proto.getMacAddr());
}
@Test
public void testConvertPortSelectorToProto_withInterfaceName_setsIfaceNameField() {
final EthernetPortSelector portSelector = new EthernetPortSelector(IFACE_NAME);
final EthernetPortSelectorProto proto =
ProtoConfigUtils.convertPortSelectorToProto(portSelector);
assertNotNull(proto);
assertTrue(proto.hasIfaceName());
assertFalse(proto.hasMacAddr());
assertEquals(IFACE_NAME, proto.getIfaceName());
}
@Test
public void testConvertPortSelectorFromProto_withMacAddress() {
EthernetPortSelectorProto proto = EthernetPortSelectorProto.newBuilder()
.setMacAddr(MAC_ADDR.toString())
.build();
EthernetPortSelector selector = ProtoConfigUtils.convertPortSelectorFromProto(proto);
assertEquals(MAC_ADDR, selector.getMacAddress());
}
@Test
public void testConvertPortSelectorFromProto_withInterfaceName() {
EthernetPortSelectorProto proto = EthernetPortSelectorProto.newBuilder()
.setIfaceName(IFACE_NAME)
.build();
EthernetPortSelector selector = ProtoConfigUtils.convertPortSelectorFromProto(proto);
assertEquals(IFACE_NAME, selector.getInterfaceName());
}
@Test
public void testConvertPortSelectorFromProto_emptyProto_throwsException() {
EthernetPortSelectorProto emptyProto = EthernetPortSelectorProto.newBuilder().build();
assertThrows(IllegalArgumentException.class, () ->
ProtoConfigUtils.convertPortSelectorFromProto(emptyProto));
}
}