fs-verity: fix broken test testInstallEverything

- Force GC on all processes listed by lsof to help this test walk around
  the FD leaks.

BUG: 253463456
Change-Id: Ida8cdfd8469452e028ad736497f5862a0df2a9a0
(cherry picked from commit 67884da910daeca4cb6da7568e7978fcaf4a55b3)
This commit is contained in:
Chen Bai
2022-11-14 15:14:15 -08:00
parent d186a0a952
commit 4d4eb9ac88

View File

@@ -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("/"));