blob: 12c3dddaebca498ff23113b158ccfb6718439c30 [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;
Stuart Scotte00a2b82014-09-05 17:37:18 -070069 private IAbi mAbi = null;
Guang Zhu7afa91e2015-02-06 00:44:34 -080070 private List<ITargetPreparer> mPreparers = null;
Brett Chabot3add9162010-09-12 17:23:05 -070071
Brett Chabot13638ab2011-10-16 13:59:03 -070072 // use a LinkedHashSet for predictable iteration insertion-order, and fast
73 // lookups
Brett Chabotef5a6042011-01-19 19:54:14 -080074 private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>();
75 // also maintain an index of known test classes
76 private Collection<String> mTestClasses = new LinkedHashSet<String>();
Jarkko Pöyry0df34742015-01-14 10:46:41 -080077 // store instance arguments in order too for consistency
78 private Map<TestIdentifier, List<Map<String, String>>> mTestInstanceArguments =
79 new LinkedHashMap<>();
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;
Stuart Scotte00a2b82014-09-05 17:37:18 -070084 private TestFilter mTestFilter = 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
Stuart Scotte00a2b82014-09-05 17:37:18 -070092 @Override
93 public IAbi getAbi() {
94 return mAbi;
95 }
96
97 /**
98 * @param abi the ABI to run this package on
99 */
100 public void setAbi(IAbi abi) {
101 mAbi = abi;
102 }
103
104 /**
105 * @return unique id representing this test package for this ABI.
106 */
107 @Override
108 public String getId() {
109 return AbiUtils.createId(getAbi().getName(), getAppPackageName());
110 }
111
112 void setAppPackageName(String appPackageName) {
113 mAppPackageName = appPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700114 }
115
Nicholas Sauer6396d212014-11-05 20:10:21 -0800116 String getAppPackageName() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700117 return mAppPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700118 }
119
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700120 void setRunTimeArgs(String runTimeArgs) {
121 mRunTimeArgs = runTimeArgs;
122 }
123
Brett Chabot3add9162010-09-12 17:23:05 -0700124 void setAppNameSpace(String appNameSpace) {
125 mAppNameSpace = appNameSpace;
126 }
127
128 String getAppNameSpace() {
129 return mAppNameSpace;
130 }
131
132 void setName(String name) {
133 mName = name;
134 }
135
Brett Chabot58c43a82011-09-13 14:13:57 -0700136 /**
137 * {@inheritDoc}
138 */
139 @Override
140 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700141 return mName;
142 }
143
144 void setRunner(String runnerName) {
145 mRunner = runnerName;
146 }
147
148 String getRunner() {
149 return mRunner;
150 }
151
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800152 void setTestType(String testType) {
153 mTestType = testType;
154 }
155
Brian Muramatsu60695072012-01-09 18:11:03 -0800156 String getTestType() {
157 return mTestType;
158 }
159
Brett Chabot3add9162010-09-12 17:23:05 -0700160 void setJarPath(String jarPath) {
161 mJarPath = jarPath;
162 }
163
164 String getJarPath() {
165 return mJarPath;
166 }
167
Brian Carlstrom022aff42011-05-17 23:16:51 -0700168 void setTestPackageName(String testPackageName) {
169 mTestPackageName = testPackageName;
170 }
171
Brett Chabot358dc562011-10-25 15:46:48 -0700172 void setTargetBinaryName(String targetBinaryName) {
173 mTargetBinaryName = targetBinaryName;
174 }
175
176 void setTargetNameSpace(String targetNameSpace) {
177 mTargetNameSpace = targetNameSpace;
178 }
179
180 @Override
181 public String getTargetApkName() {
182 if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) {
183 return String.format("%s.apk", mTargetBinaryName);
184 }
185 return null;
186 }
187
188 @Override
189 public String getTargetPackageName() {
190 if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) {
191 return null;
192 }
193 return mTargetNameSpace;
194 }
195
Brett Chabot3add9162010-09-12 17:23:05 -0700196 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800197 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700198 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700199 @Override
Stuart Scotte00a2b82014-09-05 17:37:18 -0700200 public void setTestFilter(TestFilter testFilter) {
201 mTestFilter = testFilter;
Brett Chabot53e68a32011-10-05 14:14:55 -0700202 }
203
204 /**
205 * {@inheritDoc}
206 */
207 @Override
208 public void setClassName(String className, String methodName) {
209 mClassName = className;
210 mMethodName = methodName;
211 }
212
213 /**
Guang Zhu7afa91e2015-02-06 00:44:34 -0800214 * Setter for injecting a list of {@link ITargetPreparer}s as configured in module test config.
215 * @param preparers
216 */
217 void setPackagePreparers(List<ITargetPreparer> preparers) {
218 mPreparers = preparers;
219 }
220
221 /**
222 * {@inheritDoc}
223 */
224 @Override
225 public List<ITargetPreparer> getPackagePreparers() {
226 return mPreparers;
227 }
228
229 /**
Brett Chabot53e68a32011-10-05 14:14:55 -0700230 * {@inheritDoc}
231 */
232 @Override
233 public IRemoteTest createTest(File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700234 mTestFilter.setTestInclusion(mClassName, mMethodName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700235 mTests = filterTests();
236
Brian Muramatsu60695072012-01-09 18:11:03 -0800237 if (HOST_SIDE_ONLY_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700238 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700239 JarHostTest hostTest = new JarHostTest();
Keun young Park14a5bfa2012-10-02 13:45:30 -0700240 if (mTimeoutInMins >= 0) {
241 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
242 hostTest.setTimeout(mTimeoutInMins * 60 * 1000);
243 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700244 hostTest.setRunName(mAppPackageName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700245 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700246 hostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700247 hostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700248 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700249 return hostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800250 } else if (VM_HOST_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700251 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700252 VMHostTest vmHostTest = new VMHostTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700253 vmHostTest.setRunName(mAppPackageName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700254 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700255 vmHostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700256 vmHostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700257 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700258 return vmHostTest;
Mika Isojärvif1e23ee2014-08-25 13:17:58 +0300259 } else if (DEQP_TEST.equals(mTestType)) {
Jarkko Pöyry91d00e02015-01-15 18:02:15 -0800260 DeqpTestRunner deqpTest =
261 new DeqpTestRunner(mAppPackageName, mName, mTests, mTestInstanceArguments);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700262 deqpTest.setAbi(mAbi);
263 return deqpTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800264 } else if (NATIVE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700265 GeeTest geeTest = new GeeTest(mAppPackageName, mName);
266 geeTest.setAbi(mAbi);
267 return geeTest;
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700268 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
269 CLog.d("Creating new wrapped native test for %s", mName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700270 WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner);
271 wrappedGeeTest.setAbi(mAbi);
272 return wrappedGeeTest;
Brett Chabot87f44512012-11-16 16:00:02 -0800273 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
274 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
275 return setUiAutomatorTest(uiautomatorTest);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700276 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
277 CLog.d("Creating JUnit device test %s", mName);
278 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700279 jUnitDeviceTest.setRunName(mAppPackageName);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700280 jUnitDeviceTest.addTestJarFileName(mJarPath);
281 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
282 jUnitDeviceTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700283 jUnitDeviceTest.setAbi(mAbi);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700284 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
299 /**
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700300 * Populates given {@link CtsInstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800301 *
302 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800303 * @param instrTest
304 * @return the populated {@link InstrumentationTest} or <code>null</code>
305 */
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700306 private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest,
Brett Chabot53e68a32011-10-05 14:14:55 -0700307 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700308 instrTest.setRunName(mAppPackageName);
Brett Chabota0f443c2011-02-04 13:57:55 -0800309 instrTest.setPackageName(mAppNameSpace);
310 instrTest.setRunnerName(mRunner);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700311 instrTest.setAbi(mAbi);
Nick Korostelev4bfd0752014-08-06 14:56:08 -0700312 instrTest.setTestsToRun(mTests, false
313 /* force batch mode off to always run using testFile */);
314 instrTest.setReRunUsingTestFile(true);
Brett Chabota0f443c2011-02-04 13:57:55 -0800315 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700316 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700317 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700318 if (mTests.size() > 1000) {
319 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700320 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800321 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800322 return instrTest;
323 }
324
325 /**
Brett Chabot87f44512012-11-16 16:00:02 -0800326 * Populates given {@link UiAutomatorJarTest} with data from the package xml.
327 *
328 * @param uiautomatorTest
329 * @return the populated {@link UiAutomatorJarTest} or <code>null</code>
330 */
Stuart Scotte00a2b82014-09-05 17:37:18 -0700331 @SuppressWarnings("deprecation")
Brett Chabot87f44512012-11-16 16:00:02 -0800332 private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) {
333 uiautomatorTest.setInstallArtifacts(getJarPath());
334 if (mClassName != null) {
335 if (mMethodName != null) {
336 CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" +
337 "running individual methods");
338 }
339 uiautomatorTest.addClassName(mClassName);
340 } else {
341 uiautomatorTest.addClassNames(mTestClasses);
342 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700343 uiautomatorTest.setRunName(mAppPackageName);
Brett Chabot87f44512012-11-16 16:00:02 -0800344 uiautomatorTest.setCaptureLogs(false);
345 return uiautomatorTest;
346 }
347
348 /**
Stuart Scotte00a2b82014-09-05 17:37:18 -0700349 * Filter the tests to run based on list of included/excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800350 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800351 * @return the filtered collection of tests
352 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700353 private Collection<TestIdentifier> filterTests() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700354 mTestFilter.setTestInclusion(mClassName, mMethodName);
355 return mTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800356 }
357
Nicholas Sauer6396d212014-11-05 20:10:21 -0800358 boolean isKnownTestClass(String className) {
Brett Chabotef5a6042011-01-19 19:54:14 -0800359 return mTestClasses.contains(className);
360 }
361
362 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800363 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700364 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800365 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700366 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700367 */
Keun young Park34246562012-09-18 11:18:20 -0700368 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700369 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800370 mTestClasses.add(testDef.getClassName());
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800371 mTestInstanceArguments.put(testDef, new LinkedList<Map<String, String>>());
Keun young Park34246562012-09-18 11:18:20 -0700372 // 0 means no timeout, so keep 0 if already is.
373 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
374 mTimeoutInMins = timeout;
375 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700376 }
377
378 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800379 * Add a test instance to an existing {@link TestIdentifier}.
380 */
381 void addTestInstance(TestIdentifier testDef, Map<String, String> instanceArguments) {
382 if (!mTestInstanceArguments.containsKey(testDef)) {
383 throw new IllegalStateException("test id does not name an existing test");
384 }
385 mTestInstanceArguments.get(testDef).add(instanceArguments);
386 }
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 /**
Jarkko Pöyry0df34742015-01-14 10:46:41 -0800397 * Get the instance argument map for tests.
398 * <p/>
399 * Exposed for unit testing.
400 */
401 public Map<TestIdentifier, List<Map<String, String>>> getTestInstanceArguments() {
402 return mTestInstanceArguments;
403 }
404
405 /**
Brett Chabot58c43a82011-09-13 14:13:57 -0700406 * {@inheritDoc}
407 */
408 @Override
409 public String getDigest() {
410 return mDigest;
411 }
412
413 /**
414 * Generate a sha1sum digest for a file.
415 * <p/>
416 * Exposed for unit testing.
417 *
418 * @param fileDir the directory of the file
419 * @param fileName the name of the file
420 * @return a hex {@link String} of the digest
421 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700422 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700423 final String algorithm = "SHA-1";
424 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700425 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700426 try {
427 fileStream = getFileStream(fileDir, fileName);
428 MessageDigest md = MessageDigest.getInstance(algorithm);
429 d = new DigestInputStream(fileStream, md);
430 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800431 while (d.read(buffer) != -1) {
432 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700433 return toHexString(md.digest());
434 } catch (NoSuchAlgorithmException e) {
435 return algorithm + " not found";
436 } catch (IOException e) {
437 CLog.e(e);
438 } finally {
Guang Zhu7afa91e2015-02-06 00:44:34 -0800439 StreamUtil.close(d);
440 StreamUtil.close(fileStream);
Brett Chabot58c43a82011-09-13 14:13:57 -0700441 }
442 return "failed to generate digest";
443 }
444
445 /**
446 * Retrieve an input stream for given file
447 * <p/>
448 * Exposed so unit tests can mock.
449 */
450 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
451 InputStream fileStream;
452 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
453 return fileStream;
454 }
455
456 /**
457 * Convert the given byte array into a lowercase hex string.
458 *
459 * @param arr The array to convert.
460 * @return The hex encoded string.
461 */
462 private String toHexString(byte[] arr) {
Nicholas Sauer6396d212014-11-05 20:10:21 -0800463 StringBuilder buf = new StringBuilder(arr.length * 2);
Brett Chabot58c43a82011-09-13 14:13:57 -0700464 for (byte b : arr) {
465 buf.append(String.format("%02x", b & 0xFF));
466 }
467 return buf.toString();
468 }
Nicholas Sauerc176cac2014-10-23 15:36:49 -0700469
470 @Override
471 public int compareTo(ITestPackageDef testPackageDef) {
472 return getId().compareTo(testPackageDef.getId());
473 }
Brett Chabot3add9162010-09-12 17:23:05 -0700474}