summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJames Mattis <jmattis@google.com>2021-12-07 15:47:11 -0800
committerJames Mattis <jmattis@google.com>2021-12-07 16:43:33 -0800
commit6f66d134c8e3a41e4aaaa6c88c0a2df35427f268 (patch)
treed29ae528c6a90e6ab8c75b7dfe36e94b8360ec7a
parentd4f2d3fa05bcceb646c63988afc4900858bd0b01 (diff)
downloadethernet-6f66d134c8e3a41e4aaaa6c88c0a2df35427f268.tar.gz
Adding test coverage for eth transport overrides
Added test coverage regarding how EthernetTracker consumes the xml config as set in config_ethernet_interfaces. Additionally, adding test coverage for transport override functionality in EthernetNetworkFactory. Bug: 206170402 Test: atest com.android.server.ethernet.EthernetTrackerTest && atest com.android.server.ethernet.EthernetNetworkFactoryTest Change-Id: I3d55a230d3f28154ed6fdac6d63ed57bf589d3bd
-rw-r--r--java/com/android/server/ethernet/EthernetTracker.java40
-rw-r--r--tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java78
-rw-r--r--tests/java/com/android/server/ethernet/EthernetTrackerTest.java24
3 files changed, 116 insertions, 26 deletions
diff --git a/java/com/android/server/ethernet/EthernetTracker.java b/java/com/android/server/ethernet/EthernetTracker.java
index d79c7a9..86aefbc 100644
--- a/java/com/android/server/ethernet/EthernetTracker.java
+++ b/java/com/android/server/ethernet/EthernetTracker.java
@@ -18,6 +18,7 @@ package com.android.server.ethernet;
import static android.net.TestNetworkManager.TEST_TAP_PREFIX;
+import android.annotation.NonNull;
import android.annotation.Nullable;
import android.content.Context;
import android.net.IEthernetServiceListener;
@@ -453,21 +454,24 @@ final class EthernetTracker {
* <interface name|mac address>;[Network Capabilities];[IP config];[Override Transport]}
*/
private void parseEthernetConfig(String configString) {
- String[] tokens = configString.split(";", /* limit of tokens */ 4);
- String name = tokens[0];
- String capabilities = tokens.length > 1 ? tokens[1] : null;
- String transport = tokens.length > 3 ? tokens[3] : null;
+ final EthernetTrackerConfig config = createEthernetTrackerConfig(configString);
NetworkCapabilities nc = createNetworkCapabilities(
- !TextUtils.isEmpty(capabilities) /* clear default capabilities */, capabilities,
- transport).build();
- mNetworkCapabilities.put(name, nc);
+ !TextUtils.isEmpty(config.mCapabilities) /* clear default capabilities */,
+ config.mCapabilities, config.mTransport).build();
+ mNetworkCapabilities.put(config.mName, nc);
- if (tokens.length > 2 && !TextUtils.isEmpty(tokens[2])) {
- IpConfiguration ipConfig = parseStaticIpConfiguration(tokens[2]);
- mIpConfigurations.put(name, ipConfig);
+ if (null != config.mIpConfig) {
+ IpConfiguration ipConfig = parseStaticIpConfiguration(config.mIpConfig);
+ mIpConfigurations.put(config.mName, ipConfig);
}
}
+ @VisibleForTesting
+ static EthernetTrackerConfig createEthernetTrackerConfig(@NonNull final String configString) {
+ Objects.requireNonNull(configString, "EthernetTrackerConfig requires non-null config");
+ return new EthernetTrackerConfig(configString.split(";", /* limit of tokens */ 4));
+ }
+
private static NetworkCapabilities createDefaultNetworkCapabilities(boolean isTestIface) {
NetworkCapabilities.Builder builder = createNetworkCapabilities(
false /* clear default capabilities */, null, null)
@@ -672,4 +676,20 @@ final class EthernetTracker {
mFactory.dump(fd, pw, args);
});
}
+
+ @VisibleForTesting
+ static class EthernetTrackerConfig {
+ final String mName;
+ final String mCapabilities;
+ final String mIpConfig;
+ final String mTransport;
+
+ EthernetTrackerConfig(@NonNull final String[] tokens) {
+ Objects.requireNonNull(tokens, "EthernetTrackerConfig requires non-null tokens");
+ mName = tokens[0];
+ mCapabilities = tokens.length > 1 ? tokens[1] : null;
+ mIpConfig = tokens.length > 2 && !TextUtils.isEmpty(tokens[2]) ? tokens[2] : null;
+ mTransport = tokens.length > 3 ? tokens[3] : null;
+ }
+ }
}
diff --git a/tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java b/tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java
index f4bb15e..0cd9c18 100644
--- a/tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java
+++ b/tests/java/com/android/server/ethernet/EthernetNetworkFactoryTest.java
@@ -21,6 +21,7 @@ import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
+import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.same;
import static org.mockito.Mockito.clearInvocations;
@@ -29,9 +30,11 @@ import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
+import android.annotation.NonNull;
import android.app.test.MockAnswerUtil.AnswerWithArguments;
import android.content.Context;
import android.content.res.Resources;
+import android.net.ConnectivityManager;
import android.net.EthernetNetworkSpecifier;
import android.net.IpConfiguration;
import android.net.LinkProperties;
@@ -145,9 +148,9 @@ public class EthernetNetworkFactoryTest {
.build();
}
- private NetworkCapabilities.Builder createInterfaceCapsBuilder() {
+ private NetworkCapabilities.Builder createInterfaceCapsBuilder(final int transportType) {
return new NetworkCapabilities.Builder()
- .addTransportType(NetworkCapabilities.TRANSPORT_ETHERNET)
+ .addTransportType(transportType)
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.addCapability(NetworkCapabilities.NET_CAPABILITY_NOT_VCN_MANAGED);
}
@@ -172,7 +175,13 @@ public class EthernetNetworkFactoryTest {
// creates an interface with provisioning in progress (since updating the interface link state
// automatically starts the provisioning process)
private void createInterfaceUndergoingProvisioning(String iface) throws Exception {
- mNetFactory.addInterface(iface, iface, createInterfaceCapsBuilder().build(),
+ // Default to the ethernet transport type.
+ createInterfaceUndergoingProvisioning(iface, NetworkCapabilities.TRANSPORT_ETHERNET);
+ }
+
+ private void createInterfaceUndergoingProvisioning(
+ @NonNull final String iface, final int transportType) throws Exception {
+ mNetFactory.addInterface(iface, iface, createInterfaceCapsBuilder(transportType).build(),
createDefaultIpConfig());
assertTrue(mNetFactory.updateInterfaceLinkState(iface, true));
verify(mDeps).makeIpClient(any(Context.class), anyString(), any());
@@ -182,13 +191,22 @@ public class EthernetNetworkFactoryTest {
}
// creates a provisioned interface
- private void createProvisionedInterface(String iface) throws Exception {
- createInterfaceUndergoingProvisioning(iface);
+ private void createAndVerifyProvisionedInterface(String iface) throws Exception {
+ // Default to the ethernet transport type.
+ createAndVerifyProvisionedInterface(iface, NetworkCapabilities.TRANSPORT_ETHERNET,
+ ConnectivityManager.TYPE_ETHERNET);
+ }
+
+ private void createAndVerifyProvisionedInterface(
+ @NonNull final String iface, final int transportType, final int expectedLegacyType)
+ throws Exception {
+ createInterfaceUndergoingProvisioning(iface, transportType);
mIpClientCallbacks.onProvisioningSuccess(new LinkProperties());
mLooper.dispatchAll();
- // provisioning succeeded, verify that the network agent is created, registered, and marked
- // as connected.
- verify(mDeps).makeEthernetNetworkAgent(any(), any(), any(), any(), any(), any(), any());
+ // provisioning succeeded, verify that the network agent is created, registered, marked
+ // as connected and legacy type are correctly set.
+ verify(mDeps).makeEthernetNetworkAgent(any(), any(), any(), any(),
+ argThat(x -> x.getLegacyType() == expectedLegacyType), any(), any());
verify(mNetworkAgent).register();
verify(mNetworkAgent).markConnected();
clearInvocations(mDeps);
@@ -201,7 +219,7 @@ public class EthernetNetworkFactoryTest {
// followed by releaseNetworkFor which will stop the NetworkAgent and IpClient. When
// EthernetNetworkFactory#updateInterfaceLinkState(iface, true) is called, the interface
// is automatically provisioned even if nobody has ever called needNetworkFor
- createProvisionedInterface(iface);
+ createAndVerifyProvisionedInterface(iface);
// Interface is already provisioned, so startProvisioning / register should not be called
// again
@@ -240,7 +258,7 @@ public class EthernetNetworkFactoryTest {
@Test
public void testUpdateInterfaceLinkStateForProvisionedInterface() throws Exception {
String iface = "eth0";
- createProvisionedInterface(iface);
+ createAndVerifyProvisionedInterface(iface);
assertTrue(mNetFactory.updateInterfaceLinkState(iface, false));
verify(mIpClient).shutdown();
verify(mNetworkAgent).unregister();
@@ -266,7 +284,7 @@ public class EthernetNetworkFactoryTest {
@Test
public void testNeedNetworkForOnProvisionedInterface() throws Exception {
- createProvisionedInterface("eth0");
+ createAndVerifyProvisionedInterface("eth0");
mNetFactory.needNetworkFor(createDefaultRequest());
verify(mIpClient, never()).startProvisioning(any());
}
@@ -299,7 +317,7 @@ public class EthernetNetworkFactoryTest {
public void testProvisioningLoss() throws Exception {
String iface = "eth0";
when(mDeps.getNetworkInterfaceByName(iface)).thenReturn(mInterfaceParams);
- createProvisionedInterface(iface);
+ createAndVerifyProvisionedInterface(iface);
mIpClientCallbacks.onProvisioningFailure(new LinkProperties());
mLooper.dispatchAll();
@@ -315,7 +333,7 @@ public class EthernetNetworkFactoryTest {
// mocked method returns null by default, but just to be explicit in the test:
when(mDeps.getNetworkInterfaceByName(eq(iface))).thenReturn(null);
- createProvisionedInterface(iface);
+ createAndVerifyProvisionedInterface(iface);
mIpClientCallbacks.onProvisioningFailure(new LinkProperties());
mLooper.dispatchAll();
verify(mIpClient).shutdown();
@@ -345,7 +363,7 @@ public class EthernetNetworkFactoryTest {
@Test
public void testLinkPropertiesChanged() throws Exception {
- createProvisionedInterface("eth0");
+ createAndVerifyProvisionedInterface("eth0");
LinkProperties lp = new LinkProperties();
mIpClientCallbacks.onLinkPropertiesChange(lp);
@@ -355,7 +373,7 @@ public class EthernetNetworkFactoryTest {
@Test
public void testNetworkUnwanted() throws Exception {
- createProvisionedInterface("eth0");
+ createAndVerifyProvisionedInterface("eth0");
mNetworkAgent.getCallbacks().onNetworkUnwanted();
mLooper.dispatchAll();
@@ -368,7 +386,7 @@ public class EthernetNetworkFactoryTest {
String iface = "eth0";
// ensures provisioning is restarted after provisioning loss
when(mDeps.getNetworkInterfaceByName(iface)).thenReturn(mInterfaceParams);
- createProvisionedInterface(iface);
+ createAndVerifyProvisionedInterface(iface);
EthernetNetworkAgent.Callbacks oldCbs = mNetworkAgent.getCallbacks();
// replace network agent in EthernetNetworkFactory
@@ -388,4 +406,32 @@ public class EthernetNetworkFactoryTest {
verify(mIpClient, never()).shutdown();
verify(mNetworkAgent, never()).unregister();
}
+
+ @Test
+ public void testTransportOverrideIsCorrectlySet() throws Exception {
+ final String iface = "eth0";
+ // createProvisionedInterface() has verifications in place for transport override
+ // functionality which for EthernetNetworkFactory is network score and legacy type mappings.
+ createAndVerifyProvisionedInterface(iface, NetworkCapabilities.TRANSPORT_ETHERNET,
+ ConnectivityManager.TYPE_ETHERNET);
+ mNetFactory.removeInterface(iface);
+ createAndVerifyProvisionedInterface(iface, NetworkCapabilities.TRANSPORT_BLUETOOTH,
+ ConnectivityManager.TYPE_BLUETOOTH);
+ mNetFactory.removeInterface(iface);
+ createAndVerifyProvisionedInterface(iface, NetworkCapabilities.TRANSPORT_WIFI,
+ ConnectivityManager.TYPE_WIFI);
+ mNetFactory.removeInterface(iface);
+ createAndVerifyProvisionedInterface(iface, NetworkCapabilities.TRANSPORT_CELLULAR,
+ ConnectivityManager.TYPE_MOBILE);
+ mNetFactory.removeInterface(iface);
+ createAndVerifyProvisionedInterface(iface, NetworkCapabilities.TRANSPORT_LOWPAN,
+ ConnectivityManager.TYPE_NONE);
+ mNetFactory.removeInterface(iface);
+ createAndVerifyProvisionedInterface(iface, NetworkCapabilities.TRANSPORT_WIFI_AWARE,
+ ConnectivityManager.TYPE_NONE);
+ mNetFactory.removeInterface(iface);
+ createAndVerifyProvisionedInterface(iface, NetworkCapabilities.TRANSPORT_TEST,
+ ConnectivityManager.TYPE_NONE);
+ mNetFactory.removeInterface(iface);
+ }
}
diff --git a/tests/java/com/android/server/ethernet/EthernetTrackerTest.java b/tests/java/com/android/server/ethernet/EthernetTrackerTest.java
index ee9f349..6ea2154 100644
--- a/tests/java/com/android/server/ethernet/EthernetTrackerTest.java
+++ b/tests/java/com/android/server/ethernet/EthernetTrackerTest.java
@@ -17,6 +17,7 @@
package com.android.server.ethernet;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertThrows;
import static org.junit.Assert.fail;
import android.net.InetAddresses;
@@ -242,4 +243,27 @@ public class EthernetTrackerTest {
EthernetTracker.createNetworkCapabilities(clearCapabilties, configCapabiltiies,
configTransports).build());
}
+
+ @Test
+ public void testCreateEthernetTrackerConfigReturnsCorrectValue() {
+ final String name = "1";
+ final String capabilities = "2";
+ final String ipConfig = "3";
+ final String transport = "4";
+ final String configString = String.join(";", name, capabilities, ipConfig, transport);
+
+ final EthernetTracker.EthernetTrackerConfig config =
+ EthernetTracker.createEthernetTrackerConfig(configString);
+
+ assertEquals(name, config.mName);
+ assertEquals(capabilities, config.mCapabilities);
+ assertEquals(ipConfig, config.mIpConfig);
+ assertEquals(transport, config.mTransport);
+ }
+
+ @Test
+ public void testCreateEthernetTrackerConfigThrowsNpeWithNullInput() {
+ assertThrows(NullPointerException.class,
+ () -> EthernetTracker.createEthernetTrackerConfig(null));
+ }
}