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