blob: 93898162a51fbfb35f8ecfd366a61a59fd6aff12 [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
115 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800116 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700117 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800118 @Override
Stuart Scotte00a2b82014-09-05 17:37:18 -0700119 public String getAppPackageName() {
120 return mAppPackageName;
Brett Chabot3add9162010-09-12 17:23:05 -0700121 }
122
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700123 void setRunTimeArgs(String runTimeArgs) {
124 mRunTimeArgs = runTimeArgs;
125 }
126
127 String getRunTimeArgs() {
128 return mRunTimeArgs;
129 }
130
Brett Chabot3add9162010-09-12 17:23:05 -0700131 void setAppNameSpace(String appNameSpace) {
132 mAppNameSpace = appNameSpace;
133 }
134
135 String getAppNameSpace() {
136 return mAppNameSpace;
137 }
138
139 void setName(String name) {
140 mName = name;
141 }
142
Brett Chabot58c43a82011-09-13 14:13:57 -0700143 /**
144 * {@inheritDoc}
145 */
146 @Override
147 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700148 return mName;
149 }
150
151 void setRunner(String runnerName) {
152 mRunner = runnerName;
153 }
154
155 String getRunner() {
156 return mRunner;
157 }
158
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800159 void setTestType(String testType) {
160 mTestType = testType;
161 }
162
Brian Muramatsu60695072012-01-09 18:11:03 -0800163 String getTestType() {
164 return mTestType;
165 }
166
Brett Chabot3add9162010-09-12 17:23:05 -0700167 void setJarPath(String jarPath) {
168 mJarPath = jarPath;
169 }
170
171 String getJarPath() {
172 return mJarPath;
173 }
174
Brian Carlstrom022aff42011-05-17 23:16:51 -0700175 void setTestPackageName(String testPackageName) {
176 mTestPackageName = testPackageName;
177 }
178
Brett Chabot358dc562011-10-25 15:46:48 -0700179 void setTargetBinaryName(String targetBinaryName) {
180 mTargetBinaryName = targetBinaryName;
181 }
182
183 void setTargetNameSpace(String targetNameSpace) {
184 mTargetNameSpace = targetNameSpace;
185 }
186
187 @Override
188 public String getTargetApkName() {
189 if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) {
190 return String.format("%s.apk", mTargetBinaryName);
191 }
192 return null;
193 }
194
195 @Override
196 public String getTargetPackageName() {
197 if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) {
198 return null;
199 }
200 return mTargetNameSpace;
201 }
202
Brett Chabot3add9162010-09-12 17:23:05 -0700203 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800204 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700205 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700206 @Override
Stuart Scotte00a2b82014-09-05 17:37:18 -0700207 public void setTestFilter(TestFilter testFilter) {
208 mTestFilter = testFilter;
Brett Chabot53e68a32011-10-05 14:14:55 -0700209 }
210
211 /**
212 * {@inheritDoc}
213 */
214 @Override
215 public void setClassName(String className, String methodName) {
216 mClassName = className;
217 mMethodName = methodName;
218 }
219
220 /**
221 * {@inheritDoc}
222 */
223 @Override
224 public IRemoteTest createTest(File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700225 mTestFilter.setTestInclusion(mClassName, mMethodName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700226 mTests = filterTests();
227
Brian Muramatsu60695072012-01-09 18:11:03 -0800228 if (HOST_SIDE_ONLY_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700229 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700230 JarHostTest hostTest = new JarHostTest();
Keun young Park14a5bfa2012-10-02 13:45:30 -0700231 if (mTimeoutInMins >= 0) {
232 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
233 hostTest.setTimeout(mTimeoutInMins * 60 * 1000);
234 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700235 hostTest.setRunName(mAppPackageName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700236 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700237 hostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700238 hostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700239 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700240 return hostTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800241 } else if (VM_HOST_TEST.equals(mTestType)) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700242 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700243 VMHostTest vmHostTest = new VMHostTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700244 vmHostTest.setRunName(mAppPackageName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700245 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700246 vmHostTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700247 vmHostTest.setAbi(mAbi);
Brett Chabot58c43a82011-09-13 14:13:57 -0700248 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700249 return vmHostTest;
Mika Isojärvif1e23ee2014-08-25 13:17:58 +0300250 } else if (DEQP_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700251 DeqpTestRunner deqpTest = new DeqpTestRunner(mAppPackageName, mName, mTests);
252 deqpTest.setAbi(mAbi);
253 return deqpTest;
Brian Muramatsu60695072012-01-09 18:11:03 -0800254 } else if (NATIVE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700255 GeeTest geeTest = new GeeTest(mAppPackageName, mName);
256 geeTest.setAbi(mAbi);
257 return geeTest;
Thomas Tafertshoferb5385a62012-08-15 15:40:45 -0700258 } else if (WRAPPED_NATIVE_TEST.equals(mTestType)) {
259 CLog.d("Creating new wrapped native test for %s", mName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700260 WrappedGTest wrappedGeeTest = new WrappedGTest(mAppNameSpace, mAppPackageName, mName, mRunner);
261 wrappedGeeTest.setAbi(mAbi);
262 return wrappedGeeTest;
Svetoslav Ganovb803f802012-03-07 19:13:05 -0800263 } else if (ACCESSIBILITY_TEST.equals(mTestType)) {
264 AccessibilityTestRunner test = new AccessibilityTestRunner();
265 return setInstrumentationTest(test, testCaseDir);
Svetoslav12c82d42013-12-20 12:34:52 -0800266 } else if (PRINT_TEST.equals(mTestType)) {
267 PrintTestRunner test = new PrintTestRunner();
268 return setPrintTest(test, testCaseDir);
Brett Chabota595baa2013-07-01 11:55:36 -0700269 } else if (ACCESSIBILITY_SERVICE_TEST.equals(mTestType)) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700270 @SuppressWarnings("deprecation")
Brett Chabota595baa2013-07-01 11:55:36 -0700271 AccessibilityServiceTestRunner test = new AccessibilityServiceTestRunner();
272 return setInstrumentationTest(test, testCaseDir);
Craig Mautnera8e522f2012-10-22 11:46:30 -0700273 } else if (DISPLAY_TEST.equals(mTestType)) {
274 DisplayTestRunner test = new DisplayTestRunner();
275 return setInstrumentationTest(test, testCaseDir);
Brett Chabot87f44512012-11-16 16:00:02 -0800276 } else if (UIAUTOMATOR_TEST.equals(mTestType)) {
277 UiAutomatorJarTest uiautomatorTest = new UiAutomatorJarTest();
278 return setUiAutomatorTest(uiautomatorTest);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700279 } else if (JUNIT_DEVICE_TEST.equals(mTestType)){
280 CLog.d("Creating JUnit device test %s", mName);
281 JUnitDeviceTest jUnitDeviceTest = new JUnitDeviceTest();
Stuart Scotte00a2b82014-09-05 17:37:18 -0700282 jUnitDeviceTest.setRunName(mAppPackageName);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700283 jUnitDeviceTest.addTestJarFileName(mJarPath);
284 jUnitDeviceTest.addRunTimeArgs(mRunTimeArgs);
285 jUnitDeviceTest.setTests(mTests);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700286 jUnitDeviceTest.setAbi(mAbi);
Tsu Chiang Chuangd688d4b2014-06-26 11:12:37 -0700287 mDigest = generateDigest(testCaseDir, mJarPath);
288 return jUnitDeviceTest;
Brett Chabota0f443c2011-02-04 13:57:55 -0800289 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700290 CLog.d("Creating instrumentation test for %s", mName);
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700291 CtsInstrumentationApkTest instrTest = new CtsInstrumentationApkTest();
Keun young Park34246562012-09-18 11:18:20 -0700292 if (mTimeoutInMins >= 0) {
293 // as timeout cannot be set for each test,
294 // increase the time-out of the whole package
295 CLog.d("Setting new timeout to " + mTimeoutInMins + " mins");
296 instrTest.setTestTimeout(mTimeoutInMins * 60 * 1000);
297 }
Brett Chabot53e68a32011-10-05 14:14:55 -0700298 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700299 }
300 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700301
Svetoslav12c82d42013-12-20 12:34:52 -0800302 private PrintTestRunner setPrintTest(PrintTestRunner printTest,
303 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700304 printTest.setRunName(mAppPackageName);
Svetoslav12c82d42013-12-20 12:34:52 -0800305 printTest.setPackageName(mAppNameSpace);
306 printTest.setRunnerName(mRunner);
307 printTest.setTestPackageName(mTestPackageName);
308 printTest.setClassName(mClassName);
309 printTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700310 printTest.setAbi(mAbi);
Svetoslav12c82d42013-12-20 12:34:52 -0800311 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
312 return printTest;
313 }
314
Brett Chabot82d9daf2010-10-20 20:07:14 -0700315 /**
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700316 * Populates given {@link CtsInstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800317 *
318 * @param testCaseDir
Brett Chabota0f443c2011-02-04 13:57:55 -0800319 * @param instrTest
320 * @return the populated {@link InstrumentationTest} or <code>null</code>
321 */
Stuart Scottfef5e2c2014-09-05 17:37:18 -0700322 private InstrumentationTest setInstrumentationTest(CtsInstrumentationApkTest instrTest,
Brett Chabot53e68a32011-10-05 14:14:55 -0700323 File testCaseDir) {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700324 instrTest.setRunName(mAppPackageName);
Brett Chabota0f443c2011-02-04 13:57:55 -0800325 instrTest.setPackageName(mAppNameSpace);
326 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700327 instrTest.setTestPackageName(mTestPackageName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700328 instrTest.setClassName(mClassName);
329 instrTest.setMethodName(mMethodName);
Stuart Scotte00a2b82014-09-05 17:37:18 -0700330 instrTest.setAbi(mAbi);
Nick Korostelev4bfd0752014-08-06 14:56:08 -0700331 instrTest.setTestsToRun(mTests, false
332 /* force batch mode off to always run using testFile */);
333 instrTest.setReRunUsingTestFile(true);
Brett Chabota0f443c2011-02-04 13:57:55 -0800334 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700335 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700336 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700337 if (mTests.size() > 1000) {
338 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700339 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800340 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800341 return instrTest;
342 }
343
344 /**
Brett Chabot87f44512012-11-16 16:00:02 -0800345 * Populates given {@link UiAutomatorJarTest} with data from the package xml.
346 *
347 * @param uiautomatorTest
348 * @return the populated {@link UiAutomatorJarTest} or <code>null</code>
349 */
Stuart Scotte00a2b82014-09-05 17:37:18 -0700350 @SuppressWarnings("deprecation")
Brett Chabot87f44512012-11-16 16:00:02 -0800351 private IRemoteTest setUiAutomatorTest(UiAutomatorJarTest uiautomatorTest) {
352 uiautomatorTest.setInstallArtifacts(getJarPath());
353 if (mClassName != null) {
354 if (mMethodName != null) {
355 CLog.logAndDisplay(LogLevel.WARN, "ui automator tests don't currently support" +
356 "running individual methods");
357 }
358 uiautomatorTest.addClassName(mClassName);
359 } else {
360 uiautomatorTest.addClassNames(mTestClasses);
361 }
Stuart Scotte00a2b82014-09-05 17:37:18 -0700362 uiautomatorTest.setRunName(mAppPackageName);
Brett Chabot87f44512012-11-16 16:00:02 -0800363 uiautomatorTest.setCaptureLogs(false);
364 return uiautomatorTest;
365 }
366
367 /**
Stuart Scotte00a2b82014-09-05 17:37:18 -0700368 * Filter the tests to run based on list of included/excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800369 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800370 * @return the filtered collection of tests
371 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700372 private Collection<TestIdentifier> filterTests() {
Stuart Scotte00a2b82014-09-05 17:37:18 -0700373 mTestFilter.setTestInclusion(mClassName, mMethodName);
374 return mTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800375 }
376
377 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800378 * {@inheritDoc}
379 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800380 @Override
Brett Chabot4f8143c2010-12-14 18:29:44 -0800381 public boolean isKnownTest(TestIdentifier testDef) {
382 return mTests.contains(testDef);
383 }
384
385 /**
Brett Chabotef5a6042011-01-19 19:54:14 -0800386 * {@inheritDoc}
387 */
Brian Muramatsu88d32a82011-12-02 10:55:12 -0800388 @Override
Brett Chabotef5a6042011-01-19 19:54:14 -0800389 public boolean isKnownTestClass(String className) {
390 return mTestClasses.contains(className);
391 }
392
393 /**
Brian Muramatsu29d34782012-01-06 18:00:21 -0800394 * Add a {@link TestIdentifier} to the list of tests in this package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700395 *
Brian Muramatsu29d34782012-01-06 18:00:21 -0800396 * @param testDef
Keun young Park34246562012-09-18 11:18:20 -0700397 * @param timeout in mins
Brett Chabot82d9daf2010-10-20 20:07:14 -0700398 */
Keun young Park34246562012-09-18 11:18:20 -0700399 void addTest(TestIdentifier testDef, int timeout) {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700400 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800401 mTestClasses.add(testDef.getClassName());
Keun young Park34246562012-09-18 11:18:20 -0700402 // 0 means no timeout, so keep 0 if already is.
403 if ((timeout > mTimeoutInMins) && (mTimeoutInMins != 0)) {
404 mTimeoutInMins = timeout;
405 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700406 }
407
408 /**
Stuart Scottd6065522014-10-02 10:34:27 -0700409 * {@inheritDoc}
Brett Chabot82d9daf2010-10-20 20:07:14 -0700410 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800411 @Override
412 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700413 return mTests;
414 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700415
416 /**
417 * {@inheritDoc}
418 */
419 @Override
420 public String getDigest() {
421 return mDigest;
422 }
423
424 /**
425 * Generate a sha1sum digest for a file.
426 * <p/>
427 * Exposed for unit testing.
428 *
429 * @param fileDir the directory of the file
430 * @param fileName the name of the file
431 * @return a hex {@link String} of the digest
432 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700433 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700434 final String algorithm = "SHA-1";
435 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700436 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700437 try {
438 fileStream = getFileStream(fileDir, fileName);
439 MessageDigest md = MessageDigest.getInstance(algorithm);
440 d = new DigestInputStream(fileStream, md);
441 byte[] buffer = new byte[8196];
Brian Muramatsu29d34782012-01-06 18:00:21 -0800442 while (d.read(buffer) != -1) {
443 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700444 return toHexString(md.digest());
445 } catch (NoSuchAlgorithmException e) {
446 return algorithm + " not found";
447 } catch (IOException e) {
448 CLog.e(e);
449 } finally {
450 StreamUtil.closeStream(d);
451 StreamUtil.closeStream(fileStream);
452 }
453 return "failed to generate digest";
454 }
455
456 /**
457 * Retrieve an input stream for given file
458 * <p/>
459 * Exposed so unit tests can mock.
460 */
461 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
462 InputStream fileStream;
463 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
464 return fileStream;
465 }
466
467 /**
468 * Convert the given byte array into a lowercase hex string.
469 *
470 * @param arr The array to convert.
471 * @return The hex encoded string.
472 */
473 private String toHexString(byte[] arr) {
474 StringBuffer buf = new StringBuffer(arr.length * 2);
475 for (byte b : arr) {
476 buf.append(String.format("%02x", b & 0xFF));
477 }
478 return buf.toString();
479 }
Nicholas Sauerc176cac2014-10-23 15:36:49 -0700480
481 @Override
482 public int compareTo(ITestPackageDef testPackageDef) {
483 return getId().compareTo(testPackageDef.getId());
484 }
Brett Chabot3add9162010-09-12 17:23:05 -0700485}