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 | |
| 17 | package com.android.server; |
| 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 | |
| 32 | import static java.io.FileDescriptor.*; |
| 33 | |
| 34 | import android.app.ActivityManager; |
| 35 | import android.content.Context; |
| 36 | import android.os.Binder; |
| 37 | import android.os.Debug; |
| 38 | import android.os.Handler; |
| 39 | import android.os.Looper; |
| 40 | import android.os.ResultReceiver; |
| 41 | import android.os.ShellCallback; |
| 42 | import android.platform.test.annotations.Presubmit; |
| 43 | import android.support.test.InstrumentationRegistry; |
| 44 | import android.support.test.filters.SmallTest; |
| 45 | import android.support.test.runner.AndroidJUnit4; |
| 46 | |
| 47 | import com.android.internal.widget.LockPatternUtils; |
| 48 | |
| 49 | import junit.framework.Assert; |
| 50 | |
| 51 | import org.junit.Before; |
| 52 | import org.junit.Test; |
| 53 | import org.junit.runner.RunWith; |
| 54 | import org.mockito.Mock; |
| 55 | import org.mockito.MockitoAnnotations; |
| 56 | |
| 57 | import java.io.FileDescriptor; |
| 58 | |
| 59 | /** |
| 60 | * Test class for {@link LockSettingsShellCommand}. |
| 61 | * |
| 62 | * runtest frameworks-services -c com.android.server.LockSettingsShellCommandTest |
| 63 | */ |
| 64 | @SmallTest |
| 65 | @Presubmit |
| 66 | @RunWith(AndroidJUnit4.class) |
| 67 | public 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(mUserId); |
| 141 | } |
| 142 | } |