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 17package com.android.server.locksettings; 18 19import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; 20import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; 21 22import static com.android.internal.widget.LockPatternUtils.stringToPattern; 23 24import static junit.framework.Assert.*; 25 26import static org.mockito.Matchers.anyInt; 27import static org.mockito.Mockito.any; 28import static org.mockito.Mockito.never; 29import static org.mockito.Mockito.verify; 30import static org.mockito.Mockito.when; 31 32import static java.io.FileDescriptor.*; 33 34import android.app.ActivityManager; 35import android.content.Context; 36import android.os.Binder; 37import android.os.Debug; 38import android.os.Handler; 39import android.os.Looper; 40import android.os.ResultReceiver; 41import android.os.ShellCallback; 42import android.platform.test.annotations.Presubmit; 43import android.support.test.InstrumentationRegistry; 44import android.support.test.filters.SmallTest; 45import android.support.test.runner.AndroidJUnit4; 46 47import com.android.internal.widget.LockPatternUtils; 48 49import junit.framework.Assert; 50 51import org.junit.Before; 52import org.junit.Test; 53import org.junit.runner.RunWith; 54import org.mockito.Mock; 55import org.mockito.MockitoAnnotations; 56 57import java.io.FileDescriptor; 58 59/** 60 * Test class for {@link LockSettingsShellCommand}. 61 * 62 * runtest frameworks-services -c com.android.server.locksettings.LockSettingsShellCommandTest 63 */ 64@SmallTest 65@Presubmit 66@RunWith(AndroidJUnit4.class) 67public class LockSettingsShellCommandTest { 68 69 private LockSettingsShellCommand mCommand; 70 71 private @Mock LockPatternUtils mLockPatternUtils; 72 private int mUserId; 73 private final Binder mBinder = new Binder(); 74 private final ShellCallback mShellCallback = new ShellCallback(); 75 private final ResultReceiver mResultReceiver = new ResultReceiver( 76 new Handler(Looper.getMainLooper())); 77 78 @Before 79 public void setUp() throws Exception { 80 MockitoAnnotations.initMocks(this); 81 final Context context = InstrumentationRegistry.getTargetContext(); 82 mUserId = ActivityManager.getCurrentUser(); 83 mCommand = new LockSettingsShellCommand(context, mLockPatternUtils); 84 } 85 86 @Test 87 public void testWrongPassword() throws Exception { 88 when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(false); 89 when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(true); 90 when(mLockPatternUtils.checkPassword("1234", mUserId)).thenReturn(false); 91 assertEquals(-1, mCommand.exec(mBinder, in, out, err, 92 new String[] { "set-pin", "--old", "1234" }, 93 mShellCallback, mResultReceiver)); 94 verify(mLockPatternUtils, never()).saveLockPassword(any(), any(), anyInt(), anyInt()); 95 } 96 97 @Test 98 public void testChangePin() throws Exception { 99 when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(false); 100 when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(true); 101 when(mLockPatternUtils.checkPassword("1234", mUserId)).thenReturn(true); 102 assertEquals(0, mCommand.exec(new Binder(), in, out, err, 103 new String[] { "set-pin", "--old", "1234", "4321" }, 104 mShellCallback, mResultReceiver)); 105 verify(mLockPatternUtils).saveLockPassword("4321", "1234", PASSWORD_QUALITY_NUMERIC, 106 mUserId); 107 } 108 109 @Test 110 public void testChangePassword() throws Exception { 111 when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(false); 112 when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(true); 113 when(mLockPatternUtils.checkPassword("1234", mUserId)).thenReturn(true); 114 assertEquals(0, mCommand.exec(new Binder(), in, out, err, 115 new String[] { "set-password", "--old", "1234", "4321" }, 116 mShellCallback, mResultReceiver)); 117 verify(mLockPatternUtils).saveLockPassword("4321", "1234", PASSWORD_QUALITY_ALPHABETIC, 118 mUserId); 119 } 120 121 @Test 122 public void testChangePattern() throws Exception { 123 when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(true); 124 when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(false); 125 when(mLockPatternUtils.checkPattern(stringToPattern("1234"), mUserId)).thenReturn(true); 126 assertEquals(0, mCommand.exec(new Binder(), in, out, err, 127 new String[] { "set-pattern", "--old", "1234", "4321" }, 128 mShellCallback, mResultReceiver)); 129 verify(mLockPatternUtils).saveLockPattern(stringToPattern("4321"), "1234", mUserId); 130 } 131 132 @Test 133 public void testClear() throws Exception { 134 when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(true); 135 when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(false); 136 when(mLockPatternUtils.checkPattern(stringToPattern("1234"), mUserId)).thenReturn(true); 137 assertEquals(0, mCommand.exec(new Binder(), in, out, err, 138 new String[] { "clear", "--old", "1234" }, 139 mShellCallback, mResultReceiver)); 140 verify(mLockPatternUtils).clearLock("1234", mUserId); 141 } 142} 143