1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.permissioncontroller.lint.permissionpolicy
18 
19 import com.android.tools.lint.checks.infrastructure.LintDetectorTest
20 import com.android.tools.lint.detector.api.Detector
21 import com.android.tools.lint.detector.api.Issue
22 import org.junit.Test
23 
24 class PlatformPermissionWithoutFlagDetectorTest : LintDetectorTest() {
getDetectornull25     override fun getDetector(): Detector = PlatformPermissionWithoutFlagDetector()
26 
27     override fun getIssues(): List<Issue> = listOf(PlatformPermissionWithoutFlagDetector.ISSUE)
28 
29     @Test
30     fun testManifestPermissionsMustHaveFeatureFlag() {
31         lint()
32             .files(
33                 xml(
34                         "AndroidManifest.xml",
35                         """
36 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
37         package="android" coreApp="true" android:sharedUserId="android.uid.system"
38         android:sharedUserLabel="@string/android_system_label">
39 
40     <permission android:name="android.permission.EXAMPLE"
41             android:protectionLevel="normal" />
42 
43     <permission android:name="com.android.permission.EXAMPLE2"
44             android:protectionLevel="normal" />
45 
46     <permission android:name="android.permission.EXAMPLE3"
47             android:protectionLevel="normal"
48             android:featureFlag="this.is.a.feature.flag"/>
49 
50     <attribution android:tag="ExampleTag" android:label="@string/example_label"/>
51 
52     <application android:process="system"
53             android:persistent="true"
54             android:hasCode="false"
55             android:label="@string/android_system_label"
56             android:allowClearUserData="false"
57             android:backupAgent="com.android.server.backup.SystemBackupAgent"
58             android:killAfterRestore="false"
59             android:icon="@drawable/ic_launcher_android"
60             android:supportsRtl="true"
61             android:theme="@style/Theme.DeviceDefault.Light.DarkActionBar"
62             android:defaultToDeviceProtectedStorage="true"
63             android:forceQueryable="true"
64             android:directBootAware="true">
65         <activity android:name="com.android.internal.ExampleActivity"
66                   android:exported="false"
67                   android:theme="@style/Theme.DeviceDefault.Dialog.Alert.DayNight"
68                   android:finishOnCloseSystemDialogs="true"
69                   android:excludeFromRecents="true"
70                   android:documentLaunchMode="never"
71                   android:relinquishTaskIdentity="true"
72                   android:process=":ui"
73                   android:visibleToInstantApps="true">
74             <intent-filter>
75                 <action android:name="com.android.internal.intent.action.EXAMPLE_ACTION" />
76                 <category android:name="android.intent.category.DEFAULT" />
77             </intent-filter>
78         </activity>
79 
80     </application>
81 
82     <permission android:name="android.permission.EXAMPLE4"
83             android:protectionLevel="normal" />
84 </manifest>
85 """
86                     )
87                     .indented()
88             )
89             .allowMissingSdk()
90             .run()
91             .expect(
92                 """
93 AndroidManifest.xml:5: Error: android.permission.EXAMPLE isn't guarded by a feature flag. New <permission> elements must have the android:featureFlag attribute. [PlatformPermissionWithoutFlag]
94     <permission android:name="android.permission.EXAMPLE"
95     ^
96 AndroidManifest.xml:47: Error: android.permission.EXAMPLE4 isn't guarded by a feature flag. New <permission> elements must have the android:featureFlag attribute. [PlatformPermissionWithoutFlag]
97     <permission android:name="android.permission.EXAMPLE4"
98     ^
99 2 errors, 0 warnings
100 """
101                     .trimIndent()
102             )
103     }
104 }
105