| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.blockdevicewriter; |
| 18 |
|
| 19 |
import static com.google.common.truth.Truth.assertThat; |
| 20 |
import static com.google.common.truth.Truth.assertWithMessage; |
| 21 |
|
| 22 |
import com.android.tradefed.device.DeviceNotAvailableException; |
| 23 |
import com.android.tradefed.device.ITestDevice; |
| 24 |
import com.android.tradefed.util.CommandResult; |
| 25 |
import com.android.tradefed.util.CommandStatus; |
| 26 |
|
| 27 |
import java.util.ArrayList; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public final class BlockDeviceWriter { |
| 46 |
private static final String EXECUTABLE = "/data/local/tmp/block_device_writer"; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public static void damageFileAgainstBlockDevice(ITestDevice device, String path, |
| 55 |
long offsetOfTargetingByte) |
| 56 |
throws DeviceNotAvailableException { |
| 57 |
assertThat(path).startsWith("/data/"); |
| 58 |
ITestDevice.MountPointInfo mountPoint = device.getMountPointInfo("/data"); |
| 59 |
ArrayList<String> args = new ArrayList<>(); |
| 60 |
args.add(EXECUTABLE); |
| 61 |
if ("f2fs".equals(mountPoint.type)) { |
| 62 |
args.add("--use-f2fs-pinning"); |
| 63 |
} |
| 64 |
args.add(mountPoint.filesystem); |
| 65 |
args.add(path); |
| 66 |
args.add(Long.toString(offsetOfTargetingByte)); |
| 67 |
CommandResult result = device.executeShellV2Command(String.join(" ", args)); |
| 68 |
assertWithMessage( |
| 69 |
String.format("stdout=%s\nstderr=%s", result.getStdout(), result.getStderr())) |
| 70 |
.that(result.getStatus()).isEqualTo(CommandStatus.SUCCESS); |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
public static void dropCaches(ITestDevice device) throws DeviceNotAvailableException { |
| 79 |
CommandResult result = device.executeShellV2Command( |
| 80 |
"sync && echo 1 > /proc/sys/vm/drop_caches"); |
| 81 |
assertThat(result.getStatus()).isEqualTo(CommandStatus.SUCCESS); |
| 82 |
} |
| 83 |
|
| 84 |
public static void assertFileNotOpen(ITestDevice device, String path) |
| 85 |
throws DeviceNotAvailableException { |
| 86 |
CommandResult result = device.executeShellV2Command("lsof " + path); |
| 87 |
assertThat(result.getStatus()).isEqualTo(CommandStatus.SUCCESS); |
| 88 |
assertThat(result.getStdout()).isEmpty(); |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static boolean canReadByte(ITestDevice device, String filePath, long offset) |
| 96 |
throws DeviceNotAvailableException { |
| 97 |
CommandResult result = device.executeShellV2Command( |
| 98 |
"dd if=" + filePath + " bs=1 count=1 skip=" + Long.toString(offset)); |
| 99 |
return result.getStatus() == CommandStatus.SUCCESS; |
| 100 |
} |
| 101 |
} |