diff options
| -rw-r--r-- | tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java | 36 |
1 files changed, 33 insertions, 3 deletions
diff --git a/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java b/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java index 16f005f28856..591ffeb39721 100644 --- a/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java +++ b/tests/ApkVerityTest/src/com/android/apkverity/ApkVerityTest.java @@ -42,6 +42,7 @@ import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; +import java.util.Set; /** * This test makes sure app installs with fs-verity signature, and on-access verification works. @@ -465,10 +466,10 @@ public class ApkVerityTest extends BaseHostJUnit4Test { break; } try { - CLog.d("lsof: " + expectRemoteCommandToSucceed("lsof " + apkPath)); + String openFiles = expectRemoteCommandToSucceed("lsof " + apkPath); + CLog.d("lsof: " + openFiles); Thread.sleep(1000); - String pid = expectRemoteCommandToSucceed("pidof system_server"); - mDevice.executeShellV2Command("kill -10 " + pid); // force GC + forceGCOnOpenFilesProcess(getOpenFilesPIDs(openFiles)); } catch (InterruptedException e) { Thread.currentThread().interrupt(); return; @@ -478,6 +479,35 @@ public class ApkVerityTest extends BaseHostJUnit4Test { } } + /** + * This is a helper method that parses the lsof output to get PIDs of process holding FD. + * Here is an example output of lsof. This method extracts the second columns(PID). + * + * Example lsof output: + * COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME + * .example.app 1063 u0_a38 mem REG 253,6 8599 12826 example.apk + * .example.app 1063 u0_a38 99r REG 253,6 8599 12826 example.apk + */ + private Set<String> getOpenFilesPIDs(String lsof) { + Set<String> openFilesPIDs = new HashSet<>(); + String[] lines = lsof.split("\n"); + for (int i = 1; i < lines.length; i++) { + openFilesPIDs.add(lines[i].split("\\s+")[1]); + } + return openFilesPIDs; + } + + /** + * This is a helper method that forces GC on processes given their PIDs. + * That is to execute shell command "kill -10" on PIDs. + */ + private void forceGCOnOpenFilesProcess(Set<String> openFilesPIDs) + throws DeviceNotAvailableException { + for (String openFilePID : openFilesPIDs) { + mDevice.executeShellV2Command("kill -10 " + openFilePID); + } + } + private void verifyInstalledFiles(String... filenames) throws DeviceNotAvailableException { String apkPath = getApkPath(TARGET_PACKAGE); String appDir = apkPath.substring(0, apkPath.lastIndexOf("/")); |