1 /* 2 * Copyright (C) 2017 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 package com.android.cts.transferowner; 17 18 import static junit.framework.Assert.assertNotNull; 19 20 import static org.junit.Assert.assertEquals; 21 import static org.testng.Assert.assertNull; 22 import static org.testng.Assert.assertThrows; 23 24 import android.app.admin.DeviceAdminReceiver; 25 import android.app.admin.DevicePolicyManager; 26 import android.content.ComponentName; 27 import android.content.Context; 28 import android.content.Intent; 29 import android.content.SharedPreferences; 30 import android.os.PersistableBundle; 31 import android.os.UserHandle; 32 import android.os.UserManager; 33 34 import androidx.test.InstrumentationRegistry; 35 36 import com.android.compatibility.common.util.BlockingBroadcastReceiver; 37 38 import org.junit.Before; 39 import org.junit.Test; 40 41 import java.util.Collections; 42 import java.util.Set; 43 44 public abstract class DeviceAndProfileOwnerTransferOutgoingTest { 45 public static class BasicAdminReceiver extends DeviceAdminReceiver { BasicAdminReceiver()46 public BasicAdminReceiver() {} 47 48 @Override onTransferAffiliatedProfileOwnershipComplete(Context context, UserHandle user)49 public void onTransferAffiliatedProfileOwnershipComplete(Context context, UserHandle user) { 50 putBooleanPref(context, KEY_TRANSFER_AFFILIATED_PROFILE_COMPLETED_CALLED, true); 51 } 52 } 53 54 private static final String TRANSFER_OWNER_INCOMING_PKG = 55 "com.android.cts.transferownerincoming"; 56 private static final String TRANSFER_OWNER_INCOMING_TEST_RECEIVER_CLASS = 57 "com.android.cts.transferowner.DeviceAndProfileOwnerTransferIncomingTest" 58 + "$BasicAdminReceiver"; 59 private static final String TRANSFER_OWNER_INCOMING_TEST_RECEIVER_NO_METADATA_CLASS = 60 "com.android.cts.transferowner.DeviceAndProfileOwnerTransferIncomingTest" 61 + "$BasicAdminReceiverNoMetadata"; 62 private static final String ARE_PARAMETERS_SAVED = "ARE_PARAMETERS_SAVED"; 63 static final ComponentName INCOMING_COMPONENT_NAME = 64 new ComponentName( 65 TRANSFER_OWNER_INCOMING_PKG, TRANSFER_OWNER_INCOMING_TEST_RECEIVER_CLASS); 66 static final ComponentName INCOMING_NO_METADATA_COMPONENT_NAME = 67 new ComponentName(TRANSFER_OWNER_INCOMING_PKG, 68 TRANSFER_OWNER_INCOMING_TEST_RECEIVER_NO_METADATA_CLASS); 69 private static final ComponentName INVALID_TARGET_COMPONENT = 70 new ComponentName("com.android.cts.intent.receiver", ".BroadcastIntentReceiver"); 71 private final static String SHARED_PREFERENCE_NAME = "shared-preference-name"; 72 static final String KEY_TRANSFER_AFFILIATED_PROFILE_COMPLETED_CALLED = 73 "key-transfer-affiliated-completed-called"; 74 75 protected DevicePolicyManager mDevicePolicyManager; 76 protected ComponentName mOutgoingComponentName; 77 protected Context mContext; 78 private String mOwnerChangedBroadcastAction; 79 80 @Before setUp()81 public void setUp() throws Exception { 82 mContext = InstrumentationRegistry.getTargetContext(); 83 mDevicePolicyManager = mContext.getSystemService(DevicePolicyManager.class); 84 mOutgoingComponentName = new ComponentName(mContext, BasicAdminReceiver.class.getName()); 85 } 86 setupTestParameters(String ownerChangedBroadcastAction)87 protected final void setupTestParameters(String ownerChangedBroadcastAction) { 88 mOwnerChangedBroadcastAction = ownerChangedBroadcastAction; 89 } 90 91 @Test testTransferSameAdmin()92 public void testTransferSameAdmin() { 93 PersistableBundle b = new PersistableBundle(); 94 assertThrows( 95 IllegalArgumentException.class, 96 () -> { 97 mDevicePolicyManager.transferOwnership( 98 mOutgoingComponentName, mOutgoingComponentName, b); 99 }); 100 } 101 102 @Test testTransferInvalidTarget()103 public void testTransferInvalidTarget() { 104 PersistableBundle b = new PersistableBundle(); 105 assertThrows( 106 IllegalArgumentException.class, 107 () -> { 108 mDevicePolicyManager.transferOwnership(mOutgoingComponentName, 109 INVALID_TARGET_COMPONENT, b); 110 }); 111 } 112 113 @Test testTransferOwnershipChangedBroadcast()114 public void testTransferOwnershipChangedBroadcast() throws Throwable { 115 BlockingBroadcastReceiver receiver = new BlockingBroadcastReceiver(mContext, 116 mOwnerChangedBroadcastAction); 117 try { 118 receiver.register(); 119 PersistableBundle b = new PersistableBundle(); 120 mDevicePolicyManager.transferOwnership(mOutgoingComponentName, 121 INCOMING_COMPONENT_NAME, b); 122 Intent intent = receiver.awaitForBroadcast(); 123 assertNotNull(intent); 124 } finally { 125 receiver.unregisterQuietly(); 126 } 127 } 128 testTransferOwnership()129 abstract public void testTransferOwnership() throws Throwable; 130 131 @Test testTransferOwnershipNullBundle()132 public void testTransferOwnershipNullBundle() throws Throwable { 133 mDevicePolicyManager.transferOwnership(mOutgoingComponentName, 134 INCOMING_COMPONENT_NAME, null); 135 } 136 137 @Test testTransferOwnershipNoMetadata()138 public void testTransferOwnershipNoMetadata() throws Throwable { 139 PersistableBundle b = new PersistableBundle(); 140 assertThrows( 141 IllegalArgumentException.class, 142 () -> { 143 mDevicePolicyManager.transferOwnership(mOutgoingComponentName, 144 INCOMING_NO_METADATA_COMPONENT_NAME, b); 145 }); 146 } 147 148 @Test testClearDisallowAddManagedProfileRestriction()149 public void testClearDisallowAddManagedProfileRestriction() { 150 setUserRestriction(UserManager.DISALLOW_ADD_MANAGED_PROFILE, false); 151 } 152 153 @Test testAddDisallowAddManagedProfileRestriction()154 public void testAddDisallowAddManagedProfileRestriction() { 155 setUserRestriction(UserManager.DISALLOW_REMOVE_MANAGED_PROFILE, true); 156 } 157 158 @Test testSetAffiliationId1()159 public void testSetAffiliationId1() { 160 setAffiliationId("id.number.1"); 161 } 162 163 @Test testTransferOwnershipBundleSaved()164 public void testTransferOwnershipBundleSaved() throws Throwable { 165 PersistableBundle b = new PersistableBundle(); 166 b.putBoolean(ARE_PARAMETERS_SAVED, true); 167 mDevicePolicyManager.transferOwnership(mOutgoingComponentName, INCOMING_COMPONENT_NAME, b); 168 } 169 170 @Test testGetTransferOwnershipBundleOnlyCalledFromAdmin()171 public void testGetTransferOwnershipBundleOnlyCalledFromAdmin() throws Throwable { 172 PersistableBundle b = new PersistableBundle(); 173 b.putBoolean(ARE_PARAMETERS_SAVED, true); 174 mDevicePolicyManager.transferOwnership(mOutgoingComponentName, INCOMING_COMPONENT_NAME, b); 175 assertThrows(SecurityException.class, mDevicePolicyManager::getTransferOwnershipBundle); 176 } 177 178 @Test testIsBundleNullNoTransfer()179 public void testIsBundleNullNoTransfer() throws Throwable { 180 assertNull(mDevicePolicyManager.getTransferOwnershipBundle()); 181 } 182 setUserRestriction(String restriction, boolean add)183 private void setUserRestriction(String restriction, boolean add) { 184 DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); 185 if (add) { 186 dpm.addUserRestriction(mOutgoingComponentName, restriction); 187 } else { 188 dpm.clearUserRestriction(mOutgoingComponentName, restriction); 189 } 190 } 191 setAffiliationId(String id)192 private void setAffiliationId(String id) { 193 ComponentName admin = mOutgoingComponentName; 194 DevicePolicyManager dpm = (DevicePolicyManager) 195 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); 196 Set<String> ids = Collections.singleton(id); 197 dpm.setAffiliationIds(admin, ids); 198 assertEquals(ids, dpm.getAffiliationIds(admin)); 199 } 200 getPrefs(Context context)201 private static SharedPreferences getPrefs(Context context) { 202 return context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); 203 } 204 putBooleanPref(Context context, String key, boolean value)205 private static void putBooleanPref(Context context, String key, boolean value) { 206 getPrefs(context).edit().putBoolean(key, value).apply(); 207 } 208 getBooleanPref(Context context, String key)209 protected static boolean getBooleanPref(Context context, String key) { 210 return getPrefs(context).getBoolean(key, false); 211 } 212 } 213