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