• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.compatibility.common.util;
18 
19 import android.app.UiAutomation;
20 
21 import androidx.annotation.NonNull;
22 import androidx.annotation.Nullable;
23 import androidx.test.InstrumentationRegistry;
24 
25 import org.junit.rules.TestRule;
26 import org.junit.runner.Description;
27 import org.junit.runners.model.Statement;
28 
29 /**
30  * Custom JUnit4 rule that runs a test adopting Shell's permissions, revoking them at the end.
31  *
32  * <p>NOTE: should only be used in the cases where *every* test in a class requires the permission.
33  * For a more fine-grained access, use {@link SystemUtil#runWithShellPermissionIdentity(Runnable)}
34  * or {@link SystemUtil#callWithShellPermissionIdentity(java.util.concurrent.Callable)} instead.
35  */
36 public class AdoptShellPermissionsRule implements TestRule {
37 
38     private final UiAutomation mUiAutomation;
39 
40     private final String[] mPermissions;
41 
AdoptShellPermissionsRule()42     public AdoptShellPermissionsRule() {
43         this(InstrumentationRegistry.getInstrumentation().getUiAutomation());
44     }
45 
AdoptShellPermissionsRule(@onNull UiAutomation uiAutomation)46     public AdoptShellPermissionsRule(@NonNull UiAutomation uiAutomation) {
47         this(uiAutomation, (String[]) null);
48     }
49 
AdoptShellPermissionsRule(@onNull UiAutomation uiAutomation, @Nullable String... permissions)50     public AdoptShellPermissionsRule(@NonNull UiAutomation uiAutomation,
51             @Nullable String... permissions) {
52         mUiAutomation = uiAutomation;
53         mPermissions = permissions;
54     }
55 
56     @Override
apply(Statement base, Description description)57     public Statement apply(Statement base, Description description) {
58         return new Statement() {
59 
60             @Override
61             public void evaluate() throws Throwable {
62                 if (mPermissions != null) {
63                     mUiAutomation.adoptShellPermissionIdentity(mPermissions);
64                 } else {
65                     mUiAutomation.adoptShellPermissionIdentity();
66                 }
67                 try {
68                     base.evaluate();
69                 } finally {
70                     mUiAutomation.dropShellPermissionIdentity();
71                 }
72             }
73         };
74     }
75 }
76