blob: 9ef62573514501bef08af2616bbb468694603192 [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;
Stuart Scotte00a2b82014-09-05 17:37:18 -070023import com.android.tradefed.testtype.IAbi;
Brett Chabot3add9162010-09-12 17:23:05 -070024import com.android.tradefed.testtype.IRemoteTest;
25import com.android.tradefed.testtype.InstrumentationTest;
Brett Chabot58c43a82011-09-13 14:13:57 -070026import com.android.tradefed.util.StreamUtil;
Brett Chabot3add9162010-09-12 17:23:05 -070027
Brett Chabot58c43a82011-09-13 14:13:57 -070028import java.io.BufferedInputStream;
Brett Chabot3add9162010-09-12 17:23:05 -070029import java.io.File;
Brett Chabot58c43a82011-09-13 14:13:57 -070030import java.io.FileInputStream;
31import java.io.FileNotFoundException;
32import java.io.IOException;
33import java.io.InputStream;
34import java.security.DigestInputStream;
35import java.security.MessageDigest;
36import java.security.NoSuchAlgorithmException;
Brett Chabot82d9daf2010-10-20 20:07:14 -070037import java.util.Collection;
Brett Chabotef5a6042011-01-19 19:54:14 -080038import java.util.LinkedHashSet;
Brett Chabot3add9162010-09-12 17:23:05 -070039
40/**
41 * Container for CTS test info.
42 * <p/>
43 * Knows how to translate this info into a runnable {@link IRemoteTest}.
44 */
Brett Chabot4f8143c2010-12-14 18:29:44 -080045class TestPackageDef implements ITestPackageDef {
Brett Chabot3add9162010-09-12 17:23:05 -070046
Brian Muramatsu60695072012-01-09 18:11:03 -080047 public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly";
48 public static final String NATIVE_TEST = "native";
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -070049 public static final String WRAPPED_NATIVE_TEST = "wrappednative";
Brian Muramatsu60695072012-01-09 18:11:03 -080050 public static final String VM_HOST_TEST = "vmHostTest";
Mika Isojärvif1e23ee2014-08-25 13:17:58 +030051 public static final String DEQP_TEST = "deqpTest";
Svetoslav Ganovb803f802012-03-07 19:13:05 -080052 public static final String ACCESSIBILITY_TEST =
Svetoslav12c82d42013-12-20 12:34:52 -080053 "com.android.cts.tradefed.testtype.AccessibilityTestRunner";
Brett Chabota595baa2013-07-01 11:55:36 -070054 public static final String ACCESSIBILITY_SERVICE_TEST =
Svetoslav12c82d42013-12-20 12:34:52 -080055 "com.android.cts.tradefed.testtype.AccessibilityServiceTestRunner";
56 public static final String PRINT_TEST =
57 "com.android.cts.tradefed.testtype.PrintTestRunner";
Craig Mautnera8e522f2012-10-22 11:46:30 -070058 public static final String DISPLAY_TEST =
59 "com.android.cts.tradefed.testtype.DisplayTestRunner";
Brett Chabot87f44512012-11-16 16:00:02 -080060 public static final String UIAUTOMATOR_TEST = "uiAutomator";
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070061 public static final String JUNIT_DEVICE_TEST = "jUnitDeviceTest";
Brian Muramatsu60695072012-01-09 18:11:03 -080062
Stuart Scotte00a2b82014-09-05 17:37:18 -070063 private String mAppPackageName = null;
Brett Chabot3add9162010-09-12 17:23:05 -070064 private String mAppNameSpace = null;
65 private String mName = null;
66 private String mRunner = null;
Brian Muramatsu88d32a82011-12-02 10:55:12 -080067 private String mTestType = null;
Brett Chabot3add9162010-09-12 17:23:05 -070068 private String mJarPath = null;
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070069 private String mRunTimeArgs = null;
Brian Carlstrom022aff42011-05-17 23:16:51 -070070 private String mTestPackageName = null;
Brett Chabot58c43a82011-09-13 14:13:57 -070071 private String mDigest = null;
Stuart Scotte00a2b82014-09-05 17:37:18 -070072 private IAbi mAbi = 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>();
Brett Chabot4f8143c2010-12-14 18:29:44 -080079
Brett Chabot53e68a32011-10-05 14:14:55 -070080 // dynamic options, not parsed from package xml
81 private String mClassName;
82 private String mMethodName;
Stuart Scotte00a2b82014-09-05 17:37:18 -070083 private TestFilter mTestFilter = new TestFilter();
Brett Chabot358dc562011-10-25 15:46:48 -070084 private String mTargetBinaryName;
85 private String mTargetNameSpace;
Keun young Park34246562012-09-18 11:18:20 -070086 // only timeout per package is supported. To change this to method granularity,
87 // test invocation should be done in method level.
88 // So for now, only max timeout for the package is used.
89 private int mTimeoutInMins = -1;
Brett Chabot53e68a32011-10-05 14:14:55 -070090
Stuart Scotte00a2b82014-09-05 17:37:18 -070091 @Override
92 public IAbi getAbi() {
93 return mAbi;
94 }
95
96 /**
97 * @param abi the ABI to run this package on
98 */
99 public void setAbi(IAbi abi) {
100 mAbi = abi;
101 }
102
103 /**
104 * @return unique id representing this test package for this ABI.
105 */
106 @Override
107 public String getId() {
108 return AbiUtils.createId(getAbi().getName(), getAppPackageName());
109 }
110
111 void setAppPackageName(String appPackageName) {
112 mAppPackageName = appPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700113 }
114
Nicholas Sauer6396d212014-11-05 20:10:21 -0800115 String getAppPackageName() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700116 return mAppPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700117 }
118
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700119 void setRunTimeArgs(String runTimeArgs) {
120 mRunTimeArgs = runTimeArgs;
121 }
122
Brett Chabot3add9162010-09-12 17:23:05 -0700123 void setAppNameSpace(String appNameSpace) {
124 mAppNameSpace = appNameSpace;
125 }
126
127 String getAppNameSpace() {
128 return mAppNameSpace;
129 }
130
131 void setName(String name) {
132 mName = name;
133 }
134
Brett Chabot58c43a82011-09-13 14:13:57 -0700135 /**
136 * {@inheritDoc}
137 */
138 @Override
139 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700140 return mName;
141 }
142
143 void setRunner(String runnerName) {
144 mRunner = runnerName;
145 }
146
147 String getRunner() {
148 return mRunner;
149 }
150
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800151 void setTestType(String testType) {
152 mTestType = testType;
153 }
154
Brian Muramatsu60695072012-01-09 18:11:03 -0800155 String getTestType() {
156 return mTestType;
157 }
158
Brett Chabot3add9162010-09-12 17:23:05 -0700159 void setJarPath(String jarPath) {
160 mJarPath = jarPath;
161 }
162
163 String getJarPath() {
164 return mJarPath;
165 }
166
Brian Carlstrom022aff42011-05-17 23:16:51 -0700167 void setTestPackageName(String testPackageName) {
168 mTestPackageName = testPackageName;
169 }
170
Brett Chabot358dc562011-10-25 15:46:48 -0700171 void setTargetBinaryName(String targetBinaryName) {
172 mTargetBinaryName = targetBinaryName;
173 }
174
175 void setTargetNameSpace(String targetNameSpace) {
176 mTargetNameSpace = targetNameSpace;
177 }
178
179 @Override
180 public String getTargetApkName() {
181 if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) {
182 return String.format("%s.apk", mTargetBinaryName);
183 }
184 return null;
185 }
186
187 @Override
188 public String getTargetPackageName() {
189 if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) {
190 return null;
191 }
192 return mTargetNameSpace;
193 }
194
Brett Chabot3add9162010-09-12 17:23:05 -0700195 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800196 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700197 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700198 @Override
Stuart Scotte00a2b82014-09-05 17:37:18 -0700199 public void setTestFilter(TestFilter testFilter) {
200 mTestFilter = testFilter;
Brett Chabot53e68a32011-10-05 14:14:55 -0700201 }
202
203 /**
204 * {@inheritDoc}
205 */
206 @Override
207 public void setClassName(String className, String methodName) {
208 mClassName = className;
209 mMethodName = methodName;
210 }
211
212 /**
213 * {@inheritDoc}
214 */
215 @Override
216 public IRemoteTest createTest(File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700217 mTestFilter.setTestInclusion(mClassName, mMethodName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700218 mTests = filterTests();
219
Brian Muramatsu60695072012-01-09 18:11:03 -0800220 if (HOST_SIDE_ONLY_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700221 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700222 JarHostTest hostTest = new JarHostTest();
Keun young Park14a5bfa2012-10-02 13:45:30 -0700223 if (mTimeoutInMins >= 0) {
224 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
225 hostTest.setTimeout(mTimeoutInMins * 60 * 1000);
226 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700227 hostTest.setRunName(mAppPackageName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700228 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700229 hostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700230 hostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700231 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700232 return hostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800233 } else if (VM_HOST_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700234 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700235 VMHostTest vmHostTest = new VMHostTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700236 vmHostTest.setRunName(mAppPackageName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700237 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700238 vmHostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700239 vmHostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700240 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700241 return vmHostTest;
Mika Isojärvif1e23ee2014-08-25 13:17:58 +0300242 } else if (DEQP_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700243 DeqpTestRunner deqpTest = new DeqpTestRunner(mAppPackageName, mName, mTests);
244 deqpTest.setAbi(mAbi);
245 return deqpTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800246 } else if (NATIVE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700247 GeeTest geeTest = new GeeTest(mAppPackageName, mName);
248 geeTest.setAbi(mAbi);
249 return geeTest;
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700250 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
251 CLog.d("Creating new wrapped native test for %s", mName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700252 WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner);
253 wrappedGeeTest.setAbi(mAbi);
254 return wrappedGeeTest;
Svetoslav Ganovb803f802012-03-07 19:13:05 -0800255 } else if (ACCESSIBILITY_TEST.equals(mTestType)) {
256 AccessibilityTestRunner test = new AccessibilityTestRunner();
257 return setInstrumentationTest(test, testCaseDir);
Svetoslav12c82d42013-12-20 12:34:52 -0800258 } else if (PRINT_TEST.equals(mTestType)) {
259 PrintTestRunner test = new PrintTestRunner();
260 return setPrintTest(test, testCaseDir);
Brett Chabota595baa2013-07-01 11:55:36 -0700261 } else if (ACCESSIBILITY_SERVICE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700262 @SuppressWarnings("deprecation")
Brett Chabota595baa2013-07-01 11:55:36 -0700263 AccessibilityServiceTestRunner test = new AccessibilityServiceTestRunner();
264 return setInstrumentationTest(test, testCaseDir);
Craig Mautnera8e522f2012-10-22 11:46:30 -0700265 } else if (DISPLAY_TEST.equals(mTestType)) {
266 DisplayTestRunner test = new DisplayTestRunner();
267 return setInstrumentationTest(test, testCaseDir);
Brett Chabot87f44512012-11-16 16:00:02 -0800268 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
269 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
270 return setUiAutomatorTest(uiautomatorTest);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700271 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
272 CLog.d("Creating JUnit device test %s", mName);
273 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700274 jUnitDeviceTest.setRunName(mAppPackageName);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700275 jUnitDeviceTest.addTestJarFileName(mJarPath);
276 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
277 jUnitDeviceTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700278 jUnitDeviceTest.setAbi(mAbi);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700279 mDigest = generateDigest(testCaseDir, mJarPath);
280 return jUnitDeviceTest;
Brett Chabota0f443c2011-02-04 13:57:55 -0800281 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700282 CLog.d("Creating instrumentation test for %s", mName);
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700283 CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest();
Keun young Park34246562012-09-18 11:18:20 -0700284 if (mTimeoutInMins >= 0) {
285 // as timeout cannot be set for each test,
286 // increase the time-out of the whole package
287 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
288 instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000);
289 }
Brett Chabot53e68a32011-10-05 14:14:55 -0700290 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700291 }
292 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700293
Svetoslav12c82d42013-12-20 12:34:52 -0800294 private PrintTestRunner setPrintTest(PrintTestRunner printTest,
295 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700296 printTest.setRunName(mAppPackageName);
Svetoslav12c82d42013-12-20 12:34:52 -0800297 printTest.setPackageName(mAppNameSpace);
298 printTest.setRunnerName(mRunner);
299 printTest.setTestPackageName(mTestPackageName);
300 printTest.setClassName(mClassName);
301 printTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700302 printTest.setAbi(mAbi);
Svetoslav12c82d42013-12-20 12:34:52 -0800303 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
304 return printTest;
305 }
306
Brett Chabot82d9daf2010-10-20 20:07:14 -0700307 /**
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700308 * Populates given {@link CtsInstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800309 *
310 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800311 * @param instrTest
312 * @return the populated {@link InstrumentationTest} or <code>null</code>
313 */
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700314 private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest,
Brett Chabot53e68a32011-10-05 14:14:55 -0700315 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700316 instrTest.setRunName(mAppPackageName);
Brett Chabota0f443c2011-02-04 13:57:55 -0800317 instrTest.setPackageName(mAppNameSpace);
318 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700319 instrTest.setTestPackageName(mTestPackageName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700320 instrTest.setClassName(mClassName);
321 instrTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700322 instrTest.setAbi(mAbi);
Nick Korostelev4bfd0752014-08-06 14:56:08 -0700323 instrTest.setTestsToRun(mTests, false
324 /* force batch mode off to always run using testFile */);
325 instrTest.setReRunUsingTestFile(true);
Brett Chabota0f443c2011-02-04 13:57:55 -0800326 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700327 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700328 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700329 if (mTests.size() > 1000) {
330 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700331 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800332 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800333 return instrTest;
334 }
335
336 /**
Brett Chabot87f44512012-11-16 16:00:02 -0800337 * Populates given {@link UiAutomatorJarTest} with data from the package xml.
338 *
339 * @param uiautomatorTest
340 * @return the populated {@link UiAutomatorJarTest} or <code>null</code>
341 */
Stuart Scotte00a2b82014-09-05 17:37:18 -0700342 @SuppressWarnings("deprecation")
Brett Chabot87f44512012-11-16 16:00:02 -0800343 private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) {
344 uiautomatorTest.setInstallArtifacts(getJarPath());
345 if (mClassName != null) {
346 if (mMethodName != null) {
347 CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" +
348 "running individual methods");
349 }
350 uiautomatorTest.addClassName(mClassName);
351 } else {
352 uiautomatorTest.addClassNames(mTestClasses);
353 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700354 uiautomatorTest.setRunName(mAppPackageName);
Brett Chabot87f44512012-11-16 16:00:02 -0800355 uiautomatorTest.setCaptureLogs(false);
356 return uiautomatorTest;
357 }
358
359 /**
Stuart Scotte00a2b82014-09-05 17:37:18 -0700360 * Filter the tests to run based on list of included/excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800361 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800362 * @return the filtered collection of tests
363 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700364 private Collection<TestIdentifier> filterTests() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700365 mTestFilter.setTestInclusion(mClassName, mMethodName);
366 return mTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800367 }
368
Nicholas Sauer6396d212014-11-05 20:10:21 -0800369 boolean isKnownTestClass(String className) {
Brett Chabotef5a6042011-01-19 19:54:14 -0800370 return mTestClasses.contains(className);
371 }
372
373 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800374 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700375 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800376 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700377 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700378 */
Keun young Park34246562012-09-18 11:18:20 -0700379 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700380 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800381 mTestClasses.add(testDef.getClassName());
Keun young Park34246562012-09-18 11:18:20 -0700382 // 0 means no timeout, so keep 0 if already is.
383 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
384 mTimeoutInMins = timeout;
385 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700386 }
387
388 /**
Stuart Scottd6065522014-10-02 10:34:27 -0700389 * {@inheritDoc}
Brett Chabot82d9daf2010-10-20 20:07:14 -0700390 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800391 @Override
392 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700393 return mTests;
394 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700395
396 /**
397 * {@inheritDoc}
398 */
399 @Override
400 public String getDigest() {
401 return mDigest;
402 }
403
404 /**
405 * Generate a sha1sum digest for a file.
406 * <p/>
407 * Exposed for unit testing.
408 *
409 * @param fileDir the directory of the file
410 * @param fileName the name of the file
411 * @return a hex {@link String} of the digest
412 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700413 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700414 final String algorithm = "SHA-1";
415 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700416 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700417 try {
418 fileStream = getFileStream(fileDir, fileName);
419 MessageDigest md = MessageDigest.getInstance(algorithm);
420 d = new DigestInputStream(fileStream, md);
421 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800422 while (d.read(buffer) != -1) {
423 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700424 return toHexString(md.digest());
425 } catch (NoSuchAlgorithmException e) {
426 return algorithm + " not found";
427 } catch (IOException e) {
428 CLog.e(e);
429 } finally {
430 StreamUtil.closeStream(d);
431 StreamUtil.closeStream(fileStream);
432 }
433 return "failed to generate digest";
434 }
435
436 /**
437 * Retrieve an input stream for given file
438 * <p/>
439 * Exposed so unit tests can mock.
440 */
441 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
442 InputStream fileStream;
443 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
444 return fileStream;
445 }
446
447 /**
448 * Convert the given byte array into a lowercase hex string.
449 *
450 * @param arr The array to convert.
451 * @return The hex encoded string.
452 */
453 private String toHexString(byte[] arr) {
Nicholas Sauer6396d212014-11-05 20:10:21 -0800454 StringBuilder buf = new StringBuilder(arr.length * 2);
Brett Chabot58c43a82011-09-13 14:13:57 -0700455 for (byte b : arr) {
456 buf.append(String.format("%02x", b & 0xFF));
457 }
458 return buf.toString();
459 }
Nicholas Sauerc176cac2014-10-23 15:36:49 -0700460
461 @Override
462 public int compareTo(ITestPackageDef testPackageDef) {
463 return getId().compareTo(testPackageDef.getId());
464 }
Brett Chabot3add9162010-09-12 17:23:05 -0700465}