summaryrefslogtreecommitdiff
path: root/build-system/integration-test/application/src/test/java/com/android/build/gradle/integration/application/ProfileableTest.kt
blob: d0c50382ba6fecea89b3f46241a1b09f29c00fd3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/*
 * 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.integration.application

import com.android.build.gradle.integration.common.fixture.DEFAULT_COMPILE_SDK_VERSION
import com.android.build.gradle.integration.common.fixture.GradleTestProject
import com.android.build.gradle.integration.common.fixture.app.MinimalSubProject
import com.android.build.gradle.integration.common.fixture.app.MultiModuleTestProject
import com.android.build.gradle.integration.common.truth.ApkSubject
import com.android.build.gradle.integration.common.truth.ScannerSubject
import com.android.build.gradle.integration.common.truth.TruthHelper.assertThat
import com.android.build.gradle.integration.common.utils.SigningHelper
import com.android.build.gradle.integration.common.utils.TestFileUtils
import com.android.build.gradle.options.BooleanOption
import com.android.build.gradle.options.StringOption
import org.junit.Rule
import org.junit.Test
import org.junit.rules.TemporaryFolder
import java.util.Locale

/**
 * Tests verifying that builds using the profileable option are configured correctly.
 * For example, including the profileable tag in AndroidManifest, disable debuggable features and
 * doesn't use release signing configs etc.
 */
class ProfileableTest {

    @get:Rule
    val project = GradleTestProject.builder().fromTestApp(
        MultiModuleTestProject.builder()
            .subproject(":app", MinimalSubProject.app("com.profilabletest.app")).build()
    ).create()

    @get:Rule
    val temporaryDirectory = TemporaryFolder()

    @Test
    fun `test dsl setting the release build type to be profileable`() {
        val app = project.getSubproject(":app")
        app.buildFile.appendText("android.buildTypes.release.profileable true")
        project.executor()
                .with(BooleanOption.ENABLE_DEFAULT_DEBUG_SIGNING_CONFIG, true)
                .run("assembleRelease")
        val apkSigned =
                project.getSubproject("app").getApk(GradleTestProject.ApkType.RELEASE_SIGNED)
        val verificationResult = SigningHelper.assertApkSignaturesVerify(apkSigned, 30)
        assertThat(
            verificationResult.signerCertificates.first().subjectX500Principal.name
        ).isEqualTo("C=US,O=Android,CN=Android Debug")
        val manifest = ApkSubject.getManifestContent(apkSigned.file.toAbsolutePath())
        assertThat(manifest).containsAtLeastElementsIn(
            arrayListOf(
                "        E: application (line=11)",
                "            E: profileable (line=12)",
                "              A: http://schemas.android.com/apk/res/android:enabled(0x0101000e)=true",
                "              A: http://schemas.android.com/apk/res/android:shell(0x01010594)=true"
            )
        )

        // Test no signing config configured, if the automatic signing config assignment is disabled.
        project.executor().with(BooleanOption.ENABLE_DEFAULT_DEBUG_SIGNING_CONFIG, false)
                .run("clean", "assembleRelease")
        val apkUnsigned =
                project.getSubproject("app").getApk(GradleTestProject.ApkType.RELEASE)
        ApkSubject.assertThat(apkUnsigned).doesNotContainApkSigningBlock()
    }

    @Test
    fun `test dsl setting the release build type to be profileable on Api 29`() {
        val app = project.getSubproject(":app")
        app.buildFile.apply {
            appendText("android.buildTypes.release.profileable true\n")
            appendText("android.compileSdk 29")
        }

        project.executor()
                .with(BooleanOption.ENABLE_DEFAULT_DEBUG_SIGNING_CONFIG, true)
                .run("assembleRelease")
        val apkSigned =
                project.getSubproject("app").getApk(GradleTestProject.ApkType.RELEASE_SIGNED)
        val manifest = ApkSubject.getManifestContent(apkSigned.file.toAbsolutePath())
        assertThat(manifest).containsAtLeastElementsIn(
                arrayListOf(
                        "        E: application (line=11)",
                        "            E: profileable (line=12)",
                        "              A: http://schemas.android.com/apk/res/android:shell(0x01010594)=true"
                )
        )
        assertThat(manifest).doesNotContain(
                "              A: http://schemas.android.com/apk/res/android:enabled(0x0101000e)=true"
        )
    }

    @Test
    fun `test dsl when profileable and debuggable enabled`() {
        val app = project.getSubproject(":app")
        app.buildFile.appendText("android.buildTypes.debug.debuggable true\n")
        app.buildFile.appendText("android.buildTypes.debug.profileable true\n")
        val result = project.executor().run("assembleDebug")
        // Ensure profileable is not applied (debuggable dsl option overrides profileable).
        val manifest = ApkSubject.getManifestContent(
            project.getApkAsFile(GradleTestProject.ApkType.DEBUG).toPath()
        )
        assertThat(manifest).doesNotContain(
            arrayListOf(
                "        E: application (line=11)",
                "            E: profileable (line=12)",
                "              A: http://schemas.android.com/apk/res/android:shell(0x01010594)=true"
            )
        )
        result.stdout.use { out ->
            ScannerSubject.assertThat(out).contains(
                ":app build type 'debug' can only have debuggable or profileable enabled.\n" +
                        "Only one of these options can be used at a time.\n" +
                        "Recommended action: Only set one of debuggable=true and profileable=true.\n"
            )
        }
    }

    @Test
    fun `test injecting the debug build type to be profileable`() {
        val app = project.getSubproject(":app")
        val result = project.executor()
                .with(StringOption.PROFILING_MODE, "profileable")
                .run("assembleDebug")
        assertThat(result.tasks.filter { it.lowercase(Locale.US).contains("lint") })
                .named("Lint tasks")
                .isEmpty()
        checkProjectContainsProfileableInManifest(app, GradleTestProject.ApkType.DEBUG)
    }

    @Test
    fun `test injecting the release build type to be profileable`() {
        val app = project.getSubproject(":app")
        val result = project.executor()
            .with(StringOption.PROFILING_MODE, "profileable")
            .with(BooleanOption.ENABLE_DEFAULT_DEBUG_SIGNING_CONFIG, true)
            .run("assembleRelease")
        assertThat(result.tasks.filter { it.lowercase(Locale.US).contains("lint") })
                .named("Lint tasks")
                .isEmpty()
        checkProjectContainsProfileableInManifest(app, GradleTestProject.ApkType.RELEASE_SIGNED)
    }

    @Test
    fun `build with compileSdk less than 29 fails`() {
        val app = project.getSubproject(":app")
        val lowCompileSdkVersion = 28
        TestFileUtils.searchAndReplace(
            app.buildFile.absoluteFile,
            "android.compileSdkVersion $DEFAULT_COMPILE_SDK_VERSION",
            "android.compileSdkVersion $lowCompileSdkVersion",
        )
        val result = project.executor()
            .with(StringOption.PROFILING_MODE, "profileable")
            .expectFailure()
            .run("assembleDebug")

        result.stderr.use { out ->
            ScannerSubject.assertThat(out).contains(
                "'profileable' is enabled with compile SDK less than API 29."
            )
            ScannerSubject.assertThat(out).contains(
                    "Recommended action: If possible, upgrade compileSdk from $lowCompileSdkVersion to at least API 29."
            )
        }
    }

    private fun checkProjectContainsProfileableInManifest(
        project: GradleTestProject,
        apkType: GradleTestProject.ApkType
    ) {
        val manifest = ApkSubject.getManifestContent(project.getApkAsFile(apkType).toPath())
        assertThat(manifest).containsAtLeastElementsIn(
            arrayListOf(
                "        E: application (line=11)",
                "          A: http://schemas.android.com/apk/res/android:testOnly(0x01010272)=true",
                "            E: profileable (line=12)",
                "              A: http://schemas.android.com/apk/res/android:enabled(0x0101000e)=true",
                "              A: http://schemas.android.com/apk/res/android:shell(0x01010594)=true"
            )
        )
    }
}