Don't throw in FullScore#policyNameOf.

This code is correct on userdebug builds, but it is dangerous on
user builds because proguard might strip out the POLICY_*
constants and that would lead to crashes.

For now just log a wtf if an invalid policy name is found. A
better solution would be to make MessageUtils robust to this
problem, e.g., by having it store the SparseArray internally and
providing getters that do not throw, instead of the current
behaviour that returns the SparseArray. That is left to a future
CL.

Fix: 227161413
Test: atest FullScoreTest
Change-Id: I68b69ee9dd84773018e62c9a8f43e754ae04c486
This commit is contained in:
Lorenzo Colitti
2022-03-24 20:53:24 +09:00
parent a63e2341d6
commit 1727d9f1f2
2 changed files with 34 additions and 5 deletions

View File

@@ -22,6 +22,7 @@ import android.net.NetworkScore.KEEP_CONNECTED_NONE
import android.os.Build
import android.text.TextUtils
import android.util.ArraySet
import android.util.Log
import androidx.test.filters.SmallTest
import com.android.server.connectivity.FullScore.MAX_CS_MANAGED_POLICY
import com.android.server.connectivity.FullScore.POLICY_ACCEPT_UNVALIDATED
@@ -32,11 +33,12 @@ import com.android.server.connectivity.FullScore.POLICY_IS_VALIDATED
import com.android.server.connectivity.FullScore.POLICY_IS_VPN
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRunner
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import kotlin.reflect.full.staticProperties
import kotlin.test.assertEquals
import kotlin.test.assertFailsWith
import kotlin.test.assertFalse
import kotlin.test.assertTrue
@@ -63,6 +65,23 @@ class FullScoreTest {
return mixInScore(nc, nac, validated, false /* yieldToBadWifi */, destroyed)
}
private val TAG = this::class.simpleName
private var wtfHandler: Log.TerribleFailureHandler? = null
@Before
fun setUp() {
// policyNameOf will call Log.wtf if passed an invalid policy.
wtfHandler = Log.setWtfHandler() { tagString, what, system ->
Log.d(TAG, "WTF captured, ignoring: $tagString $what")
}
}
@After
fun tearDown() {
Log.setWtfHandler(wtfHandler)
}
@Test
fun testGetLegacyInt() {
val ns = FullScore(50, 0L /* policy */, KEEP_CONNECTED_NONE)
@@ -101,10 +120,9 @@ class FullScoreTest {
assertFalse(foundNames.contains(name))
foundNames.add(name)
}
assertFailsWith<IllegalArgumentException> {
FullScore.policyNameOf(MAX_CS_MANAGED_POLICY + 1)
}
assertEquals("IS_UNMETERED", FullScore.policyNameOf(POLICY_IS_UNMETERED))
val invalidPolicy = MAX_CS_MANAGED_POLICY + 1
assertEquals(Integer.toString(invalidPolicy), FullScore.policyNameOf(invalidPolicy))
}
fun getAllPolicies() = Regex("POLICY_.*").let { nameRegex ->