Makoto Onuki | cc4bbeb | 2015-09-17 10:28:24 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2015 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.devicepolicy; |
| 18 | |
| 19 | import com.android.server.LocalServices; |
| 20 | |
| 21 | import android.app.admin.DeviceAdminReceiver; |
| 22 | import android.app.admin.DevicePolicyManager; |
| 23 | import android.app.admin.DevicePolicyManagerInternal; |
| 24 | import android.content.ComponentName; |
| 25 | import android.content.Intent; |
| 26 | import android.content.pm.PackageManager; |
| 27 | import android.content.pm.ResolveInfo; |
| 28 | import android.os.UserHandle; |
| 29 | |
| 30 | import org.mockito.ArgumentCaptor; |
| 31 | |
| 32 | import java.util.List; |
| 33 | |
| 34 | import static org.mockito.Matchers.any; |
| 35 | import static org.mockito.Matchers.eq; |
| 36 | import static org.mockito.Mockito.doReturn; |
| 37 | import static org.mockito.Mockito.times; |
| 38 | import static org.mockito.Mockito.verify; |
| 39 | import static org.mockito.Mockito.when; |
| 40 | |
| 41 | /** |
| 42 | * Tests for {@link DevicePolicyManager} and {@link DevicePolicyManagerService}. |
| 43 | * |
| 44 | m FrameworksServicesTests && |
| 45 | adb install \ |
| 46 | -r out/target/product/hammerhead/data/app/FrameworksServicesTests/FrameworksServicesTests.apk && |
| 47 | adb shell am instrument -e class com.android.server.devicepolicy.DevicePolicyManagerTest \ |
| 48 | -w com.android.frameworks.servicestests/android.support.test.runner.AndroidJUnitRunner |
| 49 | |
| 50 | (mmma frameworks/base/services/tests/servicestests/ for non-ninja build) |
| 51 | */ |
| 52 | public class DevicePolicyManagerTest extends DpmTestBase { |
| 53 | |
| 54 | private DpmMockContext mContext; |
| 55 | public DevicePolicyManager dpm; |
| 56 | public DevicePolicyManagerServiceTestable dpms; |
| 57 | |
| 58 | @Override |
| 59 | protected void setUp() throws Exception { |
| 60 | super.setUp(); |
| 61 | |
| 62 | mContext = getContext(); |
| 63 | |
| 64 | when(mContext.packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN))) |
| 65 | .thenReturn(true); |
| 66 | |
| 67 | LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); |
| 68 | dpms = new DevicePolicyManagerServiceTestable(mContext, dataDir); |
| 69 | dpm = new DevicePolicyManagerTestable(mContext, dpms); |
| 70 | } |
| 71 | |
| 72 | public void testHasNoFeature() { |
| 73 | when(mContext.packageManager.hasSystemFeature(eq(PackageManager.FEATURE_DEVICE_ADMIN))) |
| 74 | .thenReturn(false); |
| 75 | |
| 76 | LocalServices.removeServiceForTest(DevicePolicyManagerInternal.class); |
| 77 | new DevicePolicyManagerServiceTestable(mContext, dataDir); |
| 78 | |
| 79 | // If the device has no DPMS feature, it shouldn't register the local service. |
| 80 | assertNull(LocalServices.getService(DevicePolicyManagerInternal.class)); |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Caller doesn't have proper permissions. |
| 85 | */ |
| 86 | public void testSetActiveAdmin_SecurityException() { |
| 87 | final ComponentName admin = new ComponentName(mRealTestContext, DummyDeviceAdmin.class); |
| 88 | |
| 89 | // 1. Failure cases. |
| 90 | |
| 91 | // Caller doesn't have MANAGE_DEVICE_ADMINS. |
| 92 | try { |
| 93 | dpm.setActiveAdmin(admin, false); |
| 94 | fail("Didn't throw SecurityException"); |
| 95 | } catch (SecurityException expected) { |
| 96 | } |
| 97 | |
| 98 | // Caller has MANAGE_DEVICE_ADMINS, but for different user. |
| 99 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 100 | try { |
| 101 | dpm.setActiveAdmin(admin, false, DpmMockContext.CALLER_USER_HANDLE + 1); |
| 102 | fail("Didn't throw SecurityException"); |
| 103 | } catch (SecurityException expected) { |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | public void testSetActiveAdmin() { |
| 108 | final ComponentName admin = new ComponentName(mRealTestContext, DummyDeviceAdmin.class); |
| 109 | |
| 110 | // 1. Prepare mock package manager (and other mocks) |
| 111 | |
| 112 | mContext.callerPermissions.add(android.Manifest.permission.MANAGE_DEVICE_ADMINS); |
| 113 | |
| 114 | // Create ResolveInfo for the admin. |
| 115 | final Intent resolveIntent = new Intent(); |
| 116 | resolveIntent.setComponent(admin); |
| 117 | final List<ResolveInfo> realResolveInfo = |
| 118 | mRealTestContext.getPackageManager().queryBroadcastReceivers( |
| 119 | resolveIntent, |
| 120 | PackageManager.GET_META_DATA |
| 121 | | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS); |
| 122 | assertNotNull(realResolveInfo); |
| 123 | assertEquals(1, realResolveInfo.size()); |
| 124 | |
| 125 | // We need to rewrite the UID in the activity info. |
| 126 | realResolveInfo.get(0).activityInfo.applicationInfo.uid = DpmMockContext.CALLER_UID; |
| 127 | |
| 128 | doReturn(realResolveInfo).when(mContext.packageManager).queryBroadcastReceivers( |
| 129 | any(Intent.class), // TODO check the intent too. |
| 130 | eq(PackageManager.GET_META_DATA |
| 131 | | PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS), |
| 132 | eq(DpmMockContext.CALLER_USER_HANDLE) |
| 133 | ); |
| 134 | |
| 135 | // 2. Everything is ready; call the method. |
| 136 | dpm.setActiveAdmin(admin, false); |
| 137 | |
| 138 | // 3. Verify internal calls. |
| 139 | |
| 140 | // Check if the boradcast is sent. |
| 141 | final ArgumentCaptor<Intent> intentCap = ArgumentCaptor.forClass(Intent.class); |
| 142 | final ArgumentCaptor<UserHandle> uhCap = ArgumentCaptor.forClass(UserHandle.class); |
| 143 | |
| 144 | verify(mContext.spiedContext, times(2)).sendBroadcastAsUser( |
| 145 | intentCap.capture(), |
| 146 | MockUtils.checkUserHandle(DpmMockContext.CALLER_USER_HANDLE)); |
| 147 | |
| 148 | // First call from saveSettingsLocked(). |
| 149 | assertEquals(DevicePolicyManager.ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED, |
| 150 | intentCap.getAllValues().get(0).getAction()); |
| 151 | |
| 152 | // Second call from setActiveAdmin/sendAdminCommandLocked() |
| 153 | assertEquals(DeviceAdminReceiver.ACTION_DEVICE_ADMIN_ENABLED, |
| 154 | intentCap.getAllValues().get(1).getAction()); |
| 155 | |
| 156 | // TODO Verify other calls too. |
| 157 | } |
| 158 | } |
| 159 | |