1 /* 2 * Copyright (C) 2023 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.os.test; 18 19 import static android.permission.PermissionManager.PERMISSION_GRANTED; 20 import static android.permission.PermissionManager.PERMISSION_HARD_DENIED; 21 22 import android.annotation.NonNull; 23 import android.content.AttributionSource; 24 import android.os.PermissionEnforcer; 25 26 import java.util.HashSet; 27 import java.util.Set; 28 29 /** 30 * Fake for {@link PermissionEnforcer}. Useful for tests wanting to mock the 31 * permission checks of an AIDL service. FakePermissionEnforcer may be passed 32 * to the constructor of the AIDL-generated Stub class. 33 * 34 */ 35 public class FakePermissionEnforcer extends PermissionEnforcer { 36 private Set<String> mGranted; 37 FakePermissionEnforcer()38 public FakePermissionEnforcer() { 39 mGranted = new HashSet(); 40 } 41 grant(String permission)42 public void grant(String permission) { 43 mGranted.add(permission); 44 } 45 revoke(String permission)46 public void revoke(String permission) { 47 mGranted.remove(permission); 48 } 49 granted(String permission)50 private boolean granted(String permission) { 51 return mGranted.contains(permission); 52 } 53 54 @Override checkPermission(@onNull String permission, @NonNull AttributionSource source)55 protected int checkPermission(@NonNull String permission, 56 @NonNull AttributionSource source) { 57 return granted(permission) ? PERMISSION_GRANTED : PERMISSION_HARD_DENIED; 58 } 59 60 @Override checkPermission(@onNull String permission, int pid, int uid)61 protected int checkPermission(@NonNull String permission, int pid, int uid) { 62 return granted(permission) ? PERMISSION_GRANTED : PERMISSION_HARD_DENIED; 63 } 64 } 65