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 android.service.persistentdata; 18 19 import static com.android.compatibility.common.util.PropertyUtil.getFirstApiLevel; 20 21 import static com.google.common.truth.Truth.assertThat; 22 23 import static org.junit.Assert.assertThrows; 24 import static org.junit.Assume.assumeTrue; 25 26 import android.content.Context; 27 import android.os.Build; 28 import android.os.SystemProperties; 29 30 import com.android.bedstead.harrier.BedsteadJUnit4; 31 import com.android.bedstead.harrier.DeviceState; 32 import com.android.bedstead.nene.TestApis; 33 import com.android.bedstead.permissions.annotations.EnsureDoesNotHavePermission; 34 import com.android.bedstead.permissions.annotations.EnsureHasPermission; 35 36 import org.junit.ClassRule; 37 import org.junit.Rule; 38 import org.junit.Test; 39 import org.junit.runner.RunWith; 40 41 @RunWith(BedsteadJUnit4.class) 42 public class PersistentDataBlockManagerTest { 43 @ClassRule 44 @Rule 45 public static final DeviceState sDeviceState = new DeviceState(); 46 47 private static final Context sContext = TestApis.context().instrumentedContext(); 48 private static final PersistentDataBlockManager sPersistentDataBlockManager = 49 sContext.getSystemService(PersistentDataBlockManager.class); 50 public static final int FACTORY_RESET_SECRET_SIZE = 32; 51 public static final String PERSISTENT_DATA_BLOCK_PROPERTY = "ro.frp.pst"; 52 53 @EnsureHasPermission(android.Manifest.permission.ACCESS_PDB_STATE) 54 @Test getPersistentDataPackageName_returnsNonNullResult()55 public void getPersistentDataPackageName_returnsNonNullResult() { 56 if (sPersistentDataBlockManager == null) { 57 return; 58 } 59 assertThat(sPersistentDataBlockManager.getPersistentDataPackageName()).isNotNull(); 60 } 61 62 @EnsureDoesNotHavePermission(android.Manifest.permission.ACCESS_PDB_STATE) 63 @Test getPersistentDataPackageName_withoutPermission_throwsException()64 public void getPersistentDataPackageName_withoutPermission_throwsException() { 65 if (sPersistentDataBlockManager == null) { 66 return; 67 } 68 assertThrows(SecurityException.class, 69 sPersistentDataBlockManager::getPersistentDataPackageName); 70 } 71 deviceHasPersistentDataBlock()72 private static boolean deviceHasPersistentDataBlock() { 73 return !SystemProperties.get(PERSISTENT_DATA_BLOCK_PROPERTY).equals(""); 74 } 75 shouldSupportFrpActiveApi()76 private static boolean shouldSupportFrpActiveApi() { 77 return getFirstApiLevel() >= Build.VERSION_CODES.VANILLA_ICE_CREAM 78 && deviceHasPersistentDataBlock(); 79 } 80 81 @EnsureDoesNotHavePermission(android.Manifest.permission.ACCESS_PDB_STATE) 82 @Test checkFactoryResetProtection()83 public void checkFactoryResetProtection() { 84 assumeTrue(shouldSupportFrpActiveApi()); 85 86 assertThat(sPersistentDataBlockManager).isNotNull(); 87 assertThat(sPersistentDataBlockManager.isFactoryResetProtectionActive()).isFalse(); 88 } 89 90 @EnsureDoesNotHavePermission(android.Manifest.permission.ACCESS_PDB_STATE) 91 @Test verifyOtherMethodsCannotBeCalledByNonPrivilegedApps()92 public void verifyOtherMethodsCannotBeCalledByNonPrivilegedApps() { 93 assumeTrue(shouldSupportFrpActiveApi()); 94 95 assertThat(sPersistentDataBlockManager).isNotNull(); 96 assertThrows(SecurityException.class, 97 () -> sPersistentDataBlockManager.write(new byte[0])); 98 assertThrows(SecurityException.class, 99 () -> sPersistentDataBlockManager.write(new byte[10])); 100 assertThrows(SecurityException.class, 101 () -> sPersistentDataBlockManager.read()); 102 assertThrows(SecurityException.class, 103 () -> sPersistentDataBlockManager.getDataBlockSize()); 104 assertThrows(SecurityException.class, 105 () -> sPersistentDataBlockManager.getMaximumDataBlockSize()); 106 assertThrows(SecurityException.class, 107 () -> sPersistentDataBlockManager.wipe()); 108 assertThrows(SecurityException.class, 109 () -> sPersistentDataBlockManager.setOemUnlockEnabled(true)); 110 assertThrows(SecurityException.class, 111 () -> sPersistentDataBlockManager.setOemUnlockEnabled(false)); 112 assertThrows(SecurityException.class, 113 () -> sPersistentDataBlockManager.getOemUnlockEnabled()); 114 assertThrows(SecurityException.class, 115 () -> sPersistentDataBlockManager.getFlashLockState()); 116 assertThrows(SecurityException.class, 117 () -> sPersistentDataBlockManager.deactivateFactoryResetProtection( 118 new byte[FACTORY_RESET_SECRET_SIZE])); 119 assertThrows(SecurityException.class, 120 () -> sPersistentDataBlockManager.setFactoryResetProtectionSecret( 121 new byte[FACTORY_RESET_SECRET_SIZE])); 122 } 123 } 124