• 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.managedprofile;
17 
18 import static android.app.admin.DevicePolicyManager.ACTION_PROVISION_MANAGED_PROFILE;
19 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE;
20 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE;
21 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME;
22 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION;
23 import static android.app.admin.DevicePolicyManager.EXTRA_PROVISIONING_SKIP_ENCRYPTION;
24 
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29 
30 import android.accounts.Account;
31 import android.accounts.AccountManager;
32 import android.app.admin.DeviceAdminReceiver;
33 import android.app.admin.DevicePolicyManager;
34 import android.content.ComponentName;
35 import android.content.Context;
36 import android.content.Intent;
37 import android.content.SharedPreferences;
38 import android.os.PersistableBundle;
39 import android.util.Log;
40 
41 import androidx.test.InstrumentationRegistry;
42 import androidx.test.filters.SmallTest;
43 
44 import com.android.compatibility.common.util.devicepolicy.provisioning.SilentProvisioningTestManager;
45 
46 import org.junit.Before;
47 import org.junit.Test;
48 
49 @SmallTest
50 public class ProvisioningTest {
51     private static final String TAG = ProvisioningTest.class.getSimpleName();
52 
53     private static final String SHARED_PREFERENCE_FILE_NAME = "shared-preferences-file-name";
54 
55     private static final PersistableBundle ADMIN_EXTRAS_BUNDLE = new PersistableBundle();
56     private static final String ADMIN_EXTRAS_BUNDLE_KEY_1 = "KEY_1";
57     private static final String ADMIN_EXTRAS_BUNDLE_VALUE_1 = "VALUE_1";
58     static {
ADMIN_EXTRAS_BUNDLE.putString(ADMIN_EXTRAS_BUNDLE_KEY_1, ADMIN_EXTRAS_BUNDLE_VALUE_1)59         ADMIN_EXTRAS_BUNDLE.putString(ADMIN_EXTRAS_BUNDLE_KEY_1, ADMIN_EXTRAS_BUNDLE_VALUE_1);
60     }
61 
62     public static final String KEY_PROVISIONING_SUCCESSFUL_RECEIVED =
63             "key-provisioning-successful-received";
64 
65     private static final ComponentName ADMIN_RECEIVER_COMPONENT = new ComponentName(
66             ProvisioningAdminReceiver.class.getPackage().getName(),
67             ProvisioningAdminReceiver.class.getName());
68 
69     public static class ProvisioningAdminReceiver extends DeviceAdminReceiver {
70         @Override
onProfileProvisioningComplete(Context context, Intent intent)71         public void onProfileProvisioningComplete(Context context, Intent intent) {
72             super.onProfileProvisioningComplete(context, intent);
73             // Enabled profile
74             getManager(context).setProfileName(ADMIN_RECEIVER_COMPONENT, "Managed Profile");
75             getManager(context).setProfileEnabled(ADMIN_RECEIVER_COMPONENT);
76             Log.i(TAG, "onProfileProvisioningComplete");
77 
78             saveBundle(context, intent.getParcelableExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE));
79         }
80     }
81 
82     private Context mContext;
83     private DevicePolicyManager mDpm;
84 
85     @Before
setUp()86     public void setUp() {
87         mContext = InstrumentationRegistry.getTargetContext();
88         mDpm = mContext.getSystemService(DevicePolicyManager.class);
89     }
90 
91     @Test
testIsManagedProfile()92     public void testIsManagedProfile() {
93         assertTrue(mDpm.isManagedProfile(ADMIN_RECEIVER_COMPONENT));
94         Log.i(TAG, "managed profile app: " + ADMIN_RECEIVER_COMPONENT.getPackageName());
95     }
96 
97     @Test
testProvisionManagedProfile()98     public void testProvisionManagedProfile() throws InterruptedException {
99         provisionManagedProfile(createBaseProvisioningIntent());
100     }
101 
102     @Test
testProvisionManagedProfile_accountCopy()103     public void testProvisionManagedProfile_accountCopy() throws InterruptedException {
104         provisionManagedProfile(createBaseProvisioningIntent()
105                 .putExtra(EXTRA_PROVISIONING_KEEP_ACCOUNT_ON_MIGRATION, true));
106     }
107 
108     @Test
testVerifyAdminExtraBundle()109     public void testVerifyAdminExtraBundle() {
110         PersistableBundle bundle = loadBundle(mContext);
111         assertNotNull(bundle);
112         assertEquals(ADMIN_EXTRAS_BUNDLE_VALUE_1, bundle.getString(ADMIN_EXTRAS_BUNDLE_KEY_1));
113     }
114 
115     @Test
testVerifySuccessfulIntentWasReceived()116     public void testVerifySuccessfulIntentWasReceived() {
117         assertTrue(getSharedPreferences(mContext).getBoolean(KEY_PROVISIONING_SUCCESSFUL_RECEIVED,
118                 false));
119     }
120 
121     @Test
testAccountExist()122     public void testAccountExist() {
123         AccountManager am = AccountManager.get(mContext);
124         for (Account account : am.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE)) {
125             if (AccountAuthenticator.TEST_ACCOUNT.equals(account)) {
126                 return;
127             }
128         }
129         fail("can't find migrated account");
130     }
131 
132     @Test
testAccountNotExist()133     public void testAccountNotExist() {
134         AccountManager am = AccountManager.get(mContext);
135         assertTrue("test account still exists after account migration",
136                 am.getAccountsByType(AccountAuthenticator.ACCOUNT_TYPE).length == 0);
137     }
138 
createBaseProvisioningIntent()139     private Intent createBaseProvisioningIntent() {
140         return new Intent(ACTION_PROVISION_MANAGED_PROFILE)
141                 .putExtra(EXTRA_PROVISIONING_DEVICE_ADMIN_COMPONENT_NAME, ADMIN_RECEIVER_COMPONENT)
142                 .putExtra(EXTRA_PROVISIONING_SKIP_ENCRYPTION, true)
143                 .putExtra(EXTRA_PROVISIONING_ADMIN_EXTRAS_BUNDLE, ADMIN_EXTRAS_BUNDLE)
144                 .putExtra(EXTRA_PROVISIONING_ACCOUNT_TO_MIGRATE, addAndGetTestAccount());
145     }
146 
provisionManagedProfile(Intent intent)147     private void provisionManagedProfile(Intent intent) throws InterruptedException {
148         SilentProvisioningTestManager provisioningManager = new SilentProvisioningTestManager(mContext);
149         assertTrue(provisioningManager.startProvisioningAndWait(intent));
150         Log.i(TAG, "managed profile provisioning successful");
151     }
152 
addAndGetTestAccount()153     private Account addAndGetTestAccount() {
154         Account account = AccountAuthenticator.TEST_ACCOUNT;
155         AccountManager.get(mContext).addAccountExplicitly(account, null, null);
156         return account;
157     }
158 
saveBundle(Context context, PersistableBundle bundle)159     private static void saveBundle(Context context, PersistableBundle bundle) {
160         if (bundle == null) {
161             Log.e(TAG, "null saveBundle");
162             return;
163         }
164 
165         getSharedPreferences(context).edit()
166                 .putString(ADMIN_EXTRAS_BUNDLE_KEY_1, bundle.getString(ADMIN_EXTRAS_BUNDLE_KEY_1))
167                 .commit();
168     }
169 
loadBundle(Context context)170     private static PersistableBundle loadBundle(Context context) {
171         SharedPreferences pref = getSharedPreferences(context);
172         PersistableBundle bundle = new PersistableBundle();
173         bundle.putString(ADMIN_EXTRAS_BUNDLE_KEY_1,
174                 pref.getString(ADMIN_EXTRAS_BUNDLE_KEY_1, null));
175         return bundle;
176     }
177 
getSharedPreferences(Context context)178     public static SharedPreferences getSharedPreferences(Context context) {
179         return context.getSharedPreferences(SHARED_PREFERENCE_FILE_NAME, 0);
180     }
181 
182 }
183