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 static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; 20 21 import android.app.admin.DevicePolicyManager; 22 import android.content.ComponentName; 23 24 import java.lang.reflect.Method; 25 import java.util.ArrayList; 26 import java.util.List; 27 28 /** Tests minimum password restriction APIs, including on parent profile instances. */ 29 public class PasswordMinimumRestrictionsTest extends BaseManagedProfileTest { 30 31 private static final int TEST_PASSWORD_LENGTH = 5; 32 private static final int TEST_PASSWORD_LENGTH_LOW = 2; 33 private static final String[] METHOD_LIST = { 34 "PasswordMinimumLength", 35 "PasswordMinimumUpperCase", 36 "PasswordMinimumLowerCase", 37 "PasswordMinimumLetters", 38 "PasswordMinimumNumeric", 39 "PasswordMinimumSymbols", 40 "PasswordMinimumNonLetter", 41 "PasswordHistoryLength"}; 42 43 private DevicePolicyManager mParentDpm; 44 private int mCurrentAdminPreviousPasswordQuality; 45 private int mParentPreviousPasswordQuality; 46 private List<Integer> mCurrentAdminPreviousPasswordRestriction = new ArrayList<Integer>(); 47 private List<Integer> mParentPreviousPasswordRestriction = new ArrayList<Integer>(); 48 49 @Override 50 protected void setUp() throws Exception { 51 super.setUp(); 52 53 mParentDpm = mDevicePolicyManager.getParentProfileInstance(ADMIN_RECEIVER_COMPONENT); 54 mCurrentAdminPreviousPasswordQuality = 55 mDevicePolicyManager.getPasswordQuality(ADMIN_RECEIVER_COMPONENT); 56 mParentPreviousPasswordQuality = mParentDpm.getPasswordQuality(ADMIN_RECEIVER_COMPONENT); 57 mDevicePolicyManager.setPasswordQuality(ADMIN_RECEIVER_COMPONENT, PASSWORD_QUALITY_COMPLEX); 58 mParentDpm.setPasswordQuality(ADMIN_RECEIVER_COMPONENT, PASSWORD_QUALITY_COMPLEX); 59 for (String method : METHOD_LIST) { 60 mCurrentAdminPreviousPasswordRestriction 61 .add(invokeGetMethod(method, mDevicePolicyManager, ADMIN_RECEIVER_COMPONENT)); 62 mParentPreviousPasswordRestriction 63 .add(invokeGetMethod(method, mParentDpm, ADMIN_RECEIVER_COMPONENT)); 64 } 65 } 66 67 @Override 68 protected void tearDown() throws Exception { 69 for (int i = 0; i < METHOD_LIST.length; i++) { 70 invokeSetMethod(METHOD_LIST[i], mDevicePolicyManager, ADMIN_RECEIVER_COMPONENT, 71 mCurrentAdminPreviousPasswordRestriction.get(i)); 72 invokeSetMethod(METHOD_LIST[i], mParentDpm, ADMIN_RECEIVER_COMPONENT, 73 mCurrentAdminPreviousPasswordRestriction.get(i)); 74 } 75 mDevicePolicyManager.setPasswordQuality(ADMIN_RECEIVER_COMPONENT, 76 mCurrentAdminPreviousPasswordQuality); 77 mParentDpm.setPasswordQuality(ADMIN_RECEIVER_COMPONENT, mParentPreviousPasswordQuality); 78 super.tearDown(); 79 } 80 81 public void testPasswordMinimumRestriction() throws Exception { 82 for (int i = 0; i < METHOD_LIST.length; i++) { 83 invokeSetMethod(METHOD_LIST[i], mDevicePolicyManager, ADMIN_RECEIVER_COMPONENT, 84 TEST_PASSWORD_LENGTH + i); 85 invokeSetMethod(METHOD_LIST[i], mParentDpm, ADMIN_RECEIVER_COMPONENT, 86 TEST_PASSWORD_LENGTH + 2 * i); 87 88 // Passing the admin component returns the value set for that admin, rather than 89 // aggregated values. 90 assertEquals( 91 getMethodName(METHOD_LIST[i]) 92 + " failed to get expected value on mDevicePolicyManager.", 93 TEST_PASSWORD_LENGTH + i, invokeGetMethod(METHOD_LIST[i], mDevicePolicyManager, 94 ADMIN_RECEIVER_COMPONENT)); 95 96 // Passing the admin component returns the value set for that admin, rather than 97 // aggregated values. 98 assertEquals( 99 getMethodName(METHOD_LIST[i]) + " failed to get expected value on mParentDpm.", 100 TEST_PASSWORD_LENGTH + 2 * i, 101 invokeGetMethod(METHOD_LIST[i], mParentDpm, ADMIN_RECEIVER_COMPONENT)); 102 } 103 } 104 105 public void testSetPasswordMinimumRestrictionWithNull() { 106 // Test with mDevicePolicyManager. 107 for (String method : METHOD_LIST) { 108 try { 109 invokeSetMethod(method, mDevicePolicyManager, null, TEST_PASSWORD_LENGTH); 110 fail("Exception should have been thrown for null admin ComponentName"); 111 } catch (Exception e) { 112 if (!(e.getCause() instanceof NullPointerException)) { 113 fail("Failed to execute set method: " + setMethodName(method)); 114 } 115 // Expected to throw NullPointerException. 116 } 117 } 118 119 // Test with mParentDpm. 120 for (String method : METHOD_LIST) { 121 try { 122 invokeSetMethod(method, mParentDpm, null, TEST_PASSWORD_LENGTH); 123 fail("Exception should have been thrown for null admin ComponentName"); 124 } catch (Exception e) { 125 if (!(e.getCause() instanceof NullPointerException)) { 126 fail("Failed to execute set method: " + setMethodName(method)); 127 } 128 // Expected to throw NullPointerException. 129 } 130 } 131 } 132 133 public void testGetPasswordMinimumRestrictionWithNullAdmin() throws Exception { 134 for (int i = 0; i < METHOD_LIST.length; i++) { 135 // Check getMethod with null admin. It should return the aggregated value (which is the 136 // only value set so far). 137 invokeSetMethod(METHOD_LIST[i], mDevicePolicyManager, ADMIN_RECEIVER_COMPONENT, 138 TEST_PASSWORD_LENGTH_LOW + i); 139 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH_LOW + i, 140 invokeGetMethod(METHOD_LIST[i], mDevicePolicyManager, null)); 141 142 // Set strict password minimum restriction using parent instance. 143 invokeSetMethod(METHOD_LIST[i], mParentDpm, ADMIN_RECEIVER_COMPONENT, 144 TEST_PASSWORD_LENGTH + i); 145 // With null admin, the restriction should be the aggregate of all admins. 146 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH + i, 147 invokeGetMethod(METHOD_LIST[i], mDevicePolicyManager, null)); 148 // With null admin, the restriction should be the aggregate of all admins. 149 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH + i, 150 invokeGetMethod(METHOD_LIST[i], mParentDpm, null)); 151 152 // Passing the admin component returns the value set for that admin, rather than 153 // aggregated values. 154 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH_LOW + i, 155 invokeGetMethod(METHOD_LIST[i], mDevicePolicyManager, 156 ADMIN_RECEIVER_COMPONENT)); 157 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH + i, 158 invokeGetMethod(METHOD_LIST[i], mParentDpm, ADMIN_RECEIVER_COMPONENT)); 159 160 // Set strict password minimum restriction on current admin. 161 invokeSetMethod(METHOD_LIST[i], mDevicePolicyManager, ADMIN_RECEIVER_COMPONENT, 162 TEST_PASSWORD_LENGTH + i); 163 // Set password minimum restriction using parent instance. 164 invokeSetMethod(METHOD_LIST[i], mParentDpm, ADMIN_RECEIVER_COMPONENT, 165 TEST_PASSWORD_LENGTH_LOW + i); 166 // With null admin, the restriction should be the aggregate of all admins. 167 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH + i, 168 invokeGetMethod(METHOD_LIST[i], mDevicePolicyManager, null)); 169 // With null admin, the restriction should be the aggregate of all admins. 170 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH + i, 171 invokeGetMethod(METHOD_LIST[i], mParentDpm, null)); 172 173 // Passing the admin component returns the value set for that admin, rather than 174 // aggregated values. 175 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH + i, 176 invokeGetMethod(METHOD_LIST[i], mDevicePolicyManager, 177 ADMIN_RECEIVER_COMPONENT)); 178 assertEquals(getMethodName(METHOD_LIST[i]) + " failed.", TEST_PASSWORD_LENGTH_LOW + i, 179 invokeGetMethod(METHOD_LIST[i], mParentDpm, ADMIN_RECEIVER_COMPONENT)); 180 } 181 } 182 183 /** 184 * Calls dpm.set{methodName} with given component name and length arguments using reflection. 185 */ 186 private void invokeSetMethod(String methodName, DevicePolicyManager dpm, 187 ComponentName componentName, int length) throws Exception { 188 final Method setMethod = DevicePolicyManager.class.getMethod(setMethodName(methodName), 189 ComponentName.class, int.class); 190 setMethod.invoke(dpm, componentName, length); 191 } 192 193 /** 194 * Calls dpm.get{methodName} with given component name using reflection. 195 */ 196 private int invokeGetMethod(String methodName, DevicePolicyManager dpm, 197 ComponentName componentName) throws Exception { 198 final Method getMethod = 199 DevicePolicyManager.class.getMethod(getMethodName(methodName), ComponentName.class); 200 return (int) getMethod.invoke(dpm, componentName); 201 } 202 203 private String setMethodName(String methodName) { 204 return "set" + methodName; 205 } 206 207 private String getMethodName(String methodName) { 208 return "get" + methodName; 209 } 210 } 211