1 /* 2 * Copyright (C) 2017 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.managedprofile; 17 18 import android.app.admin.DevicePolicyManager; 19 import android.content.BroadcastReceiver; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.os.UserManager; 23 24 public class ResetPasswordWithTokenTest extends BaseManagedProfileTest { 25 26 private static final String PASSWORD0 = "1234"; 27 // This needs to be in sync with ManagedProfileTest.RESET_PASSWORD_TEST_DEFAULT_PASSWORD 28 private static final String PASSWORD1 = "123456"; 29 30 private static final byte[] token = "abcdefghijklmnopqrstuvwxyz0123456789".getBytes(); 31 32 /** 33 * A dummy receiver marked as direct boot aware in manifest to make this test app 34 * runnable by instrumentation before FBE unlock. 35 */ 36 public static class DummyReceiver extends BroadcastReceiver { 37 @Override 38 public void onReceive(Context context, Intent intent) { 39 } 40 } 41 42 /** 43 * Set a reset password token and work challenge on the work profile. This is the preparation 44 * step for {@link #testResetPasswordBeforeUnlock} and will be called by the hostside logic 45 * before it is exercised. 46 */ 47 public void testSetupWorkProfile() { 48 testSetResetPasswordToken(); 49 // Reset password on the work profile will enable separate work challenge for it. 50 assertTrue(mDevicePolicyManager.resetPasswordWithToken(ADMIN_RECEIVER_COMPONENT, PASSWORD0, 51 token, 0)); 52 53 mDevicePolicyManager.setPasswordQuality(ADMIN_RECEIVER_COMPONENT, 54 DevicePolicyManager.PASSWORD_QUALITY_NUMERIC); 55 mDevicePolicyManager.setPasswordMinimumLength(ADMIN_RECEIVER_COMPONENT, 6); 56 } 57 58 public void testResetPasswordBeforeUnlock() { 59 UserManager um = mContext.getSystemService(UserManager.class); 60 assertFalse(um.isUserUnlocked()); 61 assertTrue(mDevicePolicyManager.isResetPasswordTokenActive(ADMIN_RECEIVER_COMPONENT)); 62 assertTrue(mDevicePolicyManager.resetPasswordWithToken(ADMIN_RECEIVER_COMPONENT, PASSWORD1, 63 token, 0)); 64 try { 65 mDevicePolicyManager.isActivePasswordSufficient(); 66 fail("Did not throw expected exception."); 67 } catch (IllegalStateException expected) {} 68 } 69 70 public void testSetResetPasswordToken() { 71 assertTrue(mDevicePolicyManager.setResetPasswordToken(ADMIN_RECEIVER_COMPONENT, token)); 72 assertTrue(mDevicePolicyManager.isResetPasswordTokenActive(ADMIN_RECEIVER_COMPONENT)); 73 } 74 }