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 testTransferOwnershipChangedBroadcast()103 public void testTransferOwnershipChangedBroadcast() throws Throwable { 104 BlockingBroadcastReceiver receiver = new BlockingBroadcastReceiver(mContext, 105 mOwnerChangedBroadcastAction); 106 try { 107 receiver.register(); 108 PersistableBundle b = new PersistableBundle(); 109 mDevicePolicyManager.transferOwnership(mOutgoingComponentName, 110 INCOMING_COMPONENT_NAME, b); 111 Intent intent = receiver.awaitForBroadcast(); 112 assertNotNull(intent); 113 } finally { 114 receiver.unregisterQuietly(); 115 } 116 } 117 testTransferOwnership()118 abstract public void testTransferOwnership() throws Throwable; 119 120 @Test testTransferOwnershipNoMetadata()121 public void testTransferOwnershipNoMetadata() throws Throwable { 122 PersistableBundle b = new PersistableBundle(); 123 assertThrows( 124 IllegalArgumentException.class, 125 () -> { 126 mDevicePolicyManager.transferOwnership(mOutgoingComponentName, 127 INCOMING_NO_METADATA_COMPONENT_NAME, b); 128 }); 129 } 130 131 @Test testSetAffiliationId1()132 public void testSetAffiliationId1() { 133 setAffiliationId("id.number.1"); 134 } 135 136 @Test testIsBundleNullNoTransfer()137 public void testIsBundleNullNoTransfer() throws Throwable { 138 assertNull(mDevicePolicyManager.getTransferOwnershipBundle()); 139 } 140 setUserRestriction(String restriction, boolean add)141 private void setUserRestriction(String restriction, boolean add) { 142 DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class); 143 if (add) { 144 dpm.addUserRestriction(mOutgoingComponentName, restriction); 145 } else { 146 dpm.clearUserRestriction(mOutgoingComponentName, restriction); 147 } 148 } 149 setAffiliationId(String id)150 private void setAffiliationId(String id) { 151 ComponentName admin = mOutgoingComponentName; 152 DevicePolicyManager dpm = (DevicePolicyManager) 153 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE); 154 Set<String> ids = Collections.singleton(id); 155 dpm.setAffiliationIds(admin, ids); 156 assertEquals(ids, dpm.getAffiliationIds(admin)); 157 } 158 getPrefs(Context context)159 private static SharedPreferences getPrefs(Context context) { 160 return context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE); 161 } 162 putBooleanPref(Context context, String key, boolean value)163 private static void putBooleanPref(Context context, String key, boolean value) { 164 getPrefs(context).edit().putBoolean(key, value).apply(); 165 } 166 getBooleanPref(Context context, String key)167 protected static boolean getBooleanPref(Context context, String key) { 168 return getPrefs(context).getBoolean(key, false); 169 } 170 } 171