Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 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 | |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 17 | package com.android.server.locksettings; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 18 | |
| 19 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC; |
| 20 | import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_NUMERIC; |
| 21 | |
| 22 | import static com.android.internal.widget.LockPatternUtils.stringToPattern; |
| 23 | |
| 24 | import static junit.framework.Assert.*; |
| 25 | |
| 26 | import static org.mockito.Matchers.anyInt; |
| 27 | import static org.mockito.Mockito.any; |
| 28 | import static org.mockito.Mockito.never; |
| 29 | import static org.mockito.Mockito.verify; |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame^] | 30 | import static org.mockito.Mockito.verifyNoMoreInteractions; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 31 | import static org.mockito.Mockito.when; |
| 32 | |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 33 | import android.app.ActivityManager; |
| 34 | import android.content.Context; |
| 35 | import android.os.Binder; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 36 | import android.os.Handler; |
| 37 | import android.os.Looper; |
| 38 | import android.os.ResultReceiver; |
| 39 | import android.os.ShellCallback; |
| 40 | import android.platform.test.annotations.Presubmit; |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 41 | |
| 42 | import androidx.test.InstrumentationRegistry; |
| 43 | import androidx.test.filters.SmallTest; |
| 44 | import androidx.test.runner.AndroidJUnit4; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 45 | |
| 46 | import com.android.internal.widget.LockPatternUtils; |
| 47 | |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 48 | import org.junit.Before; |
| 49 | import org.junit.Test; |
| 50 | import org.junit.runner.RunWith; |
| 51 | import org.mockito.Mock; |
| 52 | import org.mockito.MockitoAnnotations; |
| 53 | |
Brett Chabot | a26eda9 | 2018-07-23 13:08:30 -0700 | [diff] [blame] | 54 | import static java.io.FileDescriptor.*; |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 55 | |
| 56 | /** |
| 57 | * Test class for {@link LockSettingsShellCommand}. |
| 58 | * |
Andrew Scull | 507d11c | 2017-05-03 17:19:01 +0100 | [diff] [blame] | 59 | * runtest frameworks-services -c com.android.server.locksettings.LockSettingsShellCommandTest |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 60 | */ |
| 61 | @SmallTest |
| 62 | @Presubmit |
| 63 | @RunWith(AndroidJUnit4.class) |
| 64 | public class LockSettingsShellCommandTest { |
| 65 | |
| 66 | private LockSettingsShellCommand mCommand; |
| 67 | |
| 68 | private @Mock LockPatternUtils mLockPatternUtils; |
| 69 | private int mUserId; |
| 70 | private final Binder mBinder = new Binder(); |
| 71 | private final ShellCallback mShellCallback = new ShellCallback(); |
| 72 | private final ResultReceiver mResultReceiver = new ResultReceiver( |
| 73 | new Handler(Looper.getMainLooper())); |
| 74 | |
| 75 | @Before |
| 76 | public void setUp() throws Exception { |
| 77 | MockitoAnnotations.initMocks(this); |
| 78 | final Context context = InstrumentationRegistry.getTargetContext(); |
| 79 | mUserId = ActivityManager.getCurrentUser(); |
Felipe Leme | e72ec17 | 2018-07-16 13:33:36 -0700 | [diff] [blame] | 80 | mCommand = new LockSettingsShellCommand(mLockPatternUtils); |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame^] | 81 | when(mLockPatternUtils.hasSecureLockScreen()).thenReturn(true); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | @Test |
| 85 | public void testWrongPassword() throws Exception { |
| 86 | when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(false); |
| 87 | when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(true); |
| 88 | when(mLockPatternUtils.checkPassword("1234", mUserId)).thenReturn(false); |
| 89 | assertEquals(-1, mCommand.exec(mBinder, in, out, err, |
| 90 | new String[] { "set-pin", "--old", "1234" }, |
| 91 | mShellCallback, mResultReceiver)); |
| 92 | verify(mLockPatternUtils, never()).saveLockPassword(any(), any(), anyInt(), anyInt()); |
| 93 | } |
| 94 | |
| 95 | @Test |
| 96 | public void testChangePin() throws Exception { |
| 97 | when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(false); |
| 98 | when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(true); |
| 99 | when(mLockPatternUtils.checkPassword("1234", mUserId)).thenReturn(true); |
| 100 | assertEquals(0, mCommand.exec(new Binder(), in, out, err, |
| 101 | new String[] { "set-pin", "--old", "1234", "4321" }, |
| 102 | mShellCallback, mResultReceiver)); |
| 103 | verify(mLockPatternUtils).saveLockPassword("4321", "1234", PASSWORD_QUALITY_NUMERIC, |
| 104 | mUserId); |
| 105 | } |
| 106 | |
| 107 | @Test |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame^] | 108 | public void testChangePin_noLockScreen() throws Exception { |
| 109 | when(mLockPatternUtils.hasSecureLockScreen()).thenReturn(false); |
| 110 | assertEquals(-1, mCommand.exec(new Binder(), in, out, err, |
| 111 | new String[] { "set-pin", "--old", "1234", "4321" }, |
| 112 | mShellCallback, mResultReceiver)); |
| 113 | verify(mLockPatternUtils).hasSecureLockScreen(); |
| 114 | verifyNoMoreInteractions(mLockPatternUtils); |
| 115 | } |
| 116 | |
| 117 | @Test |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 118 | public void testChangePassword() throws Exception { |
| 119 | when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(false); |
| 120 | when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(true); |
| 121 | when(mLockPatternUtils.checkPassword("1234", mUserId)).thenReturn(true); |
| 122 | assertEquals(0, mCommand.exec(new Binder(), in, out, err, |
| 123 | new String[] { "set-password", "--old", "1234", "4321" }, |
| 124 | mShellCallback, mResultReceiver)); |
| 125 | verify(mLockPatternUtils).saveLockPassword("4321", "1234", PASSWORD_QUALITY_ALPHABETIC, |
| 126 | mUserId); |
| 127 | } |
| 128 | |
| 129 | @Test |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame^] | 130 | public void testChangePassword_noLockScreen() throws Exception { |
| 131 | when(mLockPatternUtils.hasSecureLockScreen()).thenReturn(false); |
| 132 | assertEquals(-1, mCommand.exec(new Binder(), in, out, err, |
| 133 | new String[] { "set-password", "--old", "1234", "4321" }, |
| 134 | mShellCallback, mResultReceiver)); |
| 135 | verify(mLockPatternUtils).hasSecureLockScreen(); |
| 136 | verifyNoMoreInteractions(mLockPatternUtils); |
| 137 | } |
| 138 | |
| 139 | @Test |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 140 | public void testChangePattern() throws Exception { |
| 141 | when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(true); |
| 142 | when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(false); |
| 143 | when(mLockPatternUtils.checkPattern(stringToPattern("1234"), mUserId)).thenReturn(true); |
| 144 | assertEquals(0, mCommand.exec(new Binder(), in, out, err, |
| 145 | new String[] { "set-pattern", "--old", "1234", "4321" }, |
| 146 | mShellCallback, mResultReceiver)); |
| 147 | verify(mLockPatternUtils).saveLockPattern(stringToPattern("4321"), "1234", mUserId); |
| 148 | } |
| 149 | |
| 150 | @Test |
Lenka Trochtova | 66c492a | 2018-12-06 11:29:21 +0100 | [diff] [blame^] | 151 | public void testChangePattern_noLockScreen() throws Exception { |
| 152 | when(mLockPatternUtils.hasSecureLockScreen()).thenReturn(false); |
| 153 | assertEquals(-1, mCommand.exec(new Binder(), in, out, err, |
| 154 | new String[] { "set-pattern", "--old", "1234", "4321" }, |
| 155 | mShellCallback, mResultReceiver)); |
| 156 | verify(mLockPatternUtils).hasSecureLockScreen(); |
| 157 | verifyNoMoreInteractions(mLockPatternUtils); |
| 158 | } |
| 159 | |
| 160 | @Test |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 161 | public void testClear() throws Exception { |
| 162 | when(mLockPatternUtils.isLockPatternEnabled(mUserId)).thenReturn(true); |
| 163 | when(mLockPatternUtils.isLockPasswordEnabled(mUserId)).thenReturn(false); |
| 164 | when(mLockPatternUtils.checkPattern(stringToPattern("1234"), mUserId)).thenReturn(true); |
| 165 | assertEquals(0, mCommand.exec(new Binder(), in, out, err, |
| 166 | new String[] { "clear", "--old", "1234" }, |
| 167 | mShellCallback, mResultReceiver)); |
Rubin Xu | a55b168 | 2017-01-31 10:06:56 +0000 | [diff] [blame] | 168 | verify(mLockPatternUtils).clearLock("1234", mUserId); |
Jorim Jaggi | 2fef6f7 | 2016-11-01 19:06:25 -0700 | [diff] [blame] | 169 | } |
| 170 | } |