blob: d08c0bc41261a7301171147b020bdc258c3440ca [file] [log] [blame]
Brett Chabot3add9162010-09-12 17:23:05 -07001/*
2 * Copyright (C) 2010 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 */
Brett Chabot13638ab2011-10-16 13:59:03 -070016
Brett Chabot3add9162010-09-12 17:23:05 -070017package com.android.cts.tradefed.testtype;
18
Stuart Scotte00a2b82014-09-05 17:37:18 -070019import com.android.cts.util.AbiUtils;
Brett Chabot87f44512012-11-16 16:00:02 -080020import com.android.ddmlib.Log.LogLevel;
Brett Chabot82d9daf2010-10-20 20:07:14 -070021import com.android.ddmlib.testrunner.TestIdentifier;
Brett Chabot58c43a82011-09-13 14:13:57 -070022import com.android.tradefed.log.LogUtil.CLog;
Guang Zhu7afa91e2015-02-06 00:44:34 -080023import com.android.tradefed.targetprep.ITargetPreparer;
Stuart Scotte00a2b82014-09-05 17:37:18 -070024import com.android.tradefed.testtype.IAbi;
Brett Chabot3add9162010-09-12 17:23:05 -070025import com.android.tradefed.testtype.IRemoteTest;
26import com.android.tradefed.testtype.InstrumentationTest;
Brett Chabot58c43a82011-09-13 14:13:57 -070027import com.android.tradefed.util.StreamUtil;
Brett Chabot3add9162010-09-12 17:23:05 -070028
Brett Chabot58c43a82011-09-13 14:13:57 -070029import java.io.BufferedInputStream;
Brett Chabot3add9162010-09-12 17:23:05 -070030import java.io.File;
Brett Chabot58c43a82011-09-13 14:13:57 -070031import java.io.FileInputStream;
32import java.io.FileNotFoundException;
33import java.io.IOException;
34import java.io.InputStream;
35import java.security.DigestInputStream;
36import java.security.MessageDigest;
37import java.security.NoSuchAlgorithmException;
Brett Chabot82d9daf2010-10-20 20:07:14 -070038import java.util.Collection;
Jarkko Pöyry0df34742015-01-14 10:46:41 -080039import java.util.LinkedHashMap;
Brett Chabotef5a6042011-01-19 19:54:14 -080040import java.util.LinkedHashSet;
Jarkko Pöyry0df34742015-01-14 10:46:41 -080041import java.util.LinkedList;
42import java.util.List;
43import java.util.Map;
Brett Chabot3add9162010-09-12 17:23:05 -070044
45/**
46 * Container for CTS test info.
47 * <p/>
48 * Knows how to translate this info into a runnable {@link IRemoteTest}.
49 */
Brett Chabot4f8143c2010-12-14 18:29:44 -080050class TestPackageDef implements ITestPackageDef {
Brett Chabot3add9162010-09-12 17:23:05 -070051
Brian Muramatsu60695072012-01-09 18:11:03 -080052 public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly";
53 public static final String NATIVE_TEST = "native";
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -070054 public static final String WRAPPED_NATIVE_TEST = "wrappednative";
Brian Muramatsu60695072012-01-09 18:11:03 -080055 public static final String VM_HOST_TEST = "vmHostTest";
Mika Isojärvif1e23ee2014-08-25 13:17:58 +030056 public static final String DEQP_TEST = "deqpTest";
Brett Chabot87f44512012-11-16 16:00:02 -080057 public static final String UIAUTOMATOR_TEST = "uiAutomator";
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070058 public static final String JUNIT_DEVICE_TEST = "jUnitDeviceTest";
Brian Muramatsu60695072012-01-09 18:11:03 -080059
Stuart Scotte00a2b82014-09-05 17:37:18 -070060 private String mAppPackageName = null;
Brett Chabot3add9162010-09-12 17:23:05 -070061 private String mAppNameSpace = null;
62 private String mName = null;
63 private String mRunner = null;
Brian Muramatsu88d32a82011-12-02 10:55:12 -080064 private String mTestType = null;
Brett Chabot3add9162010-09-12 17:23:05 -070065 private String mJarPath = null;
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070066 private String mRunTimeArgs = null;
Brian Carlstrom022aff42011-05-17 23:16:51 -070067 private String mTestPackageName = null;
Brett Chabot58c43a82011-09-13 14:13:57 -070068 private String mDigest = null;
Jed Estep6aab9db2015-10-20 10:19:39 -070069 private long mRuntimeHint = 0;
Stuart Scotte00a2b82014-09-05 17:37:18 -070070 private IAbi mAbi = null;
Guang Zhu7afa91e2015-02-06 00:44:34 -080071 private List<ITargetPreparer> mPreparers = null;
Brett Chabot3add9162010-09-12 17:23:05 -070072
Brett Chabot13638ab2011-10-16 13:59:03 -070073 // use a LinkedHashSet for predictable iteration insertion-order, and fast
74 // lookups
Brett Chabotef5a6042011-01-19 19:54:14 -080075 private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>();
76 // also maintain an index of known test classes
77 private Collection<String> mTestClasses = new LinkedHashSet<String>();
Jarkko Pöyry0df34742015-01-14 10:46:41 -080078 // store instance arguments in order too for consistency
79 private Map<TestIdentifier, List<Map<String, String>>> mTestInstanceArguments =
80 new LinkedHashMap<>();
Brett Chabot4f8143c2010-12-14 18:29:44 -080081
Brett Chabot53e68a32011-10-05 14:14:55 -070082 // dynamic options, not parsed from package xml
83 private String mClassName;
84 private String mMethodName;
Stuart Scotte00a2b82014-09-05 17:37:18 -070085 private TestFilter mTestFilter = new TestFilter();
Brett Chabot358dc562011-10-25 15:46:48 -070086 private String mTargetBinaryName;
87 private String mTargetNameSpace;
Keun young Park34246562012-09-18 11:18:20 -070088 // only timeout per package is supported. To change this to method granularity,
89 // test invocation should be done in method level.
90 // So for now, only max timeout for the package is used.
91 private int mTimeoutInMins = -1;
Brett Chabot53e68a32011-10-05 14:14:55 -070092
Stuart Scotte00a2b82014-09-05 17:37:18 -070093 @Override
94 public IAbi getAbi() {
95 return mAbi;
96 }
97
98 /**
99 * @param abi the ABI to run this package on
100 */
101 public void setAbi(IAbi abi) {
102 mAbi = abi;
103 }
104
105 /**
106 * @return unique id representing this test package for this ABI.
107 */
108 @Override
109 public String getId() {
110 return AbiUtils.createId(getAbi().getName(), getAppPackageName());
111 }
112
113 void setAppPackageName(String appPackageName) {
114 mAppPackageName = appPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700115 }
116
Nicholas Sauer6396d212014-11-05 20:10:21 -0800117 String getAppPackageName() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700118 return mAppPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700119 }
120
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700121 void setRunTimeArgs(String runTimeArgs) {
122 mRunTimeArgs = runTimeArgs;
123 }
124
Brett Chabot3add9162010-09-12 17:23:05 -0700125 void setAppNameSpace(String appNameSpace) {
126 mAppNameSpace = appNameSpace;
127 }
128
129 String getAppNameSpace() {
130 return mAppNameSpace;
131 }
132
133 void setName(String name) {
134 mName = name;
135 }
136
Brett Chabot58c43a82011-09-13 14:13:57 -0700137 /**
138 * {@inheritDoc}
139 */
140 @Override
Jed Estep6aab9db2015-10-20 10:19:39 -0700141 public long getRuntimeHint() {
142 return mRuntimeHint;
143 }
144
145 void setRuntimeHint(long runtimeHint) {
146 mRuntimeHint = runtimeHint;
147 }
148
149 /**
150 * {@inheritDoc}
151 */
152 @Override
Brett Chabot58c43a82011-09-13 14:13:57 -0700153 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700154 return mName;
155 }
156
157 void setRunner(String runnerName) {
158 mRunner = runnerName;
159 }
160
161 String getRunner() {
162 return mRunner;
163 }
164
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800165 void setTestType(String testType) {
166 mTestType = testType;
167 }
168
Brian Muramatsu60695072012-01-09 18:11:03 -0800169 String getTestType() {
170 return mTestType;
171 }
172
Brett Chabot3add9162010-09-12 17:23:05 -0700173 void setJarPath(String jarPath) {
174 mJarPath = jarPath;
175 }
176
177 String getJarPath() {
178 return mJarPath;
179 }
180
Brian Carlstrom022aff42011-05-17 23:16:51 -0700181 void setTestPackageName(String testPackageName) {
182 mTestPackageName = testPackageName;
183 }
184
Brett Chabot358dc562011-10-25 15:46:48 -0700185 void setTargetBinaryName(String targetBinaryName) {
186 mTargetBinaryName = targetBinaryName;
187 }
188
189 void setTargetNameSpace(String targetNameSpace) {
190 mTargetNameSpace = targetNameSpace;
191 }
192
193 @Override
194 public String getTargetApkName() {
195 if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) {
196 return String.format("%s.apk", mTargetBinaryName);
197 }
198 return null;
199 }
200
201 @Override
202 public String getTargetPackageName() {
203 if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) {
204 return null;
205 }
206 return mTargetNameSpace;
207 }
208
Brett Chabot3add9162010-09-12 17:23:05 -0700209 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800210 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700211 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700212 @Override
Stuart Scotte00a2b82014-09-05 17:37:18 -0700213 public void setTestFilter(TestFilter testFilter) {
214 mTestFilter = testFilter;
Brett Chabot53e68a32011-10-05 14:14:55 -0700215 }
216
217 /**
218 * {@inheritDoc}
219 */
220 @Override
221 public void setClassName(String className, String methodName) {
222 mClassName = className;
223 mMethodName = methodName;
224 }
225
226 /**
Guang Zhu7afa91e2015-02-06 00:44:34 -0800227 * Setter for injecting a list of {@link ITargetPreparer}s as configured in module test config.
228 * @param preparers
229 */
230 void setPackagePreparers(List<ITargetPreparer> preparers) {
231 mPreparers = preparers;
232 }
233
234 /**
235 * {@inheritDoc}
236 */
237 @Override
238 public List<ITargetPreparer> getPackagePreparers() {
239 return mPreparers;
240 }
241
242 /**
Brett Chabot53e68a32011-10-05 14:14:55 -0700243 * {@inheritDoc}
244 */
245 @Override
246 public IRemoteTest createTest(File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700247 mTestFilter.setTestInclusion(mClassName, mMethodName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700248 mTests = filterTests();
249
Brian Muramatsu60695072012-01-09 18:11:03 -0800250 if (HOST_SIDE_ONLY_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700251 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700252 JarHostTest hostTest = new JarHostTest();
Keun young Park14a5bfa2012-10-02 13:45:30 -0700253 if (mTimeoutInMins >= 0) {
254 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
255 hostTest.setTimeout(mTimeoutInMins * 60 * 1000);
256 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700257 hostTest.setRunName(mAppPackageName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700258 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700259 hostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700260 hostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700261 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700262 return hostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800263 } else if (VM_HOST_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700264 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700265 VMHostTest vmHostTest = new VMHostTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700266 vmHostTest.setRunName(mAppPackageName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700267 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700268 vmHostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700269 vmHostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700270 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700271 return vmHostTest;
Mika Isojärvif1e23ee2014-08-25 13:17:58 +0300272 } else if (DEQP_TEST.equals(mTestType)) {
Jarkko Pöyry91d00e02015-01-15 18:02:15 -0800273 DeqpTestRunner deqpTest =
274 new DeqpTestRunner(mAppPackageName, mName, mTests, mTestInstanceArguments);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700275 deqpTest.setAbi(mAbi);
276 return deqpTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800277 } else if (NATIVE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700278 GeeTest geeTest = new GeeTest(mAppPackageName, mName);
279 geeTest.setAbi(mAbi);
280 return geeTest;
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700281 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
282 CLog.d("Creating new wrapped native test for %s", mName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700283 WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner);
284 wrappedGeeTest.setAbi(mAbi);
285 return wrappedGeeTest;
Brett Chabot87f44512012-11-16 16:00:02 -0800286 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
287 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
288 return setUiAutomatorTest(uiautomatorTest);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700289 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
290 CLog.d("Creating JUnit device test %s", mName);
291 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700292 jUnitDeviceTest.setRunName(mAppPackageName);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700293 jUnitDeviceTest.addTestJarFileName(mJarPath);
294 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
295 jUnitDeviceTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700296 jUnitDeviceTest.setAbi(mAbi);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700297 mDigest = generateDigest(testCaseDir, mJarPath);
298 return jUnitDeviceTest;
Brett Chabota0f443c2011-02-04 13:57:55 -0800299 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700300 CLog.d("Creating instrumentation test for %s", mName);
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700301 CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest();
Keun young Park34246562012-09-18 11:18:20 -0700302 if (mTimeoutInMins >= 0) {
303 // as timeout cannot be set for each test,
304 // increase the time-out of the whole package
305 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
306 instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000);
307 }
Brett Chabot53e68a32011-10-05 14:14:55 -0700308 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700309 }
310 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700311
312 /**
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700313 * Populates given {@link CtsInstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800314 *
315 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800316 * @param instrTest
317 * @return the populated {@link InstrumentationTest} or <code>null</code>
318 */
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700319 private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest,
Brett Chabot53e68a32011-10-05 14:14:55 -0700320 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700321 instrTest.setRunName(mAppPackageName);
Brett Chabota0f443c2011-02-04 13:57:55 -0800322 instrTest.setPackageName(mAppNameSpace);
323 instrTest.setRunnerName(mRunner);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700324 instrTest.setAbi(mAbi);
Nick Korostelev4bfd0752014-08-06 14:56:08 -0700325 instrTest.setTestsToRun(mTests, false
326 /* force batch mode off to always run using testFile */);
327 instrTest.setReRunUsingTestFile(true);
Brett Chabota0f443c2011-02-04 13:57:55 -0800328 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700329 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700330 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700331 if (mTests.size() > 1000) {
332 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700333 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800334 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800335 return instrTest;
336 }
337
338 /**
Brett Chabot87f44512012-11-16 16:00:02 -0800339 * Populates given {@link UiAutomatorJarTest} with data from the package xml.
340 *
341 * @param uiautomatorTest
342 * @return the populated {@link UiAutomatorJarTest} or <code>null</code>
343 */
Stuart Scotte00a2b82014-09-05 17:37:18 -0700344 @SuppressWarnings("deprecation")
Brett Chabot87f44512012-11-16 16:00:02 -0800345 private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) {
346 uiautomatorTest.setInstallArtifacts(getJarPath());
347 if (mClassName != null) {
348 if (mMethodName != null) {
349 CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" +
350 "running individual methods");
351 }
352 uiautomatorTest.addClassName(mClassName);
353 } else {
354 uiautomatorTest.addClassNames(mTestClasses);
355 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700356 uiautomatorTest.setRunName(mAppPackageName);
Brett Chabot87f44512012-11-16 16:00:02 -0800357 uiautomatorTest.setCaptureLogs(false);
358 return uiautomatorTest;
359 }
360
361 /**
Stuart Scotte00a2b82014-09-05 17:37:18 -0700362 * Filter the tests to run based on list of included/excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800363 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800364 * @return the filtered collection of tests
365 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700366 private Collection<TestIdentifier> filterTests() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700367 mTestFilter.setTestInclusion(mClassName, mMethodName);
368 return mTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800369 }
370
Nicholas Sauer6396d212014-11-05 20:10:21 -0800371 boolean isKnownTestClass(String className) {
Brett Chabotef5a6042011-01-19 19:54:14 -0800372 return mTestClasses.contains(className);
373 }
374
375 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800376 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700377 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800378 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700379 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700380 */
Keun young Park34246562012-09-18 11:18:20 -0700381 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700382 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800383 mTestClasses.add(testDef.getClassName());
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800384 mTestInstanceArguments.put(testDef, new LinkedList<Map<String, String>>());
Keun young Park34246562012-09-18 11:18:20 -0700385 // 0 means no timeout, so keep 0 if already is.
386 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
387 mTimeoutInMins = timeout;
388 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700389 }
390
391 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800392 * Add a test instance to an existing {@link TestIdentifier}.
393 */
394 void addTestInstance(TestIdentifier testDef, Map<String, String> instanceArguments) {
395 if (!mTestInstanceArguments.containsKey(testDef)) {
396 throw new IllegalStateException("test id does not name an existing test");
397 }
398 mTestInstanceArguments.get(testDef).add(instanceArguments);
399 }
400
401 /**
Stuart Scottd6065522014-10-02 10:34:27 -0700402 * {@inheritDoc}
Brett Chabot82d9daf2010-10-20 20:07:14 -0700403 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800404 @Override
405 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700406 return mTests;
407 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700408
409 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800410 * Get the instance argument map for tests.
411 * <p/>
412 * Exposed for unit testing.
413 */
414 public Map<TestIdentifier, List<Map<String, String>>> getTestInstanceArguments() {
415 return mTestInstanceArguments;
416 }
417
418 /**
Brett Chabot58c43a82011-09-13 14:13:57 -0700419 * {@inheritDoc}
420 */
421 @Override
422 public String getDigest() {
423 return mDigest;
424 }
425
426 /**
427 * Generate a sha1sum digest for a file.
428 * <p/>
429 * Exposed for unit testing.
430 *
431 * @param fileDir the directory of the file
432 * @param fileName the name of the file
433 * @return a hex {@link String} of the digest
434 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700435 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700436 final String algorithm = "SHA-1";
437 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700438 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700439 try {
440 fileStream = getFileStream(fileDir, fileName);
441 MessageDigest md = MessageDigest.getInstance(algorithm);
442 d = new DigestInputStream(fileStream, md);
443 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800444 while (d.read(buffer) != -1) {
445 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700446 return toHexString(md.digest());
447 } catch (NoSuchAlgorithmException e) {
448 return algorithm + " not found";
449 } catch (IOException e) {
450 CLog.e(e);
451 } finally {
Guang Zhu7afa91e2015-02-06 00:44:34 -0800452 StreamUtil.close(d);
453 StreamUtil.close(fileStream);
Brett Chabot58c43a82011-09-13 14:13:57 -0700454 }
455 return "failed to generate digest";
456 }
457
458 /**
459 * Retrieve an input stream for given file
460 * <p/>
461 * Exposed so unit tests can mock.
462 */
463 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
464 InputStream fileStream;
465 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
466 return fileStream;
467 }
468
469 /**
470 * Convert the given byte array into a lowercase hex string.
471 *
472 * @param arr The array to convert.
473 * @return The hex encoded string.
474 */
475 private String toHexString(byte[] arr) {
Nicholas Sauer6396d212014-11-05 20:10:21 -0800476 StringBuilder buf = new StringBuilder(arr.length * 2);
Brett Chabot58c43a82011-09-13 14:13:57 -0700477 for (byte b : arr) {
478 buf.append(String.format("%02x", b & 0xFF));
479 }
480 return buf.toString();
481 }
Nicholas Sauerc176cac2014-10-23 15:36:49 -0700482
483 @Override
484 public int compareTo(ITestPackageDef testPackageDef) {
485 return getId().compareTo(testPackageDef.getId());
486 }
Brett Chabot3add9162010-09-12 17:23:05 -0700487}