• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.permissionui
18 
19 import android.Manifest.permission.CAMERA
20 import android.app.AppOpsManager
21 import android.app.AppOpsManager.MODE_ALLOWED
22 import android.app.AppOpsManager.OPSTR_CAMERA
23 import android.content.ComponentName
24 import android.content.Intent
25 import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
26 import android.content.pm.PackageManager.FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED
27 import android.os.Process.myUserHandle
28 import android.provider.DeviceConfig
29 import android.provider.DeviceConfig.NAMESPACE_PRIVACY
30 import androidx.test.platform.app.InstrumentationRegistry
31 import com.android.compatibility.common.util.SystemUtil.eventually
32 import com.android.compatibility.common.util.SystemUtil.runShellCommand
33 import com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity
34 import com.google.common.truth.Truth.assertThat
35 import org.junit.AfterClass
36 import org.junit.BeforeClass
37 
38 /**
39  * Super class with utilities for testing permission hub 2 code
40  */
41 open class PermissionHub2Test {
42     private val APP = "com.android.permissioncontroller.tests.appthatrequestpermission"
43 
44     private val instrumentation = InstrumentationRegistry.getInstrumentation()
45     protected val context = instrumentation.targetContext
46 
47     companion object {
48         private const val PROPERTY_PERMISSIONS_HUB_2_ENABLED = "permissions_hub_2_enabled"
49 
50         private var wasPermissionHubEnabled = false
51 
52         @JvmStatic
53         @BeforeClass
enablePermissionHub2null54         fun enablePermissionHub2() {
55 
56             runWithShellPermissionIdentity {
57                 wasPermissionHubEnabled = DeviceConfig.getBoolean(NAMESPACE_PRIVACY,
58                     PROPERTY_PERMISSIONS_HUB_2_ENABLED, false)
59             }
60 
61             if (!wasPermissionHubEnabled) {
62                 runShellCommand(
63                     "device_config put privacy $PROPERTY_PERMISSIONS_HUB_2_ENABLED true")
64             }
65         }
66 
67         @JvmStatic
68         @AfterClass
disablePermissionHub2null69         fun disablePermissionHub2() {
70             if (!wasPermissionHubEnabled) {
71                 runShellCommand(
72                     "device_config put privacy $PROPERTY_PERMISSIONS_HUB_2_ENABLED false")
73             }
74         }
75     }
76 
77     /**
78      * Make {@value #APP} access the camera
79      */
accessCameranull80     protected fun accessCamera() {
81         // App needs to be in foreground to be able to access camera
82         context.startActivity(
83             Intent().setComponent(ComponentName.createRelative(APP, ".DummyActivity"))
84                 .setFlags(FLAG_ACTIVITY_NEW_TASK))
85 
86         runWithShellPermissionIdentity {
87             eventually {
88                 assertThat(
89                     context.packageManager.getPermissionFlags(CAMERA, APP, myUserHandle()) and
90                         FLAG_PERMISSION_USER_SENSITIVE_WHEN_GRANTED
91                 ).isNotEqualTo(0)
92             }
93 
94             eventually {
95                 assertThat(
96                     context.getSystemService(AppOpsManager::class.java).startOp(
97                         OPSTR_CAMERA, context.packageManager.getPackageUid(APP, 0), APP, null, null
98                     )
99                 ).isEqualTo(MODE_ALLOWED)
100             }
101         }
102     }
103 }
104