blob: a28a5a10e8321b533c56ace8ce2e1a4c8fe74689 [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;
30import static org.mockito.Mockito.when;
31
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070032import android.app.ActivityManager;
33import android.content.Context;
34import android.os.Binder;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070035import android.os.Handler;
36import android.os.Looper;
37import android.os.ResultReceiver;
38import android.os.ShellCallback;
39import android.platform.test.annotations.Presubmit;
Brett Chabota26eda92018-07-23 13:08:30 -070040
41import androidx.test.InstrumentationRegistry;
42import androidx.test.filters.SmallTest;
43import androidx.test.runner.AndroidJUnit4;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070044
45import com.android.internal.widget.LockPatternUtils;
46
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070047import org.junit.Before;
48import org.junit.Test;
49import org.junit.runner.RunWith;
50import org.mockito.Mock;
51import org.mockito.MockitoAnnotations;
52
Brett Chabota26eda92018-07-23 13:08:30 -070053import static java.io.FileDescriptor.*;
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070054
55/**
56 * Test class for {@link LockSettingsShellCommand}.
57 *
Andrew Scull507d11c2017-05-03 17:19:01 +010058 * runtest frameworks-services -c com.android.server.locksettings.LockSettingsShellCommandTest
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070059 */
60@SmallTest
61@Presubmit
62@RunWith(AndroidJUnit4.class)
63public 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 Lemee72ec172018-07-16 13:33:36 -070079 mCommand = new LockSettingsShellCommand(mLockPatternUtils);
Jorim Jaggi2fef6f72016-11-01 19:06:25 -070080 }
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 Xua55b1682017-01-31 10:06:56 +0000136 verify(mLockPatternUtils).clearLock("1234", mUserId);
Jorim Jaggi2fef6f72016-11-01 19:06:25 -0700137 }
138}