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