1 /*
<lambda>null2  * Copyright (C) 2017 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 android.permissionui.cts.permissionpolicy
18 
19 import android.app.Activity
20 import android.content.Intent
21 import android.content.pm.PackageManager
22 import android.content.pm.PermissionInfo
23 import android.os.Bundle
24 
25 /** An activity that can test platform permission protection flags. */
26 class TestProtectionFlagsActivity : Activity() {
27     override fun onCreate(savedInstanceState: Bundle?) {
28         super.onCreate(savedInstanceState)
29 
30         setResult(
31             RESULT_OK,
32             Intent().apply {
33                 putExtra("$packageName.ERROR_MESSAGE", getProtectionFlagsErrorMessage())
34             }
35         )
36         finish()
37     }
38 
39     private fun getProtectionFlagsErrorMessage(): String {
40         val packageInfo = packageManager.getPackageInfo("android", PackageManager.GET_PERMISSIONS)
41         val errorMessageBuilder = StringBuilder()
42         for (declaredPermissionInfo in packageInfo.permissions ?: emptyArray()) {
43             val permissionInfo = packageManager.getPermissionInfo(declaredPermissionInfo.name, 0)
44             val protection =
45                 permissionInfo.protection and
46                     (PermissionInfo.PROTECTION_NORMAL or
47                         PermissionInfo.PROTECTION_DANGEROUS or
48                         PermissionInfo.PROTECTION_SIGNATURE or
49                         PermissionInfo.PROTECTION_INTERNAL)
50             val protectionFlags = permissionInfo.protectionLevel and protection.inv()
51             if (
52                 (protection == PermissionInfo.PROTECTION_NORMAL ||
53                     protection == PermissionInfo.PROTECTION_DANGEROUS) && protectionFlags != 0
54             ) {
55                 errorMessageBuilder.apply {
56                     if (isNotEmpty()) {
57                         append("\n")
58                     }
59                     append(
60                         "Cannot add protection flags ${protectionFlagsToString(protectionFlags)
61                     } to a ${protectionToString(protection)} protection permission: ${
62                     permissionInfo.name}"
63                     )
64                 }
65             }
66         }
67         return errorMessageBuilder.toString()
68     }
69 
70     private fun protectionToString(protection: Int): String =
71         when (protection) {
72             PermissionInfo.PROTECTION_NORMAL -> "normal"
73             PermissionInfo.PROTECTION_DANGEROUS -> "dangerous"
74             PermissionInfo.PROTECTION_SIGNATURE -> "signature"
75             PermissionInfo.PROTECTION_INTERNAL -> "internal"
76             else -> Integer.toHexString(protection)
77         }
78 
79     private fun protectionFlagsToString(protectionFlags: Int): String {
80         var unknownProtectionFlags = protectionFlags
81         val stringBuilder = StringBuilder()
82         val appendProtectionFlag = { protectionFlag: Int, protectionFlagString: String ->
83             if (unknownProtectionFlags and protectionFlag == protectionFlag) {
84                 stringBuilder.apply {
85                     if (isNotEmpty()) {
86                         append("|")
87                     }
88                     append(protectionFlagString)
89                 }
90                 unknownProtectionFlags = unknownProtectionFlags and protectionFlag.inv()
91             }
92         }
93         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_PRIVILEGED, "privileged")
94         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_DEVELOPMENT, "development")
95         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_APPOP, "appop")
96         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_PRE23, "pre23")
97         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_INSTALLER, "installer")
98         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_VERIFIER, "verifier")
99         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_PREINSTALLED, "preinstalled")
100         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_SETUP, "setup")
101         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_INSTANT, "instant")
102         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_RUNTIME_ONLY, "runtimeOnly")
103         appendProtectionFlag(PermissionInfo.PROTECTION_FLAG_ROLE, "role")
104         if (unknownProtectionFlags != 0) {
105             appendProtectionFlag(
106                 unknownProtectionFlags,
107                 Integer.toHexString(unknownProtectionFlags)
108             )
109         }
110         return stringBuilder.toString()
111     }
112 }
113