1 /* 2 * Copyright (C) 2018 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.enterprise; 18 19 import static android.content.Intent.FLAG_ACTIVITY_NEW_TASK; 20 import static android.security.advancedprotection.AdvancedProtectionManager.ACTION_SHOW_ADVANCED_PROTECTION_SUPPORT_DIALOG; 21 import static android.security.advancedprotection.AdvancedProtectionManager.ADVANCED_PROTECTION_SYSTEM_ENTITY; 22 import static android.security.advancedprotection.AdvancedProtectionManager.EXTRA_SUPPORT_DIALOG_FEATURE; 23 import static android.security.advancedprotection.AdvancedProtectionManager.EXTRA_SUPPORT_DIALOG_TYPE; 24 import static android.security.advancedprotection.AdvancedProtectionManager.FEATURE_ID_DISALLOW_INSTALL_UNKNOWN_SOURCES; 25 import static android.security.advancedprotection.AdvancedProtectionManager.SUPPORT_DIALOG_TYPE_UNKNOWN; 26 27 import static org.junit.Assert.assertEquals; 28 import static org.junit.Assert.assertTrue; 29 import static org.mockito.ArgumentMatchers.any; 30 import static org.mockito.ArgumentMatchers.eq; 31 import static org.mockito.Mockito.doNothing; 32 import static org.mockito.Mockito.doReturn; 33 import static org.mockito.Mockito.spy; 34 import static org.mockito.Mockito.verify; 35 import static org.mockito.Mockito.when; 36 37 import android.app.admin.Authority; 38 import android.app.admin.DevicePolicyManager; 39 import android.app.admin.EnforcingAdmin; 40 import android.app.admin.UnknownAuthority; 41 import android.content.ComponentName; 42 import android.content.Intent; 43 import android.os.UserHandle; 44 import android.os.UserManager; 45 import android.platform.test.annotations.RequiresFlagsEnabled; 46 import android.platform.test.flag.junit.CheckFlagsRule; 47 import android.platform.test.flag.junit.DeviceFlagsValueProvider; 48 49 import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 50 51 import org.junit.Before; 52 import org.junit.Rule; 53 import org.junit.Test; 54 import org.junit.runner.RunWith; 55 import org.mockito.ArgumentCaptor; 56 import org.mockito.Mock; 57 import org.mockito.MockitoAnnotations; 58 import org.robolectric.RobolectricTestRunner; 59 60 @RunWith(RobolectricTestRunner.class) 61 public class ActionDisabledByAdminDialogTest { 62 @Rule 63 public final CheckFlagsRule mCheckFlagsRule = DeviceFlagsValueProvider.createCheckFlagsRule(); 64 65 @Mock 66 private DevicePolicyManager mDevicePolicyManager; 67 68 private ActionDisabledByAdminDialog mDialog; 69 private final ComponentName mAdminComponent = new ComponentName("admin", "adminclass"); 70 71 @Before setUp()72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 mDialog = spy(new ActionDisabledByAdminDialog()); 75 doReturn(mDevicePolicyManager).when(mDialog).getSystemService(DevicePolicyManager.class); 76 } 77 78 @Test testGetAdminDetailsFromIntent()79 public void testGetAdminDetailsFromIntent() { 80 final int userId = 123; 81 final EnforcedAdmin expectedAdmin = new EnforcedAdmin(mAdminComponent, UserHandle.of( 82 userId)); 83 84 final Intent intent = new Intent(); 85 intent.putExtra(DevicePolicyManager.EXTRA_DEVICE_ADMIN, mAdminComponent); 86 intent.putExtra(Intent.EXTRA_USER_ID, userId); 87 assertEquals(expectedAdmin, mDialog.getAdminDetailsFromIntent(intent)); 88 } 89 90 @Test testGetAdminDetailsFromNullIntent()91 public void testGetAdminDetailsFromNullIntent() { 92 final int userId = UserHandle.myUserId(); 93 final EnforcedAdmin expectedAdmin = new EnforcedAdmin(null, UserHandle.of(userId)); 94 95 assertEquals(expectedAdmin, mDialog.getAdminDetailsFromIntent(null)); 96 } 97 98 @Test testGetRestrictionFromIntent()99 public void testGetRestrictionFromIntent() { 100 final String restriction = "someRestriction"; 101 final Intent intent = new Intent(); 102 103 intent.putExtra(DevicePolicyManager.EXTRA_RESTRICTION, restriction); 104 assertEquals(restriction, mDialog.getRestrictionFromIntent(intent)); 105 } 106 107 @Test testGetRestrictionFromNullIntent()108 public void testGetRestrictionFromNullIntent() { 109 assertEquals(null, mDialog.getRestrictionFromIntent(null)); 110 } 111 112 @RequiresFlagsEnabled(android.security.Flags.FLAG_AAPM_API) 113 @Test testGetAdminDetailsFromIntent_nullComponent_advancedProtection_launchesNewDialog()114 public void testGetAdminDetailsFromIntent_nullComponent_advancedProtection_launchesNewDialog() { 115 final int userId = UserHandle.myUserId(); 116 final Authority advancedProtectionAuthority = new UnknownAuthority( 117 ADVANCED_PROTECTION_SYSTEM_ENTITY); 118 final EnforcingAdmin advancedProtectionEnforcingAdmin = new EnforcingAdmin("test.pkg", 119 advancedProtectionAuthority, UserHandle.of(userId), mAdminComponent); 120 final String userRestriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY; 121 122 final Intent dialogIntent = new Intent(); 123 dialogIntent.putExtra(Intent.EXTRA_USER_ID, userId); 124 dialogIntent.putExtra(DevicePolicyManager.EXTRA_RESTRICTION, userRestriction); 125 126 when(mDevicePolicyManager.getEnforcingAdmin(userId, userRestriction)) 127 .thenReturn(advancedProtectionEnforcingAdmin); 128 doNothing().when(mDialog).startActivityAsUser(any(), eq(UserHandle.of(userId))); 129 130 mDialog.getAdminDetailsFromIntent(dialogIntent); 131 132 ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); 133 verify(mDialog).startActivityAsUser(intentCaptor.capture(), eq(UserHandle.of(userId))); 134 assertTrue(mDialog.isFinishing()); 135 136 Intent launchedIntent = intentCaptor.getValue(); 137 assertEquals("Intent action is incorrect", ACTION_SHOW_ADVANCED_PROTECTION_SUPPORT_DIALOG, 138 launchedIntent.getAction()); 139 assertEquals("Feature ID extra is incorrect", FEATURE_ID_DISALLOW_INSTALL_UNKNOWN_SOURCES, 140 launchedIntent.getIntExtra(EXTRA_SUPPORT_DIALOG_FEATURE, -1)); 141 assertEquals("Type is incorrect", SUPPORT_DIALOG_TYPE_UNKNOWN, 142 launchedIntent.getIntExtra(EXTRA_SUPPORT_DIALOG_TYPE, -1)); 143 assertEquals(FLAG_ACTIVITY_NEW_TASK, launchedIntent.getFlags()); 144 } 145 146 @RequiresFlagsEnabled(android.security.Flags.FLAG_AAPM_API) 147 @Test testGetAdminDetailsFromIntent_nullComponent_notAdvancedProtection_retrievesAdmin()148 public void testGetAdminDetailsFromIntent_nullComponent_notAdvancedProtection_retrievesAdmin() { 149 final int userId = UserHandle.myUserId(); 150 final EnforcingAdmin nonAdvancedProtectionEnforcingAdmin = new EnforcingAdmin("test.pkg", 151 UnknownAuthority.UNKNOWN_AUTHORITY, UserHandle.of(userId), mAdminComponent); 152 final String userRestriction = UserManager.DISALLOW_INSTALL_UNKNOWN_SOURCES_GLOBALLY; 153 154 final Intent dialogIntent = new Intent(); 155 dialogIntent.putExtra(Intent.EXTRA_USER_ID, userId); 156 dialogIntent.putExtra(DevicePolicyManager.EXTRA_RESTRICTION, userRestriction); 157 158 when(mDevicePolicyManager.getEnforcingAdmin(userId, userRestriction)) 159 .thenReturn(nonAdvancedProtectionEnforcingAdmin); 160 161 EnforcedAdmin admin = mDialog.getAdminDetailsFromIntent(dialogIntent); 162 assertEquals(mAdminComponent, admin.component); 163 } 164 } 165