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