From cde74dff5def3df19820ebbc980c7ab9a4e9ee56 Mon Sep 17 00:00:00 2001 From: Lorenzo Colitti Date: Tue, 3 Dec 2019 23:50:57 +0900 Subject: [PATCH] Improve code that decides whether Ethernet should be supported. Currently, ConnectivityManagerTest checks whether the device supports Ethernet by checking whether the OEM has configured it in the networkAttributes array, or whether the Ethernet service is running. It does this because devices that support USB host mode enable Ethernet even if the OEM does not add Ethernet to the networkAttributes array. Unfortunately this does not work in instant mode because instant apps cannot query the Ethernet service due to selinux policies. In addition to querying whether the service is running (which will continue to work in non-instant mode), also assume that Ethernet is supported if the FEATURE_ETHERNET or FEATURE_USB_HOST PackageManager flags are present. This fixes various ConnectivityManagerTest testcases on devices that support USB host mode but where the OEM has not added Ethernet to the networkAttributes array. Bug: 137630789 Bug: 143850345 Bug: 145482540 Test: atest android.net.cts.ConnectivityManagerTest#testIsNetworkSupported Test: atest --instant android.net.cts.ConnectivityManagerTest#testIsNetworkSupported Change-Id: Ie8a701f607967576a45c6b3c4f25c9c003aa3165 --- .../net/cts/ConnectivityManagerTest.java | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java index bbc2298623..fa7e1381b3 100644 --- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java +++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java @@ -16,8 +16,10 @@ package android.net.cts; +import static android.content.pm.PackageManager.FEATURE_ETHERNET; import static android.content.pm.PackageManager.FEATURE_TELEPHONY; import static android.content.pm.PackageManager.FEATURE_WIFI; +import static android.content.pm.PackageManager.FEATURE_USB_HOST; import static android.net.NetworkCapabilities.NET_CAPABILITY_IMS; import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_METERED; @@ -257,7 +259,7 @@ public class ConnectivityManagerTest extends AndroidTestCase { public void testGetNetworkInfo() { for (int type = -1; type <= ConnectivityManager.MAX_NETWORK_TYPE+1; type++) { - if (isSupported(type)) { + if (shouldBeSupported(type)) { NetworkInfo ni = mCm.getNetworkInfo(type); assertTrue("Info shouldn't be null for " + type, ni != null); State state = ni.getState(); @@ -277,7 +279,7 @@ public class ConnectivityManagerTest extends AndroidTestCase { NetworkInfo[] ni = mCm.getAllNetworkInfo(); assertTrue(ni.length >= MIN_NUM_NETWORK_TYPES); for (int type = 0; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) { - int desiredFoundCount = (isSupported(type) ? 1 : 0); + int desiredFoundCount = (shouldBeSupported(type) ? 1 : 0); int foundCount = 0; for (NetworkInfo i : ni) { if (i.getType() == type) foundCount++; @@ -385,20 +387,32 @@ public class ConnectivityManagerTest extends AndroidTestCase { assertStartUsingNetworkFeatureUnsupported(TYPE_WIFI, mmsFeature); } - private boolean isSupported(int networkType) { + private boolean shouldEthernetBeSupported() { + // Instant mode apps aren't allowed to query the Ethernet service due to selinux policies. + // When in instant mode, don't fail if the Ethernet service is available. Instead, rely on + // the fact that Ethernet should be supported if the device has a hardware Ethernet port, or + // if the device can be a USB host and thus can use USB Ethernet adapters. + // + // Note that this test this will still fail in instant mode if a device supports Ethernet + // via other hardware means. We are not currently aware of any such device. + return (mContext.getSystemService(Context.ETHERNET_SERVICE) != null) || + mPackageManager.hasSystemFeature(FEATURE_ETHERNET) || + mPackageManager.hasSystemFeature(FEATURE_USB_HOST); + } + + private boolean shouldBeSupported(int networkType) { return mNetworks.containsKey(networkType) || (networkType == ConnectivityManager.TYPE_VPN) || - (networkType == ConnectivityManager.TYPE_ETHERNET && - mContext.getSystemService(Context.ETHERNET_SERVICE) != null); + (networkType == ConnectivityManager.TYPE_ETHERNET && shouldEthernetBeSupported()); } public void testIsNetworkSupported() { for (int type = -1; type <= ConnectivityManager.MAX_NETWORK_TYPE; type++) { boolean supported = mCm.isNetworkSupported(type); - if (isSupported(type)) { - assertTrue(supported); + if (shouldBeSupported(type)) { + assertTrue("Network type " + type + " should be supported", supported); } else { - assertFalse(supported); + assertFalse("Network type " + type + " should not be supported", supported); } } }