Allow all device types to call updateConfiguration

Usage of this API should not be limited to Automotive devices as
TvSettings also needs to update the IpConfiguration.

Test: TH
Change-Id: I838a0a8684e9f944801718a4d688666de45f42fb
This commit is contained in:
Patrick Rohr
2022-03-08 13:10:18 +01:00
parent 0f66192d88
commit 24fb1ef5b0
2 changed files with 59 additions and 34 deletions

View File

@@ -215,45 +215,34 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
"EthernetServiceImpl"); "EthernetServiceImpl");
} }
/**
* Validate the state of ethernet for APIs tied to network management.
*
* @param iface the ethernet interface name to operate on.
* @param methodName the name of the calling method.
*/
private void validateNetworkManagementState(@NonNull final String iface,
final @NonNull String methodName) {
Objects.requireNonNull(iface, "Pass a non-null iface.");
Objects.requireNonNull(methodName, "Pass a non-null methodName.");
// Only bypass the permission/device checks if this is a valid test interface.
if (mTracker.isValidTestInterface(iface)) {
enforceManageTestNetworksPermission();
Log.i(TAG, "Ethernet network management API used with test interface " + iface);
} else {
enforceAutomotiveDevice(methodName);
enforceNetworkManagementPermission();
}
logIfEthernetNotStarted();
}
private void validateTestCapabilities(@Nullable final NetworkCapabilities nc) { private void validateTestCapabilities(@Nullable final NetworkCapabilities nc) {
if (null != nc && nc.hasTransport(TRANSPORT_TEST)) { // For test capabilities, only null or capabilities that include TRANSPORT_TEST are allowed.
return; if (nc != null && !nc.hasTransport(TRANSPORT_TEST)) {
throw new IllegalArgumentException(
"Updates to test interfaces must have NetworkCapabilities.TRANSPORT_TEST.");
} }
throw new IllegalArgumentException(
"Updates to test interfaces must have NetworkCapabilities.TRANSPORT_TEST.");
} }
@Override @Override
public void updateConfiguration(@NonNull final String iface, public void updateConfiguration(@NonNull final String iface,
@NonNull final EthernetNetworkUpdateRequest request, @NonNull final EthernetNetworkUpdateRequest request,
@Nullable final IEthernetNetworkManagementListener listener) { @Nullable final IEthernetNetworkManagementListener listener) {
validateNetworkManagementState(iface, "updateConfiguration()"); Objects.requireNonNull(iface);
Objects.requireNonNull(request);
// TODO: rename to throwIfEthernetNotStarted.
logIfEthernetNotStarted();
if (mTracker.isValidTestInterface(iface)) { if (mTracker.isValidTestInterface(iface)) {
enforceManageTestNetworksPermission();
validateTestCapabilities(request.getNetworkCapabilities()); validateTestCapabilities(request.getNetworkCapabilities());
// TODO: use NetworkCapabilities#restrictCapabilitiesForTestNetwork when available on a // TODO: use NetworkCapabilities#restrictCapabilitiesForTestNetwork when available on a
// local NetworkCapabilities copy to pass to mTracker.updateConfiguration. // local NetworkCapabilities copy to pass to mTracker.updateConfiguration.
} else {
enforceNetworkManagementPermission();
if (request.getNetworkCapabilities() != null) {
// only automotive devices are allowed to set the NetworkCapabilities using this API
enforceAutomotiveDevice("updateConfiguration() with non-null capabilities");
}
} }
// TODO: validate that iface is listed in overlay config_ethernet_interfaces // TODO: validate that iface is listed in overlay config_ethernet_interfaces
@@ -265,7 +254,17 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
public void connectNetwork(@NonNull final String iface, public void connectNetwork(@NonNull final String iface,
@Nullable final IEthernetNetworkManagementListener listener) { @Nullable final IEthernetNetworkManagementListener listener) {
Log.i(TAG, "connectNetwork called with: iface=" + iface + ", listener=" + listener); Log.i(TAG, "connectNetwork called with: iface=" + iface + ", listener=" + listener);
validateNetworkManagementState(iface, "connectNetwork()"); Objects.requireNonNull(iface);
logIfEthernetNotStarted();
if (mTracker.isValidTestInterface(iface)) {
enforceManageTestNetworksPermission();
} else {
// only automotive devices are allowed to use this API.
enforceNetworkManagementPermission();
enforceAutomotiveDevice("connectNetwork()");
}
mTracker.connectNetwork(iface, listener); mTracker.connectNetwork(iface, listener);
} }
@@ -273,7 +272,17 @@ public class EthernetServiceImpl extends IEthernetManager.Stub {
public void disconnectNetwork(@NonNull final String iface, public void disconnectNetwork(@NonNull final String iface,
@Nullable final IEthernetNetworkManagementListener listener) { @Nullable final IEthernetNetworkManagementListener listener) {
Log.i(TAG, "disconnectNetwork called with: iface=" + iface + ", listener=" + listener); Log.i(TAG, "disconnectNetwork called with: iface=" + iface + ", listener=" + listener);
validateNetworkManagementState(iface, "disconnectNetwork()"); Objects.requireNonNull(iface);
logIfEthernetNotStarted();
if (mTracker.isValidTestInterface(iface)) {
enforceManageTestNetworksPermission();
} else {
// only automotive devices are allowed to use this API.
enforceNetworkManagementPermission();
enforceAutomotiveDevice("disconnectNetwork()");
}
mTracker.disconnectNetwork(iface, listener); mTracker.disconnectNetwork(iface, listener);
} }
} }

View File

@@ -22,6 +22,7 @@ import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
@@ -55,6 +56,10 @@ public class EthernetServiceImplTest {
.setIpConfiguration(new IpConfiguration()) .setIpConfiguration(new IpConfiguration())
.setNetworkCapabilities(new NetworkCapabilities.Builder().build()) .setNetworkCapabilities(new NetworkCapabilities.Builder().build())
.build(); .build();
private static final EthernetNetworkUpdateRequest UPDATE_REQUEST_WITHOUT_CAPABILITIES =
new EthernetNetworkUpdateRequest.Builder()
.setIpConfiguration(new IpConfiguration())
.build();
private static final IEthernetNetworkManagementListener NULL_LISTENER = null; private static final IEthernetNetworkManagementListener NULL_LISTENER = null;
private EthernetServiceImpl mEthernetServiceImpl; private EthernetServiceImpl mEthernetServiceImpl;
@Mock private Context mContext; @Mock private Context mContext;
@@ -136,13 +141,23 @@ public class EthernetServiceImplTest {
} }
@Test @Test
public void testUpdateConfigurationRejectsWithoutAutomotiveFeature() { public void testUpdateConfigurationWithCapabilitiesRejectsWithoutAutomotiveFeature() {
toggleAutomotiveFeature(false); toggleAutomotiveFeature(false);
assertThrows(UnsupportedOperationException.class, () -> { assertThrows(UnsupportedOperationException.class, () -> {
mEthernetServiceImpl.updateConfiguration(TEST_IFACE, UPDATE_REQUEST, NULL_LISTENER); mEthernetServiceImpl.updateConfiguration(TEST_IFACE, UPDATE_REQUEST, NULL_LISTENER);
}); });
} }
@Test
public void testUpdateConfigurationWithCapabilitiesWithAutomotiveFeature() {
toggleAutomotiveFeature(false);
mEthernetServiceImpl.updateConfiguration(TEST_IFACE, UPDATE_REQUEST_WITHOUT_CAPABILITIES,
NULL_LISTENER);
verify(mEthernetTracker).updateConfiguration(eq(TEST_IFACE),
eq(UPDATE_REQUEST_WITHOUT_CAPABILITIES.getIpConfiguration()),
eq(UPDATE_REQUEST_WITHOUT_CAPABILITIES.getNetworkCapabilities()), isNull());
}
@Test @Test
public void testConnectNetworkRejectsWithoutAutomotiveFeature() { public void testConnectNetworkRejectsWithoutAutomotiveFeature() {
toggleAutomotiveFeature(false); toggleAutomotiveFeature(false);
@@ -248,15 +263,16 @@ public class EthernetServiceImplTest {
} }
@Test @Test
public void testUpdateConfigurationRejectsTestRequestWithNullCapabilities() { public void testUpdateConfigurationAcceptsTestRequestWithNullCapabilities() {
enableTestInterface(); enableTestInterface();
final EthernetNetworkUpdateRequest request = final EthernetNetworkUpdateRequest request =
new EthernetNetworkUpdateRequest new EthernetNetworkUpdateRequest
.Builder() .Builder()
.setIpConfiguration(new IpConfiguration()).build(); .setIpConfiguration(new IpConfiguration()).build();
assertThrows(IllegalArgumentException.class, () -> { mEthernetServiceImpl.updateConfiguration(TEST_IFACE, request, NULL_LISTENER);
mEthernetServiceImpl.updateConfiguration(TEST_IFACE, request, NULL_LISTENER); verify(mEthernetTracker).updateConfiguration(eq(TEST_IFACE),
}); eq(request.getIpConfiguration()),
eq(request.getNetworkCapabilities()), isNull());
} }
@Test @Test