From 387ff6cfe8be71ce6c15b630b63a369e36bf685e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A5rten=20Kongstad?= Date: Tue, 16 Apr 2024 12:42:14 +0200 Subject: [PATCH] check-flagged-apis: parse flag names and values Teach check-flagged-apis to parse the parsed_flags protobuf generated by aconfig. Note: `m all_aconfig_declarations` generates a protobuf file that contains all info about all flags. 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 Change-Id: I397b32ae2a373b429ef6ce22e0a06a0f15202b91 --- tools/check-flagged-apis/Android.bp | 1 + .../checkflaggedapis/CheckFlaggedApisTest.kt | 26 +++++++++++++++++++ .../src/com/android/checkflaggedapis/Main.kt | 19 ++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/tools/check-flagged-apis/Android.bp b/tools/check-flagged-apis/Android.bp index 7d0d33b383..ebd79c1df4 100644 --- a/tools/check-flagged-apis/Android.bp +++ b/tools/check-flagged-apis/Android.bp @@ -23,6 +23,7 @@ java_defaults { "src/com/android/checkflaggedapis/Main.kt", ], static_libs: [ + "libaconfig_java_proto_lite", "metalava-signature-reader", "metalava-tools-common-m2-deps", ], 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 d7890d7583..5fb67bedab 100644 --- a/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt +++ b/tools/check-flagged-apis/src/com/android/checkflaggedapis/CheckFlaggedApisTest.kt @@ -15,8 +15,11 @@ */ package com.android.checkflaggedapis +import android.aconfig.Aconfig import com.android.tradefed.testtype.DeviceJUnit4ClassRunner import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test +import java.io.ByteArrayInputStream +import java.io.ByteArrayOutputStream import org.junit.Assert.assertEquals import org.junit.Test import org.junit.runner.RunWith @@ -33,6 +36,22 @@ private val API_SIGNATURE = """ .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()) + }() + @RunWith(DeviceJUnit4ClassRunner::class) class CheckFlaggedApisTest : BaseHostJUnit4Test() { @Test @@ -41,4 +60,11 @@ class CheckFlaggedApisTest : BaseHostJUnit4Test() { val actual = parseApiSignature("in-memory", API_SIGNATURE.byteInputStream()) assertEquals(expected, actual) } + + @Test + fun testParseFlagValues() { + val expected: Map = mapOf(Flag("android.flag.foo") to true) + val actual = parseFlagValues(PARSED_FLAGS) + assertEquals(expected, actual) + } } diff --git a/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt b/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt index 5fede7b86c..005f6c0fb8 100644 --- a/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt +++ b/tools/check-flagged-apis/src/com/android/checkflaggedapis/Main.kt @@ -17,6 +17,7 @@ package com.android.checkflaggedapis +import android.aconfig.Aconfig import com.android.tools.metalava.model.BaseItemVisitor import com.android.tools.metalava.model.FieldItem import com.android.tools.metalava.model.text.ApiFile @@ -98,6 +99,15 @@ The tool will exit with a non-zero exit code if any flagged APIs are found to be """) .path(mustExist = true, canBeDir = false, mustBeReadable = true) .required() + private val flagValuesPath by + option("--flag-values") + .help( + """ + Path to aconfig parsed_flags binary proto file. + Tip: `m all_aconfig_declarations` will generate a file that includes all information about all flags. + """) + .path(mustExist = true, canBeDir = false, mustBeReadable = true) + .required() override fun run() { @Suppress("UNUSED_VARIABLE") @@ -105,6 +115,8 @@ The tool will exit with a non-zero exit code if any flagged APIs are found to be apiSignaturePath.toFile().inputStream().use { parseApiSignature(apiSignaturePath.toString(), it) } + @Suppress("UNUSED_VARIABLE") + val flags = flagValuesPath.toFile().inputStream().use { parseFlagValues(it) } throw ProgramResult(0) } } @@ -132,4 +144,11 @@ internal fun parseApiSignature(path: String, input: InputStream): Set { + val parsedFlags = Aconfig.parsed_flags.parseFrom(input).getParsedFlagList() + return parsedFlags.associateBy( + { Flag("${it.getPackage()}.${it.getName()}") }, + { it.getState() == Aconfig.flag_state.ENABLED }) +} + fun main(args: Array) = CheckCommand().main(args)