diff options
author | Jerome Dochez <jedo@google.com> | 2021-04-28 18:46:23 -0700 |
---|---|---|
committer | Jerome Dochez <jedo@google.com> | 2021-05-01 02:38:48 +0000 |
commit | 854e6b59383f717687b85948cf72c4ee8df808f9 (patch) | |
tree | 916ec321b5a3e542861797fd0b814bfb7dc0b4c5 | |
parent | 94020ee7e592673abbb85889051b2ce32f4f0ed2 (diff) | |
download | base-854e6b59383f717687b85948cf72c4ee8df808f9.tar.gz |
add verification step in library's art-profile processing
when processing the art profile human readable file in
library, ensure that the file is a valid art profile definition.
Test: added unit test.
Bug: N/A
Change-Id: I519fb71ac58bfc60da6eaa29b5601e569946130e
2 files changed, 93 insertions, 0 deletions
diff --git a/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessLibraryArtProfileTask.kt b/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessLibraryArtProfileTask.kt index 2b41cac8fe..fef236218f 100644 --- a/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessLibraryArtProfileTask.kt +++ b/build-system/gradle-core/src/main/java/com/android/build/gradle/tasks/ProcessLibraryArtProfileTask.kt @@ -21,6 +21,7 @@ import com.android.build.gradle.internal.component.ComponentCreationConfig import com.android.build.gradle.internal.scope.InternalArtifactType import com.android.build.gradle.internal.tasks.NonIncrementalTask import com.android.build.gradle.internal.tasks.factory.VariantTaskCreationAction +import com.android.tools.profgen.HumanReadableProfile import org.gradle.api.file.RegularFileProperty import org.gradle.api.tasks.InputFile import org.gradle.api.tasks.Optional @@ -29,6 +30,7 @@ import org.gradle.api.tasks.PathSensitive import org.gradle.api.tasks.PathSensitivity import org.gradle.api.tasks.TaskAction import org.gradle.api.tasks.TaskProvider +import java.lang.RuntimeException /** * Task that processes profiles files for library. @@ -48,6 +50,12 @@ abstract class ProcessLibraryArtProfileTask: NonIncrementalTask() { @TaskAction override fun doTaskAction() { if (profileSource.isPresent) { + val sourceFile = profileSource.get().asFile + // verify the human readable profile is valid so we error early if necessary + HumanReadableProfile(sourceFile) { + throw RuntimeException("Error while parsing ${sourceFile.absolutePath} : $it") + } + // all good, copy to target area. profileSource.get().asFile.copyTo(outputFile.get().asFile, true) } } diff --git a/build-system/gradle-core/src/test/java/com/android/build/gradle/tasks/ProcessLibraryArtProfileTaskTest.kt b/build-system/gradle-core/src/test/java/com/android/build/gradle/tasks/ProcessLibraryArtProfileTaskTest.kt new file mode 100644 index 0000000000..4ebf1ef5e7 --- /dev/null +++ b/build-system/gradle-core/src/test/java/com/android/build/gradle/tasks/ProcessLibraryArtProfileTaskTest.kt @@ -0,0 +1,85 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.build.gradle.tasks + +import com.android.SdkConstants +import com.google.common.truth.Truth +import org.gradle.api.Project +import org.gradle.testfixtures.ProjectBuilder +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import org.mockito.MockitoAnnotations +import java.io.IOException +import java.lang.IllegalStateException + +internal class ProcessLibraryArtProfileTaskTest { + @get:Rule + var projectFolder = TemporaryFolder() + + @get:Rule + var sourceFolder = TemporaryFolder() + + private lateinit var task: ProcessLibraryArtProfileTask + private lateinit var project: Project + + @Before + @Throws(IOException::class) + fun setUp() { + MockitoAnnotations.initMocks(this) + project= ProjectBuilder.builder().withProjectDir(projectFolder.root).build() + val taskProvider = project.tasks.register( + "processLibraryArtProfile", ProcessLibraryArtProfileTask::class.java + ) + task = taskProvider.get() + } + + @Test + fun testValidArtProfile() { + val sourceFile = sourceFolder.newFile(SdkConstants.FN_ART_PROFILE).also { + it.writeText( + """ + HSPLcom/google/Foo;->method(II)I + HSPLcom/google/Foo;->method-name-with-hyphens(II)I + """.trimIndent() + ) + } + task.profileSource.set(sourceFile) + val outputFile = projectFolder.newFile("output.txt") + task.outputFile.set(outputFile) + task.taskAction() + + Truth.assertThat(outputFile.exists()).isTrue() + Truth.assertThat(outputFile.readText()).isEqualTo(sourceFile.readText()) + } + + @Test(expected = RuntimeException::class) + fun testInvalidArtProfile() { + val sourceFile = sourceFolder.newFile(SdkConstants.FN_ART_PROFILE).also { + it.writeText( + """ + garbage + """.trimIndent() + ) + } + task.profileSource.set(sourceFile) + val outputFile = projectFolder.newFile("output.txt") + task.outputFile.set(outputFile) + task.taskAction() + } +} |