fix: do not add custom passes for fallback and simple modes (#2276)

This commit is contained in:
Skylot
2024-09-13 21:01:19 +01:00
parent 889a945cf1
commit 603863403f
2 changed files with 66 additions and 37 deletions
+59 -37
View File
@@ -24,6 +24,17 @@ import static org.assertj.core.api.Assertions.assertThat;
public class TestInput {
private static final Logger LOG = LoggerFactory.getLogger(TestInput.class);
private static final PathMatcher LOG_ALL_FILES = path -> {
LOG.debug("File in result dir: {}", path);
return true;
};
@Test
public void testHelp() {
int result = JadxCLI.execute(new String[] { "--help" });
assertThat(result).isEqualTo(0);
}
@Test
public void testDexInput() throws Exception {
decompile("dex", "samples/hello.dex");
@@ -45,35 +56,61 @@ public class TestInput {
}
@Test
public void testResourceOnly() throws Exception {
decode("resourceOnly", "samples/resources-only.apk");
}
private void decode(String tmpDirName, String apkSample) throws URISyntaxException, IOException {
List<String> args = new ArrayList<>();
Path tempDir = FileUtils.createTempDir(tmpDirName);
args.add("-v");
args.add("-d");
args.add(tempDir.toAbsolutePath().toString());
URL resource = getClass().getClassLoader().getResource(apkSample);
assertThat(resource).isNotNull();
String sampleFile = resource.toURI().getRawPath();
args.add(sampleFile);
public void testFallbackMode() throws Exception {
Path tempDir = FileUtils.createTempDir("fallback");
List<String> args = buildArgs(tempDir, "samples/hello.dex");
args.add(0, "-f");
int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> files = Files.find(
tempDir,
3,
(file, attr) -> file.getFileName().toString().equalsIgnoreCase("AndroidManifest.xml"))
.collect(Collectors.toList());
List<Path> files = collectJavaFilesInDir(tempDir);
assertThat(files).hasSize(1);
}
@Test
public void testSimpleMode() throws Exception {
Path tempDir = FileUtils.createTempDir("simple");
List<String> args = buildArgs(tempDir, "samples/hello.dex");
args.add(0, "--decompilation-mode");
args.add(1, "simple");
int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> files = collectJavaFilesInDir(tempDir);
assertThat(files).hasSize(1);
}
@Test
public void testResourceOnly() throws Exception {
Path tempDir = FileUtils.createTempDir("resourceOnly");
List<String> args = buildArgs(tempDir, "samples/resources-only.apk");
int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> files = collectFilesInDir(tempDir,
path -> path.getFileName().toString().equalsIgnoreCase("AndroidManifest.xml"));
assertThat(files).isNotEmpty();
}
private void decompile(String tmpDirName, String... inputSamples) throws URISyntaxException, IOException {
List<String> args = new ArrayList<>();
Path tempDir = FileUtils.createTempDir(tmpDirName);
List<String> args = buildArgs(tempDir, inputSamples);
int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> resultJavaFiles = collectJavaFilesInDir(tempDir);
assertThat(resultJavaFiles).isNotEmpty();
// do not copy input files as resources
for (Path path : collectFilesInDir(tempDir, LOG_ALL_FILES)) {
for (String inputSample : inputSamples) {
assertThat(path.toAbsolutePath().toString()).doesNotContain(inputSample);
}
}
}
private List<String> buildArgs(Path tempDir, String... inputSamples) throws URISyntaxException {
List<String> args = new ArrayList<>();
args.add("-v");
args.add("-d");
args.add(tempDir.toAbsolutePath().toString());
@@ -84,22 +121,7 @@ public class TestInput {
String sampleFile = resource.toURI().getRawPath();
args.add(sampleFile);
}
int result = JadxCLI.execute(args.toArray(new String[0]));
assertThat(result).isEqualTo(0);
List<Path> resultJavaFiles = collectJavaFilesInDir(tempDir);
assertThat(resultJavaFiles).isNotEmpty();
// do not copy input files as resources
PathMatcher logAllFiles = path -> {
LOG.debug("File in result dir: {}", path);
return true;
};
for (Path path : collectFilesInDir(tempDir, logAllFiles)) {
for (String inputSample : inputSamples) {
assertThat(path.toAbsolutePath().toString()).doesNotContain(inputSample);
}
}
return args;
}
private static List<Path> collectJavaFilesInDir(Path dir) throws IOException {
@@ -13,6 +13,7 @@ import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import jadx.api.DecompilationMode;
import jadx.api.ICodeCache;
import jadx.api.ICodeWriter;
import jadx.api.JadxArgs;
@@ -310,6 +311,12 @@ public class RootNode {
}
public void mergePasses(Map<JadxPassType, List<JadxPass>> customPasses) {
DecompilationMode mode = args.getDecompilationMode();
if (mode == DecompilationMode.FALLBACK || mode == DecompilationMode.SIMPLE) {
// for predefined modes ignore custom (and plugin) passes
return;
}
new PassMerge(preDecompilePasses)
.merge(customPasses.get(JadxPreparePass.TYPE), p -> new PreparePassWrapper((JadxPreparePass) p));
new PassMerge(processClasses.getPasses())