Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 1 | /* |
| 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 Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 16 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 17 | package com.android.cts.tradefed.testtype; |
| 18 | |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 19 | import com.android.ddmlib.testrunner.TestIdentifier; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 20 | import com.android.tradefed.log.LogUtil.CLog; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 21 | import com.android.tradefed.testtype.IRemoteTest; |
| 22 | import com.android.tradefed.testtype.InstrumentationTest; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 23 | import com.android.tradefed.util.StreamUtil; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 24 | |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 25 | import java.io.BufferedInputStream; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 26 | import java.io.File; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 27 | import java.io.FileInputStream; |
| 28 | import java.io.FileNotFoundException; |
| 29 | import java.io.IOException; |
| 30 | import java.io.InputStream; |
| 31 | import java.security.DigestInputStream; |
| 32 | import java.security.MessageDigest; |
| 33 | import java.security.NoSuchAlgorithmException; |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 34 | import java.util.Collection; |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 35 | import java.util.LinkedHashSet; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 36 | |
| 37 | /** |
| 38 | * Container for CTS test info. |
| 39 | * <p/> |
| 40 | * Knows how to translate this info into a runnable {@link IRemoteTest}. |
| 41 | */ |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 42 | class TestPackageDef implements ITestPackageDef { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 43 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame^] | 44 | public static final String HOST_SIDE_ONLY_TEST = "hostSideOnly"; |
| 45 | public static final String NATIVE_TEST = "native"; |
| 46 | public static final String VM_HOST_TEST = "vmHostTest"; |
| 47 | |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 48 | private static final String SIGNATURE_TEST_METHOD = "testSignature"; |
| 49 | private static final String SIGNATURE_TEST_CLASS = "android.tests.sigtest.SimpleSignatureTest"; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 50 | |
| 51 | private String mUri = null; |
| 52 | private String mAppNameSpace = null; |
| 53 | private String mName = null; |
| 54 | private String mRunner = null; |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 55 | private boolean mIsVMHostTest = false; |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 56 | private String mTestType = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 57 | private String mJarPath = null; |
| 58 | private boolean mIsSignatureTest = false; |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 59 | private String mTestPackageName = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 60 | private String mDigest = null; |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 61 | |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 62 | // use a LinkedHashSet for predictable iteration insertion-order, and fast |
| 63 | // lookups |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 64 | private Collection<TestIdentifier> mTests = new LinkedHashSet<TestIdentifier>(); |
| 65 | // also maintain an index of known test classes |
| 66 | private Collection<String> mTestClasses = new LinkedHashSet<String>(); |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 67 | |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 68 | // dynamic options, not parsed from package xml |
| 69 | private String mClassName; |
| 70 | private String mMethodName; |
| 71 | private TestFilter mExcludedTestFilter = new TestFilter(); |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 72 | private String mTargetBinaryName; |
| 73 | private String mTargetNameSpace; |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 74 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 75 | void setUri(String uri) { |
| 76 | mUri = uri; |
| 77 | } |
| 78 | |
| 79 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 80 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 81 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 82 | @Override |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 83 | public String getUri() { |
| 84 | return mUri; |
| 85 | } |
| 86 | |
| 87 | void setAppNameSpace(String appNameSpace) { |
| 88 | mAppNameSpace = appNameSpace; |
| 89 | } |
| 90 | |
| 91 | String getAppNameSpace() { |
| 92 | return mAppNameSpace; |
| 93 | } |
| 94 | |
| 95 | void setName(String name) { |
| 96 | mName = name; |
| 97 | } |
| 98 | |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 99 | /** |
| 100 | * {@inheritDoc} |
| 101 | */ |
| 102 | @Override |
| 103 | public String getName() { |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 104 | return mName; |
| 105 | } |
| 106 | |
| 107 | void setRunner(String runnerName) { |
| 108 | mRunner = runnerName; |
| 109 | } |
| 110 | |
| 111 | String getRunner() { |
| 112 | return mRunner; |
| 113 | } |
| 114 | |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 115 | void setTestType(String testType) { |
| 116 | mTestType = testType; |
| 117 | } |
| 118 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame^] | 119 | String getTestType() { |
| 120 | return mTestType; |
| 121 | } |
| 122 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 123 | void setJarPath(String jarPath) { |
| 124 | mJarPath = jarPath; |
| 125 | } |
| 126 | |
| 127 | String getJarPath() { |
| 128 | return mJarPath; |
| 129 | } |
| 130 | |
| 131 | void setIsSignatureCheck(boolean isSignatureCheckTest) { |
| 132 | mIsSignatureTest = isSignatureCheckTest; |
| 133 | } |
| 134 | |
| 135 | boolean isSignatureCheck() { |
| 136 | return mIsSignatureTest; |
| 137 | } |
| 138 | |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 139 | void setTestPackageName(String testPackageName) { |
| 140 | mTestPackageName = testPackageName; |
| 141 | } |
| 142 | |
Brett Chabot | 358dc56 | 2011-10-25 15:46:48 -0700 | [diff] [blame] | 143 | void setTargetBinaryName(String targetBinaryName) { |
| 144 | mTargetBinaryName = targetBinaryName; |
| 145 | } |
| 146 | |
| 147 | void setTargetNameSpace(String targetNameSpace) { |
| 148 | mTargetNameSpace = targetNameSpace; |
| 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public String getTargetApkName() { |
| 153 | if (mTargetBinaryName != null && !mTargetBinaryName.isEmpty()) { |
| 154 | return String.format("%s.apk", mTargetBinaryName); |
| 155 | } |
| 156 | return null; |
| 157 | } |
| 158 | |
| 159 | @Override |
| 160 | public String getTargetPackageName() { |
| 161 | if (mTargetNameSpace != null && mTargetNameSpace.isEmpty()) { |
| 162 | return null; |
| 163 | } |
| 164 | return mTargetNameSpace; |
| 165 | } |
| 166 | |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 167 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 168 | * {@inheritDoc} |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 169 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 170 | @Override |
| 171 | public void setExcludedTestFilter(TestFilter excludeFilter) { |
| 172 | mExcludedTestFilter = excludeFilter; |
| 173 | } |
| 174 | |
| 175 | /** |
| 176 | * {@inheritDoc} |
| 177 | */ |
| 178 | @Override |
| 179 | public void setClassName(String className, String methodName) { |
| 180 | mClassName = className; |
| 181 | mMethodName = methodName; |
| 182 | } |
| 183 | |
| 184 | /** |
| 185 | * {@inheritDoc} |
| 186 | */ |
| 187 | @Override |
| 188 | public IRemoteTest createTest(File testCaseDir) { |
| 189 | mExcludedTestFilter.setTestInclusion(mClassName, mMethodName); |
| 190 | mTests = filterTests(); |
| 191 | |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame^] | 192 | if (HOST_SIDE_ONLY_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 193 | CLog.d("Creating host test for %s", mName); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 194 | JarHostTest hostTest = new JarHostTest(); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 195 | hostTest.setRunName(getUri()); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 196 | hostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 197 | hostTest.setTests(mTests); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 198 | mDigest = generateDigest(testCaseDir, mJarPath); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 199 | return hostTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame^] | 200 | } else if (VM_HOST_TEST.equals(mTestType)) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 201 | CLog.d("Creating vm host test for %s", mName); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 202 | VMHostTest vmHostTest = new VMHostTest(); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 203 | vmHostTest.setRunName(getUri()); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 204 | vmHostTest.setJarFileName(mJarPath); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 205 | vmHostTest.setTests(mTests); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 206 | mDigest = generateDigest(testCaseDir, mJarPath); |
Tsu Chiang Chuang | 9a223d7 | 2011-04-27 17:19:46 -0700 | [diff] [blame] | 207 | return vmHostTest; |
Brian Muramatsu | 6069507 | 2012-01-09 18:11:03 -0800 | [diff] [blame^] | 208 | } else if (NATIVE_TEST.equals(mTestType)) { |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 209 | return new GeeTest(mUri, mName); |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 210 | } else if (mIsSignatureTest) { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 211 | // TODO: hardcode the runner/class/method for now, since current package xml points to |
| 212 | // specialized instrumentation. Eventually this special case for signatureTest can be |
| 213 | // removed, and it can be treated just like a normal InstrumentationTest |
| 214 | CLog.d("Creating signature test %s", mName); |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 215 | InstrumentationApkTest instrTest = new InstrumentationApkTest(); |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 216 | instrTest.setPackageName(mAppNameSpace); |
| 217 | instrTest.setRunnerName("android.test.InstrumentationTestRunner"); |
| 218 | instrTest.setClassName(SIGNATURE_TEST_CLASS); |
| 219 | instrTest.setMethodName(SIGNATURE_TEST_METHOD); |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 220 | // set expected tests to the single signature test |
| 221 | TestIdentifier t = new TestIdentifier(SIGNATURE_TEST_CLASS, SIGNATURE_TEST_METHOD); |
| 222 | mTests.clear(); |
| 223 | mTests.add(t); |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 224 | // mName means 'apk file name' for instrumentation tests |
Brett Chabot | f4bec73 | 2011-04-19 17:31:06 -0700 | [diff] [blame] | 225 | instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 226 | mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName)); |
Brett Chabot | 584d2b9 | 2011-02-03 17:56:49 -0800 | [diff] [blame] | 227 | return instrTest; |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 228 | } else { |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 229 | CLog.d("Creating instrumentation test for %s", mName); |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 230 | InstrumentationApkTest instrTest = new InstrumentationApkTest(); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 231 | return setInstrumentationTest(instrTest, testCaseDir); |
Brett Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 234 | |
| 235 | /** |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 236 | * Populates given {@link InstrumentationApkTest} with data from the package xml. |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 237 | * |
| 238 | * @param testCaseDir |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 239 | * @param instrTest |
| 240 | * @return the populated {@link InstrumentationTest} or <code>null</code> |
| 241 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 242 | private InstrumentationTest setInstrumentationTest(InstrumentationApkTest instrTest, |
| 243 | File testCaseDir) { |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 244 | instrTest.setRunName(getUri()); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 245 | instrTest.setPackageName(mAppNameSpace); |
| 246 | instrTest.setRunnerName(mRunner); |
Brian Carlstrom | 022aff4 | 2011-05-17 23:16:51 -0700 | [diff] [blame] | 247 | instrTest.setTestPackageName(mTestPackageName); |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 248 | instrTest.setClassName(mClassName); |
| 249 | instrTest.setMethodName(mMethodName); |
| 250 | instrTest.setTestsToRun(mTests, |
| 251 | !mExcludedTestFilter.hasExclusion() |
| 252 | /* only force batch mode if no tests are excluded */); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 253 | // mName means 'apk file name' for instrumentation tests |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 254 | instrTest.addInstallApk(String.format("%s.apk", mName), mAppNameSpace); |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 255 | mDigest = generateDigest(testCaseDir, String.format("%s.apk", mName)); |
Brett Chabot | 4263db4 | 2011-04-15 13:51:48 -0700 | [diff] [blame] | 256 | if (mTests.size() > 1000) { |
| 257 | // TODO: hack, large test suites can take longer to collect tests, increase timeout |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 258 | instrTest.setCollectsTestsShellTimeout(10 * 60 * 1000); |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 259 | } |
Brett Chabot | a0f443c | 2011-02-04 13:57:55 -0800 | [diff] [blame] | 260 | return instrTest; |
| 261 | } |
| 262 | |
| 263 | /** |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 264 | * Filter the tests to run based on list of excluded tests, class and method name. |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 265 | * |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 266 | * @return the filtered collection of tests |
| 267 | */ |
Brett Chabot | 53e68a3 | 2011-10-05 14:14:55 -0700 | [diff] [blame] | 268 | private Collection<TestIdentifier> filterTests() { |
| 269 | mExcludedTestFilter.setTestInclusion(mClassName, mMethodName); |
| 270 | return mExcludedTestFilter.filter(mTests); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | /** |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 274 | * {@inheritDoc} |
| 275 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 276 | @Override |
Brett Chabot | 4f8143c | 2010-12-14 18:29:44 -0800 | [diff] [blame] | 277 | public boolean isKnownTest(TestIdentifier testDef) { |
| 278 | return mTests.contains(testDef); |
| 279 | } |
| 280 | |
| 281 | /** |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 282 | * {@inheritDoc} |
| 283 | */ |
Brian Muramatsu | 88d32a8 | 2011-12-02 10:55:12 -0800 | [diff] [blame] | 284 | @Override |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 285 | public boolean isKnownTestClass(String className) { |
| 286 | return mTestClasses.contains(className); |
| 287 | } |
| 288 | |
| 289 | /** |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 290 | * Add a {@link TestIdentifier} to the list of tests in this package. |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 291 | * |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 292 | * @param testDef |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 293 | */ |
| 294 | void addTest(TestIdentifier testDef) { |
| 295 | mTests.add(testDef); |
Brett Chabot | ef5a604 | 2011-01-19 19:54:14 -0800 | [diff] [blame] | 296 | mTestClasses.add(testDef.getClassName()); |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Get the collection of tests in this test package. |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 301 | */ |
Brett Chabot | 1dcb9a5 | 2011-02-10 20:26:57 -0800 | [diff] [blame] | 302 | @Override |
| 303 | public Collection<TestIdentifier> getTests() { |
Brett Chabot | 82d9daf | 2010-10-20 20:07:14 -0700 | [diff] [blame] | 304 | return mTests; |
| 305 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 306 | |
| 307 | /** |
| 308 | * {@inheritDoc} |
| 309 | */ |
| 310 | @Override |
| 311 | public String getDigest() { |
| 312 | return mDigest; |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Generate a sha1sum digest for a file. |
| 317 | * <p/> |
| 318 | * Exposed for unit testing. |
| 319 | * |
| 320 | * @param fileDir the directory of the file |
| 321 | * @param fileName the name of the file |
| 322 | * @return a hex {@link String} of the digest |
| 323 | */ |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 324 | String generateDigest(File fileDir, String fileName) { |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 325 | final String algorithm = "SHA-1"; |
| 326 | InputStream fileStream = null; |
Brett Chabot | 13638ab | 2011-10-16 13:59:03 -0700 | [diff] [blame] | 327 | DigestInputStream d = null; |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 328 | try { |
| 329 | fileStream = getFileStream(fileDir, fileName); |
| 330 | MessageDigest md = MessageDigest.getInstance(algorithm); |
| 331 | d = new DigestInputStream(fileStream, md); |
| 332 | byte[] buffer = new byte[8196]; |
Brian Muramatsu | 29d3478 | 2012-01-06 18:00:21 -0800 | [diff] [blame] | 333 | while (d.read(buffer) != -1) { |
| 334 | } |
Brett Chabot | 58c43a8 | 2011-09-13 14:13:57 -0700 | [diff] [blame] | 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 Chabot | 3add916 | 2010-09-12 17:23:05 -0700 | [diff] [blame] | 371 | } |