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 package com.android.cts.deviceadmin; 17 18 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_COMPLEX; 19 20 import android.app.admin.DevicePolicyManager; 21 22 /** 23 * Tests for {@link DevicePolicyManager#resetPassword} for complex cases. 24 * 25 * This needs to be run as device owner, because in NYC DA can't clear or change the password. 26 * @deprecated New tests related to password quality and reset password API should be added to 27 * {@code com.android.cts.deviceandprofileowner.ResetPasswordWithTokenTest} 28 */ 29 public class DeviceOwnerPasswordTest extends BaseDeviceAdminTest { 30 31 @Override setUp()32 protected void setUp() throws Exception { 33 super.setUp(); 34 35 assertDeviceOwner(); 36 clearPassword(); 37 } 38 39 @Override tearDown()40 protected void tearDown() throws Exception { 41 clearPassword(); 42 43 super.tearDown(); 44 } 45 testPasswordQuality_something()46 public void testPasswordQuality_something() { 47 dpm.setPasswordQuality(mAdminComponent, 48 DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); 49 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING, 50 dpm.getPasswordQuality(mAdminComponent)); 51 assertPasswordSufficiency(false); 52 53 String caseDescription = "initial"; 54 assertPasswordSucceeds("1234", caseDescription); 55 assertPasswordSucceeds("abcd", caseDescription); // can't change. 56 assertPasswordSucceeds("abcd1234", caseDescription); 57 58 dpm.setPasswordMinimumLength(mAdminComponent, 10); 59 caseDescription = "minimum password length = 10"; 60 assertEquals(10, dpm.getPasswordMinimumLength(mAdminComponent)); 61 assertPasswordSufficiency(true); // length not checked for this quality 62 63 // TODO(ascull): fix resetPassword() logic so these succeed 64 assertPasswordFails("1234", caseDescription); 65 assertPasswordFails("abcd", caseDescription); 66 assertPasswordFails("abcd1234", caseDescription); 67 68 dpm.setPasswordMinimumLength(mAdminComponent, 4); 69 caseDescription = "minimum password length = 4"; 70 assertEquals(4, dpm.getPasswordMinimumLength( 71 mAdminComponent)); 72 assertPasswordSufficiency(true); 73 74 assertPasswordSucceeds("1234", caseDescription); 75 assertPasswordSucceeds("abcd", caseDescription); 76 assertPasswordSucceeds("abcd1234", caseDescription); 77 } 78 testPasswordQuality_numeric()79 public void testPasswordQuality_numeric() { 80 dpm.setPasswordQuality(mAdminComponent, 81 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC); 82 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC, 83 dpm.getPasswordQuality(mAdminComponent)); 84 assertPasswordSufficiency(false); // failure 85 86 String caseDescription = "initial"; 87 assertPasswordSucceeds("1234", caseDescription); 88 assertPasswordSucceeds("abcd", caseDescription); 89 assertPasswordSucceeds("abcd1234", caseDescription); 90 91 dpm.setPasswordMinimumLength(mAdminComponent, 10); 92 caseDescription = "minimum password length = 10"; 93 assertEquals(10, dpm.getPasswordMinimumLength(mAdminComponent)); 94 assertPasswordSufficiency(false); 95 96 assertPasswordFails("1234", caseDescription); 97 assertPasswordFails("abcd", caseDescription); 98 assertPasswordFails("abcd1234", caseDescription); 99 100 dpm.setPasswordMinimumLength(mAdminComponent, 4); 101 caseDescription = "minimum password length = 4"; 102 assertEquals(4, dpm.getPasswordMinimumLength( 103 mAdminComponent)); 104 assertPasswordSufficiency(true); 105 106 assertPasswordSucceeds("1234", caseDescription); 107 assertPasswordSucceeds("abcd", caseDescription); 108 assertPasswordSucceeds("abcd1234", caseDescription); 109 } 110 testPasswordQuality_alphabetic()111 public void testPasswordQuality_alphabetic() { 112 dpm.setPasswordQuality(mAdminComponent, 113 DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC); 114 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC, 115 dpm.getPasswordQuality(mAdminComponent)); 116 assertPasswordSufficiency(false); 117 118 String caseDescription = "initial"; 119 assertPasswordFails("1234", caseDescription); // can't change 120 assertPasswordSucceeds("abcd", caseDescription); 121 assertPasswordSucceeds("abcd1234", caseDescription); 122 123 dpm.setPasswordMinimumLength(mAdminComponent, 10); 124 caseDescription = "minimum password length = 10"; 125 assertEquals(10, dpm.getPasswordMinimumLength(mAdminComponent)); 126 assertPasswordSufficiency(false); 127 128 assertPasswordFails("1234", caseDescription); 129 assertPasswordFails("abcd", caseDescription); 130 assertPasswordFails("abcd1234", caseDescription); 131 132 dpm.setPasswordMinimumLength(mAdminComponent, 4); 133 caseDescription = "minimum password length = 4"; 134 assertEquals(4, dpm.getPasswordMinimumLength( 135 mAdminComponent)); 136 assertPasswordSufficiency(true); 137 138 assertPasswordFails("1234", caseDescription); 139 assertPasswordSucceeds("abcd", caseDescription); 140 assertPasswordSucceeds("abcd1234", caseDescription); 141 } 142 testPasswordQuality_alphanumeric()143 public void testPasswordQuality_alphanumeric() { 144 dpm.setPasswordQuality(mAdminComponent, 145 DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC); 146 assertEquals(DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC, 147 dpm.getPasswordQuality(mAdminComponent)); 148 assertPasswordSufficiency(false); 149 150 String caseDescription = "initial"; 151 assertPasswordFails("1234", caseDescription); 152 assertPasswordFails("abcd", caseDescription); 153 assertPasswordSucceeds("abcd1234", caseDescription); 154 155 dpm.setPasswordMinimumLength(mAdminComponent, 10); 156 caseDescription = "minimum password length = 10"; 157 assertEquals(10, dpm.getPasswordMinimumLength(mAdminComponent)); 158 assertPasswordSufficiency(false); 159 160 assertPasswordFails("1234", caseDescription); 161 assertPasswordFails("abcd", caseDescription); 162 assertPasswordFails("abcd1234", caseDescription); 163 164 dpm.setPasswordMinimumLength(mAdminComponent, 4); 165 caseDescription = "minimum password length = 4"; 166 assertEquals(4, dpm.getPasswordMinimumLength( 167 mAdminComponent)); 168 assertPasswordSufficiency(true); 169 170 assertPasswordFails("1234", caseDescription); 171 assertPasswordFails("abcd", caseDescription); 172 assertPasswordSucceeds("abcd1234", caseDescription); 173 } 174 testPasswordQuality_complexUpperCase()175 public void testPasswordQuality_complexUpperCase() { 176 dpm.setPasswordQuality(mAdminComponent, PASSWORD_QUALITY_COMPLEX); 177 assertEquals(PASSWORD_QUALITY_COMPLEX, dpm.getPasswordQuality(mAdminComponent)); 178 resetComplexPasswordRestrictions(); 179 180 String caseDescription = "minimum UpperCase=0"; 181 assertPasswordSucceeds("abc1", caseDescription); 182 assertPasswordSucceeds("aBc1", caseDescription); 183 assertPasswordSucceeds("ABC1", caseDescription); 184 assertPasswordSucceeds("ABCD", caseDescription); 185 assertPasswordFails("123", caseDescription); // too short 186 187 dpm.setPasswordMinimumUpperCase(mAdminComponent, 1); 188 assertEquals(1, dpm.getPasswordMinimumUpperCase(mAdminComponent)); 189 caseDescription = "minimum UpperCase=1"; 190 assertPasswordFails("abc1", caseDescription); 191 assertPasswordSucceeds("aBc1", caseDescription); 192 assertPasswordSucceeds("ABC1", caseDescription); 193 assertPasswordSucceeds("ABCD", caseDescription); 194 assertPasswordFails("123", caseDescription); // too short 195 196 dpm.setPasswordMinimumUpperCase(mAdminComponent, 3); 197 assertEquals(3, dpm.getPasswordMinimumUpperCase(mAdminComponent)); 198 caseDescription = "minimum UpperCase=3"; 199 assertPasswordFails("abc1", caseDescription); 200 assertPasswordFails("aBC1", caseDescription); 201 assertPasswordSucceeds("ABC1", caseDescription); 202 assertPasswordSucceeds("ABCD", caseDescription); 203 assertPasswordFails("123", caseDescription); // too short 204 } 205 testPasswordQuality_complexLowerCase()206 public void testPasswordQuality_complexLowerCase() { 207 dpm.setPasswordQuality(mAdminComponent, PASSWORD_QUALITY_COMPLEX); 208 assertEquals(PASSWORD_QUALITY_COMPLEX, dpm.getPasswordQuality(mAdminComponent)); 209 resetComplexPasswordRestrictions(); 210 211 String caseDescription = "minimum LowerCase=0"; 212 assertPasswordSucceeds("ABCD", caseDescription); 213 assertPasswordSucceeds("aBC1", caseDescription); 214 assertPasswordSucceeds("abc1", caseDescription); 215 assertPasswordSucceeds("abcd", caseDescription); 216 assertPasswordFails("123", caseDescription); // too short 217 218 dpm.setPasswordMinimumLowerCase(mAdminComponent, 1); 219 assertEquals(1, dpm.getPasswordMinimumLowerCase(mAdminComponent)); 220 caseDescription = "minimum LowerCase=1"; 221 assertPasswordFails("ABCD", caseDescription); 222 assertPasswordSucceeds("aBC1", caseDescription); 223 assertPasswordSucceeds("abc1", caseDescription); 224 assertPasswordSucceeds("abcd", caseDescription); 225 assertPasswordFails("123", caseDescription); // too short 226 227 dpm.setPasswordMinimumLowerCase(mAdminComponent, 3); 228 assertEquals(3, dpm.getPasswordMinimumLowerCase(mAdminComponent)); 229 caseDescription = "minimum LowerCase=3"; 230 assertPasswordFails("ABCD", caseDescription); 231 assertPasswordFails("aBC1", caseDescription); 232 assertPasswordSucceeds("abc1", caseDescription); 233 assertPasswordSucceeds("abcd", caseDescription); 234 assertPasswordFails("123", caseDescription); // too short 235 } 236 testPasswordQuality_complexLetters()237 public void testPasswordQuality_complexLetters() { 238 dpm.setPasswordQuality(mAdminComponent, PASSWORD_QUALITY_COMPLEX); 239 assertEquals(PASSWORD_QUALITY_COMPLEX, dpm.getPasswordQuality(mAdminComponent)); 240 resetComplexPasswordRestrictions(); 241 242 String caseDescription = "minimum Letters=0"; 243 assertPasswordSucceeds("1234", caseDescription); 244 assertPasswordSucceeds("a123", caseDescription); 245 assertPasswordSucceeds("abc1", caseDescription); 246 assertPasswordSucceeds("abcd", caseDescription); 247 assertPasswordFails("123", caseDescription); // too short 248 249 dpm.setPasswordMinimumLetters(mAdminComponent, 1); 250 assertEquals(1, dpm.getPasswordMinimumLetters(mAdminComponent)); 251 caseDescription = "minimum Letters=1"; 252 assertPasswordFails("1234", caseDescription); 253 assertPasswordSucceeds("a123", caseDescription); 254 assertPasswordSucceeds("abc1", caseDescription); 255 assertPasswordSucceeds("abcd", caseDescription); 256 assertPasswordFails("123", caseDescription); // too short 257 258 dpm.setPasswordMinimumLetters(mAdminComponent, 3); 259 assertEquals(3, dpm.getPasswordMinimumLetters(mAdminComponent)); 260 caseDescription = "minimum Letters=3"; 261 assertPasswordFails("1234", caseDescription); 262 assertPasswordFails("a123", caseDescription); 263 assertPasswordSucceeds("abc1", caseDescription); 264 assertPasswordSucceeds("abcd", caseDescription); 265 assertPasswordFails("123", caseDescription); // too short 266 } 267 testPasswordQuality_complexNumeric()268 public void testPasswordQuality_complexNumeric() { 269 dpm.setPasswordQuality(mAdminComponent, PASSWORD_QUALITY_COMPLEX); 270 assertEquals(PASSWORD_QUALITY_COMPLEX, dpm.getPasswordQuality(mAdminComponent)); 271 resetComplexPasswordRestrictions(); 272 273 String caseDescription = "minimum Numeric=0"; 274 assertPasswordSucceeds("abcd", caseDescription); 275 assertPasswordSucceeds("1abc", caseDescription); 276 assertPasswordSucceeds("123a", caseDescription); 277 assertPasswordSucceeds("1234", caseDescription); 278 assertPasswordFails("123", caseDescription); // too short 279 280 dpm.setPasswordMinimumNumeric(mAdminComponent, 1); 281 assertEquals(1, dpm.getPasswordMinimumNumeric(mAdminComponent)); 282 caseDescription = "minimum Numeric=1"; 283 assertPasswordFails("abcd", caseDescription); 284 assertPasswordSucceeds("1abc", caseDescription); 285 assertPasswordSucceeds("123a", caseDescription); 286 assertPasswordSucceeds("1234", caseDescription); 287 assertPasswordFails("123", caseDescription); // too short 288 289 dpm.setPasswordMinimumNumeric(mAdminComponent, 3); 290 assertEquals(3, dpm.getPasswordMinimumNumeric(mAdminComponent)); 291 caseDescription = "minimum Numeric=3"; 292 assertPasswordFails("abcd", caseDescription); 293 assertPasswordFails("1abc", caseDescription); 294 assertPasswordSucceeds("123a", caseDescription); 295 assertPasswordSucceeds("1234", caseDescription); 296 assertPasswordFails("123", caseDescription); // too short 297 } 298 testPasswordQuality_complexSymbols()299 public void testPasswordQuality_complexSymbols() { 300 dpm.setPasswordQuality(mAdminComponent, PASSWORD_QUALITY_COMPLEX); 301 assertEquals(PASSWORD_QUALITY_COMPLEX, dpm.getPasswordQuality(mAdminComponent)); 302 resetComplexPasswordRestrictions(); 303 304 String caseDescription = "minimum Symbols=0"; 305 assertPasswordSucceeds("abcd", caseDescription); 306 assertPasswordSucceeds("_bc1", caseDescription); 307 assertPasswordSucceeds("@#!1", caseDescription); 308 assertPasswordSucceeds("_@#!", caseDescription); 309 assertPasswordFails("123", caseDescription); // too short 310 311 dpm.setPasswordMinimumSymbols(mAdminComponent, 1); 312 assertEquals(1, dpm.getPasswordMinimumSymbols(mAdminComponent)); 313 caseDescription = "minimum Symbols=1"; 314 assertPasswordFails("abcd", caseDescription); 315 assertPasswordSucceeds("_bc1", caseDescription); 316 assertPasswordSucceeds("@#!1", caseDescription); 317 assertPasswordSucceeds("_@#!", caseDescription); 318 assertPasswordFails("123", caseDescription); // too short 319 320 dpm.setPasswordMinimumSymbols(mAdminComponent, 3); 321 assertEquals(3, dpm.getPasswordMinimumSymbols(mAdminComponent)); 322 caseDescription = "minimum Symbols=3"; 323 assertPasswordFails("abcd", caseDescription); 324 assertPasswordFails("_bc1", caseDescription); 325 assertPasswordSucceeds("@#!1", caseDescription); 326 assertPasswordSucceeds("_@#!", caseDescription); 327 assertPasswordFails("123", caseDescription); // too short 328 } 329 testPasswordQuality_complexNonLetter()330 public void testPasswordQuality_complexNonLetter() { 331 dpm.setPasswordQuality(mAdminComponent, PASSWORD_QUALITY_COMPLEX); 332 assertEquals(PASSWORD_QUALITY_COMPLEX, dpm.getPasswordQuality(mAdminComponent)); 333 resetComplexPasswordRestrictions(); 334 335 String caseDescription = "minimum NonLetter=0"; 336 assertPasswordSucceeds("Abcd", caseDescription); 337 assertPasswordSucceeds("_bcd", caseDescription); 338 assertPasswordSucceeds("3bcd", caseDescription); 339 assertPasswordSucceeds("_@3c", caseDescription); 340 assertPasswordSucceeds("_25!", caseDescription); 341 assertPasswordFails("123", caseDescription); // too short 342 343 dpm.setPasswordMinimumNonLetter(mAdminComponent, 1); 344 assertEquals(1, dpm.getPasswordMinimumNonLetter(mAdminComponent)); 345 caseDescription = "minimum NonLetter=1"; 346 assertPasswordFails("Abcd", caseDescription); 347 assertPasswordSucceeds("_bcd", caseDescription); 348 assertPasswordSucceeds("3bcd", caseDescription); 349 assertPasswordSucceeds("_@3c", caseDescription); 350 assertPasswordSucceeds("_25!", caseDescription); 351 assertPasswordFails("123", caseDescription); // too short 352 353 dpm.setPasswordMinimumNonLetter(mAdminComponent, 3); 354 assertEquals(3, dpm.getPasswordMinimumNonLetter(mAdminComponent)); 355 caseDescription = "minimum NonLetter=3"; 356 assertPasswordFails("Abcd", caseDescription); 357 assertPasswordFails("_bcd", caseDescription); 358 assertPasswordFails("3bcd", caseDescription); 359 assertPasswordSucceeds("_@3c", caseDescription); 360 assertPasswordSucceeds("_25!", caseDescription); 361 assertPasswordFails("123", caseDescription); // too short 362 } 363 assertPasswordFails(String password, String restriction)364 private void assertPasswordFails(String password, String restriction) { 365 try { 366 boolean passwordResetResult = dpm.resetPassword(password, /* flags= */0); 367 assertFalse("Password '" + password + "' should have failed on " + restriction, 368 passwordResetResult); 369 } catch (IllegalArgumentException e) { 370 // yesss, we have failed! 371 } 372 } 373 assertPasswordSucceeds(String password, String restriction)374 private void assertPasswordSucceeds(String password, String restriction) { 375 boolean passwordResetResult = dpm.resetPassword(password, /* flags= */0); 376 assertTrue("Password '" + password + "' failed on " + restriction, passwordResetResult); 377 assertPasswordSufficiency(true); 378 } 379 assertPasswordSufficiency(boolean expectPasswordSufficient)380 private void assertPasswordSufficiency(boolean expectPasswordSufficient) { 381 int retries = 15; 382 // isActivePasswordSufficient() gets the result asynchronously so let's retry a few times 383 while (retries >= 0 && dpm.isActivePasswordSufficient() != expectPasswordSufficient) { 384 retries--; 385 try { 386 Thread.sleep(200); 387 } catch (InterruptedException e) { 388 break; 389 } 390 } 391 assertEquals(expectPasswordSufficient, dpm.isActivePasswordSufficient()); 392 393 } 394 } 395