blob: 929c3b525db947acad9df51af3de18fa1fb29824 [file] [log] [blame]
Jorim Jaggi2fef6f72016-11-01 19:06:25 -07001/*
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 Scull507d11c2017-05-03 17:19:01 +010017package com.android.server.locksettings;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070018
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;
Lenka Trochtova66c492a2018-12-06 11:29:21 +010030import static org.mockito.Mockito.verifyNoMoreInteractions;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070031import static org.mockito.Mockito.when;
32
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070033import android.app.ActivityManager;
34import android.content.Context;
35import android.os.Binder;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070036import android.os.Handler;
37import android.os.Looper;
38import android.os.ResultReceiver;
39import android.os.ShellCallback;
40import android.platform.test.annotations.Presubmit;
Brett Chabota26eda92018-07-23 13:08:30 -070041
42import androidx.test.InstrumentationRegistry;
43import androidx.test.filters.SmallTest;
44import androidx.test.runner.AndroidJUnit4;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070045
46import com.android.internal.widget.LockPatternUtils;
47
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070048import org.junit.Before;
49import org.junit.Test;
50import org.junit.runner.RunWith;
51import org.mockito.Mock;
52import org.mockito.MockitoAnnotations;
53
Brett Chabota26eda92018-07-23 13:08:30 -070054import static java.io.FileDescriptor.*;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070055
56/**
57 * Test class for {@link LockSettingsShellCommand}.
58 *
Andrew Scull507d11c2017-05-03 17:19:01 +010059 * runtest frameworks-services -c com.android.server.locksettings.LockSettingsShellCommandTest
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070060 */
61@SmallTest
62@Presubmit
63@RunWith(AndroidJUnit4.class)
64public 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 Lemee72ec172018-07-16 13:33:36 -070080 mCommand = new LockSettingsShellCommand(mLockPatternUtils);
Lenka Trochtova66c492a2018-12-06 11:29:21 +010081 when(mLockPatternUtils.hasSecureLockScreen()).thenReturn(true);
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070082 }
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 Trochtova66c492a2018-12-06 11:29:21 +0100108 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 Jaggi2fef6f72016-11-01 19:06:25 -0700118 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 Trochtova66c492a2018-12-06 11:29:21 +0100130 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 Jaggi2fef6f72016-11-01 19:06:25 -0700140 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 Trochtova66c492a2018-12-06 11:29:21 +0100151 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 Jaggi2fef6f72016-11-01 19:06:25 -0700161 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 Xua55b1682017-01-31 10:06:56 +0000168 verify(mLockPatternUtils).clearLock("1234", mUserId);
Jorim Jaggi2fef6f72016-11-01 19:06:25 -0700169 }
170}