check-flagged-apis: create list of @FlaggedApi errors
Teach check-flagged-apis to cross-check the data from its three input
sources. This allows the tool to detect
- @FlaggedApi references to non-existent flags
- @FlaggedApi APIs present in the build artifacts even though the flag
is disabled
- @FlaggedApi APIs not present in the build artifacts even though the
flag is enabled
By passing in different sources, the tool can detect these errors for
any of the API surfaces (public, @SystemApi(MODULE_LIBRARIES), etc).
Note: the tool assumes that a disabled flag means that the @FlaggedApi
should not be present in the build output. This is currently true, but
won't be once metalava starts reverting @FlaggedApis to their previous
SDK snapshot.
Bug: 334870672
Test: atest --host check-flagged-apis-test
Test: check-flagged-apis --api-signature out/target/product/mainline_x86/obj/ETC/frameworks-base-api-current.txt_intermediates/frameworks-base-api-current.txt --flag-values out/soong/.intermediates/all_aconfig_declarations.pb --api-versions out/dist/data/api-versions.xml
Change-Id: I790234865f831af7d45895def14d1d6740365622
diff --git a/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt b/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt
index 9e6a6e3..d2b75d4 100644
--- a/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt
+++ b/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt
@@ -20,6 +20,7 @@
import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test
import java.io.ByteArrayInputStream
import java.io.ByteArrayOutputStream
+import java.io.InputStream
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith
@@ -36,22 +37,6 @@
"""
.trim()
-private val PARSED_FLAGS =
- {
- val parsed_flag =
- Aconfig.parsed_flag
- .newBuilder()
- .setPackage("android.flag")
- .setName("foo")
- .setState(Aconfig.flag_state.ENABLED)
- .setPermission(Aconfig.flag_permission.READ_ONLY)
- .build()
- val parsed_flags = Aconfig.parsed_flags.newBuilder().addParsedFlag(parsed_flag).build()
- val binaryProto = ByteArrayOutputStream()
- parsed_flags.writeTo(binaryProto)
- ByteArrayInputStream(binaryProto.toByteArray())
- }()
-
private val API_VERSIONS =
"""
<?xml version="1.0" encoding="utf-8"?>
@@ -64,6 +49,21 @@
"""
.trim()
+private fun generateFlagsProto(fooState: Aconfig.flag_state): InputStream {
+ val parsed_flag =
+ Aconfig.parsed_flag
+ .newBuilder()
+ .setPackage("android.flag")
+ .setName("foo")
+ .setState(fooState)
+ .setPermission(Aconfig.flag_permission.READ_ONLY)
+ .build()
+ val parsed_flags = Aconfig.parsed_flags.newBuilder().addParsedFlag(parsed_flag).build()
+ val binaryProto = ByteArrayOutputStream()
+ parsed_flags.writeTo(binaryProto)
+ return ByteArrayInputStream(binaryProto.toByteArray())
+}
+
@RunWith(DeviceJUnit4ClassRunner::class)
class CheckFlaggedApisTest : BaseHostJUnit4Test() {
@Test
@@ -76,7 +76,7 @@
@Test
fun testParseFlagValues() {
val expected: Map<Flag, Boolean> = mapOf(Flag("android.flag.foo") to true)
- val actual = parseFlagValues(PARSED_FLAGS)
+ val actual = parseFlagValues(generateFlagsProto(Aconfig.flag_state.ENABLED))
assertEquals(expected, actual)
}
@@ -86,4 +86,28 @@
val actual = parseApiVersions(API_VERSIONS.byteInputStream())
assertEquals(expected, actual)
}
+
+ @Test
+ fun testFindErrorsNoErrors() {
+ val expected = setOf<ApiError>()
+ val actual =
+ findErrors(
+ parseApiSignature("in-memory", API_SIGNATURE.byteInputStream()),
+ parseFlagValues(generateFlagsProto(Aconfig.flag_state.ENABLED)),
+ parseApiVersions(API_VERSIONS.byteInputStream()))
+ assertEquals(expected, actual)
+ }
+
+ @Test
+ fun testFindErrorsDisabledFlaggedApiIsPresent() {
+ val expected =
+ setOf<ApiError>(
+ DisabledFlaggedApiIsPresentError(Symbol("android.Clazz.FOO"), Flag("android.flag.foo")))
+ val actual =
+ findErrors(
+ parseApiSignature("in-memory", API_SIGNATURE.byteInputStream()),
+ parseFlagValues(generateFlagsProto(Aconfig.flag_state.DISABLED)),
+ parseApiVersions(API_VERSIONS.byteInputStream()))
+ assertEquals(expected, actual)
+ }
}