Fix CarrierConfigRule#setHoldCarrierPrivilege race

CarrierConfigRule#setHoldCarrierPrivilege will try to acquire/drop the
carrier privilege, but does not wait for the result of the operation. If
a test immediately calls an API that requires carrier privilege before
the operation completes, then it will fail.

Fix this by waiting for the operation to succeed before returning.

Bug: 295979433
Test: presubmit
Change-Id: Ic7cc9e1dbc206fbc0c3b79058b5782d93e89f1d7
This commit is contained in:
Quang Anh Luong 2025-03-21 18:06:27 +09:00
commit d3058d5ffe

View file

@ -40,6 +40,7 @@ import com.android.modules.utils.build.SdkLevel
import com.android.testutils.runAsShell
import com.android.testutils.tryTest
import java.security.MessageDigest
import kotlin.test.assertEquals
import kotlin.test.assertNotNull
import kotlin.test.assertTrue
import org.junit.rules.TestRule
@ -204,6 +205,7 @@ class CarrierConfigRule : TestRule {
}
return@tryTest
}
cv.close()
if (hold) {
addConfigOverrides(subId, PersistableBundle().also {
it.putStringArray(CarrierConfigManager.KEY_CARRIER_CERTIFICATE_STRING_ARRAY,
@ -212,6 +214,9 @@ class CarrierConfigRule : TestRule {
} else {
cleanUpNow()
}
assertTrue(cv.block(CARRIER_CONFIG_CHANGE_TIMEOUT_MS),
"Timed out waiting for CarrierPrivilegesCallback")
assertEquals(cpb.hasPrivilege, hold, "Couldn't set carrier privilege")
} cleanup @JvmSerializableLambda {
runAsShell(READ_PRIVILEGED_PHONE_STATE) @JvmSerializableLambda {
tm.unregisterCarrierPrivilegesCallback(cpb)