1 /* 2 * Copyright (C) 2020 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.settings.security; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.any; 22 import static org.mockito.ArgumentMatchers.eq; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.when; 26 27 import android.app.admin.DevicePolicyManager; 28 import android.content.ComponentName; 29 import android.content.Intent; 30 import android.net.Uri; 31 import android.os.UserHandle; 32 import android.security.AppUriAuthenticationPolicy; 33 import android.security.Credentials; 34 import android.security.IKeyChainService; 35 import android.security.KeyChain; 36 import android.widget.Button; 37 import android.widget.LinearLayout; 38 39 import androidx.recyclerview.widget.RecyclerView; 40 41 import com.android.settings.R; 42 43 import org.junit.Before; 44 import org.junit.Test; 45 import org.junit.runner.RunWith; 46 import org.mockito.Mock; 47 import org.mockito.MockitoAnnotations; 48 import org.robolectric.Robolectric; 49 import org.robolectric.RobolectricTestRunner; 50 51 @RunWith(RobolectricTestRunner.class) 52 public class RequestManageCredentialsTest { 53 54 private static final AppUriAuthenticationPolicy AUTHENTICATION_POLICY = 55 new AppUriAuthenticationPolicy.Builder() 56 .addAppAndUriMapping("com.android.test", Uri.parse("test.com"), "testAlias") 57 .build(); 58 59 private RequestManageCredentials mActivity; 60 61 @Mock 62 private DevicePolicyManager mDevicePolicyManager; 63 64 @Mock 65 private KeyChain.KeyChainConnection mKeyChainConnection; 66 67 @Mock 68 private IKeyChainService mKeyChainService; 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 } 74 75 @Test onCreate_intentActionNotManageCredentials_finishActivity()76 public void onCreate_intentActionNotManageCredentials_finishActivity() 77 throws Exception { 78 final Intent intent = new Intent(Credentials.ACTION_MANAGE_CREDENTIALS + "_bad"); 79 intent.putExtra(KeyChain.EXTRA_AUTHENTICATION_POLICY, AUTHENTICATION_POLICY); 80 setupActivityWithAction(intent); 81 when(mDevicePolicyManager.getDeviceOwnerUser()).thenReturn(null); 82 when(mDevicePolicyManager.getProfileOwner()).thenReturn(null); 83 when(mActivity.getLaunchedFromPackage()).thenReturn("com.example.credapp"); 84 85 mActivity.onCreate(null); 86 87 assertThat(mActivity).isNotNull(); 88 assertThat(mActivity.isFinishing()).isTrue(); 89 } 90 91 @Test onCreate_noAuthenticationPolicy_finishActivity()92 public void onCreate_noAuthenticationPolicy_finishActivity() 93 throws Exception { 94 final Intent intent = new Intent(Credentials.ACTION_MANAGE_CREDENTIALS); 95 setupActivityWithAction(intent); 96 when(mDevicePolicyManager.getDeviceOwnerUser()).thenReturn(null); 97 when(mDevicePolicyManager.getProfileOwner()).thenReturn(null); 98 when(mActivity.getLaunchedFromPackage()).thenReturn("com.example.credapp"); 99 100 mActivity.onCreate(null); 101 102 assertThat(mActivity).isNotNull(); 103 assertThat(mActivity.isFinishing()).isTrue(); 104 } 105 106 @Test onCreate_invalidAuthenticationPolicy_finishActivity()107 public void onCreate_invalidAuthenticationPolicy_finishActivity() 108 throws Exception { 109 final Intent intent = new Intent(Credentials.ACTION_MANAGE_CREDENTIALS); 110 intent.putExtra(KeyChain.EXTRA_AUTHENTICATION_POLICY, "Invalid policy"); 111 112 setupActivityWithAction(intent); 113 when(mDevicePolicyManager.getDeviceOwnerUser()).thenReturn(null); 114 when(mDevicePolicyManager.getProfileOwner()).thenReturn(null); 115 when(mActivity.getLaunchedFromPackage()).thenReturn("com.example.credapp"); 116 117 mActivity.onCreate(null); 118 119 assertThat(mActivity).isNotNull(); 120 assertThat(mActivity.isFinishing()).isTrue(); 121 } 122 123 @Test onCreate_runOnManagedProfile_finishActivity()124 public void onCreate_runOnManagedProfile_finishActivity() 125 throws Exception { 126 final Intent intent = new Intent(Credentials.ACTION_MANAGE_CREDENTIALS); 127 intent.putExtra(KeyChain.EXTRA_AUTHENTICATION_POLICY, AUTHENTICATION_POLICY); 128 129 setupActivityWithAction(intent); 130 when(mDevicePolicyManager.getDeviceOwnerUser()).thenReturn(null); 131 when(mDevicePolicyManager.getProfileOwner()).thenReturn(new ComponentName("pkg", "cls")); 132 when(mActivity.getLaunchedFromPackage()).thenReturn("com.example.credapp"); 133 134 mActivity.onCreate(null); 135 136 assertThat(mActivity).isNotNull(); 137 assertThat(mActivity.isFinishing()).isTrue(); 138 } 139 140 @Test onCreate_runOnManagedDevice_finishActivity()141 public void onCreate_runOnManagedDevice_finishActivity() 142 throws Exception { 143 final Intent intent = new Intent(Credentials.ACTION_MANAGE_CREDENTIALS); 144 intent.putExtra(KeyChain.EXTRA_AUTHENTICATION_POLICY, AUTHENTICATION_POLICY); 145 146 setupActivityWithAction(intent); 147 when(mDevicePolicyManager.getDeviceOwnerUser()).thenReturn(UserHandle.SYSTEM); 148 when(mDevicePolicyManager.getProfileOwner()).thenReturn(null); 149 when(mActivity.getLaunchedFromPackage()).thenReturn("com.example.credapp"); 150 151 mActivity.onCreate(null); 152 153 assertThat(mActivity).isNotNull(); 154 assertThat(mActivity.isFinishing()).isTrue(); 155 } 156 157 @Test onCreate_authenticationPolicyProvided_startActivity()158 public void onCreate_authenticationPolicyProvided_startActivity() throws Exception { 159 final Intent intent = new Intent(Credentials.ACTION_MANAGE_CREDENTIALS); 160 intent.putExtra(KeyChain.EXTRA_AUTHENTICATION_POLICY, AUTHENTICATION_POLICY); 161 setupActivityWithAction(intent); 162 163 when(mDevicePolicyManager.getDeviceOwnerUser()).thenReturn(null); 164 when(mDevicePolicyManager.getProfileOwner()).thenReturn(null); 165 when(mActivity.getLaunchedFromPackage()).thenReturn("com.example.credapp"); 166 167 mActivity.onCreate(null); 168 169 assertThat(mActivity).isNotNull(); 170 assertThat(mActivity.isFinishing()).isFalse(); 171 assertThat((RecyclerView) mActivity.findViewById(R.id.apps_list)).isNotNull(); 172 assertThat((LinearLayout) mActivity.findViewById(R.id.button_panel)).isNotNull(); 173 assertThat((Button) mActivity.findViewById(R.id.allow_button)).isNotNull(); 174 assertThat((Button) mActivity.findViewById(R.id.dont_allow_button)).isNotNull(); 175 } 176 setupActivityWithAction(Intent intent)177 private void setupActivityWithAction(Intent intent) throws Exception { 178 mActivity = spy(Robolectric.buildActivity(RequestManageCredentials.class, intent).get()); 179 doReturn(mKeyChainConnection).when(mActivity).getKeyChainConnection(eq(mActivity), any()); 180 doReturn(mDevicePolicyManager).when(mActivity).getSystemService(DevicePolicyManager.class); 181 when(mKeyChainConnection.getService()).thenReturn(mKeyChainService); 182 } 183 } 184