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.tradefed.device.DeviceNotAvailableException; 20 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 21 22 import org.junit.Assert; 23 import org.junit.Before; 24 25 import java.util.ArrayList; 26 import java.util.HashMap; 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 setUpBaseAppSecurityTest()41 public void setUpBaseAppSecurityTest() throws Exception { 42 Assert.assertNotNull(getBuild()); // ensure build has been set before test is run. 43 44 mSupportsMultiUser = getDevice().getMaxNumberOfUsersSupported() > 1; 45 mPrimaryUserId = getDevice().getPrimaryUserId(); 46 mFixedUsers = new ArrayList<>(); 47 mFixedUsers.add(mPrimaryUserId); 48 if (mPrimaryUserId != Utils.USER_SYSTEM) { 49 mFixedUsers.add(Utils.USER_SYSTEM); 50 } 51 } 52 installTestAppForUser(String apk, int userId)53 protected void installTestAppForUser(String apk, int userId) throws Exception { 54 installTestAppForUser(apk, false, userId); 55 } 56 installTestAppForUser(String apk, boolean instant, int userId)57 protected void installTestAppForUser(String apk, boolean instant, int userId) throws Exception { 58 if (userId < 0) { 59 userId = mPrimaryUserId; 60 } 61 new InstallMultiple(instant) 62 .addApk(apk) 63 .allowTest() 64 .forUser(userId) 65 .run(); 66 } 67 68 // TODO: We should be able to set test arguments from the BaseHostJUnit4Test methods runDeviceTests(String packageName, String testClassName, String testMethodName, boolean instant)69 protected void runDeviceTests(String packageName, String testClassName, 70 String testMethodName, boolean instant) throws DeviceNotAvailableException { 71 final HashMap<String, String> testArgs; 72 if (instant) { 73 testArgs = new HashMap<>(); 74 testArgs.put("is_instant", Boolean.TRUE.toString()); 75 } else { 76 testArgs = null; 77 } 78 Utils.runDeviceTestsAsCurrentUser(getDevice(), packageName, testClassName, testMethodName, 79 testArgs); 80 } 81 isAppVisibleForUser(String packageName, int userId, boolean matchUninstalled)82 protected boolean isAppVisibleForUser(String packageName, int userId, 83 boolean matchUninstalled) throws Exception { 84 String command = "cmd package list packages --user " + userId; 85 if (matchUninstalled) command += " -u"; 86 String output = getDevice().executeShellCommand(command); 87 return output.contains(packageName); 88 } 89 90 protected class InstallMultiple extends BaseInstallMultiple<InstallMultiple> { InstallMultiple()91 public InstallMultiple() { 92 this(false); 93 } InstallMultiple(boolean instant)94 public InstallMultiple(boolean instant) { 95 super(getDevice(), getBuild(), getAbi()); 96 addArg(instant ? "--instant" : ""); 97 } 98 } 99 } 100