blob: c6affba8c8f00686bab60c01a2e0ee4f5926f7e4 [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";
Craig Mautnera8e522f2012-10-22 11:46:30 -070057 public static final String DISPLAY_TEST =
58 "com.android.cts.tradefed.testtype.DisplayTestRunner";
Brett Chabot87f44512012-11-16 16:00:02 -080059 public static final String UIAUTOMATOR_TEST = "uiAutomator";
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070060 public static final String JUNIT_DEVICE_TEST = "jUnitDeviceTest";
Brian Muramatsu60695072012-01-09 18:11:03 -080061
Stuart Scotte00a2b82014-09-05 17:37:18 -070062 private String mAppPackageName = null;
Brett Chabot3add9162010-09-12 17:23:05 -070063 private String mAppNameSpace = null;
64 private String mName = null;
65 private String mRunner = null;
Brian Muramatsu88d32a82011-12-02 10:55:12 -080066 private String mTestType = null;
Brett Chabot3add9162010-09-12 17:23:05 -070067 private String mJarPath = null;
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070068 private String mRunTimeArgs = null;
Brian Carlstrom022aff42011-05-17 23:16:51 -070069 private String mTestPackageName = null;
Brett Chabot58c43a82011-09-13 14:13:57 -070070 private String mDigest = null;
Stuart Scotte00a2b82014-09-05 17:37:18 -070071 private IAbi mAbi = null;
Guang Zhu7afa91e2015-02-06 00:44:34 -080072 private List<ITargetPreparer> mPreparers = null;
Brett Chabot3add9162010-09-12 17:23:05 -070073
Brett Chabot13638ab2011-10-16 13:59:03 -070074 // use a LinkedHashSet for predictable iteration insertion-order, and fast
75 // lookups
Brett Chabotef5a6042011-01-19 19:54:14 -080076 private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>();
77 // also maintain an index of known test classes
78 private Collection<String> mTestClasses = new LinkedHashSet<String>();
Jarkko Pöyry0df34742015-01-14 10:46:41 -080079 // store instance arguments in order too for consistency
80 private Map<TestIdentifier, List<Map<String, String>>> mTestInstanceArguments =
81 new LinkedHashMap<>();
Brett Chabot4f8143c2010-12-14 18:29:44 -080082
Brett Chabot53e68a32011-10-05 14:14:55 -070083 // dynamic options, not parsed from package xml
84 private String mClassName;
85 private String mMethodName;
Stuart Scotte00a2b82014-09-05 17:37:18 -070086 private TestFilter mTestFilter = new TestFilter();
Brett Chabot358dc562011-10-25 15:46:48 -070087 private String mTargetBinaryName;
88 private String mTargetNameSpace;
Keun young Park34246562012-09-18 11:18:20 -070089 // only timeout per package is supported. To change this to method granularity,
90 // test invocation should be done in method level.
91 // So for now, only max timeout for the package is used.
92 private int mTimeoutInMins = -1;
Brett Chabot53e68a32011-10-05 14:14:55 -070093
Stuart Scotte00a2b82014-09-05 17:37:18 -070094 @Override
95 public IAbi getAbi() {
96 return mAbi;
97 }
98
99 /**
100 * @param abi the ABI to run this package on
101 */
102 public void setAbi(IAbi abi) {
103 mAbi = abi;
104 }
105
106 /**
107 * @return unique id representing this test package for this ABI.
108 */
109 @Override
110 public String getId() {
111 return AbiUtils.createId(getAbi().getName(), getAppPackageName());
112 }
113
114 void setAppPackageName(String appPackageName) {
115 mAppPackageName = appPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700116 }
117
Nicholas Sauer6396d212014-11-05 20:10:21 -0800118 String getAppPackageName() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700119 return mAppPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700120 }
121
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700122 void setRunTimeArgs(String runTimeArgs) {
123 mRunTimeArgs = runTimeArgs;
124 }
125
Brett Chabot3add9162010-09-12 17:23:05 -0700126 void setAppNameSpace(String appNameSpace) {
127 mAppNameSpace = appNameSpace;
128 }
129
130 String getAppNameSpace() {
131 return mAppNameSpace;
132 }
133
134 void setName(String name) {
135 mName = name;
136 }
137
Brett Chabot58c43a82011-09-13 14:13:57 -0700138 /**
139 * {@inheritDoc}
140 */
141 @Override
142 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700143 return mName;
144 }
145
146 void setRunner(String runnerName) {
147 mRunner = runnerName;
148 }
149
150 String getRunner() {
151 return mRunner;
152 }
153
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800154 void setTestType(String testType) {
155 mTestType = testType;
156 }
157
Brian Muramatsu60695072012-01-09 18:11:03 -0800158 String getTestType() {
159 return mTestType;
160 }
161
Brett Chabot3add9162010-09-12 17:23:05 -0700162 void setJarPath(String jarPath) {
163 mJarPath = jarPath;
164 }
165
166 String getJarPath() {
167 return mJarPath;
168 }
169
Brian Carlstrom022aff42011-05-17 23:16:51 -0700170 void setTestPackageName(String testPackageName) {
171 mTestPackageName = testPackageName;
172 }
173
Brett Chabot358dc562011-10-25 15:46:48 -0700174 void setTargetBinaryName(String targetBinaryName) {
175 mTargetBinaryName = targetBinaryName;
176 }
177
178 void setTargetNameSpace(String targetNameSpace) {
179 mTargetNameSpace = targetNameSpace;
180 }
181
182 @Override
183 public String getTargetApkName() {
184 if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) {
185 return String.format("%s.apk", mTargetBinaryName);
186 }
187 return null;
188 }
189
190 @Override
191 public String getTargetPackageName() {
192 if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) {
193 return null;
194 }
195 return mTargetNameSpace;
196 }
197
Brett Chabot3add9162010-09-12 17:23:05 -0700198 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800199 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700200 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700201 @Override
Stuart Scotte00a2b82014-09-05 17:37:18 -0700202 public void setTestFilter(TestFilter testFilter) {
203 mTestFilter = testFilter;
Brett Chabot53e68a32011-10-05 14:14:55 -0700204 }
205
206 /**
207 * {@inheritDoc}
208 */
209 @Override
210 public void setClassName(String className, String methodName) {
211 mClassName = className;
212 mMethodName = methodName;
213 }
214
215 /**
Guang Zhu7afa91e2015-02-06 00:44:34 -0800216 * Setter for injecting a list of {@link ITargetPreparer}s as configured in module test config.
217 * @param preparers
218 */
219 void setPackagePreparers(List<ITargetPreparer> preparers) {
220 mPreparers = preparers;
221 }
222
223 /**
224 * {@inheritDoc}
225 */
226 @Override
227 public List<ITargetPreparer> getPackagePreparers() {
228 return mPreparers;
229 }
230
231 /**
Brett Chabot53e68a32011-10-05 14:14:55 -0700232 * {@inheritDoc}
233 */
234 @Override
235 public IRemoteTest createTest(File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700236 mTestFilter.setTestInclusion(mClassName, mMethodName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700237 mTests = filterTests();
238
Brian Muramatsu60695072012-01-09 18:11:03 -0800239 if (HOST_SIDE_ONLY_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700240 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700241 JarHostTest hostTest = new JarHostTest();
Keun young Park14a5bfa2012-10-02 13:45:30 -0700242 if (mTimeoutInMins >= 0) {
243 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
244 hostTest.setTimeout(mTimeoutInMins * 60 * 1000);
245 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700246 hostTest.setRunName(mAppPackageName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700247 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700248 hostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700249 hostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700250 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700251 return hostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800252 } else if (VM_HOST_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700253 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700254 VMHostTest vmHostTest = new VMHostTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700255 vmHostTest.setRunName(mAppPackageName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700256 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700257 vmHostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700258 vmHostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700259 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700260 return vmHostTest;
Mika Isojärvif1e23ee2014-08-25 13:17:58 +0300261 } else if (DEQP_TEST.equals(mTestType)) {
Jarkko Pöyry91d00e02015-01-15 18:02:15 -0800262 DeqpTestRunner deqpTest =
263 new DeqpTestRunner(mAppPackageName, mName, mTests, mTestInstanceArguments);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700264 deqpTest.setAbi(mAbi);
265 return deqpTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800266 } else if (NATIVE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700267 GeeTest geeTest = new GeeTest(mAppPackageName, mName);
268 geeTest.setAbi(mAbi);
269 return geeTest;
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700270 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
271 CLog.d("Creating new wrapped native test for %s", mName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700272 WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner);
273 wrappedGeeTest.setAbi(mAbi);
274 return wrappedGeeTest;
Craig Mautnera8e522f2012-10-22 11:46:30 -0700275 } else if (DISPLAY_TEST.equals(mTestType)) {
276 DisplayTestRunner test = new DisplayTestRunner();
277 return setInstrumentationTest(test, testCaseDir);
Brett Chabot87f44512012-11-16 16:00:02 -0800278 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
279 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
280 return setUiAutomatorTest(uiautomatorTest);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700281 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
282 CLog.d("Creating JUnit device test %s", mName);
283 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700284 jUnitDeviceTest.setRunName(mAppPackageName);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700285 jUnitDeviceTest.addTestJarFileName(mJarPath);
286 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
287 jUnitDeviceTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700288 jUnitDeviceTest.setAbi(mAbi);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700289 mDigest = generateDigest(testCaseDir, mJarPath);
290 return jUnitDeviceTest;
Brett Chabota0f443c2011-02-04 13:57:55 -0800291 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700292 CLog.d("Creating instrumentation test for %s", mName);
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700293 CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest();
Keun young Park34246562012-09-18 11:18:20 -0700294 if (mTimeoutInMins >= 0) {
295 // as timeout cannot be set for each test,
296 // increase the time-out of the whole package
297 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
298 instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000);
299 }
Brett Chabot53e68a32011-10-05 14:14:55 -0700300 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700301 }
302 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700303
304 /**
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700305 * Populates given {@link CtsInstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800306 *
307 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800308 * @param instrTest
309 * @return the populated {@link InstrumentationTest} or <code>null</code>
310 */
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700311 private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest,
Brett Chabot53e68a32011-10-05 14:14:55 -0700312 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700313 instrTest.setRunName(mAppPackageName);
Brett Chabota0f443c2011-02-04 13:57:55 -0800314 instrTest.setPackageName(mAppNameSpace);
315 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700316 instrTest.setTestPackageName(mTestPackageName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700317 instrTest.setClassName(mClassName);
318 instrTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700319 instrTest.setAbi(mAbi);
Nick Korostelev4bfd0752014-08-06 14:56:08 -0700320 instrTest.setTestsToRun(mTests, false
321 /* force batch mode off to always run using testFile */);
322 instrTest.setReRunUsingTestFile(true);
Brett Chabota0f443c2011-02-04 13:57:55 -0800323 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700324 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700325 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700326 if (mTests.size() > 1000) {
327 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700328 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800329 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800330 return instrTest;
331 }
332
333 /**
Brett Chabot87f44512012-11-16 16:00:02 -0800334 * Populates given {@link UiAutomatorJarTest} with data from the package xml.
335 *
336 * @param uiautomatorTest
337 * @return the populated {@link UiAutomatorJarTest} or <code>null</code>
338 */
Stuart Scotte00a2b82014-09-05 17:37:18 -0700339 @SuppressWarnings("deprecation")
Brett Chabot87f44512012-11-16 16:00:02 -0800340 private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) {
341 uiautomatorTest.setInstallArtifacts(getJarPath());
342 if (mClassName != null) {
343 if (mMethodName != null) {
344 CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" +
345 "running individual methods");
346 }
347 uiautomatorTest.addClassName(mClassName);
348 } else {
349 uiautomatorTest.addClassNames(mTestClasses);
350 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700351 uiautomatorTest.setRunName(mAppPackageName);
Brett Chabot87f44512012-11-16 16:00:02 -0800352 uiautomatorTest.setCaptureLogs(false);
353 return uiautomatorTest;
354 }
355
356 /**
Stuart Scotte00a2b82014-09-05 17:37:18 -0700357 * Filter the tests to run based on list of included/excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800358 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800359 * @return the filtered collection of tests
360 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700361 private Collection<TestIdentifier> filterTests() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700362 mTestFilter.setTestInclusion(mClassName, mMethodName);
363 return mTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800364 }
365
Nicholas Sauer6396d212014-11-05 20:10:21 -0800366 boolean isKnownTestClass(String className) {
Brett Chabotef5a6042011-01-19 19:54:14 -0800367 return mTestClasses.contains(className);
368 }
369
370 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800371 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700372 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800373 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700374 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700375 */
Keun young Park34246562012-09-18 11:18:20 -0700376 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700377 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800378 mTestClasses.add(testDef.getClassName());
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800379 mTestInstanceArguments.put(testDef, new LinkedList<Map<String, String>>());
Keun young Park34246562012-09-18 11:18:20 -0700380 // 0 means no timeout, so keep 0 if already is.
381 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
382 mTimeoutInMins = timeout;
383 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700384 }
385
386 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800387 * Add a test instance to an existing {@link TestIdentifier}.
388 */
389 void addTestInstance(TestIdentifier testDef, Map<String, String> instanceArguments) {
390 if (!mTestInstanceArguments.containsKey(testDef)) {
391 throw new IllegalStateException("test id does not name an existing test");
392 }
393 mTestInstanceArguments.get(testDef).add(instanceArguments);
394 }
395
396 /**
Stuart Scottd6065522014-10-02 10:34:27 -0700397 * {@inheritDoc}
Brett Chabot82d9daf2010-10-20 20:07:14 -0700398 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800399 @Override
400 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700401 return mTests;
402 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700403
404 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800405 * Get the instance argument map for tests.
406 * <p/>
407 * Exposed for unit testing.
408 */
409 public Map<TestIdentifier, List<Map<String, String>>> getTestInstanceArguments() {
410 return mTestInstanceArguments;
411 }
412
413 /**
Brett Chabot58c43a82011-09-13 14:13:57 -0700414 * {@inheritDoc}
415 */
416 @Override
417 public String getDigest() {
418 return mDigest;
419 }
420
421 /**
422 * Generate a sha1sum digest for a file.
423 * <p/>
424 * Exposed for unit testing.
425 *
426 * @param fileDir the directory of the file
427 * @param fileName the name of the file
428 * @return a hex {@link String} of the digest
429 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700430 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700431 final String algorithm = "SHA-1";
432 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700433 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700434 try {
435 fileStream = getFileStream(fileDir, fileName);
436 MessageDigest md = MessageDigest.getInstance(algorithm);
437 d = new DigestInputStream(fileStream, md);
438 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800439 while (d.read(buffer) != -1) {
440 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700441 return toHexString(md.digest());
442 } catch (NoSuchAlgorithmException e) {
443 return algorithm + " not found";
444 } catch (IOException e) {
445 CLog.e(e);
446 } finally {
Guang Zhu7afa91e2015-02-06 00:44:34 -0800447 StreamUtil.close(d);
448 StreamUtil.close(fileStream);
Brett Chabot58c43a82011-09-13 14:13:57 -0700449 }
450 return "failed to generate digest";
451 }
452
453 /**
454 * Retrieve an input stream for given file
455 * <p/>
456 * Exposed so unit tests can mock.
457 */
458 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
459 InputStream fileStream;
460 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
461 return fileStream;
462 }
463
464 /**
465 * Convert the given byte array into a lowercase hex string.
466 *
467 * @param arr The array to convert.
468 * @return The hex encoded string.
469 */
470 private String toHexString(byte[] arr) {
Nicholas Sauer6396d212014-11-05 20:10:21 -0800471 StringBuilder buf = new StringBuilder(arr.length * 2);
Brett Chabot58c43a82011-09-13 14:13:57 -0700472 for (byte b : arr) {
473 buf.append(String.format("%02x", b & 0xFF));
474 }
475 return buf.toString();
476 }
Nicholas Sauerc176cac2014-10-23 15:36:49 -0700477
478 @Override
479 public int compareTo(ITestPackageDef testPackageDef) {
480 return getId().compareTo(testPackageDef.getId());
481 }
Brett Chabot3add9162010-09-12 17:23:05 -0700482}