Home | History | Annotate | Download | only in multiuser
      1 /*
      2  * Copyright (C) 2016 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 android.host.multiuser;
     17 
     18 import com.android.tradefed.device.DeviceNotAvailableException;
     19 import com.android.tradefed.device.ITestDevice;
     20 import com.android.tradefed.log.LogUtil.CLog;
     21 import com.android.tradefed.testtype.IDeviceTest;
     22 
     23 import org.junit.After;
     24 import org.junit.Before;
     25 
     26 import java.util.ArrayList;
     27 
     28 /**
     29  * Base class for multi user tests.
     30  */
     31 public class BaseMultiUserTest implements IDeviceTest {
     32     protected static final int USER_SYSTEM = 0; // From the UserHandle class.
     33 
     34     /** Whether multi-user is supported. */
     35     protected boolean mSupportsMultiUser;
     36     protected boolean mIsSplitSystemUser;
     37     protected int mPrimaryUserId;
     38     /** Users we shouldn't delete in the tests */
     39     private ArrayList<Integer> mFixedUsers;
     40 
     41     private ITestDevice mDevice;
     42 
     43     @Before
     44     public void setUp() throws Exception {
     45         mSupportsMultiUser = getDevice().getMaxNumberOfUsersSupported() > 1;
     46         mIsSplitSystemUser = checkIfSplitSystemUser();
     47         mPrimaryUserId = getDevice().getPrimaryUserId();
     48         mFixedUsers = new ArrayList<>();
     49         mFixedUsers.add(mPrimaryUserId);
     50         if (mPrimaryUserId != USER_SYSTEM) {
     51             mFixedUsers.add(USER_SYSTEM);
     52         }
     53         getDevice().switchUser(mPrimaryUserId);
     54         removeTestUsers();
     55     }
     56 
     57     @After
     58     public void tearDown() throws Exception {
     59         if (getDevice().getCurrentUser() != mPrimaryUserId) {
     60             CLog.w("User changed during test. Switching back to " + mPrimaryUserId);
     61             getDevice().switchUser(mPrimaryUserId);
     62         }
     63         removeTestUsers();
     64     }
     65 
     66     @Override
     67     public void setDevice(ITestDevice device) {
     68         mDevice = device;
     69     }
     70 
     71     @Override
     72     public ITestDevice getDevice() {
     73         return mDevice;
     74     }
     75 
     76     protected int createRestrictedProfile(int userId)
     77             throws DeviceNotAvailableException, IllegalStateException{
     78         final String command = "pm create-user --profileOf " + userId + " --restricted "
     79                 + "TestUser_" + System.currentTimeMillis();
     80         CLog.d("Starting command: " + command);
     81         final String output = getDevice().executeShellCommand(command);
     82         CLog.d("Output for command " + command + ": " + output);
     83 
     84         if (output.startsWith("Success")) {
     85             try {
     86                 return Integer.parseInt(output.substring(output.lastIndexOf(" ")).trim());
     87             } catch (NumberFormatException e) {
     88                 CLog.e("Failed to parse result: %s", output);
     89             }
     90         } else {
     91             CLog.e("Failed to create restricted profile: %s", output);
     92         }
     93         throw new IllegalStateException();
     94     }
     95 
     96     /**
     97      * @return the userid of the created user
     98      */
     99     protected int createUser()
    100             throws DeviceNotAvailableException, IllegalStateException {
    101         final String command = "pm create-user "
    102                 + "TestUser_" + System.currentTimeMillis();
    103         CLog.d("Starting command: " + command);
    104         final String output = getDevice().executeShellCommand(command);
    105         CLog.d("Output for command " + command + ": " + output);
    106 
    107         if (output.startsWith("Success")) {
    108             try {
    109                 return Integer.parseInt(output.substring(output.lastIndexOf(" ")).trim());
    110             } catch (NumberFormatException e) {
    111                 CLog.e("Failed to parse result: %s", output);
    112             }
    113         } else {
    114             CLog.e("Failed to create user: %s", output);
    115         }
    116         throw new IllegalStateException();
    117     }
    118 
    119     private void removeTestUsers() throws Exception {
    120         for (int userId : getDevice().listUsers()) {
    121             if (!mFixedUsers.contains(userId)) {
    122                 getDevice().removeUser(userId);
    123             }
    124         }
    125     }
    126 
    127     private boolean checkIfSplitSystemUser() throws DeviceNotAvailableException {
    128         final String commandOuput = getDevice().executeShellCommand(
    129                 "getprop ro.fw.system_user_split");
    130         return "y".equals(commandOuput) || "yes".equals(commandOuput)
    131                 || "1".equals(commandOuput) || "true".equals(commandOuput)
    132                 || "on".equals(commandOuput);
    133     }
    134 }