Merge "fs-verity: fix broken test testInstallEverything"

This commit is contained in:
Chen Bai
2022-11-24 05:19:39 +00:00
committed by Android (Google) Code Review

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