blob: c47758514bcdc1807f711a83690ab67adc00be05 [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";
Svetoslav Ganovb803f802012-03-07 19:13:05 -080049 public static final String ACCESSIBILITY_TEST =
Svetoslav12c82d42013-12-20 12:34:52 -080050 "com.android.cts.tradefed.testtype.AccessibilityTestRunner";
Brett Chabota595baa2013-07-01 11:55:36 -070051 public static final String ACCESSIBILITY_SERVICE_TEST =
Svetoslav12c82d42013-12-20 12:34:52 -080052 "com.android.cts.tradefed.testtype.AccessibilityServiceTestRunner";
53 public static final String PRINT_TEST =
54 "com.android.cts.tradefed.testtype.PrintTestRunner";
Craig Mautnera8e522f2012-10-22 11:46:30 -070055 public static final String DISPLAY_TEST =
56 "com.android.cts.tradefed.testtype.DisplayTestRunner";
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
Brett Chabot584d2b92011-02-03 17:56:49 -080060 private static final String SIGNATURE_TEST_METHOD = "testSignature";
Stuart Scottc09a2e02013-11-15 13:03:29 -080061 private static final String SIGNATURE_TEST_CLASS = "android.tests.sigtest.SignatureTest";
Brett Chabot3add9162010-09-12 17:23:05 -070062
63 private String mUri = null;
64 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;
Brett Chabot3add9162010-09-12 17:23:05 -070070 private boolean mIsSignatureTest = false;
Brian Carlstrom022aff42011-05-17 23:16:51 -070071 private String mTestPackageName = null;
Brett Chabot58c43a82011-09-13 14:13:57 -070072 private String mDigest = 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;
83 private TestFilter mExcludedTestFilter = 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
Brett Chabot3add9162010-09-12 17:23:05 -070091 void setUri(String uri) {
92 mUri = uri;
93 }
94
95 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -080096 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -070097 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -080098 @Override
Brett Chabot3add9162010-09-12 17:23:05 -070099 public String getUri() {
100 return mUri;
101 }
102
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700103 void setRunTimeArgs(String runTimeArgs) {
104 mRunTimeArgs = runTimeArgs;
105 }
106
107 String getRunTimeArgs() {
108 return mRunTimeArgs;
109 }
110
Brett Chabot3add9162010-09-12 17:23:05 -0700111 void setAppNameSpace(String appNameSpace) {
112 mAppNameSpace = appNameSpace;
113 }
114
115 String getAppNameSpace() {
116 return mAppNameSpace;
117 }
118
119 void setName(String name) {
120 mName = name;
121 }
122
Brett Chabot58c43a82011-09-13 14:13:57 -0700123 /**
124 * {@inheritDoc}
125 */
126 @Override
127 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700128 return mName;
129 }
130
131 void setRunner(String runnerName) {
132 mRunner = runnerName;
133 }
134
135 String getRunner() {
136 return mRunner;
137 }
138
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800139 void setTestType(String testType) {
140 mTestType = testType;
141 }
142
Brian Muramatsu60695072012-01-09 18:11:03 -0800143 String getTestType() {
144 return mTestType;
145 }
146
Brett Chabot3add9162010-09-12 17:23:05 -0700147 void setJarPath(String jarPath) {
148 mJarPath = jarPath;
149 }
150
151 String getJarPath() {
152 return mJarPath;
153 }
154
155 void setIsSignatureCheck(boolean isSignatureCheckTest) {
156 mIsSignatureTest = isSignatureCheckTest;
157 }
158
159 boolean isSignatureCheck() {
160 return mIsSignatureTest;
161 }
162
Brian Carlstrom022aff42011-05-17 23:16:51 -0700163 void setTestPackageName(String testPackageName) {
164 mTestPackageName = testPackageName;
165 }
166
Brett Chabot358dc562011-10-25 15:46:48 -0700167 void setTargetBinaryName(String targetBinaryName) {
168 mTargetBinaryName = targetBinaryName;
169 }
170
171 void setTargetNameSpace(String targetNameSpace) {
172 mTargetNameSpace = targetNameSpace;
173 }
174
175 @Override
176 public String getTargetApkName() {
177 if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) {
178 return String.format("%s.apk", mTargetBinaryName);
179 }
180 return null;
181 }
182
183 @Override
184 public String getTargetPackageName() {
185 if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) {
186 return null;
187 }
188 return mTargetNameSpace;
189 }
190
Brett Chabot3add9162010-09-12 17:23:05 -0700191 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800192 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700193 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700194 @Override
195 public void setExcludedTestFilter(TestFilter excludeFilter) {
196 mExcludedTestFilter = excludeFilter;
197 }
198
199 /**
200 * {@inheritDoc}
201 */
202 @Override
203 public void setClassName(String className, String methodName) {
204 mClassName = className;
205 mMethodName = methodName;
206 }
207
208 /**
209 * {@inheritDoc}
210 */
211 @Override
212 public IRemoteTest createTest(File testCaseDir) {
213 mExcludedTestFilter.setTestInclusion(mClassName, mMethodName);
214 mTests = filterTests();
215
Brian Muramatsu60695072012-01-09 18:11:03 -0800216 if (HOST_SIDE_ONLY_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700217 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700218 JarHostTest hostTest = new JarHostTest();
Keun young Park14a5bfa2012-10-02 13:45:30 -0700219 if (mTimeoutInMins >= 0) {
220 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
221 hostTest.setTimeout(mTimeoutInMins * 60 * 1000);
222 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700223 hostTest.setRunName(getUri());
Brett Chabotf4bec732011-04-19 17:31:06 -0700224 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700225 hostTest.setTests(mTests);
Brett Chabot58c43a82011-09-13 14:13:57 -0700226 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700227 return hostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800228 } else if (VM_HOST_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700229 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700230 VMHostTest vmHostTest = new VMHostTest();
Brett Chabot58c43a82011-09-13 14:13:57 -0700231 vmHostTest.setRunName(getUri());
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700232 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700233 vmHostTest.setTests(mTests);
Brett Chabot58c43a82011-09-13 14:13:57 -0700234 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700235 return vmHostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800236 } else if (NATIVE_TEST.equals(mTestType)) {
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800237 return new GeeTest(mUri, mName);
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700238 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
239 CLog.d("Creating new wrapped native test for %s", mName);
240 return new WrappedGTest(mAppNameSpace, mUri, mName, mRunner);
Svetoslav Ganovb803f802012-03-07 19:13:05 -0800241 } else if (ACCESSIBILITY_TEST.equals(mTestType)) {
242 AccessibilityTestRunner test = new AccessibilityTestRunner();
243 return setInstrumentationTest(test, testCaseDir);
Svetoslav12c82d42013-12-20 12:34:52 -0800244 } else if (PRINT_TEST.equals(mTestType)) {
245 PrintTestRunner test = new PrintTestRunner();
246 return setPrintTest(test, testCaseDir);
Brett Chabota595baa2013-07-01 11:55:36 -0700247 } else if (ACCESSIBILITY_SERVICE_TEST.equals(mTestType)) {
248 AccessibilityServiceTestRunner test = new AccessibilityServiceTestRunner();
249 return setInstrumentationTest(test, testCaseDir);
Craig Mautnera8e522f2012-10-22 11:46:30 -0700250 } else if (DISPLAY_TEST.equals(mTestType)) {
251 DisplayTestRunner test = new DisplayTestRunner();
252 return setInstrumentationTest(test, testCaseDir);
Brett Chabot87f44512012-11-16 16:00:02 -0800253 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
254 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
255 return setUiAutomatorTest(uiautomatorTest);
Brett Chabot3add9162010-09-12 17:23:05 -0700256 } else if (mIsSignatureTest) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700257 // TODO: hardcode the runner/class/method for now, since current package xml points to
258 // specialized instrumentation. Eventually this special case for signatureTest can be
259 // removed, and it can be treated just like a normal InstrumentationTest
260 CLog.d("Creating signature test %s", mName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700261 InstrumentationApkTest instrTest = new InstrumentationApkTest();
Brett Chabot584d2b92011-02-03 17:56:49 -0800262 instrTest.setPackageName(mAppNameSpace);
263 instrTest.setRunnerName("android.test.InstrumentationTestRunner");
264 instrTest.setClassName(SIGNATURE_TEST_CLASS);
265 instrTest.setMethodName(SIGNATURE_TEST_METHOD);
Brett Chabot13638ab2011-10-16 13:59:03 -0700266 // set expected tests to the single signature test
267 TestIdentifier t = new TestIdentifier(SIGNATURE_TEST_CLASS, SIGNATURE_TEST_METHOD);
268 mTests.clear();
269 mTests.add(t);
Brett Chabot584d2b92011-02-03 17:56:49 -0800270 // mName means 'apk file name' for instrumentation tests
Brett Chabotf4bec732011-04-19 17:31:06 -0700271 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700272 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot584d2b92011-02-03 17:56:49 -0800273 return instrTest;
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700274 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
275 CLog.d("Creating JUnit device test %s", mName);
276 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
277 jUnitDeviceTest.setRunName(getUri());
278 jUnitDeviceTest.addTestJarFileName(mJarPath);
279 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
280 jUnitDeviceTest.setTests(mTests);
281 mDigest = generateDigest(testCaseDir, mJarPath);
282 return jUnitDeviceTest;
Brett Chabota0f443c2011-02-04 13:57:55 -0800283 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700284 CLog.d("Creating instrumentation test for %s", mName);
Brett Chabot4263db42011-04-15 13:51:48 -0700285 InstrumentationApkTest instrTest = new InstrumentationApkTest();
Keun young Park34246562012-09-18 11:18:20 -0700286 if (mTimeoutInMins >= 0) {
287 // as timeout cannot be set for each test,
288 // increase the time-out of the whole package
289 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
290 instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000);
291 }
Brett Chabot53e68a32011-10-05 14:14:55 -0700292 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700293 }
294 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700295
Svetoslav12c82d42013-12-20 12:34:52 -0800296 private PrintTestRunner setPrintTest(PrintTestRunner printTest,
297 File testCaseDir) {
298 printTest.setRunName(getUri());
299 printTest.setPackageName(mAppNameSpace);
300 printTest.setRunnerName(mRunner);
301 printTest.setTestPackageName(mTestPackageName);
302 printTest.setClassName(mClassName);
303 printTest.setMethodName(mMethodName);
304 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
305 return printTest;
306 }
307
Brett Chabot82d9daf2010-10-20 20:07:14 -0700308 /**
Brett Chabot13638ab2011-10-16 13:59:03 -0700309 * Populates given {@link InstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800310 *
311 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800312 * @param instrTest
313 * @return the populated {@link InstrumentationTest} or <code>null</code>
314 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700315 private InstrumentationTest setInstrumentationTest(InstrumentationApkTest instrTest,
316 File testCaseDir) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700317 instrTest.setRunName(getUri());
Brett Chabota0f443c2011-02-04 13:57:55 -0800318 instrTest.setPackageName(mAppNameSpace);
319 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700320 instrTest.setTestPackageName(mTestPackageName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700321 instrTest.setClassName(mClassName);
322 instrTest.setMethodName(mMethodName);
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 */
342 private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) {
343 uiautomatorTest.setInstallArtifacts(getJarPath());
344 if (mClassName != null) {
345 if (mMethodName != null) {
346 CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" +
347 "running individual methods");
348 }
349 uiautomatorTest.addClassName(mClassName);
350 } else {
351 uiautomatorTest.addClassNames(mTestClasses);
352 }
353 uiautomatorTest.setRunName(getUri());
354 uiautomatorTest.setCaptureLogs(false);
355 return uiautomatorTest;
356 }
357
358 /**
Brett Chabot13638ab2011-10-16 13:59:03 -0700359 * Filter the tests to run based on list of excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800360 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800361 * @return the filtered collection of tests
362 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700363 private Collection<TestIdentifier> filterTests() {
364 mExcludedTestFilter.setTestInclusion(mClassName, mMethodName);
365 return mExcludedTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800366 }
367
368 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800369 * {@inheritDoc}
370 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800371 @Override
Brett Chabot4f8143c2010-12-14 18:29:44 -0800372 public boolean isKnownTest(TestIdentifier testDef) {
373 return mTests.contains(testDef);
374 }
375
376 /**
Brett Chabotef5a6042011-01-19 19:54:14 -0800377 * {@inheritDoc}
378 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800379 @Override
Brett Chabotef5a6042011-01-19 19:54:14 -0800380 public boolean isKnownTestClass(String className) {
381 return mTestClasses.contains(className);
382 }
383
384 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800385 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700386 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800387 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700388 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700389 */
Keun young Park34246562012-09-18 11:18:20 -0700390 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700391 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800392 mTestClasses.add(testDef.getClassName());
Keun young Park34246562012-09-18 11:18:20 -0700393 // 0 means no timeout, so keep 0 if already is.
394 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
395 mTimeoutInMins = timeout;
396 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700397 }
398
399 /**
400 * Get the collection of tests in this test package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700401 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800402 @Override
403 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700404 return mTests;
405 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700406
407 /**
408 * {@inheritDoc}
409 */
410 @Override
411 public String getDigest() {
412 return mDigest;
413 }
414
415 /**
416 * Generate a sha1sum digest for a file.
417 * <p/>
418 * Exposed for unit testing.
419 *
420 * @param fileDir the directory of the file
421 * @param fileName the name of the file
422 * @return a hex {@link String} of the digest
423 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700424 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700425 final String algorithm = "SHA-1";
426 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700427 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700428 try {
429 fileStream = getFileStream(fileDir, fileName);
430 MessageDigest md = MessageDigest.getInstance(algorithm);
431 d = new DigestInputStream(fileStream, md);
432 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800433 while (d.read(buffer) != -1) {
434 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700435 return toHexString(md.digest());
436 } catch (NoSuchAlgorithmException e) {
437 return algorithm + " not found";
438 } catch (IOException e) {
439 CLog.e(e);
440 } finally {
441 StreamUtil.closeStream(d);
442 StreamUtil.closeStream(fileStream);
443 }
444 return "failed to generate digest";
445 }
446
447 /**
448 * Retrieve an input stream for given file
449 * <p/>
450 * Exposed so unit tests can mock.
451 */
452 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
453 InputStream fileStream;
454 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
455 return fileStream;
456 }
457
458 /**
459 * Convert the given byte array into a lowercase hex string.
460 *
461 * @param arr The array to convert.
462 * @return The hex encoded string.
463 */
464 private String toHexString(byte[] arr) {
465 StringBuffer buf = new StringBuffer(arr.length * 2);
466 for (byte b : arr) {
467 buf.append(String.format("%02x", b & 0xFF));
468 }
469 return buf.toString();
470 }
Brett Chabot3add9162010-09-12 17:23:05 -0700471}