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