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.nfc; 18 19 import static android.content.pm.PackageManager.PERMISSION_GRANTED; 20 21 import static com.android.nfc.NfcPermissions.NFC_PERMISSION; 22 import static com.android.nfc.NfcPermissions.NFC_PREFERRED_PAYMENT_INFO_PERMISSION; 23 import static com.android.nfc.NfcPermissions.NFC_SET_CONTROLLER_ALWAYS_ON; 24 25 import static org.junit.Assert.assertEquals; 26 import static org.junit.Assert.assertFalse; 27 import static org.junit.Assert.assertTrue; 28 import static org.mockito.ArgumentMatchers.any; 29 import static org.mockito.ArgumentMatchers.anyInt; 30 import static org.mockito.ArgumentMatchers.anyString; 31 import static org.mockito.Mockito.mock; 32 import static org.mockito.Mockito.verify; 33 import static org.mockito.Mockito.when; 34 35 import android.app.AppOpsManager; 36 import android.app.admin.DevicePolicyManager; 37 import android.content.Context; 38 import android.content.pm.PackageManager; 39 import android.os.UserHandle; 40 import android.os.UserManager; 41 42 import com.android.dx.mockito.inline.extended.ExtendedMockito; 43 44 import org.junit.After; 45 import org.junit.Before; 46 import org.junit.Test; 47 import org.mockito.Mock; 48 import org.mockito.MockitoAnnotations; 49 import org.mockito.MockitoSession; 50 import org.mockito.quality.Strictness; 51 52 import java.util.Collections; 53 import java.util.List; 54 55 public class NfcPermissionsTest { 56 57 @Mock 58 private Context mMockContext; 59 @Mock 60 private AppOpsManager mMockAppOpsMngr; 61 @Mock 62 private UserManager mMockUserMngr; 63 @Mock 64 private UserHandle mMockUserHandle; 65 private MockitoSession mStaticMockSession; 66 private NfcPermissions mNfcPermissions; 67 68 @Before setUp()69 public void setUp() { 70 mStaticMockSession = ExtendedMockito.mockitoSession() 71 .mockStatic(UserHandle.class) 72 .strictness(Strictness.LENIENT).startMocking(); 73 MockitoAnnotations.initMocks(this); 74 when(mMockContext.getSystemService(AppOpsManager.class)).thenReturn(mMockAppOpsMngr); 75 76 mNfcPermissions = new NfcPermissions(mMockContext); 77 } 78 79 @After tearDown()80 public void tearDown() { 81 mStaticMockSession.finishMocking(); 82 } 83 84 @Test testValidateProfileIdOfCurrentUser()85 public void testValidateProfileIdOfCurrentUser() { 86 Context mockContextAsUser = mock(Context.class); 87 List<UserHandle> luh = Collections.singletonList(mMockUserHandle); 88 int profileId = 1001; 89 when(mMockContext.createContextAsUser(any(), anyInt())).thenReturn(mockContextAsUser); 90 when(mockContextAsUser.getSystemService(UserManager.class)).thenReturn(mMockUserMngr); 91 when(mMockUserMngr.getEnabledProfiles()).thenReturn(luh); 92 when(mMockUserHandle.getIdentifier()).thenReturn(profileId); 93 94 NfcPermissions.validateProfileId(mMockContext, profileId); 95 verify(mMockUserMngr).getEnabledProfiles(); 96 } 97 98 @Test(expected = SecurityException.class) testValidateProfileIdOfDifferentUser()99 public void testValidateProfileIdOfDifferentUser() { 100 Context mockContextAsUser = mock(Context.class); 101 List<UserHandle> luh = Collections.singletonList(mMockUserHandle); 102 int currentProfileId = 1001; 103 int diffProfileId = 1122; 104 when(mMockContext.createContextAsUser(any(), anyInt())).thenReturn(mockContextAsUser); 105 when(mockContextAsUser.getSystemService(UserManager.class)).thenReturn(mMockUserMngr); 106 when(mMockUserMngr.getEnabledProfiles()).thenReturn(luh); 107 when(mMockUserHandle.getIdentifier()).thenReturn(currentProfileId); 108 109 NfcPermissions.validateProfileId(mMockContext, diffProfileId); 110 } 111 112 @Test testEnforceAdminPermissions()113 public void testEnforceAdminPermissions() { 114 NfcPermissions.enforceAdminPermissions(mMockContext); 115 verify(mMockContext).enforceCallingOrSelfPermission( 116 android.Manifest.permission.WRITE_SECURE_SETTINGS, 117 "WRITE_SECURE_SETTINGS permission required"); 118 } 119 120 @Test testCheckAdminPermissions()121 public void testCheckAdminPermissions() { 122 when(mMockContext.checkCallingOrSelfPermission( 123 android.Manifest.permission.WRITE_SECURE_SETTINGS)).thenReturn(PERMISSION_GRANTED); 124 125 assertTrue(NfcPermissions.checkAdminPermissions(mMockContext)); 126 verify(mMockContext).checkCallingOrSelfPermission( 127 android.Manifest.permission.WRITE_SECURE_SETTINGS); 128 } 129 130 @Test testEnforceUserPermissions()131 public void testEnforceUserPermissions() { 132 NfcPermissions.enforceUserPermissions(mMockContext); 133 verify(mMockContext).enforceCallingOrSelfPermission(NFC_PERMISSION, 134 "NFC permission required"); 135 } 136 137 @Test testEnforcePreferredPaymentInfoPermissions()138 public void testEnforcePreferredPaymentInfoPermissions() { 139 NfcPermissions.enforcePreferredPaymentInfoPermissions(mMockContext); 140 verify(mMockContext).enforceCallingOrSelfPermission(NFC_PREFERRED_PAYMENT_INFO_PERMISSION, 141 "NFC_PREFERRED_PAYMENT_INFO permission required"); 142 } 143 144 @Test testEnforceSetControllerAlwaysOnPermissions()145 public void testEnforceSetControllerAlwaysOnPermissions() { 146 NfcPermissions.enforceSetControllerAlwaysOnPermissions(mMockContext); 147 verify(mMockContext).enforceCallingOrSelfPermission(NFC_SET_CONTROLLER_ALWAYS_ON, 148 "NFC_SET_CONTROLLER_ALWAYS_ON permission required"); 149 } 150 151 @Test testRetrieveDevicePolicyManagerFromContext()152 public void testRetrieveDevicePolicyManagerFromContext() { 153 DevicePolicyManager mockDevicePolicyManager = mock(DevicePolicyManager.class); 154 PackageManager mockPkgMagr = mock(PackageManager.class); 155 when(mMockContext.getSystemService(DevicePolicyManager.class)).thenReturn( 156 mockDevicePolicyManager); 157 when(mMockContext.getPackageManager()).thenReturn(mockPkgMagr); 158 when(mockPkgMagr.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)).thenReturn(true); 159 160 assertEquals(mockDevicePolicyManager, 161 NfcPermissions.retrieveDevicePolicyManagerFromContext(mMockContext)); 162 } 163 164 @Test testIsDeviceOwnerWhenPackageNameNull()165 public void testIsDeviceOwnerWhenPackageNameNull() { 166 assertFalse(mNfcPermissions.isDeviceOwner(1, null)); 167 } 168 169 @Test testISDeviceOwnerWhenDevicePolicyManagerNull()170 public void testISDeviceOwnerWhenDevicePolicyManagerNull() 171 throws PackageManager.NameNotFoundException { 172 when(mMockContext.createPackageContextAsUser(anyString(), anyInt(), any())) 173 .thenReturn(null); 174 175 assertFalse(mNfcPermissions.isDeviceOwner(101, "com.sample.package")); 176 } 177 178 @Test testISDeviceOwnerWhenPackageRegisterAsDeviceOwner()179 public void testISDeviceOwnerWhenPackageRegisterAsDeviceOwner() 180 throws PackageManager.NameNotFoundException { 181 String packageName = "com.sample.package"; 182 Context mockUserContext = mock(Context.class); 183 DevicePolicyManager mockDevicePolicyManager = mock(DevicePolicyManager.class); 184 PackageManager mockPkgMagr = mock(PackageManager.class); 185 when(mockPkgMagr.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)).thenReturn(true); 186 when(UserHandle.getUserHandleForUid(101)).thenReturn(mMockUserHandle); 187 when(mMockContext.getPackageName()).thenReturn(packageName); 188 when(mMockContext.createPackageContextAsUser(packageName, 0, 189 mMockUserHandle)).thenReturn(mockUserContext); 190 when(mockUserContext.getSystemService(DevicePolicyManager.class)) 191 .thenReturn(mockDevicePolicyManager); 192 when(mockUserContext.getPackageManager()).thenReturn(mockPkgMagr); 193 when(mockDevicePolicyManager.isDeviceOwnerApp(packageName)).thenReturn(true); 194 195 assertTrue(mNfcPermissions.isDeviceOwner(101, packageName)); 196 } 197 198 @Test(expected = SecurityException.class) testCheckPackageWithNullPackageName()199 public void testCheckPackageWithNullPackageName() { 200 mNfcPermissions.checkPackage(101, null); 201 } 202 203 @Test testCheckPackageWithPackageName()204 public void testCheckPackageWithPackageName() { 205 String packageName = "com.sample.package"; 206 207 mNfcPermissions.checkPackage(101, packageName); 208 verify(mMockAppOpsMngr).checkPackage(101, packageName); 209 } 210 211 @Test testIsProfileOwnerWhenPackageNameNull()212 public void testIsProfileOwnerWhenPackageNameNull() { 213 assertFalse(mNfcPermissions.isProfileOwner(1, null)); 214 } 215 216 @Test testIsProfileOwnerWhenDevicePolicyManagerNull()217 public void testIsProfileOwnerWhenDevicePolicyManagerNull() 218 throws PackageManager.NameNotFoundException { 219 when(mMockContext.createPackageContextAsUser(anyString(), anyInt(), any())) 220 .thenReturn(null); 221 222 assertFalse(mNfcPermissions.isProfileOwner(101, "com.sample.package")); 223 } 224 225 @Test testIsProfileOwnerWhenPackageRegisterAsProfileOwner()226 public void testIsProfileOwnerWhenPackageRegisterAsProfileOwner() 227 throws PackageManager.NameNotFoundException { 228 String packageName = "com.sample.package"; 229 Context mockUserContext = mock(Context.class); 230 DevicePolicyManager mockDevicePolicyManager = mock(DevicePolicyManager.class); 231 PackageManager mockPkgMagr = mock(PackageManager.class); 232 when(mockPkgMagr.hasSystemFeature(PackageManager.FEATURE_DEVICE_ADMIN)).thenReturn(true); 233 when(UserHandle.getUserHandleForUid(101)).thenReturn(mMockUserHandle); 234 when(mMockContext.getPackageName()).thenReturn(packageName); 235 when(mMockContext.createPackageContextAsUser(packageName, 0, 236 mMockUserHandle)).thenReturn(mockUserContext); 237 when(mockUserContext.getSystemService(DevicePolicyManager.class)) 238 .thenReturn(mockDevicePolicyManager); 239 when(mockUserContext.getPackageManager()).thenReturn(mockPkgMagr); 240 when(mockDevicePolicyManager.isProfileOwnerApp(packageName)).thenReturn(true); 241 242 assertTrue(mNfcPermissions.isProfileOwner(101, packageName)); 243 } 244 } 245