1 /* 2 * Copyright (C) 2017 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 package com.android.cts.delegate; 17 18 import static android.app.admin.DevicePolicyManager.DELEGATION_BLOCK_UNINSTALL; 19 import static com.android.cts.delegate.DelegateTestUtils.assertExpectException; 20 21 import android.app.admin.DevicePolicyManager; 22 import android.content.Context; 23 import android.test.InstrumentationTestCase; 24 import android.test.MoreAsserts; 25 26 import java.util.List; 27 28 /** 29 * Test that an app given the {@link DevicePolicyManager#DELEGATION_BLOCK_UNINSTALL} scope via 30 * {@link DevicePolicyManager#setDelegatedScopes} can choose packages that are block uninstalled. 31 */ 32 public class BlockUninstallDelegateTest extends InstrumentationTestCase { 33 34 private static final String TEST_APP_PKG = "com.android.cts.launcherapps.simpleapp"; 35 36 private DevicePolicyManager mDpm; 37 38 @Override 39 protected void setUp() throws Exception { 40 super.setUp(); 41 42 Context context = getInstrumentation().getContext(); 43 mDpm = context.getSystemService(DevicePolicyManager.class); 44 } 45 46 public void testCannotAccessApis() { 47 assertFalse("DelegateApp should not be a block uninstall delegate", 48 amIBlockUninstallDelegate()); 49 50 assertExpectException(SecurityException.class, 51 "Caller with uid \\d+ is not a delegate of scope", () -> { 52 mDpm.setUninstallBlocked(null, TEST_APP_PKG, true); 53 }); 54 } 55 56 public void testCanAccessApis() { 57 assertTrue("DelegateApp is not a block uninstall delegate", 58 amIBlockUninstallDelegate()); 59 try { 60 // Exercise setUninstallBlocked. 61 mDpm.setUninstallBlocked(null, TEST_APP_PKG, true); 62 assertTrue("App not uninstall blocked", mDpm.isUninstallBlocked(null, TEST_APP_PKG)); 63 } finally { 64 mDpm.setUninstallBlocked(null, TEST_APP_PKG, false); 65 assertFalse("App still uninstall blocked", mDpm.isUninstallBlocked(null, TEST_APP_PKG)); 66 } 67 } 68 69 private boolean amIBlockUninstallDelegate() { 70 final String packageName = getInstrumentation().getContext().getPackageName(); 71 final List<String> scopes = mDpm.getDelegatedScopes(null, packageName); 72 return scopes.contains(DELEGATION_BLOCK_UNINSTALL); 73 } 74 } 75