• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
testSetAffiliationId1()149     public void testSetAffiliationId1() {
150         setAffiliationId("id.number.1");
151     }
152 
153     @Test
testTransferOwnershipBundleSaved()154     public void testTransferOwnershipBundleSaved() throws Throwable {
155         PersistableBundle b = new PersistableBundle();
156         b.putBoolean(ARE_PARAMETERS_SAVED, true);
157         mDevicePolicyManager.transferOwnership(mOutgoingComponentName, INCOMING_COMPONENT_NAME, b);
158     }
159 
160     @Test
testGetTransferOwnershipBundleOnlyCalledFromAdmin()161     public void testGetTransferOwnershipBundleOnlyCalledFromAdmin() throws Throwable {
162         PersistableBundle b = new PersistableBundle();
163         b.putBoolean(ARE_PARAMETERS_SAVED, true);
164         mDevicePolicyManager.transferOwnership(mOutgoingComponentName, INCOMING_COMPONENT_NAME, b);
165         assertThrows(SecurityException.class, mDevicePolicyManager::getTransferOwnershipBundle);
166     }
167 
168     @Test
testIsBundleNullNoTransfer()169     public void testIsBundleNullNoTransfer() throws Throwable {
170         assertNull(mDevicePolicyManager.getTransferOwnershipBundle());
171     }
172 
setUserRestriction(String restriction, boolean add)173     private void setUserRestriction(String restriction, boolean add) {
174         DevicePolicyManager dpm = mContext.getSystemService(DevicePolicyManager.class);
175         if (add) {
176             dpm.addUserRestriction(mOutgoingComponentName, restriction);
177         } else {
178             dpm.clearUserRestriction(mOutgoingComponentName, restriction);
179         }
180     }
181 
setAffiliationId(String id)182     private void setAffiliationId(String id) {
183         ComponentName admin = mOutgoingComponentName;
184         DevicePolicyManager dpm = (DevicePolicyManager)
185                 mContext.getSystemService(Context.DEVICE_POLICY_SERVICE);
186         Set<String> ids = Collections.singleton(id);
187         dpm.setAffiliationIds(admin, ids);
188         assertEquals(ids, dpm.getAffiliationIds(admin));
189     }
190 
getPrefs(Context context)191     private static SharedPreferences getPrefs(Context context) {
192         return context.getSharedPreferences(SHARED_PREFERENCE_NAME, Context.MODE_PRIVATE);
193     }
194 
putBooleanPref(Context context, String key, boolean value)195     private static void putBooleanPref(Context context, String key, boolean value) {
196         getPrefs(context).edit().putBoolean(key, value).apply();
197     }
198 
getBooleanPref(Context context, String key)199     protected static boolean getBooleanPref(Context context, String key) {
200         return getPrefs(context).getBoolean(key, false);
201     }
202 }
203