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 17 package android.appsecurity.cts; 18 19 import com.android.ddmlib.Log.LogLevel; 20 import com.android.tradefed.build.IBuildInfo; 21 import com.android.tradefed.device.DeviceNotAvailableException; 22 import com.android.tradefed.device.ITestDevice; 23 import com.android.tradefed.log.ITestLogger; 24 import com.android.tradefed.log.LogUtil.CLog; 25 import com.android.tradefed.result.ITestLoggerReceiver; 26 import com.android.tradefed.result.InputStreamSource; 27 import com.android.tradefed.result.LogDataType; 28 import com.android.tradefed.targetprep.BuildError; 29 import com.android.tradefed.targetprep.ITargetCleaner; 30 import com.android.tradefed.targetprep.ITargetPreparer; 31 import com.android.tradefed.targetprep.TargetSetupError; 32 33 /** 34 * Creates secondary and tertiary users for use during a test suite. 35 */ 36 public class AppSecurityPreparer implements ITargetPreparer, ITargetCleaner, ITestLoggerReceiver { 37 38 private ITestLogger mLogger; 39 40 @Override setUp(ITestDevice device, IBuildInfo buildInfo)41 public void setUp(ITestDevice device, IBuildInfo buildInfo) 42 throws TargetSetupError, BuildError, DeviceNotAvailableException { 43 // Clean up any lingering users from other tests to ensure that we have 44 // best shot at creating the users we need below. 45 removeSecondaryUsers(device); 46 47 final int maxUsers = device.getMaxNumberOfUsersSupported(); 48 try { 49 if (maxUsers > 1) { 50 CLog.logAndDisplay(LogLevel.INFO, 51 "Created secondary user " + device.createUser("CTS_" + System.nanoTime())); 52 } 53 if (maxUsers > 2) { 54 CLog.logAndDisplay(LogLevel.INFO, 55 "Created secondary user " + device.createUser("CTS_" + System.nanoTime())); 56 } 57 } catch (IllegalStateException e) { 58 try (InputStreamSource logcat = device.getLogcatDump()) { 59 mLogger.testLog("AppSecurityPrep_failed_create_user", LogDataType.LOGCAT, logcat); 60 } 61 throw new TargetSetupError("Failed to create user.", e, device.getDeviceDescriptor()); 62 } 63 } 64 65 @Override tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable throwable)66 public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable throwable) 67 throws DeviceNotAvailableException { 68 removeSecondaryUsers(device); 69 } 70 removeSecondaryUsers(ITestDevice device)71 private void removeSecondaryUsers(ITestDevice device) throws DeviceNotAvailableException { 72 final int[] userIds = Utils.getAllUsers(device); 73 for (int i = 1; i < userIds.length; i++) { 74 if (device.getCurrentUser() != userIds[i]) { 75 device.removeUser(userIds[i]); 76 CLog.logAndDisplay(LogLevel.INFO, "Destroyed secondary user " + userIds[i]); 77 } 78 } 79 } 80 81 @Override setTestLogger(ITestLogger testLogger)82 public void setTestLogger(ITestLogger testLogger) { 83 mLogger = testLogger; 84 } 85 } 86