Home | History | Annotate | Download | only in cts
      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 
     17 package android.appsecurity.cts;
     18 
     19 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
     20 import com.android.tradefed.device.DeviceNotAvailableException;
     21 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test;
     22 
     23 import org.junit.Assert;
     24 import org.junit.Before;
     25 
     26 import java.util.ArrayList;
     27 
     28 /**
     29  * Base class.
     30  */
     31 abstract class BaseAppSecurityTest extends BaseHostJUnit4Test {
     32 
     33     /** Whether multi-user is supported. */
     34     protected boolean mSupportsMultiUser;
     35     protected boolean mIsSplitSystemUser;
     36     protected int mPrimaryUserId;
     37     /** Users we shouldn't delete in the tests */
     38     private ArrayList<Integer> mFixedUsers;
     39 
     40     @Before
     41     public void setUp() throws Exception {
     42         Assert.assertNotNull(getBuild()); // ensure build has been set before test is run.
     43 
     44         mSupportsMultiUser = getDevice().getMaxNumberOfUsersSupported() > 1;
     45         mIsSplitSystemUser = checkIfSplitSystemUser();
     46         mPrimaryUserId = getDevice().getPrimaryUserId();
     47         mFixedUsers = new ArrayList<>();
     48         mFixedUsers.add(mPrimaryUserId);
     49         if (mPrimaryUserId != Utils.USER_SYSTEM) {
     50             mFixedUsers.add(Utils.USER_SYSTEM);
     51         }
     52         getDevice().switchUser(mPrimaryUserId);
     53     }
     54 
     55     private boolean checkIfSplitSystemUser() throws DeviceNotAvailableException {
     56         final String commandOuput = getDevice().executeShellCommand(
     57                 "getprop ro.fw.system_user_split");
     58         return "y".equals(commandOuput) || "yes".equals(commandOuput)
     59                 || "1".equals(commandOuput) || "true".equals(commandOuput)
     60                 || "on".equals(commandOuput);
     61     }
     62 
     63     protected void installTestAppForUser(String apk, int userId) throws Exception {
     64         if (userId < 0) {
     65             userId = mPrimaryUserId;
     66         }
     67         CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(getBuild());
     68         Assert.assertNull(getDevice().installPackageForUser(
     69                 buildHelper.getTestFile(apk), true, false, userId, "-t"));
     70     }
     71 
     72     protected boolean isAppVisibleForUser(String packageName, int userId,
     73             boolean matchUninstalled) throws DeviceNotAvailableException {
     74         String command = "cmd package list packages --user " + userId;
     75         if (matchUninstalled) command += " -u";
     76         String output = getDevice().executeShellCommand(command);
     77         return output.contains(packageName);
     78     }
     79 }
     80