Home | History | Annotate | Download | only in delegate
      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_PACKAGE_ACCESS;
     19 import static com.android.cts.delegate.DelegateTestUtils.assertExpectException;
     20 
     21 import android.app.admin.DevicePolicyManager;
     22 import android.content.Context;
     23 import android.content.pm.PackageManager.NameNotFoundException;
     24 import android.test.InstrumentationTestCase;
     25 import android.test.MoreAsserts;
     26 
     27 import java.util.Arrays;
     28 import java.util.List;
     29 
     30 /**
     31  * Test that an app given the {@link DevicePolicyManager#DELEGATION_PACKAGE_ACCESS} scope via
     32  * {@link DevicePolicyManager#setDelegatedScopes} can manage package hide and suspend status.
     33  */
     34 public class PackageAccessDelegateTest extends InstrumentationTestCase {
     35 
     36     private static final String TEST_APP_PKG = "com.android.cts.launcherapps.simpleapp";
     37 
     38     private DevicePolicyManager mDpm;
     39 
     40     @Override
     41     protected void setUp() throws Exception {
     42         super.setUp();
     43 
     44         Context context = getInstrumentation().getContext();
     45         mDpm = context.getSystemService(DevicePolicyManager.class);
     46     }
     47 
     48     public void testCannotAccessApis() throws NameNotFoundException {
     49         assertFalse("DelegateApp should not be a package access delegate",
     50             amIPackageAccessDelegate());
     51 
     52         // Exercise isApplicationHidden.
     53         assertExpectException(SecurityException.class,
     54                 "Caller with uid \\d+ is not a delegate of scope", () -> {
     55                     mDpm.isApplicationHidden(null, TEST_APP_PKG);
     56                 });
     57 
     58         // Exercise setApplicationHidden.
     59         assertExpectException(SecurityException.class,
     60                 "Caller with uid \\d+ is not a delegate of scope", () -> {
     61                     mDpm.setApplicationHidden(null, TEST_APP_PKG, true /* hide */);
     62                 });
     63 
     64         // Exercise isPackageSuspended.
     65         assertExpectException(SecurityException.class,
     66                 "Caller with uid \\d+ is not a delegate of scope", () -> {
     67                     mDpm.isPackageSuspended(null, TEST_APP_PKG);
     68                 });
     69 
     70         // Exercise setPackagesSuspended.
     71         assertExpectException(SecurityException.class,
     72                 "Caller with uid \\d+ is not a delegate of scope", () -> {
     73                     mDpm.setPackagesSuspended(null, new String[] {TEST_APP_PKG}, true /* suspend */);
     74                 });
     75     }
     76 
     77     public void testCanAccessApis() throws NameNotFoundException {
     78         assertTrue("DelegateApp is not a package access delegate", amIPackageAccessDelegate());
     79 
     80         // Exercise isApplicationHidden.
     81         assertFalse("Package should not be hidden", mDpm.isApplicationHidden(null, TEST_APP_PKG));
     82 
     83         // Exercise setApplicationHidden.
     84         assertTrue("Package not hidden successfully",
     85                 mDpm.setApplicationHidden(null, TEST_APP_PKG, true /* hide */));
     86         assertTrue("Package should be hidden", mDpm.isApplicationHidden(null, TEST_APP_PKG));
     87 
     88         // Exercise isPackageSuspended.
     89         assertFalse("Package should not be suspended", mDpm.isPackageSuspended(null, TEST_APP_PKG));
     90 
     91         // Exercise setPackagesSuspended.
     92         String[] suspended = mDpm.setPackagesSuspended(null, new String[] {TEST_APP_PKG},
     93                 true /* suspend */);
     94         assertTrue("Package not suspended successfully", suspended.length == 0);
     95         assertTrue("Package should be suspended", mDpm.isPackageSuspended(null, TEST_APP_PKG));
     96     }
     97 
     98     private boolean amIPackageAccessDelegate() {
     99         final String packageName = getInstrumentation().getContext().getPackageName();
    100         final List<String> scopes = mDpm.getDelegatedScopes(null, packageName);
    101         return scopes.contains(DELEGATION_PACKAGE_ACCESS);
    102     }
    103 }
    104