Home | History | Annotate | Download | only in managedprofile
      1 /*
      2  * Copyright (C) 2016 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.managedprofile;
     18 
     19 import android.app.admin.DevicePolicyManager;
     20 import android.content.ComponentName;
     21 import android.os.PersistableBundle;
     22 import android.test.MoreAsserts;
     23 
     24 import java.util.HashSet;
     25 import java.util.List;
     26 import java.util.Set;
     27 
     28 import static com.android.cts.managedprofile.TrustAgentInfoTest.AssertConfigMode.*;
     29 
     30 public class TrustAgentInfoTest extends BaseManagedProfileTest {
     31     private static final String BUNDLE_KEY = "testing";
     32     private static final String BUNDLE_PARENT_VALUE = "parent";
     33     private static final String BUNDLE_CHILD_VALUE = "child";
     34     private static final PersistableBundle CHILD_CONFIG = new PersistableBundle();
     35     private static final PersistableBundle PARENT_CONFIG = new PersistableBundle();
     36     private static final ComponentName TRUST_AGENT_COMPONENT =
     37             new ComponentName("com.trustagent", "com.trustagent.xxx");
     38 
     39     static {
     40         CHILD_CONFIG.putString(BUNDLE_KEY, BUNDLE_CHILD_VALUE);
     41         PARENT_CONFIG.putString(BUNDLE_KEY, BUNDLE_PARENT_VALUE);
     42     }
     43 
     44     enum AssertConfigMode {
     45         ASSERT_PARENT_CONFIG, ASSERT_CHILD_CONFIG, ASSERT_BOTH
     46     }
     47 
     48     @Override
     49     protected void tearDown() throws Exception {
     50         clearTrustAgentConfiguration(true /* isParent */);
     51         clearTrustAgentConfiguration(false /* isParent */);
     52         enableTrustAgents(true /* isParent */);
     53         enableTrustAgents(false /* isParent */);
     54         super.tearDown();
     55     }
     56 
     57     public void testSetAndGetTrustAgentConfiguration_child() {
     58         setAndGetTrustAgentConfigurationInternal(false /* isParent */);
     59     }
     60 
     61     public void testSetAndGetTrustAgentConfiguration_parent() {
     62         setAndGetTrustAgentConfigurationInternal(true /* isParent */);
     63     }
     64 
     65     public void testSetTrustAgentConfiguration_bothHaveTrustAgentConfigAndUnified() {
     66         // Set both trust agents.
     67         setTrustAgentConfiguration(false /* isParent */);
     68         setTrustAgentConfiguration(true /* isParent */);
     69 
     70         // Case when trust agents is not disabled
     71         List<PersistableBundle> parentConfig =
     72                 mParentDevicePolicyManager.getTrustAgentConfiguration(
     73                         null, TRUST_AGENT_COMPONENT);
     74         List<PersistableBundle> childConfig = mDevicePolicyManager.getTrustAgentConfiguration(
     75                 null, TRUST_AGENT_COMPONENT);
     76         assertNull(parentConfig);
     77         assertNull(childConfig);
     78 
     79         // Case when trust agents is disabled
     80         disableTrustAgents(false /* isParent */);
     81         disableTrustAgents(true /* isParent */);
     82 
     83         parentConfig = mParentDevicePolicyManager.getTrustAgentConfiguration(
     84                 null, TRUST_AGENT_COMPONENT);
     85         childConfig = mDevicePolicyManager.getTrustAgentConfiguration(
     86                 null, TRUST_AGENT_COMPONENT);
     87         assertPersistableBundleListEquals(ASSERT_BOTH, parentConfig);
     88         assertPersistableBundleListEquals(ASSERT_BOTH, childConfig);
     89     }
     90 
     91     public void testSetTrustAgentConfiguration_bothHaveTrustAgentConfigAndNonUnified() {
     92         // Precondition: separate challenge for the managed profile should have been enabled.
     93 
     94         // Set both trust agents.
     95         setTrustAgentConfiguration(false /* isParent */);
     96         setTrustAgentConfiguration(true /* isParent */);
     97         disableTrustAgents(false /* isParent */);
     98         disableTrustAgents(true /* isParent */);
     99 
    100         List<PersistableBundle> parentConfig =
    101                 mParentDevicePolicyManager.getTrustAgentConfiguration(
    102                         null, TRUST_AGENT_COMPONENT);
    103         List<PersistableBundle> childConfig =
    104                 mDevicePolicyManager.getTrustAgentConfiguration(
    105                         null, TRUST_AGENT_COMPONENT);
    106         // Separate credential in managed profile, should only get its own config.
    107         assertPersistableBundleListEquals(ASSERT_PARENT_CONFIG, parentConfig);
    108         assertPersistableBundleListEquals(ASSERT_CHILD_CONFIG, childConfig);
    109     }
    110 
    111     private void setAndGetTrustAgentConfigurationInternal(boolean isParent) {
    112         // Set the config
    113         setTrustAgentConfiguration(isParent);
    114         // Case when trust agents is not disabled
    115         List<PersistableBundle> configs = getDevicePolicyManager(isParent)
    116                 .getTrustAgentConfiguration(null, TRUST_AGENT_COMPONENT);
    117         assertNull(configs);
    118         // Case when trust agents is disabled
    119         disableTrustAgents(isParent);
    120         configs = getDevicePolicyManager(isParent)
    121                 .getTrustAgentConfiguration(null, TRUST_AGENT_COMPONENT);
    122         assertPersistableBundleListEquals(isParent ?
    123                 ASSERT_PARENT_CONFIG : ASSERT_CHILD_CONFIG, configs);
    124     }
    125 
    126     private void disableTrustAgents(boolean isParent) {
    127         getDevicePolicyManager(isParent).setKeyguardDisabledFeatures(ADMIN_RECEIVER_COMPONENT,
    128                 DevicePolicyManager.KEYGUARD_DISABLE_TRUST_AGENTS);
    129     }
    130 
    131     private void enableTrustAgents(boolean isParent) {
    132         getDevicePolicyManager(isParent).setKeyguardDisabledFeatures(ADMIN_RECEIVER_COMPONENT,
    133                 DevicePolicyManager.KEYGUARD_DISABLE_FEATURES_NONE);
    134     }
    135 
    136     private void clearTrustAgentConfiguration(boolean isParent) {
    137         getDevicePolicyManager(isParent).setTrustAgentConfiguration(ADMIN_RECEIVER_COMPONENT,
    138                 TRUST_AGENT_COMPONENT, null);
    139         assertNull(getDevicePolicyManager(isParent).getTrustAgentConfiguration(
    140                 ADMIN_RECEIVER_COMPONENT, TRUST_AGENT_COMPONENT));
    141     }
    142 
    143     private PersistableBundle setTrustAgentConfiguration(boolean isParent) {
    144         PersistableBundle expected = isParent ? PARENT_CONFIG : CHILD_CONFIG;
    145         getDevicePolicyManager(isParent).setTrustAgentConfiguration(ADMIN_RECEIVER_COMPONENT,
    146                 TRUST_AGENT_COMPONENT, expected);
    147         List<PersistableBundle> configs =
    148                 getDevicePolicyManager(isParent).getTrustAgentConfiguration(
    149                         ADMIN_RECEIVER_COMPONENT, TRUST_AGENT_COMPONENT);
    150         assertPersistableBundleListEquals(isParent ?
    151                 ASSERT_PARENT_CONFIG :
    152                 ASSERT_CHILD_CONFIG, configs);
    153         return expected;
    154     }
    155 
    156     private static void assertPersistableBundleListEquals(
    157             AssertConfigMode mode, List<PersistableBundle> actual) {
    158         Set<String> expectedValues = new HashSet<>();
    159         switch (mode) {
    160             case ASSERT_CHILD_CONFIG:
    161                 expectedValues.add(BUNDLE_CHILD_VALUE);
    162                 break;
    163             case ASSERT_PARENT_CONFIG:
    164                 expectedValues.add(BUNDLE_PARENT_VALUE);
    165                 break;
    166             case ASSERT_BOTH:
    167                 expectedValues.add(BUNDLE_PARENT_VALUE);
    168                 expectedValues.add(BUNDLE_CHILD_VALUE);
    169                 break;
    170         }
    171         assertNotNull(actual);
    172         Set<String> actualValues = new HashSet<>();
    173         for (PersistableBundle bundle : actual) {
    174             actualValues.add(bundle.getString(BUNDLE_KEY));
    175         }
    176         MoreAsserts.assertEquals(expectedValues, actualValues);
    177     }
    178 
    179 }
    180