blob: 863a7a7060496c40b8604dd528c61e0a3bbf74ad [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
Brett Chabot87f44512012-11-16 16:00:02 -080019import com.android.ddmlib.Log.LogLevel;
Brett Chabot82d9daf2010-10-20 20:07:14 -070020import com.android.ddmlib.testrunner.TestIdentifier;
Brett Chabot58c43a82011-09-13 14:13:57 -070021import com.android.tradefed.log.LogUtil.CLog;
Brett Chabot3add9162010-09-12 17:23:05 -070022import com.android.tradefed.testtype.IRemoteTest;
23import com.android.tradefed.testtype.InstrumentationTest;
Brett Chabot58c43a82011-09-13 14:13:57 -070024import com.android.tradefed.util.StreamUtil;
Brett Chabot3add9162010-09-12 17:23:05 -070025
Brett Chabot58c43a82011-09-13 14:13:57 -070026import java.io.BufferedInputStream;
Brett Chabot3add9162010-09-12 17:23:05 -070027import java.io.File;
Brett Chabot58c43a82011-09-13 14:13:57 -070028import java.io.FileInputStream;
29import java.io.FileNotFoundException;
30import java.io.IOException;
31import java.io.InputStream;
32import java.security.DigestInputStream;
33import java.security.MessageDigest;
34import java.security.NoSuchAlgorithmException;
Brett Chabot82d9daf2010-10-20 20:07:14 -070035import java.util.Collection;
Brett Chabotef5a6042011-01-19 19:54:14 -080036import java.util.LinkedHashSet;
Brett Chabot3add9162010-09-12 17:23:05 -070037
38/**
39 * Container for CTS test info.
40 * <p/>
41 * Knows how to translate this info into a runnable {@link IRemoteTest}.
42 */
Brett Chabot4f8143c2010-12-14 18:29:44 -080043class TestPackageDef implements ITestPackageDef {
Brett Chabot3add9162010-09-12 17:23:05 -070044
Brian Muramatsu60695072012-01-09 18:11:03 -080045 public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly";
46 public static final String NATIVE_TEST = "native";
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -070047 public static final String WRAPPED_NATIVE_TEST = "wrappednative";
Brian Muramatsu60695072012-01-09 18:11:03 -080048 public static final String VM_HOST_TEST = "vmHostTest";
Mika Isojärvif1e23ee2014-08-25 13:17:58 +030049 public static final String DEQP_TEST = "deqpTest";
Svetoslav Ganovb803f802012-03-07 19:13:05 -080050 public static final String ACCESSIBILITY_TEST =
Svetoslav12c82d42013-12-20 12:34:52 -080051 "com.android.cts.tradefed.testtype.AccessibilityTestRunner";
Brett Chabota595baa2013-07-01 11:55:36 -070052 public static final String ACCESSIBILITY_SERVICE_TEST =
Svetoslav12c82d42013-12-20 12:34:52 -080053 "com.android.cts.tradefed.testtype.AccessibilityServiceTestRunner";
54 public static final String PRINT_TEST =
55 "com.android.cts.tradefed.testtype.PrintTestRunner";
Craig Mautnera8e522f2012-10-22 11:46:30 -070056 public static final String DISPLAY_TEST =
57 "com.android.cts.tradefed.testtype.DisplayTestRunner";
Brett Chabot87f44512012-11-16 16:00:02 -080058 public static final String UIAUTOMATOR_TEST = "uiAutomator";
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070059 public static final String JUNIT_DEVICE_TEST = "jUnitDeviceTest";
Brian Muramatsu60695072012-01-09 18:11:03 -080060
Brett Chabot584d2b92011-02-03 17:56:49 -080061 private static final String SIGNATURE_TEST_METHOD = "testSignature";
Stuart Scottc09a2e02013-11-15 13:03:29 -080062 private static final String SIGNATURE_TEST_CLASS = "android.tests.sigtest.SignatureTest";
Brett Chabot3add9162010-09-12 17:23:05 -070063
64 private String mUri = null;
65 private String mAppNameSpace = null;
66 private String mName = null;
67 private String mRunner = null;
Brian Muramatsu88d32a82011-12-02 10:55:12 -080068 private String mTestType = null;
Brett Chabot3add9162010-09-12 17:23:05 -070069 private String mJarPath = null;
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -070070 private String mRunTimeArgs = null;
Brett Chabot3add9162010-09-12 17:23:05 -070071 private boolean mIsSignatureTest = false;
Brian Carlstrom022aff42011-05-17 23:16:51 -070072 private String mTestPackageName = null;
Brett Chabot58c43a82011-09-13 14:13:57 -070073 private String mDigest = null;
Brett Chabot3add9162010-09-12 17:23:05 -070074
Brett Chabot13638ab2011-10-16 13:59:03 -070075 // use a LinkedHashSet for predictable iteration insertion-order, and fast
76 // lookups
Brett Chabotef5a6042011-01-19 19:54:14 -080077 private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>();
78 // also maintain an index of known test classes
79 private Collection<String> mTestClasses = new LinkedHashSet<String>();
Brett Chabot4f8143c2010-12-14 18:29:44 -080080
Brett Chabot53e68a32011-10-05 14:14:55 -070081 // dynamic options, not parsed from package xml
82 private String mClassName;
83 private String mMethodName;
84 private TestFilter mExcludedTestFilter = new TestFilter();
Brett Chabot358dc562011-10-25 15:46:48 -070085 private String mTargetBinaryName;
86 private String mTargetNameSpace;
Keun young Park34246562012-09-18 11:18:20 -070087 // only timeout per package is supported. To change this to method granularity,
88 // test invocation should be done in method level.
89 // So for now, only max timeout for the package is used.
90 private int mTimeoutInMins = -1;
Brett Chabot53e68a32011-10-05 14:14:55 -070091
Brett Chabot3add9162010-09-12 17:23:05 -070092 void setUri(String uri) {
93 mUri = uri;
94 }
95
96 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -080097 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -070098 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -080099 @Override
Brett Chabot3add9162010-09-12 17:23:05 -0700100 public String getUri() {
101 return mUri;
102 }
103
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700104 void setRunTimeArgs(String runTimeArgs) {
105 mRunTimeArgs = runTimeArgs;
106 }
107
108 String getRunTimeArgs() {
109 return mRunTimeArgs;
110 }
111
Brett Chabot3add9162010-09-12 17:23:05 -0700112 void setAppNameSpace(String appNameSpace) {
113 mAppNameSpace = appNameSpace;
114 }
115
116 String getAppNameSpace() {
117 return mAppNameSpace;
118 }
119
120 void setName(String name) {
121 mName = name;
122 }
123
Brett Chabot58c43a82011-09-13 14:13:57 -0700124 /**
125 * {@inheritDoc}
126 */
127 @Override
128 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700129 return mName;
130 }
131
132 void setRunner(String runnerName) {
133 mRunner = runnerName;
134 }
135
136 String getRunner() {
137 return mRunner;
138 }
139
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800140 void setTestType(String testType) {
141 mTestType = testType;
142 }
143
Brian Muramatsu60695072012-01-09 18:11:03 -0800144 String getTestType() {
145 return mTestType;
146 }
147
Brett Chabot3add9162010-09-12 17:23:05 -0700148 void setJarPath(String jarPath) {
149 mJarPath = jarPath;
150 }
151
152 String getJarPath() {
153 return mJarPath;
154 }
155
156 void setIsSignatureCheck(boolean isSignatureCheckTest) {
157 mIsSignatureTest = isSignatureCheckTest;
158 }
159
160 boolean isSignatureCheck() {
161 return mIsSignatureTest;
162 }
163
Brian Carlstrom022aff42011-05-17 23:16:51 -0700164 void setTestPackageName(String testPackageName) {
165 mTestPackageName = testPackageName;
166 }
167
Brett Chabot358dc562011-10-25 15:46:48 -0700168 void setTargetBinaryName(String targetBinaryName) {
169 mTargetBinaryName = targetBinaryName;
170 }
171
172 void setTargetNameSpace(String targetNameSpace) {
173 mTargetNameSpace = targetNameSpace;
174 }
175
176 @Override
177 public String getTargetApkName() {
178 if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) {
179 return String.format("%s.apk", mTargetBinaryName);
180 }
181 return null;
182 }
183
184 @Override
185 public String getTargetPackageName() {
186 if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) {
187 return null;
188 }
189 return mTargetNameSpace;
190 }
191
Brett Chabot3add9162010-09-12 17:23:05 -0700192 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800193 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700194 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700195 @Override
196 public void setExcludedTestFilter(TestFilter excludeFilter) {
197 mExcludedTestFilter = excludeFilter;
198 }
199
200 /**
201 * {@inheritDoc}
202 */
203 @Override
204 public void setClassName(String className, String methodName) {
205 mClassName = className;
206 mMethodName = methodName;
207 }
208
209 /**
210 * {@inheritDoc}
211 */
212 @Override
213 public IRemoteTest createTest(File testCaseDir) {
214 mExcludedTestFilter.setTestInclusion(mClassName, mMethodName);
215 mTests = filterTests();
216
Brian Muramatsu60695072012-01-09 18:11:03 -0800217 if (HOST_SIDE_ONLY_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700218 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700219 JarHostTest hostTest = new JarHostTest();
Keun young Park14a5bfa2012-10-02 13:45:30 -0700220 if (mTimeoutInMins >= 0) {
221 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
222 hostTest.setTimeout(mTimeoutInMins * 60 * 1000);
223 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700224 hostTest.setRunName(getUri());
Brett Chabotf4bec732011-04-19 17:31:06 -0700225 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700226 hostTest.setTests(mTests);
Brett Chabot58c43a82011-09-13 14:13:57 -0700227 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700228 return hostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800229 } else if (VM_HOST_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700230 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700231 VMHostTest vmHostTest = new VMHostTest();
Brett Chabot58c43a82011-09-13 14:13:57 -0700232 vmHostTest.setRunName(getUri());
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700233 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700234 vmHostTest.setTests(mTests);
Brett Chabot58c43a82011-09-13 14:13:57 -0700235 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700236 return vmHostTest;
Mika Isojärvif1e23ee2014-08-25 13:17:58 +0300237 } else if (DEQP_TEST.equals(mTestType)) {
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700238 return new DeqpTestRunner(mUri, mName, mTests);
Brian Muramatsu60695072012-01-09 18:11:03 -0800239 } else if (NATIVE_TEST.equals(mTestType)) {
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800240 return new GeeTest(mUri, mName);
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700241 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
242 CLog.d("Creating new wrapped native test for %s", mName);
243 return new WrappedGTest(mAppNameSpace, mUri, mName, mRunner);
Svetoslav Ganovb803f802012-03-07 19:13:05 -0800244 } else if (ACCESSIBILITY_TEST.equals(mTestType)) {
245 AccessibilityTestRunner test = new AccessibilityTestRunner();
246 return setInstrumentationTest(test, testCaseDir);
Svetoslav12c82d42013-12-20 12:34:52 -0800247 } else if (PRINT_TEST.equals(mTestType)) {
248 PrintTestRunner test = new PrintTestRunner();
249 return setPrintTest(test, testCaseDir);
Brett Chabota595baa2013-07-01 11:55:36 -0700250 } else if (ACCESSIBILITY_SERVICE_TEST.equals(mTestType)) {
251 AccessibilityServiceTestRunner test = new AccessibilityServiceTestRunner();
252 return setInstrumentationTest(test, testCaseDir);
Craig Mautnera8e522f2012-10-22 11:46:30 -0700253 } else if (DISPLAY_TEST.equals(mTestType)) {
254 DisplayTestRunner test = new DisplayTestRunner();
255 return setInstrumentationTest(test, testCaseDir);
Brett Chabot87f44512012-11-16 16:00:02 -0800256 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
257 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
258 return setUiAutomatorTest(uiautomatorTest);
Brett Chabot3add9162010-09-12 17:23:05 -0700259 } else if (mIsSignatureTest) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700260 // TODO: hardcode the runner/class/method for now, since current package xml points to
261 // specialized instrumentation. Eventually this special case for signatureTest can be
262 // removed, and it can be treated just like a normal InstrumentationTest
263 CLog.d("Creating signature test %s", mName);
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700264 CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest();
Brett Chabot584d2b92011-02-03 17:56:49 -0800265 instrTest.setPackageName(mAppNameSpace);
266 instrTest.setRunnerName("android.test.InstrumentationTestRunner");
267 instrTest.setClassName(SIGNATURE_TEST_CLASS);
268 instrTest.setMethodName(SIGNATURE_TEST_METHOD);
Brett Chabot13638ab2011-10-16 13:59:03 -0700269 // set expected tests to the single signature test
270 TestIdentifier t = new TestIdentifier(SIGNATURE_TEST_CLASS, SIGNATURE_TEST_METHOD);
271 mTests.clear();
272 mTests.add(t);
Brett Chabot584d2b92011-02-03 17:56:49 -0800273 // mName means 'apk file name' for instrumentation tests
Brett Chabotf4bec732011-04-19 17:31:06 -0700274 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700275 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot584d2b92011-02-03 17:56:49 -0800276 return instrTest;
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700277 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
278 CLog.d("Creating JUnit device test %s", mName);
279 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
280 jUnitDeviceTest.setRunName(getUri());
281 jUnitDeviceTest.addTestJarFileName(mJarPath);
282 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
283 jUnitDeviceTest.setTests(mTests);
284 mDigest = generateDigest(testCaseDir, mJarPath);
285 return jUnitDeviceTest;
Brett Chabota0f443c2011-02-04 13:57:55 -0800286 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700287 CLog.d("Creating instrumentation test for %s", mName);
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700288 CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest();
Keun young Park34246562012-09-18 11:18:20 -0700289 if (mTimeoutInMins >= 0) {
290 // as timeout cannot be set for each test,
291 // increase the time-out of the whole package
292 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
293 instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000);
294 }
Brett Chabot53e68a32011-10-05 14:14:55 -0700295 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700296 }
297 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700298
Svetoslav12c82d42013-12-20 12:34:52 -0800299 private PrintTestRunner setPrintTest(PrintTestRunner printTest,
300 File testCaseDir) {
301 printTest.setRunName(getUri());
302 printTest.setPackageName(mAppNameSpace);
303 printTest.setRunnerName(mRunner);
304 printTest.setTestPackageName(mTestPackageName);
305 printTest.setClassName(mClassName);
306 printTest.setMethodName(mMethodName);
307 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
308 return printTest;
309 }
310
Brett Chabot82d9daf2010-10-20 20:07:14 -0700311 /**
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700312 * Populates given {@link CtsInstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800313 *
314 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800315 * @param instrTest
316 * @return the populated {@link InstrumentationTest} or <code>null</code>
317 */
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700318 private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest,
Brett Chabot53e68a32011-10-05 14:14:55 -0700319 File testCaseDir) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700320 instrTest.setRunName(getUri());
Brett Chabota0f443c2011-02-04 13:57:55 -0800321 instrTest.setPackageName(mAppNameSpace);
322 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700323 instrTest.setTestPackageName(mTestPackageName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700324 instrTest.setClassName(mClassName);
325 instrTest.setMethodName(mMethodName);
Nick Korostelev4bfd0752014-08-06 14:56:08 -0700326 instrTest.setTestsToRun(mTests, false
327 /* force batch mode off to always run using testFile */);
328 instrTest.setReRunUsingTestFile(true);
Brett Chabota0f443c2011-02-04 13:57:55 -0800329 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700330 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700331 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700332 if (mTests.size() > 1000) {
333 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700334 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800335 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800336 return instrTest;
337 }
338
339 /**
Brett Chabot87f44512012-11-16 16:00:02 -0800340 * Populates given {@link UiAutomatorJarTest} with data from the package xml.
341 *
342 * @param uiautomatorTest
343 * @return the populated {@link UiAutomatorJarTest} or <code>null</code>
344 */
345 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 }
356 uiautomatorTest.setRunName(getUri());
357 uiautomatorTest.setCaptureLogs(false);
358 return uiautomatorTest;
359 }
360
361 /**
Brett Chabot13638ab2011-10-16 13:59:03 -0700362 * Filter the tests to run based on list of 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() {
367 mExcludedTestFilter.setTestInclusion(mClassName, mMethodName);
368 return mExcludedTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800369 }
370
371 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800372 * {@inheritDoc}
373 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800374 @Override
Brett Chabot4f8143c2010-12-14 18:29:44 -0800375 public boolean isKnownTest(TestIdentifier testDef) {
376 return mTests.contains(testDef);
377 }
378
379 /**
Brett Chabotef5a6042011-01-19 19:54:14 -0800380 * {@inheritDoc}
381 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800382 @Override
Brett Chabotef5a6042011-01-19 19:54:14 -0800383 public boolean isKnownTestClass(String className) {
384 return mTestClasses.contains(className);
385 }
386
387 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800388 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700389 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800390 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700391 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700392 */
Keun young Park34246562012-09-18 11:18:20 -0700393 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700394 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800395 mTestClasses.add(testDef.getClassName());
Keun young Park34246562012-09-18 11:18:20 -0700396 // 0 means no timeout, so keep 0 if already is.
397 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
398 mTimeoutInMins = timeout;
399 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700400 }
401
402 /**
403 * Get the collection of tests in this test package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700404 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800405 @Override
406 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700407 return mTests;
408 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700409
410 /**
411 * {@inheritDoc}
412 */
413 @Override
414 public String getDigest() {
415 return mDigest;
416 }
417
418 /**
419 * Generate a sha1sum digest for a file.
420 * <p/>
421 * Exposed for unit testing.
422 *
423 * @param fileDir the directory of the file
424 * @param fileName the name of the file
425 * @return a hex {@link String} of the digest
426 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700427 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700428 final String algorithm = "SHA-1";
429 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700430 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700431 try {
432 fileStream = getFileStream(fileDir, fileName);
433 MessageDigest md = MessageDigest.getInstance(algorithm);
434 d = new DigestInputStream(fileStream, md);
435 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800436 while (d.read(buffer) != -1) {
437 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700438 return toHexString(md.digest());
439 } catch (NoSuchAlgorithmException e) {
440 return algorithm + " not found";
441 } catch (IOException e) {
442 CLog.e(e);
443 } finally {
444 StreamUtil.closeStream(d);
445 StreamUtil.closeStream(fileStream);
446 }
447 return "failed to generate digest";
448 }
449
450 /**
451 * Retrieve an input stream for given file
452 * <p/>
453 * Exposed so unit tests can mock.
454 */
455 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
456 InputStream fileStream;
457 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
458 return fileStream;
459 }
460
461 /**
462 * Convert the given byte array into a lowercase hex string.
463 *
464 * @param arr The array to convert.
465 * @return The hex encoded string.
466 */
467 private String toHexString(byte[] arr) {
468 StringBuffer buf = new StringBuffer(arr.length * 2);
469 for (byte b : arr) {
470 buf.append(String.format("%02x", b & 0xFF));
471 }
472 return buf.toString();
473 }
Brett Chabot3add9162010-09-12 17:23:05 -0700474}