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 android.content.ComponentName; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.pm.ActivityInfo; 23 import android.content.pm.ApplicationInfo; 24 import android.content.pm.PackageInfo; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.os.UserHandle; 28 import android.test.AndroidTestCase; 29 30 import java.io.File; 31 import java.util.List; 32 33 import static org.mockito.Matchers.anyInt; 34 import static org.mockito.Matchers.eq; 35 import static org.mockito.Mockito.doReturn; 36 37 public abstract class DpmTestBase extends AndroidTestCase { 38 public static final String TAG = "DpmTest"; 39 40 protected Context mRealTestContext; 41 protected DpmMockContext mMockContext; 42 43 public File dataDir; 44 45 public ComponentName admin1; 46 public ComponentName admin2; 47 public ComponentName admin3; 48 public ComponentName adminNoPerm; 49 50 @Override setUp()51 protected void setUp() throws Exception { 52 super.setUp(); 53 54 mRealTestContext = super.getContext(); 55 56 mMockContext = new DpmMockContext( 57 mRealTestContext, new File(mRealTestContext.getCacheDir(), "test-data")); 58 59 admin1 = new ComponentName(mRealTestContext, DummyDeviceAdmins.Admin1.class); 60 admin2 = new ComponentName(mRealTestContext, DummyDeviceAdmins.Admin2.class); 61 admin3 = new ComponentName(mRealTestContext, DummyDeviceAdmins.Admin3.class); 62 adminNoPerm = new ComponentName(mRealTestContext, DummyDeviceAdmins.AdminNoPerm.class); 63 } 64 65 @Override getContext()66 public DpmMockContext getContext() { 67 return mMockContext; 68 } 69 markPackageAsInstalled(String packageName, ApplicationInfo ai, int userId)70 protected void markPackageAsInstalled(String packageName, ApplicationInfo ai, int userId) 71 throws Exception { 72 final PackageInfo pi = DpmTestUtils.cloneParcelable( 73 mRealTestContext.getPackageManager().getPackageInfo( 74 mRealTestContext.getPackageName(), 0)); 75 assertTrue(pi.applicationInfo.flags != 0); 76 77 if (ai != null) { 78 pi.applicationInfo = ai; 79 } 80 81 doReturn(pi).when(mMockContext.ipackageManager).getPackageInfo( 82 eq(packageName), 83 eq(0), 84 eq(userId)); 85 } 86 setUpPackageManagerForAdmin(ComponentName admin, int packageUid)87 protected void setUpPackageManagerForAdmin(ComponentName admin, int packageUid) 88 throws Exception { 89 setUpPackageManagerForAdmin(admin, packageUid, 90 /* enabledSetting =*/ null, /* appTargetSdk = */ null); 91 } 92 setUpPackageManagerForAdmin(ComponentName admin, int packageUid, int enabledSetting)93 protected void setUpPackageManagerForAdmin(ComponentName admin, int packageUid, 94 int enabledSetting) throws Exception { 95 setUpPackageManagerForAdmin(admin, packageUid, enabledSetting, /* appTargetSdk = */ null); 96 } 97 setUpPackageManagerForAdmin(ComponentName admin, int packageUid, Integer enabledSetting, Integer appTargetSdk)98 protected void setUpPackageManagerForAdmin(ComponentName admin, int packageUid, 99 Integer enabledSetting, Integer appTargetSdk) throws Exception { 100 setUpPackageManagerForFakeAdmin(admin, packageUid, enabledSetting, appTargetSdk, 101 admin); 102 } 103 104 /** 105 * Set up a component in the mock package manager to be an active admin. 106 * 107 * @param admin ComponentName that's visible to the test code, which doesn't have to exist. 108 * @param copyFromAdmin package information for {@code admin} will be built based on this 109 * component's information. 110 */ setUpPackageManagerForFakeAdmin(ComponentName admin, int packageUid, Integer enabledSetting, Integer appTargetSdk, ComponentName copyFromAdmin)111 protected void setUpPackageManagerForFakeAdmin(ComponentName admin, int packageUid, 112 Integer enabledSetting, Integer appTargetSdk, ComponentName copyFromAdmin) 113 throws Exception { 114 115 // Set up getApplicationInfo(). 116 117 final ApplicationInfo ai = DpmTestUtils.cloneParcelable( 118 mRealTestContext.getPackageManager().getApplicationInfo( 119 copyFromAdmin.getPackageName(), 120 PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS)); 121 122 ai.enabledSetting = enabledSetting == null 123 ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED_UNTIL_USED 124 : enabledSetting; 125 if (appTargetSdk != null) { 126 ai.targetSdkVersion = appTargetSdk; 127 } 128 ai.uid = packageUid; 129 ai.packageName = admin.getPackageName(); 130 ai.name = admin.getClassName(); 131 132 doReturn(ai).when(mMockContext.ipackageManager).getApplicationInfo( 133 eq(admin.getPackageName()), 134 eq(PackageManager.GET_DISABLED_UNTIL_USED_COMPONENTS), 135 eq(UserHandle.getUserId(packageUid))); 136 137 // Set up queryBroadcastReceivers(). 138 139 final Intent resolveIntent = new Intent(); 140 resolveIntent.setComponent(copyFromAdmin); 141 final List<ResolveInfo> realResolveInfo = 142 mRealTestContext.getPackageManager().queryBroadcastReceivers( 143 resolveIntent, 144 PackageManager.GET_META_DATA); 145 assertNotNull(realResolveInfo); 146 assertEquals(1, realResolveInfo.size()); 147 148 // We need to change AI, so set a clone. 149 realResolveInfo.set(0, DpmTestUtils.cloneParcelable(realResolveInfo.get(0))); 150 151 // We need to rewrite the UID in the activity info. 152 final ActivityInfo aci = realResolveInfo.get(0).activityInfo; 153 aci.applicationInfo = ai; 154 aci.packageName = admin.getPackageName(); 155 aci.name = admin.getClassName(); 156 157 // Note we don't set up queryBroadcastReceivers. We don't use it in DPMS. 158 159 doReturn(aci).when(mMockContext.ipackageManager).getReceiverInfo( 160 eq(admin), 161 anyInt(), 162 eq(UserHandle.getUserId(packageUid))); 163 164 // Set up getPackageInfo(). 165 markPackageAsInstalled(admin.getPackageName(), ai, UserHandle.getUserId(packageUid)); 166 } 167 } 168