blob: 8f0ed2da560374e7b872c11cf8b3b51dfab6bcc3 [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 Chabot82d9daf2010-10-20 20:07:14 -070019import com.android.ddmlib.testrunner.TestIdentifier;
Brett Chabot58c43a82011-09-13 14:13:57 -070020import com.android.tradefed.log.LogUtil.CLog;
Brett Chabot3add9162010-09-12 17:23:05 -070021import com.android.tradefed.testtype.IRemoteTest;
22import com.android.tradefed.testtype.InstrumentationTest;
Brett Chabot58c43a82011-09-13 14:13:57 -070023import com.android.tradefed.util.StreamUtil;
Brett Chabot3add9162010-09-12 17:23:05 -070024
Brett Chabot58c43a82011-09-13 14:13:57 -070025import java.io.BufferedInputStream;
Brett Chabot3add9162010-09-12 17:23:05 -070026import java.io.File;
Brett Chabot58c43a82011-09-13 14:13:57 -070027import java.io.FileInputStream;
28import java.io.FileNotFoundException;
29import java.io.IOException;
30import java.io.InputStream;
31import java.security.DigestInputStream;
32import java.security.MessageDigest;
33import java.security.NoSuchAlgorithmException;
Brett Chabot82d9daf2010-10-20 20:07:14 -070034import java.util.Collection;
Brett Chabotef5a6042011-01-19 19:54:14 -080035import java.util.LinkedHashSet;
Brett Chabot3add9162010-09-12 17:23:05 -070036
37/**
38 * Container for CTS test info.
39 * <p/>
40 * Knows how to translate this info into a runnable {@link IRemoteTest}.
41 */
Brett Chabot4f8143c2010-12-14 18:29:44 -080042class TestPackageDef implements ITestPackageDef {
Brett Chabot3add9162010-09-12 17:23:05 -070043
Brett Chabot584d2b92011-02-03 17:56:49 -080044 private static final String SIGNATURE_TEST_METHOD = "testSignature";
45 private static final String SIGNATURE_TEST_CLASS = "android.tests.sigtest.SimpleSignatureTest";
Brett Chabot3add9162010-09-12 17:23:05 -070046
47 private String mUri = null;
48 private String mAppNameSpace = null;
49 private String mName = null;
50 private String mRunner = null;
51 private boolean mIsHostSideTest = false;
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -070052 private boolean mIsVMHostTest = false;
Brett Chabot3add9162010-09-12 17:23:05 -070053 private String mJarPath = null;
54 private boolean mIsSignatureTest = false;
55 private boolean mIsReferenceAppTest = false;
Brett Chabota0f443c2011-02-04 13:57:55 -080056 private String mPackageToTest = null;
57 private String mApkToTestName = null;
Brian Carlstrom022aff42011-05-17 23:16:51 -070058 private String mTestPackageName = null;
Brett Chabot58c43a82011-09-13 14:13:57 -070059 private String mDigest = null;
Brett Chabot3add9162010-09-12 17:23:05 -070060
Brett Chabot13638ab2011-10-16 13:59:03 -070061 // use a LinkedHashSet for predictable iteration insertion-order, and fast
62 // lookups
Brett Chabotef5a6042011-01-19 19:54:14 -080063 private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>();
64 // also maintain an index of known test classes
65 private Collection<String> mTestClasses = new LinkedHashSet<String>();
Brett Chabot4f8143c2010-12-14 18:29:44 -080066
Brett Chabot53e68a32011-10-05 14:14:55 -070067 // dynamic options, not parsed from package xml
68 private String mClassName;
69 private String mMethodName;
70 private TestFilter mExcludedTestFilter = new TestFilter();
71
Brett Chabot3add9162010-09-12 17:23:05 -070072 void setUri(String uri) {
73 mUri = uri;
74 }
75
76 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -080077 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -070078 */
79 public String getUri() {
80 return mUri;
81 }
82
83 void setAppNameSpace(String appNameSpace) {
84 mAppNameSpace = appNameSpace;
85 }
86
87 String getAppNameSpace() {
88 return mAppNameSpace;
89 }
90
91 void setName(String name) {
92 mName = name;
93 }
94
Brett Chabot58c43a82011-09-13 14:13:57 -070095 /**
96 * {@inheritDoc}
97 */
98 @Override
99 public String getName() {
Brett Chabot3add9162010-09-12 17:23:05 -0700100 return mName;
101 }
102
103 void setRunner(String runnerName) {
104 mRunner = runnerName;
105 }
106
107 String getRunner() {
108 return mRunner;
109 }
110
111 void setIsHostSideTest(boolean hostSideTest) {
112 mIsHostSideTest = hostSideTest;
113
114 }
115
116 boolean isHostSideTest() {
117 return mIsHostSideTest;
118 }
119
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700120 void setIsVMHostTest(boolean vmHostTest) {
121 mIsVMHostTest = vmHostTest;
122
123 }
124
125 boolean isVMHostTest() {
126 return mIsVMHostTest;
127 }
Brett Chabot13638ab2011-10-16 13:59:03 -0700128
Brett Chabot3add9162010-09-12 17:23:05 -0700129 void setJarPath(String jarPath) {
130 mJarPath = jarPath;
131 }
132
133 String getJarPath() {
134 return mJarPath;
135 }
136
137 void setIsSignatureCheck(boolean isSignatureCheckTest) {
138 mIsSignatureTest = isSignatureCheckTest;
139 }
140
141 boolean isSignatureCheck() {
142 return mIsSignatureTest;
143 }
144
145 void setIsReferenceApp(boolean isReferenceApp) {
146 mIsReferenceAppTest = isReferenceApp;
147 }
148
149 boolean isReferenceApp() {
150 return mIsReferenceAppTest;
151 }
152
Brett Chabota0f443c2011-02-04 13:57:55 -0800153 void setPackageToTest(String packageName) {
154 mPackageToTest = packageName;
155 }
156
Brian Carlstrom022aff42011-05-17 23:16:51 -0700157 void setTestPackageName(String testPackageName) {
158 mTestPackageName = testPackageName;
159 }
160
Brett Chabota0f443c2011-02-04 13:57:55 -0800161 void setApkToTest(String apkName) {
162 mApkToTestName = apkName;
163 }
164
Brett Chabot3add9162010-09-12 17:23:05 -0700165 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800166 * {@inheritDoc}
Brett Chabot3add9162010-09-12 17:23:05 -0700167 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700168 @Override
169 public void setExcludedTestFilter(TestFilter excludeFilter) {
170 mExcludedTestFilter = excludeFilter;
171 }
172
173 /**
174 * {@inheritDoc}
175 */
176 @Override
177 public void setClassName(String className, String methodName) {
178 mClassName = className;
179 mMethodName = methodName;
180 }
181
182 /**
183 * {@inheritDoc}
184 */
185 @Override
186 public IRemoteTest createTest(File testCaseDir) {
187 mExcludedTestFilter.setTestInclusion(mClassName, mMethodName);
188 mTests = filterTests();
189
Brett Chabot3add9162010-09-12 17:23:05 -0700190 if (mIsHostSideTest) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700191 CLog.d("Creating host test for %s", mName);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700192 JarHostTest hostTest = new JarHostTest();
Brett Chabot58c43a82011-09-13 14:13:57 -0700193 hostTest.setRunName(getUri());
Brett Chabotf4bec732011-04-19 17:31:06 -0700194 hostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700195 hostTest.setTests(mTests);
Brett Chabot58c43a82011-09-13 14:13:57 -0700196 mDigest = generateDigest(testCaseDir, mJarPath);
Brett Chabot82d9daf2010-10-20 20:07:14 -0700197 return hostTest;
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700198 } else if (mIsVMHostTest) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700199 CLog.d("Creating vm host test for %s", mName);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700200 VMHostTest vmHostTest = new VMHostTest();
Brett Chabot58c43a82011-09-13 14:13:57 -0700201 vmHostTest.setRunName(getUri());
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700202 vmHostTest.setJarFileName(mJarPath);
Brett Chabot53e68a32011-10-05 14:14:55 -0700203 vmHostTest.setTests(mTests);
Brett Chabot58c43a82011-09-13 14:13:57 -0700204 mDigest = generateDigest(testCaseDir, mJarPath);
Tsu Chiang Chuang9a223d72011-04-27 17:19:46 -0700205 return vmHostTest;
Brett Chabot3add9162010-09-12 17:23:05 -0700206 } else if (mIsSignatureTest) {
Brett Chabot13638ab2011-10-16 13:59:03 -0700207 // TODO: hardcode the runner/class/method for now, since current package xml points to
208 // specialized instrumentation. Eventually this special case for signatureTest can be
209 // removed, and it can be treated just like a normal InstrumentationTest
210 CLog.d("Creating signature test %s", mName);
Brett Chabotf4bec732011-04-19 17:31:06 -0700211 InstrumentationApkTest instrTest = new InstrumentationApkTest();
Brett Chabot584d2b92011-02-03 17:56:49 -0800212 instrTest.setPackageName(mAppNameSpace);
213 instrTest.setRunnerName("android.test.InstrumentationTestRunner");
214 instrTest.setClassName(SIGNATURE_TEST_CLASS);
215 instrTest.setMethodName(SIGNATURE_TEST_METHOD);
Brett Chabot13638ab2011-10-16 13:59:03 -0700216 // set expected tests to the single signature test
217 TestIdentifier t = new TestIdentifier(SIGNATURE_TEST_CLASS, SIGNATURE_TEST_METHOD);
218 mTests.clear();
219 mTests.add(t);
Brett Chabot584d2b92011-02-03 17:56:49 -0800220 // mName means 'apk file name' for instrumentation tests
Brett Chabotf4bec732011-04-19 17:31:06 -0700221 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700222 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot584d2b92011-02-03 17:56:49 -0800223 return instrTest;
Brett Chabot3add9162010-09-12 17:23:05 -0700224 } else if (mIsReferenceAppTest) {
Brett Chabota0f443c2011-02-04 13:57:55 -0800225 // a reference app test is just a InstrumentationTest with one extra apk to install
Brett Chabot4263db42011-04-15 13:51:48 -0700226 InstrumentationApkTest instrTest = new InstrumentationApkTest();
227 instrTest.addInstallApk(String.format("%s.apk", mApkToTestName), mPackageToTest);
Brett Chabot53e68a32011-10-05 14:14:55 -0700228 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabota0f443c2011-02-04 13:57:55 -0800229 } else {
Brett Chabot13638ab2011-10-16 13:59:03 -0700230 CLog.d("Creating instrumentation test for %s", mName);
Brett Chabot4263db42011-04-15 13:51:48 -0700231 InstrumentationApkTest instrTest = new InstrumentationApkTest();
Brett Chabot53e68a32011-10-05 14:14:55 -0700232 return setInstrumentationTest(instrTest, testCaseDir);
Brett Chabot3add9162010-09-12 17:23:05 -0700233 }
234 }
Brett Chabot82d9daf2010-10-20 20:07:14 -0700235
236 /**
Brett Chabot13638ab2011-10-16 13:59:03 -0700237 * Populates given {@link InstrumentationApkTest} with data from the package xml.
Brett Chabota0f443c2011-02-04 13:57:55 -0800238 *
239 * @param testCaseDir
240 * @param className
241 * @param methodName
242 * @param instrTest
243 * @return the populated {@link InstrumentationTest} or <code>null</code>
244 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700245 private InstrumentationTest setInstrumentationTest(InstrumentationApkTest instrTest,
246 File testCaseDir) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700247 instrTest.setRunName(getUri());
Brett Chabota0f443c2011-02-04 13:57:55 -0800248 instrTest.setPackageName(mAppNameSpace);
249 instrTest.setRunnerName(mRunner);
Brian Carlstrom022aff42011-05-17 23:16:51 -0700250 instrTest.setTestPackageName(mTestPackageName);
Brett Chabot53e68a32011-10-05 14:14:55 -0700251 instrTest.setClassName(mClassName);
252 instrTest.setMethodName(mMethodName);
253 instrTest.setTestsToRun(mTests,
254 !mExcludedTestFilter.hasExclusion()
255 /* only force batch mode if no tests are excluded */);
Brett Chabota0f443c2011-02-04 13:57:55 -0800256 // mName means 'apk file name' for instrumentation tests
Brett Chabot4263db42011-04-15 13:51:48 -0700257 instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace);
Brett Chabot58c43a82011-09-13 14:13:57 -0700258 mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName));
Brett Chabot4263db42011-04-15 13:51:48 -0700259 if (mTests.size() > 1000) {
260 // TODO: hack, large test suites can take longer to collect tests, increase timeout
Brett Chabot13638ab2011-10-16 13:59:03 -0700261 instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000);
Brett Chabota0f443c2011-02-04 13:57:55 -0800262 }
Brett Chabota0f443c2011-02-04 13:57:55 -0800263 return instrTest;
264 }
265
266 /**
Brett Chabot13638ab2011-10-16 13:59:03 -0700267 * Filter the tests to run based on list of excluded tests, class and method name.
Brett Chabotef5a6042011-01-19 19:54:14 -0800268 *
Brett Chabotef5a6042011-01-19 19:54:14 -0800269 * @return the filtered collection of tests
270 */
Brett Chabot53e68a32011-10-05 14:14:55 -0700271 private Collection<TestIdentifier> filterTests() {
272 mExcludedTestFilter.setTestInclusion(mClassName, mMethodName);
273 return mExcludedTestFilter.filter(mTests);
Brett Chabotef5a6042011-01-19 19:54:14 -0800274 }
275
276 /**
Brett Chabot4f8143c2010-12-14 18:29:44 -0800277 * {@inheritDoc}
278 */
279 public boolean isKnownTest(TestIdentifier testDef) {
280 return mTests.contains(testDef);
281 }
282
283 /**
Brett Chabotef5a6042011-01-19 19:54:14 -0800284 * {@inheritDoc}
285 */
286 public boolean isKnownTestClass(String className) {
287 return mTestClasses.contains(className);
288 }
289
290 /**
Brett Chabot82d9daf2010-10-20 20:07:14 -0700291 * Add a {@link TestDef} to the list of tests in this package.
292 *
293 * @param testdef
294 */
295 void addTest(TestIdentifier testDef) {
296 mTests.add(testDef);
Brett Chabotef5a6042011-01-19 19:54:14 -0800297 mTestClasses.add(testDef.getClassName());
Brett Chabot82d9daf2010-10-20 20:07:14 -0700298 }
299
300 /**
301 * Get the collection of tests in this test package.
Brett Chabot82d9daf2010-10-20 20:07:14 -0700302 */
Brett Chabot1dcb9a52011-02-10 20:26:57 -0800303 @Override
304 public Collection<TestIdentifier> getTests() {
Brett Chabot82d9daf2010-10-20 20:07:14 -0700305 return mTests;
306 }
Brett Chabot58c43a82011-09-13 14:13:57 -0700307
308 /**
309 * {@inheritDoc}
310 */
311 @Override
312 public String getDigest() {
313 return mDigest;
314 }
315
316 /**
317 * Generate a sha1sum digest for a file.
318 * <p/>
319 * Exposed for unit testing.
320 *
321 * @param fileDir the directory of the file
322 * @param fileName the name of the file
323 * @return a hex {@link String} of the digest
324 */
Brett Chabot13638ab2011-10-16 13:59:03 -0700325 String generateDigest(File fileDir, String fileName) {
Brett Chabot58c43a82011-09-13 14:13:57 -0700326 final String algorithm = "SHA-1";
327 InputStream fileStream = null;
Brett Chabot13638ab2011-10-16 13:59:03 -0700328 DigestInputStream d = null;
Brett Chabot58c43a82011-09-13 14:13:57 -0700329 try {
330 fileStream = getFileStream(fileDir, fileName);
331 MessageDigest md = MessageDigest.getInstance(algorithm);
332 d = new DigestInputStream(fileStream, md);
333 byte[] buffer = new byte[8196];
334 while (d.read(buffer) != -1);
335 return toHexString(md.digest());
336 } catch (NoSuchAlgorithmException e) {
337 return algorithm + " not found";
338 } catch (IOException e) {
339 CLog.e(e);
340 } finally {
341 StreamUtil.closeStream(d);
342 StreamUtil.closeStream(fileStream);
343 }
344 return "failed to generate digest";
345 }
346
347 /**
348 * Retrieve an input stream for given file
349 * <p/>
350 * Exposed so unit tests can mock.
351 */
352 InputStream getFileStream(File fileDir, String fileName) throws FileNotFoundException {
353 InputStream fileStream;
354 fileStream = new BufferedInputStream(new FileInputStream(new File(fileDir, fileName)));
355 return fileStream;
356 }
357
358 /**
359 * Convert the given byte array into a lowercase hex string.
360 *
361 * @param arr The array to convert.
362 * @return The hex encoded string.
363 */
364 private String toHexString(byte[] arr) {
365 StringBuffer buf = new StringBuffer(arr.length * 2);
366 for (byte b : arr) {
367 buf.append(String.format("%02x", b & 0xFF));
368 }
369 return buf.toString();
370 }
Brett Chabot3add9162010-09-12 17:23:05 -0700371}