Home | History | Annotate | Download | only in deviceowner
      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 
     17 package com.android.cts.deviceowner;
     18 
     19 import android.app.admin.DevicePolicyManager;
     20 import android.content.ComponentName;
     21 import android.content.Context;
     22 import android.support.test.InstrumentationRegistry;
     23 import android.support.test.runner.AndroidJUnit4;
     24 
     25 import org.junit.Before;
     26 import org.junit.Test;
     27 import org.junit.runner.RunWith;
     28 
     29 import java.util.Collections;
     30 import java.util.Set;
     31 
     32 import static junit.framework.Assert.assertEquals;
     33 import static junit.framework.Assert.assertFalse;
     34 import static junit.framework.Assert.assertTrue;
     35 import static junit.framework.Assert.fail;
     36 import static org.junit.Assert.assertArrayEquals;
     37 
     38 @RunWith(AndroidJUnit4.class)
     39 public class AffiliationTest {
     40 
     41     private DevicePolicyManager mDevicePolicyManager;
     42     private ComponentName mAdminComponent;
     43 
     44     @Before
     45     public void setUp() {
     46         Context context = InstrumentationRegistry.getContext();
     47         mDevicePolicyManager = (DevicePolicyManager)
     48                 context.getSystemService(Context.DEVICE_POLICY_SERVICE);
     49         mAdminComponent = BasicAdminReceiver.getComponentName(context);
     50     }
     51 
     52     @Test
     53     public void testSetAffiliationId_null() {
     54         try {
     55             mDevicePolicyManager.setAffiliationIds(mAdminComponent, null);
     56             fail("Should throw IllegalArgumentException");
     57         } catch (IllegalArgumentException ex) {
     58             // Expected
     59         }
     60     }
     61 
     62     @Test
     63     public void testSetAffiliationId_containsEmptyString() {
     64         try {
     65             mDevicePolicyManager.setAffiliationIds(mAdminComponent, Collections.singleton(null));
     66             fail("Should throw IllegalArgumentException");
     67         } catch (IllegalArgumentException ex) {
     68             // Expected
     69         }
     70     }
     71 
     72     @Test
     73     public void testSetAffiliationId1() {
     74         setAffiliationIds(Collections.singleton("id.number.1"));
     75     }
     76 
     77     @Test
     78     public void testSetAffiliationId2() {
     79         setAffiliationIds(Collections.singleton("id.number.2"));
     80     }
     81 
     82     @Test
     83     public void testLockTaskMethodsThrowExceptionIfUnaffiliated() {
     84         checkLockTaskMethodsThrow();
     85     }
     86 
     87     /** Assumes that the calling user is already affiliated before calling this method */
     88     @Test
     89     public void testSetLockTaskPackagesClearedIfUserBecomesUnaffiliated() {
     90         final String[] packages = {"package1", "package2"};
     91         mDevicePolicyManager.setLockTaskPackages(mAdminComponent, packages);
     92         assertArrayEquals(packages, mDevicePolicyManager.getLockTaskPackages(mAdminComponent));
     93         assertTrue(mDevicePolicyManager.isLockTaskPermitted("package1"));
     94         assertFalse(mDevicePolicyManager.isLockTaskPermitted("package3"));
     95 
     96         final Set<String> previousAffiliationIds =
     97                 mDevicePolicyManager.getAffiliationIds(mAdminComponent);
     98         try {
     99             // Clearing affiliation ids for this user. Lock task methods unavailable.
    100             setAffiliationIds(Collections.emptySet());
    101             checkLockTaskMethodsThrow();
    102             assertFalse(mDevicePolicyManager.isLockTaskPermitted("package1"));
    103 
    104             // Affiliating the user again. Previously set packages have been cleared.
    105             setAffiliationIds(previousAffiliationIds);
    106             assertEquals(0, mDevicePolicyManager.getLockTaskPackages(mAdminComponent).length);
    107             assertFalse(mDevicePolicyManager.isLockTaskPermitted("package1"));
    108         } finally {
    109             mDevicePolicyManager.setAffiliationIds(mAdminComponent, previousAffiliationIds);
    110         }
    111     }
    112 
    113     private void setAffiliationIds(Set<String> ids) {
    114         mDevicePolicyManager.setAffiliationIds(mAdminComponent, ids);
    115         assertEquals(ids, mDevicePolicyManager.getAffiliationIds(mAdminComponent));
    116     }
    117 
    118     private void checkLockTaskMethodsThrow() {
    119         try {
    120             mDevicePolicyManager.setLockTaskPackages(mAdminComponent, new String[0]);
    121             fail("setLockTaskPackages did not throw expected SecurityException");
    122         } catch (SecurityException expected) {
    123         }
    124         try {
    125             mDevicePolicyManager.getLockTaskPackages(mAdminComponent);
    126             fail("getLockTaskPackages did not throw expected SecurityException");
    127         } catch (SecurityException expected) {
    128         }
    129     }
    130 }
    131