1 /* 2 * Copyright (C) 2020 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.host.blob; 17 18 import static com.google.common.truth.Truth.assertWithMessage; 19 20 import com.android.tradefed.device.ITestDevice; 21 import com.android.tradefed.testtype.junit4.BaseHostJUnit4Test; 22 import com.android.tradefed.testtype.junit4.DeviceTestRunOptions; 23 import com.android.tradefed.util.Pair; 24 25 import java.util.Arrays; 26 import java.util.HashMap; 27 import java.util.Map; 28 import java.util.regex.Matcher; 29 import java.util.regex.Pattern; 30 import java.util.stream.Collectors; 31 32 abstract class BaseBlobStoreHostTest extends BaseHostJUnit4Test { 33 protected static final String TARGET_APK = "CtsBlobStoreHostTestHelper.apk"; 34 protected static final String TARGET_PKG = "com.android.cts.device.blob"; 35 36 protected static final String TARGET_APK_DEV = "CtsBlobStoreHostTestHelperDev.apk"; 37 protected static final String TARGET_PKG_DEV = "com.android.cts.device.blob.dev"; 38 39 protected static final String TARGET_APK_ASSIST = "CtsBlobStoreHostTestHelperAssist.apk"; 40 protected static final String TARGET_PKG_ASSIST = "com.android.cts.device.blob.assist"; 41 42 private static final long DEFAULT_INSTRUMENTATION_TIMEOUT_MS = 900_000; // 15min 43 44 protected static final String KEY_SESSION_ID = "session"; 45 46 protected static final String KEY_DIGEST = "digest"; 47 protected static final String KEY_EXPIRY = "expiry"; 48 protected static final String KEY_LABEL = "label"; 49 protected static final String KEY_TAG = "tag"; 50 51 protected static final String KEY_ALLOW_PUBLIC = "public"; 52 protected static final String KEY_ALLOW_SAME_SIGNATURE = "same_signature"; 53 runDeviceTest(String testPkg, String testClass, String testMethod)54 protected void runDeviceTest(String testPkg, String testClass, String testMethod) 55 throws Exception { 56 runDeviceTest(testPkg, testClass, testMethod, null); 57 } 58 runDeviceTestAsUser(String testPkg, String testClass, String testMethod, int userId)59 protected void runDeviceTestAsUser(String testPkg, String testClass, String testMethod, 60 int userId) throws Exception { 61 runDeviceTestAsUser(testPkg, testClass, testMethod, null, userId); 62 } 63 runDeviceTest(String testPkg, String testClass, String testMethod, Map<String, String> instrumentationArgs)64 protected void runDeviceTest(String testPkg, String testClass, String testMethod, 65 Map<String, String> instrumentationArgs) throws Exception { 66 runDeviceTestAsUser(testPkg, testClass, testMethod, instrumentationArgs, -1); 67 } 68 runDeviceTestAsUser(String testPkg, String testClass, String testMethod, Map<String, String> instrumentationArgs, int userId)69 protected void runDeviceTestAsUser(String testPkg, String testClass, String testMethod, 70 Map<String, String> instrumentationArgs, int userId) throws Exception { 71 final DeviceTestRunOptions deviceTestRunOptions = new DeviceTestRunOptions(testPkg) 72 .setTestClassName(testClass) 73 .setTestMethodName(testMethod) 74 .setMaxInstrumentationTimeoutMs(DEFAULT_INSTRUMENTATION_TIMEOUT_MS); 75 if (userId != -1) { 76 deviceTestRunOptions.setUserId(userId); 77 } 78 if (instrumentationArgs != null) { 79 for (Map.Entry<String, String> entry : instrumentationArgs.entrySet()) { 80 deviceTestRunOptions.addInstrumentationArg(entry.getKey(), entry.getValue()); 81 } 82 } 83 assertWithMessage(testMethod + " failed").that( 84 runDeviceTests(deviceTestRunOptions)).isTrue(); 85 } 86 getDeviceTimeMs()87 protected long getDeviceTimeMs() throws Exception { 88 final String timeMs = getDevice().executeShellCommand("date +%s%3N"); 89 return Long.parseLong(timeMs.trim()); 90 } 91 rebootAndWaitUntilReady()92 protected void rebootAndWaitUntilReady() throws Exception { 93 // TODO: use rebootUserspace() 94 getDevice().reboot(); // reboot() waits for device available 95 } 96 isMultiUserSupported(ITestDevice device)97 protected static boolean isMultiUserSupported(ITestDevice device) throws Exception { 98 return device.isMultiUserSupported(); 99 } 100 createArgsFromLastTestRun()101 protected Map<String, String> createArgsFromLastTestRun() { 102 final Map<String, String> args = new HashMap<>(); 103 for (String key : new String[] { 104 KEY_SESSION_ID, 105 KEY_DIGEST, 106 KEY_EXPIRY, 107 KEY_LABEL, 108 KEY_TAG 109 }) { 110 final String value = getLastDeviceRunResults().getRunMetrics().get(key); 111 if (value != null) { 112 args.put(key, value); 113 } 114 } 115 return args; 116 } 117 createArgs(Pair<String, String>.... keyValues)118 protected Map<String, String> createArgs(Pair<String, String>... keyValues) { 119 return Arrays.stream(keyValues).collect(Collectors.toMap(p -> p.first, p -> p.second)); 120 } 121 getAppUid(String pkgName)122 protected int getAppUid(String pkgName) throws Exception { 123 final int currentUser = getDevice().getCurrentUser(); 124 final String uidLine = getDevice().executeShellCommand( 125 "cmd package list packages -U --user " + currentUser + " " + pkgName); 126 final Pattern pattern = Pattern.compile("package:" + pkgName + " uid:(\\d+)"); 127 final Matcher matcher = pattern.matcher(uidLine); 128 assertWithMessage("Pkg not found: " + pkgName).that(matcher.find()).isTrue(); 129 final int appUid = Integer.parseInt(matcher.group(1)); 130 return appUid; 131 } 132 addAssistRoleHolder(String pkgName, int userId)133 protected void addAssistRoleHolder(String pkgName, int userId) throws Exception { 134 final String cmd = String.format("cmd role add-role-holder " 135 + "--user %d android.app.role.ASSISTANT %s", userId, pkgName); 136 getDevice().executeShellCommand(cmd).trim(); 137 } 138 removeAssistRoleHolder(String pkgName, int userId)139 protected void removeAssistRoleHolder(String pkgName, int userId) throws Exception { 140 final String cmd = String.format("cmd role remove-role-holder " 141 + "--user %d android.app.role.ASSISTANT %s", userId, pkgName); 142 getDevice().executeShellCommand(cmd).trim(); 143 } 144 }