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 com.android.cts.packageinstallerpermissionrequestapp 18 19 import android.Manifest 20 import android.appsecurity.cts.PackageSetInstallerConstants 21 import android.appsecurity.cts.PackageSetInstallerConstants.SHOULD_THROW_EXCEPTION_KEY 22 import android.content.Context 23 import android.content.pm.PackageManager 24 import android.os.Bundle 25 import androidx.test.InstrumentationRegistry 26 import com.google.common.truth.Truth.assertThat 27 import org.junit.After 28 import org.junit.Assert 29 import org.junit.Before 30 import org.junit.Rule 31 import org.junit.Test 32 import org.junit.rules.ExpectedException 33 34 class PermissionRequestTest { 35 36 @get:Rule 37 val expectedException = ExpectedException.none() 38 39 private val arguments: Bundle = InstrumentationRegistry.getArguments() 40 private val permission = arguments.getString(PackageSetInstallerConstants.PERMISSION_KEY)!! 41 private val context: Context = InstrumentationRegistry.getContext() 42 private val packageName = context.packageName 43 private val packageManager = context.packageManager 44 45 @Before 46 @After verifyPermissionsDeniedAndInstallerUnchangednull47 fun verifyPermissionsDeniedAndInstallerUnchanged() { 48 assertThat(context.checkSelfPermission(permission)) 49 .isEqualTo(PackageManager.PERMISSION_DENIED) 50 assertThat(context.checkSelfPermission(Manifest.permission.INSTALL_PACKAGES)) 51 .isEqualTo(PackageManager.PERMISSION_DENIED) 52 assertThat(packageManager.getInstallerPackageName(packageName)) 53 .isEqualTo(null) 54 } 55 56 @Test setSelfAsInstallerAndWhitelistPermissionnull57 fun setSelfAsInstallerAndWhitelistPermission() { 58 val shouldThrowException = arguments.getString(SHOULD_THROW_EXCEPTION_KEY)!!.toBoolean() 59 if (shouldThrowException) { 60 expectedException.expect(SecurityException::class.java) 61 } 62 63 try { 64 // This set call should fail 65 packageManager.setInstallerPackageName(packageName, packageName) 66 } finally { 67 // But also call the whitelist method to attempt to take the permission regardless. 68 // If this fails, which it should, it should also be a SecurityException. 69 try { 70 packageManager.addWhitelistedRestrictedPermission(packageName, 71 permission, PackageManager.FLAG_PERMISSION_WHITELIST_INSTALLER) 72 Assert.fail("addWhitelistedRestrictedPermission did not throw SecurityException") 73 } catch (ignored: SecurityException) { 74 // Ignore this to defer to shouldThrowException from above call 75 } 76 } 77 } 78 } 79