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.suspendapps.permission.cts; 18 19 import static android.content.pm.PackageManager.RESTRICTION_HIDE_FROM_SUGGESTIONS; 20 import static android.content.pm.PackageManager.RESTRICTION_HIDE_NOTIFICATIONS; 21 import static android.content.pm.PackageManager.RESTRICTION_NONE; 22 23 import static org.junit.Assert.fail; 24 25 import android.content.Context; 26 import android.content.pm.PackageManager; 27 import android.content.pm.SuspendDialogInfo; 28 import android.util.Log; 29 30 import androidx.test.InstrumentationRegistry; 31 import androidx.test.filters.SmallTest; 32 import androidx.test.runner.AndroidJUnit4; 33 34 import com.android.suspendapps.suspendtestapp.Constants; 35 36 import org.junit.Before; 37 import org.junit.Test; 38 import org.junit.runner.RunWith; 39 40 import java.util.concurrent.Callable; 41 42 @RunWith(AndroidJUnit4.class) 43 @SmallTest 44 public class NegativePermissionsTest { 45 private static final String TAG = NegativePermissionsTest.class.getSimpleName(); 46 private static final String[] PACKAGES_TO_SUSPEND = new String[] { 47 Constants.PACKAGE_NAME, 48 Constants.ANDROID_PACKAGE_NAME_2, 49 }; 50 51 private PackageManager mPackageManager; 52 53 @Before setUp()54 public void setUp() { 55 final Context context = InstrumentationRegistry.getTargetContext(); 56 mPackageManager = context.getPackageManager(); 57 } 58 assertSecurityException(Callable callable, String tag)59 private void assertSecurityException(Callable callable, String tag) { 60 try { 61 callable.call(); 62 } catch (SecurityException e) { 63 // Passed. 64 return; 65 } catch (Exception e) { 66 Log.e(TAG, "Unexpected exception while calling [" + tag + "]", e); 67 fail("Unexpected exception while calling [" + tag + "]: " + e.getMessage()); 68 } 69 fail("Call [" + tag + "] succeeded without permissions"); 70 } 71 72 @Test setPackagesSuspended()73 public void setPackagesSuspended() { 74 assertSecurityException( 75 () -> mPackageManager.setPackagesSuspended(PACKAGES_TO_SUSPEND, true, null, null, 76 (SuspendDialogInfo) null), "setPackagesSuspended:true"); 77 assertSecurityException( 78 () -> mPackageManager.setPackagesSuspended(PACKAGES_TO_SUSPEND, false, null, null, 79 (SuspendDialogInfo) null), "setPackagesSuspended:false"); 80 } 81 82 @Test setPackagesSuspended_deprecated()83 public void setPackagesSuspended_deprecated() { 84 assertSecurityException( 85 () -> mPackageManager.setPackagesSuspended(PACKAGES_TO_SUSPEND, true, null, null, 86 (String) null), "setPackagesSuspended:true"); 87 assertSecurityException( 88 () -> mPackageManager.setPackagesSuspended(PACKAGES_TO_SUSPEND, false, null, null, 89 (String) null), "setPackagesSuspended:false"); 90 } 91 92 @Test setDistractingPackageRestrictions()93 public void setDistractingPackageRestrictions() { 94 assertSecurityException( 95 () -> mPackageManager.setDistractingPackageRestrictions(PACKAGES_TO_SUSPEND, 96 RESTRICTION_HIDE_FROM_SUGGESTIONS), 97 "setDistractingPackageRestrictions:HIDE_FROM_SUGGESTIONS"); 98 assertSecurityException( 99 () -> mPackageManager.setDistractingPackageRestrictions(PACKAGES_TO_SUSPEND, 100 RESTRICTION_HIDE_FROM_SUGGESTIONS | RESTRICTION_HIDE_NOTIFICATIONS), 101 "setDistractingPackageRestrictions:HIDE_FROM_SUGGESTIONS|HIDE_NOTIFICATIONS"); 102 assertSecurityException( 103 () -> mPackageManager.setDistractingPackageRestrictions(PACKAGES_TO_SUSPEND, 104 RESTRICTION_HIDE_NOTIFICATIONS), 105 "setDistractingPackageRestrictions:HIDE_NOTIFICATIONS"); 106 assertSecurityException( 107 () -> mPackageManager.setDistractingPackageRestrictions(PACKAGES_TO_SUSPEND, 108 RESTRICTION_NONE), "setDistractingPackageRestrictions:NONE"); 109 } 110 } 111