From d3db165d9e899e3378b278e3cf4b324bfd8352c1 Mon Sep 17 00:00:00 2001 From: Chalard Jean Date: Thu, 27 Mar 2025 16:27:58 +0900 Subject: [PATCH] Only set the carrier service package on supported devices Not all U devices support this. It's only supported by U QPR1+. Bug: 404415649 Test: NetworkAgentTest Flag: EXEMPT bugfix Change-Id: I66d30b5f0be5628561bfeb1f90f5318954544e31 --- .../android/testutils/CarrierConfigRule.kt | 28 +++++++++++++------ .../src/android/net/cts/NetworkAgentTest.kt | 6 ++-- 2 files changed, 21 insertions(+), 13 deletions(-) diff --git a/staticlibs/testutils/devicetests/com/android/testutils/CarrierConfigRule.kt b/staticlibs/testutils/devicetests/com/android/testutils/CarrierConfigRule.kt index 076398e2f8..0d70d24e3d 100644 --- a/staticlibs/testutils/devicetests/com/android/testutils/CarrierConfigRule.kt +++ b/staticlibs/testutils/devicetests/com/android/testutils/CarrierConfigRule.kt @@ -25,7 +25,6 @@ import android.content.Intent import android.content.IntentFilter import android.content.pm.PackageManager import android.os.ConditionVariable -import android.os.ParcelFileDescriptor import android.os.PersistableBundle import android.os.Process import android.telephony.CarrierConfigManager @@ -38,6 +37,7 @@ import android.util.Log import androidx.test.platform.app.InstrumentationRegistry import com.android.modules.utils.build.SdkLevel import com.android.testutils.runAsShell +import com.android.testutils.runCommandInShell import com.android.testutils.tryTest import java.security.MessageDigest import kotlin.test.assertEquals @@ -135,11 +135,6 @@ class CarrierConfigRule : TestRule { } } - private fun runShellCommand(cmd: String) { - val fd: ParcelFileDescriptor = uiAutomation.executeShellCommand(cmd) - fd.close() // Don't care about the output. - } - /** * Converts a byte array into a String of hexadecimal characters. * @@ -234,12 +229,27 @@ class CarrierConfigRule : TestRule { */ fun dropCarrierPrivilege(subId: Int) = setHoldCarrierPrivilege(false, subId) + /** + * Setting carrier service package was added in U QPR1, so check for that. + * + * In T and below, return false. In V and above, return true as this must work. + * In U, test for the command presence. That means the test can be disabled by + * removing that command in QPR1+, but this would indicate malicious intent. + */ + fun isSettingCarrierServicePackageSupported(): Boolean { + if (SdkLevel.isAtLeastV()) return true + // A T device with this command present should not be held accountable to have it work + if (!SdkLevel.isAtLeastU()) return false + return !runCommandInShell("cmd phone set-carrier-service-package-override") + .startsWith("Unknown command") + } + /** * Sets the carrier service package override for the given subscription ID. A null argument will * clear any previously-set override. */ fun setCarrierServicePackageOverride(subId: Int, pkg: String?) { - if (!SdkLevel.isAtLeastU()) { + if (!isSettingCarrierServicePackageSupported()) { throw UnsupportedOperationException( "Setting carrier service package override requires at least U SDK" ) @@ -275,10 +285,10 @@ class CarrierConfigRule : TestRule { if (null == pkg) { // There is a bug in clear-carrier-service-package-override where not adding // the -s argument will use the wrong slot index : b/299604822 - runShellCommand("cmd phone clear-carrier-service-package-override" + + runCommandInShell("cmd phone clear-carrier-service-package-override" + " -s $subId") } else { - runShellCommand("cmd phone set-carrier-service-package-override $pkg" + + runCommandInShell("cmd phone set-carrier-service-package-override $pkg" + " -s $subId") } } diff --git a/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt b/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt index 02a5d1f654..7c75211151 100644 --- a/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt +++ b/tests/cts/net/src/android/net/cts/NetworkAgentTest.kt @@ -183,7 +183,6 @@ private const val DEFAULT_TIMEOUT_MS = 5000L private const val QUEUE_NETWORK_AGENT_EVENTS_IN_SYSTEM_SERVER = "queue_network_agent_events_in_system_server" - // When waiting for a NetworkCallback to determine there was no timeout, waiting is the // only possible thing (the relevant handler is the one in the real ConnectivityService, // and then there is the Binder call), so have a short timeout for this as it will be @@ -244,7 +243,6 @@ class NetworkAgentTest { QUEUE_NETWORK_AGENT_EVENTS_IN_SYSTEM_SERVER ) - @Before fun setUp() { instrumentation.getUiAutomation().adoptShellPermissionIdentity() @@ -777,9 +775,9 @@ class NetworkAgentTest { expectUidsPresent = false ) - // The tools to set the carrier service package override do not exist before U, + // The tools to set the carrier service package override do not exist before U QPR1, // so there is no way to test the rest of this test on < U. - if (!SdkLevel.isAtLeastU()) return@tryTest + if (!carrierConfigRule.isSettingCarrierServicePackageSupported()) return@tryTest // Acquiring carrier privilege is necessary to override the carrier service package. val defaultSlotIndex = SubscriptionManager.getSlotIndex(defaultSubId) carrierConfigRule.acquireCarrierPrivilege(defaultSubId)