提交 f72abb28 编写于 作者: S Skylot

test: add test methods for load and check classes from smali files

上级 2c072539
......@@ -91,9 +91,21 @@ public abstract class IntegrationTest extends TestUtils {
}
public ClassNode getClassNodeFromFile(File file, String clsName) {
JadxDecompiler d = loadFiles(Collections.singletonList(file));
RootNode root = JadxInternalAccess.getRoot(d);
ClassNode cls = root.searchClassByName(clsName);
assertThat("Class not found: " + clsName, cls, notNullValue());
assertThat(clsName, is(cls.getClassInfo().getFullName()));
decompileAndCheckCls(d, cls);
return cls;
}
protected JadxDecompiler loadFiles(List<File> inputFiles) {
JadxDecompiler d = null;
try {
args.setInputFiles(Collections.singletonList(file));
args.setInputFiles(inputFiles);
d = new JadxDecompiler(args);
d.load();
} catch (Exception e) {
......@@ -102,11 +114,10 @@ public abstract class IntegrationTest extends TestUtils {
}
RootNode root = JadxInternalAccess.getRoot(d);
insertResources(root);
return d;
}
ClassNode cls = root.searchClassByName(clsName);
assertThat("Class not found: " + clsName, cls, notNullValue());
assertThat(clsName, is(cls.getClassInfo().getFullName()));
protected void decompileAndCheckCls(JadxDecompiler d, ClassNode cls) {
if (unloadCls) {
decompile(d, cls);
} else {
......@@ -119,8 +130,7 @@ public abstract class IntegrationTest extends TestUtils {
checkCode(cls);
compile(cls);
runAutoCheck(clsName);
return cls;
runAutoCheck(cls.getClassInfo().getFullName());
}
private void insertResources(RootNode root) {
......
......@@ -6,11 +6,16 @@ import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.jetbrains.annotations.Nullable;
import org.jf.smali.Smali;
import org.jf.smali.SmaliOptions;
import jadx.api.JadxDecompiler;
import jadx.core.dex.nodes.ClassNode;
import static org.hamcrest.Matchers.notNullValue;
import static org.junit.Assert.assertThat;
public abstract class SmaliTest extends IntegrationTest {
private static final String SMALI_TESTS_PROJECT = "jadx-core";
......@@ -24,6 +29,10 @@ public abstract class SmaliTest extends IntegrationTest {
return getClassNodeFromFile(outDex, clsName);
}
protected ClassNode getClassNodeFromSmali(String clsName) {
return getClassNodeFromSmali(clsName, clsName);
}
protected ClassNode getClassNodeFromSmaliWithPath(String path, String clsName) {
return getClassNodeFromSmali(path + File.separatorChar + clsName, clsName);
}
......@@ -32,17 +41,37 @@ public abstract class SmaliTest extends IntegrationTest {
return getClassNodeFromSmali(pkg + File.separatorChar + clsName, pkg + '.' + clsName);
}
protected ClassNode getClassNodeFromSmaliFiles(String pkg, String testName, String clsName, String... smaliFileNames) {
protected ClassNode getClassNodeFromSmaliFiles(String pkg, String testName, String clsName) {
File outDex = createTempFile(".dex");
List<File> smaliFiles = Arrays.stream(smaliFileNames)
.map(file -> getSmaliFile(pkg + File.separatorChar + testName + File.separatorChar + file))
.collect(Collectors.toList());
compileSmali(outDex, smaliFiles);
compileSmali(outDex, collectSmaliFiles(pkg, testName));
return getClassNodeFromFile(outDex, pkg + "." + clsName);
}
protected ClassNode getClassNodeFromSmali(String clsName) {
return getClassNodeFromSmali(clsName, clsName);
protected JadxDecompiler loadSmaliFile(String pkg, String smaliFileName) {
File outDex = createTempFile(".dex");
compileSmali(outDex, Collections.singletonList(getSmaliFile(pkg + File.separatorChar + smaliFileName)));
return loadFiles(Collections.singletonList(outDex));
}
protected JadxDecompiler loadSmaliFiles(String pkg, String testNameDir) {
File outDex = createTempFile(".dex");
compileSmali(outDex, collectSmaliFiles(pkg, testNameDir));
return loadFiles(Collections.singletonList(outDex));
}
private List<File> collectSmaliFiles(String pkg, @Nullable String testDir) {
String smaliFilesDir;
if (testDir == null) {
smaliFilesDir = pkg + File.separatorChar;
} else {
smaliFilesDir = pkg + File.separatorChar + testDir + File.separatorChar;
}
File smaliDir = new File(SMALI_TESTS_DIR, smaliFilesDir);
String[] smaliFileNames = smaliDir.list((dir, name) -> name.endsWith(".smali"));
assertThat("Smali files not found", smaliFileNames, notNullValue());
return Arrays.stream(smaliFileNames)
.map(file -> new File(smaliDir, file))
.collect(Collectors.toList());
}
private static File getSmaliFile(String baseName) {
......
......@@ -33,9 +33,7 @@ public class TestSyntheticMthRename extends SmaliTest {
@Test
public void test() {
ClassNode cls = getClassNodeFromSmaliFiles("inner", "TestSyntheticMthRename", "TestCls",
"TestCls", "TestCls$I", "TestCls$A"
);
ClassNode cls = getClassNodeFromSmaliFiles("inner", "TestSyntheticMthRename", "TestCls");
String code = cls.getCode().toString();
assertThat(code, containsOne("public String call(Runnable... p) {"));
......
......@@ -24,10 +24,10 @@ public class TestBadMethodAccessModifiers extends SmaliTest {
}
}
*/
@Test
public void test() {
ClassNode cls = getClassNodeFromSmaliFiles("others", "TestBadMethodAccessModifiers", "TestCls",
"TestCls$A", "TestCls$B", "TestCls");
ClassNode cls = getClassNodeFromSmaliFiles("others", "TestBadMethodAccessModifiers", "TestCls");
String code = cls.getCode().toString();
assertThat(code, not(containsString("protected void test() {")));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册