1 /* 2 * Copyright (C) 2014 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 android.app.admin.DevicePolicyManager; 19 import android.content.ComponentName; 20 import android.content.Context; 21 import android.os.Process; 22 import android.os.UserHandle; 23 import android.os.UserManager; 24 25 import com.android.cts.managedprofile.BaseManagedProfileTest.BasicAdminReceiver; 26 27 import org.junit.Ignore; 28 29 /** 30 * Test wipeData() for use in managed profile. If called from a managed profile, wipeData() should 31 * remove the current managed profile. Also, no erasing of external storage should be allowed. 32 */ 33 public class WipeDataTest extends BaseManagedProfileTest { 34 35 private UserManager mUserManager; 36 37 @Override 38 protected void setUp() throws Exception { 39 super.setUp(); 40 // Make sure we are running in a managed profile, otherwise risk wiping the primary user's 41 // data. 42 assertTrue(mDevicePolicyManager.isAdminActive(ADMIN_RECEIVER_COMPONENT)); 43 assertTrue(mDevicePolicyManager.isProfileOwnerApp(ADMIN_RECEIVER_COMPONENT.getPackageName())); 44 mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); 45 } 46 47 public void testWipeData() throws InterruptedException { 48 try { 49 mDevicePolicyManager.wipeData(DevicePolicyManager.WIPE_EXTERNAL_STORAGE); 50 fail("Should not be able to wipe external storage from managed profile."); 51 } catch (SecurityException expected) { 52 } 53 54 UserHandle currentUser = Process.myUserHandle(); 55 assertTrue(mUserManager.getUserProfiles().contains(currentUser)); 56 57 mDevicePolicyManager.wipeData(0); 58 59 // ACTION_MANAGED_PROFILE_REMOVED is only sent to parent user. 60 // As a result, we have to poll in order to know when the profile 61 // is actually removed. 62 long epoch = System.currentTimeMillis(); 63 while (System.currentTimeMillis() - epoch <= 10 * 1000) { 64 if (!mUserManager.getUserProfiles().contains(currentUser)) { 65 break; 66 } 67 Thread.sleep(250); 68 } 69 70 // Verify the profile is deleted 71 assertFalse(mUserManager.getUserProfiles().contains(currentUser)); 72 } 73 74 // Override this test inherited from base class, as it will trigger another round of setUp() 75 // which would fail because the managed profile has been removed by this test. 76 @Override 77 @Ignore 78 public void testAndroidTestCaseSetupProperly() { 79 } 80 } 81