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