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.server.pm 18 19 import android.os.Build 20 import android.os.SystemProperties 21 import com.android.server.extendedtestutils.wheneverStatic 22 import com.android.server.testutils.spy 23 import com.android.server.testutils.whenever 24 import com.google.common.truth.Truth.assertThat 25 import org.junit.Before 26 import org.junit.Rule 27 import org.junit.Test 28 import org.junit.runner.RunWith 29 import org.junit.runners.JUnit4 30 31 @RunWith(JUnit4::class) 32 class InitAppsHelperTest { 33 @Rule 34 @JvmField 35 val rule = MockSystemRule() 36 37 @Before 38 fun setUp() { 39 rule.system().stageNominalSystemState() 40 } 41 42 private fun mockNoFirstBoot() { 43 // To mock that this is not the first boot 44 rule.system().stageScanExistingPackage("a.data.package", 1L, 45 rule.system().dataAppDirectory) 46 } 47 48 private fun mockFingerprintChanged() { 49 val versionInfo = Settings.VersionInfo() 50 versionInfo.fingerprint = "mockFingerprintForTesting" 51 whenever(rule.mocks().settings.internalVersion) { 52 versionInfo 53 } 54 } 55 56 private fun mockFingerprintUnchanged() { 57 // To mock that this is not the first boot 58 val versionInfo = Settings.VersionInfo() 59 versionInfo.fingerprint = MockSystem.DEFAULT_VERSION_INFO.fingerprint 60 whenever(rule.mocks().settings.internalVersion) { 61 versionInfo 62 } 63 } 64 65 private fun mockOta() { 66 wheneverStatic { 67 SystemProperties.getBoolean("persist.pm.mock-upgrade", false) 68 }.thenReturn(true) 69 } 70 71 private fun createPackageManagerService(): PackageManagerService { 72 return spy(PackageManagerService(rule.mocks().injector, 73 false /*factoryTest*/, 74 MockSystem.DEFAULT_VERSION_INFO.fingerprint, 75 false /*isEngBuild*/, 76 false /*isUserDebugBuild*/, 77 Build.VERSION_CODES.CUR_DEVELOPMENT, 78 Build.VERSION.INCREMENTAL)) 79 } 80 81 @Test 82 fun testSystemScanFlagOnFirstBoot() { 83 val pms = createPackageManagerService() 84 assertThat(pms.isFirstBoot).isEqualTo(true) 85 assertThat(pms.isDeviceUpgrading).isEqualTo(false) 86 val initAppsHelper = InitAppsHelper(pms, rule.mocks().apexManager, null, 87 listOf<ScanPartition>()) 88 assertThat( 89 initAppsHelper.systemScanFlags and PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE) 90 .isEqualTo(PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE) 91 } 92 93 @Test 94 fun testSystemScanFlagWithMockOTA() { 95 mockNoFirstBoot() 96 mockFingerprintUnchanged() 97 mockOta() 98 val pms = createPackageManagerService() 99 assertThat(pms.isFirstBoot).isEqualTo(false) 100 assertThat(pms.isDeviceUpgrading).isEqualTo(true) 101 val initAppsHelper = InitAppsHelper(pms, rule.mocks().apexManager, null, 102 listOf<ScanPartition>()) 103 assertThat( 104 initAppsHelper.systemScanFlags and PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE) 105 .isEqualTo(PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE) 106 } 107 108 @Test 109 fun testSystemScanFlagNoOTA() { 110 mockNoFirstBoot() 111 mockFingerprintUnchanged() 112 val pms = createPackageManagerService() 113 assertThat(pms.isFirstBoot).isEqualTo(false) 114 assertThat(pms.isDeviceUpgrading).isEqualTo(false) 115 val initAppsHelper = InitAppsHelper(pms, rule.mocks().apexManager, null, 116 listOf<ScanPartition>()) 117 assertThat( 118 initAppsHelper.systemScanFlags and PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE) 119 .isEqualTo(0) 120 } 121 122 @Test 123 fun testSystemScanFlagWithFingerprintChanged() { 124 mockNoFirstBoot() 125 mockFingerprintChanged() 126 val pms = createPackageManagerService() 127 assertThat(pms.isFirstBoot).isEqualTo(false) 128 assertThat(pms.isDeviceUpgrading).isEqualTo(true) 129 val initAppsHelper = InitAppsHelper(pms, rule.mocks().apexManager, null, 130 listOf<ScanPartition>()) 131 assertThat( 132 initAppsHelper.systemScanFlags and PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE) 133 .isEqualTo(PackageManagerService.SCAN_FIRST_BOOT_OR_UPGRADE) 134 } 135 } 136