Merge "Only set the carrier service package on supported devices" into main

This commit is contained in:
Jean Chalard 2025-04-09 15:57:02 -07:00 • committed by Android (Google) Code Review
commit 86fb573931
2 changed files with 21 additions and 13 deletions

View file

@ -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")
}
}

View file

@ -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)