1 /*
2  * Copyright (C) 2021 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.compat.testing;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 import static com.google.common.truth.Truth.assertWithMessage;
21 
22 import com.android.compatibility.common.tradefed.build.CompatibilityBuildHelper;
23 import com.android.ddmlib.testrunner.RemoteAndroidTestRunner;
24 import com.android.ddmlib.testrunner.TestResult.TestStatus;
25 import com.android.tradefed.build.IBuildInfo;
26 import com.android.tradefed.device.DeviceNotAvailableException;
27 import com.android.tradefed.device.INativeDevice;
28 import com.android.tradefed.device.ITestDevice;
29 import com.android.tradefed.result.CollectingTestListener;
30 import com.android.tradefed.result.TestDescription;
31 import com.android.tradefed.result.TestResult;
32 import com.android.tradefed.result.TestRunResult;
33 import com.android.tradefed.util.CommandResult;
34 import com.android.tradefed.util.CommandStatus;
35 import com.android.tradefed.util.FileUtil;
36 
37 import com.google.common.collect.ImmutableList;
38 import com.google.common.collect.ImmutableSet;
39 
40 import org.jf.dexlib2.DexFileFactory;
41 import org.jf.dexlib2.Opcodes;
42 import org.jf.dexlib2.dexbacked.DexBackedDexFile;
43 import org.jf.dexlib2.iface.ClassDef;
44 import org.jf.dexlib2.iface.MultiDexContainer;
45 
46 import java.io.File;
47 import java.io.FileNotFoundException;
48 import java.io.IOException;
49 import java.util.Map;
50 import java.util.Objects;
51 
52 /**
53  * Testing utilities for parsing *CLASSPATH environ variables and shared libs on a test device.
54  */
55 public final class Classpaths {
56 
Classpaths()57     private Classpaths() {
58     }
59 
60     public enum ClasspathType {
61         BOOTCLASSPATH,
62         DEX2OATBOOTCLASSPATH,
63         SYSTEMSERVERCLASSPATH,
64     }
65 
66     private static final String TEST_RUNNER = "androidx.test.runner.AndroidJUnitRunner";
67 
68     /** Returns on device filepaths to the jars that are part of a given classpath. */
getJarsOnClasspath(INativeDevice device, ClasspathType classpath)69     public static ImmutableList<String> getJarsOnClasspath(INativeDevice device,
70             ClasspathType classpath) throws DeviceNotAvailableException {
71         CommandResult shellResult = device.executeShellV2Command("echo $" + classpath);
72         assertThat(shellResult.getStatus()).isEqualTo(CommandStatus.SUCCESS);
73         assertThat(shellResult.getExitCode()).isEqualTo(0);
74 
75         String value = shellResult.getStdout().trim();
76         assertThat(value).isNotEmpty();
77         return ImmutableList.copyOf(value.split(":"));
78     }
79 
80     /** Returns {@link SharedLibraryInfo} about the shared libs available on the test device. */
getSharedLibraryInfos(ITestDevice device, IBuildInfo buildInfo)81     public static ImmutableList<SharedLibraryInfo> getSharedLibraryInfos(ITestDevice device,
82             IBuildInfo buildInfo) throws DeviceNotAvailableException, FileNotFoundException {
83         runDeviceTests(device, buildInfo, SharedLibraryInfo.HELPER_APP_APK,
84                 SharedLibraryInfo.HELPER_APP_PACKAGE, SharedLibraryInfo.HELPER_APP_CLASS);
85         String remoteFile = "/sdcard/shared-libs.txt";
86         String content;
87         try {
88             content = device.pullFileContents(remoteFile);
89         } finally {
90             device.deleteFile(remoteFile);
91         }
92         return SharedLibraryInfo.getSharedLibraryInfos(content);
93     }
94 
95     /** Returns classes defined a given jar file on the test device. */
getClassDefsFromJar(INativeDevice device, String remoteJarPath)96     public static ImmutableSet<ClassDef> getClassDefsFromJar(INativeDevice device,
97             String remoteJarPath) throws DeviceNotAvailableException, IOException {
98         File jar = null;
99         try {
100             jar = device.pullFile(remoteJarPath);
101             if (jar == null) {
102                 throw new IllegalStateException("could not pull remote file " + remoteJarPath);
103             }
104             return getClassDefsFromJar(jar);
105         } finally {
106             FileUtil.deleteFile(jar);
107         }
108     }
109 
110     /** Returns classes defined a given jar file on the test device. */
getClassDefsFromJar(File jar)111     public static ImmutableSet<ClassDef> getClassDefsFromJar(File jar) throws IOException {
112         MultiDexContainer<? extends DexBackedDexFile> container =
113                 DexFileFactory.loadDexContainer(jar, Opcodes.getDefault());
114         ImmutableSet.Builder<ClassDef> set = ImmutableSet.builder();
115         for (String dexName : container.getDexEntryNames()) {
116             set.addAll(Objects.requireNonNull(container.getEntry(dexName)).getClasses());
117         }
118         return set.build();
119     }
120 
runDeviceTests(ITestDevice device, IBuildInfo buildInfo, String apkName, String packageName, String className)121     private static void runDeviceTests(ITestDevice device, IBuildInfo buildInfo, String apkName,
122             String packageName, String className) throws DeviceNotAvailableException,
123             FileNotFoundException {
124         try {
125             final CompatibilityBuildHelper buildHelper = new CompatibilityBuildHelper(buildInfo);
126             final String installError = device.installPackage(buildHelper.getTestFile(apkName),
127                     false);
128             assertWithMessage("Failed to install %s due to: %s", apkName, installError).
129                     that(installError).isNull();
130             // Trigger helper app to collect and write info about shared libraries on the device.
131             final RemoteAndroidTestRunner testRunner = new RemoteAndroidTestRunner(packageName,
132                     TEST_RUNNER, device.getIDevice());
133             testRunner.setClassName(className);
134             final CollectingTestListener listener = new CollectingTestListener();
135             assertThat(device.runInstrumentationTests(testRunner, listener)).isTrue();
136             final TestRunResult result = listener.getCurrentRunResults();
137             assertWithMessage("Failed to successfully run device tests for " + result.getName()
138                     + ": " + result.getRunFailureMessage())
139                     .that(result.isRunFailure()).isFalse();
140             assertWithMessage("No tests were run!").that(result.getNumTests()).isGreaterThan(0);
141             StringBuilder errorBuilder = new StringBuilder("on-device tests failed:\n");
142             for (Map.Entry<TestDescription, TestResult> resultEntry :
143                     result.getTestResults().entrySet()) {
144                 if (!resultEntry.getValue().getStatus().equals(TestStatus.PASSED)) {
145                     errorBuilder.append(resultEntry.getKey().toString());
146                     errorBuilder.append(":\n");
147                     errorBuilder.append(resultEntry.getValue().getStackTrace());
148                 }
149             }
150             assertWithMessage(errorBuilder.toString()).that(result.hasFailedTests()).isFalse();
151         } finally {
152             device.uninstallPackage(packageName);
153         }
154     }
155 
156 }
157