1 /* 2 * Copyright (C) 2020 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.permission.cts; 18 19 import static org.testng.Assert.assertThrows; 20 21 import android.content.pm.PackageInstaller; 22 import android.platform.test.annotations.AppModeFull; 23 24 import androidx.test.platform.app.InstrumentationRegistry; 25 26 import org.junit.Test; 27 28 @AppModeFull(reason = "PackageInstaller cannot be accessed by instant apps") 29 public class NoRollbackPermissionTest { 30 @Test testCreateInstallSessionWithReasonRollbackFails()31 public void testCreateInstallSessionWithReasonRollbackFails() throws Exception { 32 // The INSTALL_REASON_ROLLBACK allows an APK to be rolled back to a previous signing key 33 // without setting the ROLLBACK capability in the lineage. Since only signature|privileged 34 // apps can hold the necessary permission to initiate a rollback ensure apps without this 35 // permission cannot set rollback as the install reason. 36 PackageInstaller packageInstaller = 37 InstrumentationRegistry.getInstrumentation().getTargetContext().getPackageManager() 38 .getPackageInstaller(); 39 PackageInstaller.SessionParams parentParams = new PackageInstaller.SessionParams( 40 PackageInstaller.SessionParams.MODE_FULL_INSTALL); 41 parentParams.setRequestDowngrade(true); 42 parentParams.setMultiPackage(); 43 // The constant PackageManager.INSTALL_REASON_ROLLBACK is hidden from apps, but an app can 44 // still use its constant value. 45 parentParams.setInstallReason(5); 46 assertThrows(SecurityException.class, () -> packageInstaller.createSession(parentParams)); 47 } 48 } 49