| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.internal.util.test; |
| 18 |
|
| 19 |
import static org.junit.Assert.assertEquals; |
| 20 |
import static org.junit.Assert.assertTrue; |
| 21 |
|
| 22 |
import com.android.tradefed.device.DeviceNotAvailableException; |
| 23 |
import com.android.tradefed.device.ITestDevice; |
| 24 |
|
| 25 |
import org.junit.Assert; |
| 26 |
import org.junit.ClassRule; |
| 27 |
import org.junit.rules.ExternalResource; |
| 28 |
import org.junit.rules.TemporaryFolder; |
| 29 |
import org.junit.rules.TestRule; |
| 30 |
import org.junit.runner.Description; |
| 31 |
import org.junit.runners.model.Statement; |
| 32 |
|
| 33 |
import java.io.File; |
| 34 |
import java.io.FileOutputStream; |
| 35 |
import java.io.IOException; |
| 36 |
import java.io.InputStream; |
| 37 |
import java.nio.file.Path; |
| 38 |
import java.nio.file.Paths; |
| 39 |
import java.util.ArrayList; |
| 40 |
|
| 41 |
import javax.annotation.Nullable; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public class SystemPreparer extends ExternalResource { |
| 52 |
private static final long OVERLAY_ENABLE_TIMEOUT_MS = 30000; |
| 53 |
|
| 54 |
|
| 55 |
private ArrayList<String> mPushedFiles = new ArrayList<>(); |
| 56 |
|
| 57 |
|
| 58 |
private ArrayList<String> mInstalledPackages = new ArrayList<>(); |
| 59 |
|
| 60 |
private final TemporaryFolder mHostTempFolder; |
| 61 |
private final DeviceProvider mDeviceProvider; |
| 62 |
private final RebootStrategy mRebootStrategy; |
| 63 |
private final TearDownRule mTearDownRule; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
private boolean mDebugSkipAfterReboot; |
| 68 |
|
| 69 |
public SystemPreparer(TemporaryFolder hostTempFolder, DeviceProvider deviceProvider) { |
| 70 |
this(hostTempFolder, RebootStrategy.FULL, null, deviceProvider); |
| 71 |
} |
| 72 |
|
| 73 |
public SystemPreparer(TemporaryFolder hostTempFolder, RebootStrategy rebootStrategy, |
| 74 |
@Nullable TestRuleDelegate testRuleDelegate, DeviceProvider deviceProvider) { |
| 75 |
this(hostTempFolder, rebootStrategy, testRuleDelegate, false, deviceProvider); |
| 76 |
} |
| 77 |
|
| 78 |
public SystemPreparer(TemporaryFolder hostTempFolder, RebootStrategy rebootStrategy, |
| 79 |
@Nullable TestRuleDelegate testRuleDelegate, boolean debugSkipAfterReboot, |
| 80 |
DeviceProvider deviceProvider) { |
| 81 |
mHostTempFolder = hostTempFolder; |
| 82 |
mDeviceProvider = deviceProvider; |
| 83 |
mRebootStrategy = rebootStrategy; |
| 84 |
mTearDownRule = new TearDownRule(mDeviceProvider); |
| 85 |
if (testRuleDelegate != null) { |
| 86 |
testRuleDelegate.setDelegate(mTearDownRule); |
| 87 |
} |
| 88 |
mDebugSkipAfterReboot = debugSkipAfterReboot; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
public SystemPreparer pushResourceFile(String filePath, String outputPath) |
| 93 |
throws DeviceNotAvailableException, IOException { |
| 94 |
final ITestDevice device = mDeviceProvider.getDevice(); |
| 95 |
remount(); |
| 96 |
assertTrue(device.pushFile(copyResourceToTemp(filePath), outputPath)); |
| 97 |
addPushedFile(device, outputPath); |
| 98 |
return this; |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
public SystemPreparer pushFile(File file, String outputPath) |
| 103 |
throws DeviceNotAvailableException { |
| 104 |
final ITestDevice device = mDeviceProvider.getDevice(); |
| 105 |
remount(); |
| 106 |
assertTrue(device.pushFile(file, outputPath)); |
| 107 |
addPushedFile(device, outputPath); |
| 108 |
return this; |
| 109 |
} |
| 110 |
|
| 111 |
private void addPushedFile(ITestDevice device, String outputPath) |
| 112 |
throws DeviceNotAvailableException { |
| 113 |
Path pathCreated = Paths.get(outputPath); |
| 114 |
|
| 115 |
|
| 116 |
while (pathCreated.getParent() != null |
| 117 |
&& !device.doesFileExist(pathCreated.getParent().toString())) { |
| 118 |
pathCreated = pathCreated.getParent(); |
| 119 |
} |
| 120 |
|
| 121 |
mPushedFiles.add(pathCreated.toString()); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
public SystemPreparer deleteFile(String file) throws DeviceNotAvailableException { |
| 126 |
final ITestDevice device = mDeviceProvider.getDevice(); |
| 127 |
remount(); |
| 128 |
device.deleteFile(file); |
| 129 |
return this; |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
public SystemPreparer installResourceApk(String resourcePath, String packageName) |
| 134 |
throws DeviceNotAvailableException, IOException { |
| 135 |
final ITestDevice device = mDeviceProvider.getDevice(); |
| 136 |
final File tmpFile = copyResourceToTemp(resourcePath); |
| 137 |
final String result = device.installPackage(tmpFile, true ); |
| 138 |
Assert.assertNull(result); |
| 139 |
mInstalledPackages.add(packageName); |
| 140 |
return this; |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
public SystemPreparer stageMultiplePackages(String[] resourcePaths, String[] packageNames) |
| 145 |
throws DeviceNotAvailableException, IOException { |
| 146 |
assertEquals(resourcePaths.length, packageNames.length); |
| 147 |
final ITestDevice device = mDeviceProvider.getDevice(); |
| 148 |
final String[] adbCommandLine = new String[resourcePaths.length + 2]; |
| 149 |
adbCommandLine[0] = "install-multi-package"; |
| 150 |
adbCommandLine[1] = "--staged"; |
| 151 |
for (int i = 0; i < resourcePaths.length; i++) { |
| 152 |
final File tmpFile = copyResourceToTemp(resourcePaths[i]); |
| 153 |
adbCommandLine[i + 2] = tmpFile.getAbsolutePath(); |
| 154 |
mInstalledPackages.add(packageNames[i]); |
| 155 |
} |
| 156 |
final String output = device.executeAdbCommand(adbCommandLine); |
| 157 |
assertTrue(output.contains("Success. Reboot device to apply staged session")); |
| 158 |
return this; |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
public SystemPreparer setOverlayEnabled(String packageName, boolean enabled) |
| 163 |
throws DeviceNotAvailableException { |
| 164 |
final ITestDevice device = mDeviceProvider.getDevice(); |
| 165 |
final String enable = enabled ? "enable" : "disable"; |
| 166 |
|
| 167 |
|
| 168 |
final long endMillis = System.currentTimeMillis() + OVERLAY_ENABLE_TIMEOUT_MS; |
| 169 |
String result; |
| 170 |
while (System.currentTimeMillis() <= endMillis) { |
| 171 |
device.executeShellCommand(String.format("cmd overlay %s %s", enable, packageName)); |
| 172 |
result = device.executeShellCommand("cmd overlay dump isenabled " |
| 173 |
+ packageName); |
| 174 |
if (((enabled) ? "true\n" : "false\n").equals(result)) { |
| 175 |
return this; |
| 176 |
} |
| 177 |
|
| 178 |
try { |
| 179 |
Thread.sleep(200); |
| 180 |
} catch (InterruptedException ignore) { |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
throw new IllegalStateException(String.format("Failed to %s overlay %s:\n%s", enable, |
| 185 |
packageName, device.executeShellCommand("cmd overlay list"))); |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
public SystemPreparer reboot() throws DeviceNotAvailableException { |
| 190 |
ITestDevice device = mDeviceProvider.getDevice(); |
| 191 |
switch (mRebootStrategy) { |
| 192 |
case FULL: |
| 193 |
device.reboot(); |
| 194 |
break; |
| 195 |
case UNTIL_ONLINE: |
| 196 |
device.rebootUntilOnline(); |
| 197 |
break; |
| 198 |
case USERSPACE: |
| 199 |
device.rebootUserspace(); |
| 200 |
break; |
| 201 |
case USERSPACE_UNTIL_ONLINE: |
| 202 |
device.rebootUserspaceUntilOnline(); |
| 203 |
break; |
| 204 |
|
| 205 |
|
| 206 |
default: |
| 207 |
device.executeShellCommand("stop"); |
| 208 |
device.executeShellCommand("start"); |
| 209 |
device.waitForDeviceAvailable(); |
| 210 |
break; |
| 211 |
} |
| 212 |
return this; |
| 213 |
} |
| 214 |
|
| 215 |
public SystemPreparer remount() throws DeviceNotAvailableException { |
| 216 |
mTearDownRule.remount(); |
| 217 |
return this; |
| 218 |
} |
| 219 |
|
| 220 |
private static @Nullable String getFileExtension(@Nullable String path) { |
| 221 |
if (path == null) { |
| 222 |
return null; |
| 223 |
} |
| 224 |
final int lastDot = path.lastIndexOf('.'); |
| 225 |
if (lastDot >= 0) { |
| 226 |
return path.substring(lastDot + 1); |
| 227 |
} else { |
| 228 |
return null; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
private File copyResourceToTemp(String resourcePath) throws IOException { |
| 234 |
final String ext = getFileExtension(resourcePath); |
| 235 |
final File tempFile; |
| 236 |
if (ext != null) { |
| 237 |
tempFile = File.createTempFile("junit", "." + ext, mHostTempFolder.getRoot()); |
| 238 |
} else { |
| 239 |
tempFile = mHostTempFolder.newFile(); |
| 240 |
} |
| 241 |
final ClassLoader classLoader = getClass().getClassLoader(); |
| 242 |
try (InputStream assetIs = classLoader.getResourceAsStream(resourcePath); |
| 243 |
FileOutputStream assetOs = new FileOutputStream(tempFile)) { |
| 244 |
if (assetIs == null) { |
| 245 |
throw new IllegalStateException("Failed to find resource " + resourcePath); |
| 246 |
} |
| 247 |
|
| 248 |
int b; |
| 249 |
while ((b = assetIs.read()) >= 0) { |
| 250 |
assetOs.write(b); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
return tempFile; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
@Override |
| 259 |
public void after() { |
| 260 |
final ITestDevice device = mDeviceProvider.getDevice(); |
| 261 |
try { |
| 262 |
remount(); |
| 263 |
for (final String file : mPushedFiles) { |
| 264 |
device.deleteFile(file); |
| 265 |
} |
| 266 |
for (final String packageName : mInstalledPackages) { |
| 267 |
device.uninstallPackage(packageName); |
| 268 |
} |
| 269 |
if (!mDebugSkipAfterReboot) { |
| 270 |
reboot(); |
| 271 |
} |
| 272 |
} catch (DeviceNotAvailableException e) { |
| 273 |
Assert.fail(e.toString()); |
| 274 |
} |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
public static class TestRuleDelegate implements TestRule { |
| 286 |
|
| 287 |
private boolean mThrowOnNull; |
| 288 |
|
| 289 |
@Nullable |
| 290 |
private TestRule mTestRule; |
| 291 |
|
| 292 |
public TestRuleDelegate(boolean throwOnNull) { |
| 293 |
mThrowOnNull = throwOnNull; |
| 294 |
} |
| 295 |
|
| 296 |
public void setDelegate(TestRule testRule) { |
| 297 |
mTestRule = testRule; |
| 298 |
} |
| 299 |
|
| 300 |
@Override |
| 301 |
public Statement apply(Statement base, Description description) { |
| 302 |
if (mTestRule == null) { |
| 303 |
if (mThrowOnNull) { |
| 304 |
throw new IllegalStateException("TestRule delegate was not set"); |
| 305 |
} else { |
| 306 |
return new Statement() { |
| 307 |
@Override |
| 308 |
public void evaluate() throws Throwable { |
| 309 |
base.evaluate(); |
| 310 |
} |
| 311 |
}; |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
Statement statement = mTestRule.apply(base, description); |
| 316 |
mTestRule = null; |
| 317 |
return statement; |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
private static class TearDownRule extends ExternalResource { |
| 325 |
|
| 326 |
private DeviceProvider mDeviceProvider; |
| 327 |
private boolean mInitialized; |
| 328 |
private boolean mWasVerityEnabled; |
| 329 |
private boolean mWasAdbRoot; |
| 330 |
private boolean mIsVerityEnabled; |
| 331 |
|
| 332 |
TearDownRule(DeviceProvider deviceProvider) { |
| 333 |
mDeviceProvider = deviceProvider; |
| 334 |
} |
| 335 |
|
| 336 |
@Override |
| 337 |
protected void before() { |
| 338 |
|
| 339 |
} |
| 340 |
|
| 341 |
@Override |
| 342 |
protected void after() { |
| 343 |
try { |
| 344 |
initialize(); |
| 345 |
ITestDevice device = mDeviceProvider.getDevice(); |
| 346 |
if (mWasVerityEnabled != mIsVerityEnabled) { |
| 347 |
device.executeShellCommand( |
| 348 |
mWasVerityEnabled ? "enable-verity" : "disable-verity"); |
| 349 |
} |
| 350 |
device.reboot(); |
| 351 |
if (!mWasAdbRoot) { |
| 352 |
device.disableAdbRoot(); |
| 353 |
} |
| 354 |
} catch (DeviceNotAvailableException e) { |
| 355 |
Assert.fail(e.toString()); |
| 356 |
} |
| 357 |
} |
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
public void remount() throws DeviceNotAvailableException { |
| 363 |
initialize(); |
| 364 |
ITestDevice device = mDeviceProvider.getDevice(); |
| 365 |
device.enableAdbRoot(); |
| 366 |
if (mIsVerityEnabled) { |
| 367 |
mIsVerityEnabled = false; |
| 368 |
device.executeShellCommand("disable-verity"); |
| 369 |
device.reboot(); |
| 370 |
} |
| 371 |
device.enableAdbRoot(); |
| 372 |
device.remountSystemWritable(); |
| 373 |
device.remountVendorWritable(); |
| 374 |
device.waitForDeviceAvailable(); |
| 375 |
} |
| 376 |
|
| 377 |
private void initialize() throws DeviceNotAvailableException { |
| 378 |
if (mInitialized) { |
| 379 |
return; |
| 380 |
} |
| 381 |
mInitialized = true; |
| 382 |
ITestDevice device = mDeviceProvider.getDevice(); |
| 383 |
mWasAdbRoot = device.isAdbRoot(); |
| 384 |
device.enableAdbRoot(); |
| 385 |
String veritySystem = device.getProperty("partition.system.verified"); |
| 386 |
String verityVendor = device.getProperty("partition.vendor.verified"); |
| 387 |
mWasVerityEnabled = (veritySystem != null && !veritySystem.isEmpty()) |
| 388 |
|| (verityVendor != null && !verityVendor.isEmpty()); |
| 389 |
mIsVerityEnabled = mWasVerityEnabled; |
| 390 |
} |
| 391 |
} |
| 392 |
|
| 393 |
public interface DeviceProvider { |
| 394 |
ITestDevice getDevice(); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
|
| 400 |
@SuppressWarnings("DanglingJavadoc") |
| 401 |
public enum RebootStrategy { |
| 402 |
|
| 403 |
FULL, |
| 404 |
|
| 405 |
|
| 406 |
UNTIL_ONLINE, |
| 407 |
|
| 408 |
|
| 409 |
USERSPACE, |
| 410 |
|
| 411 |
|
| 412 |
USERSPACE_UNTIL_ONLINE, |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
} |
| 431 |
} |