Commit eb141ad1 authored by Skylot's avatar Skylot

test: add tests for #474

parent b446bf27
......@@ -81,6 +81,14 @@ public abstract class IntegrationTest extends TestUtils {
args.setFsCaseSensitive(false); // use same value on all systems
}
public String getTestName() {
return this.getClass().getSimpleName();
}
public String getTestPkg() {
return this.getClass().getPackage().getName().replace("jadx.tests.integration.", "");
}
public ClassNode getClassNode(Class<?> clazz) {
try {
File jar = getJarForClass(clazz);
......
......@@ -11,7 +11,9 @@ import org.jf.smali.Smali;
import org.jf.smali.SmaliOptions;
import jadx.api.JadxDecompiler;
import jadx.api.JadxInternalAccess;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.RootNode;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.notNullValue;
......@@ -47,16 +49,17 @@ public abstract class SmaliTest extends IntegrationTest {
return getClassNodeFromFile(outDex, pkg + '.' + clsName);
}
protected JadxDecompiler loadSmaliFile(String pkg, String smaliFileName) {
protected List<ClassNode> loadFromSmaliFiles() {
File outDex = createTempFile(".dex");
compileSmali(outDex, Collections.singletonList(getSmaliFile(pkg + File.separatorChar + smaliFileName)));
return loadFiles(Collections.singletonList(outDex));
}
compileSmali(outDex, collectSmaliFiles(getTestPkg(), getTestName()));
protected JadxDecompiler loadSmaliFiles(String pkg, String testNameDir) {
File outDex = createTempFile(".dex");
compileSmali(outDex, collectSmaliFiles(pkg, testNameDir));
return loadFiles(Collections.singletonList(outDex));
JadxDecompiler d = loadFiles(Collections.singletonList(outDex));
RootNode root = JadxInternalAccess.getRoot(d);
List<ClassNode> classes = root.getClasses(false);
for (ClassNode cls : classes) {
decompileAndCheckCls(d, cls);
}
return classes;
}
private List<File> collectSmaliFiles(String pkg, @Nullable String testDir) {
......@@ -68,7 +71,7 @@ public abstract class SmaliTest extends IntegrationTest {
}
File smaliDir = new File(SMALI_TESTS_DIR, smaliFilesDir);
String[] smaliFileNames = smaliDir.list((dir, name) -> name.endsWith(".smali"));
assertThat("Smali files not found", smaliFileNames, notNullValue());
assertThat("Smali files not found in " + smaliDir, smaliFileNames, notNullValue());
return Arrays.stream(smaliFileNames)
.map(file -> new File(smaliDir, file))
.collect(Collectors.toList());
......
package jadx.tests.integration.names;
import java.util.List;
import java.util.stream.Collectors;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import jadx.core.Consts;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.SmaliTest;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.emptyString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
public class TestCaseSensitiveChecks extends SmaliTest {
/*
public class A {}
public class a {}
*/
@Test
public void test() {
args.setFsCaseSensitive(false);
List<ClassNode> classes = loadFromSmaliFiles();
for (ClassNode cls : classes) {
assertThat(cls.getPackage(), is(Consts.DEFAULT_PACKAGE_NAME));
}
long namesCount = classes.stream().map(cls -> cls.getShortName().toLowerCase()).distinct().count();
assertThat(namesCount, is(2L));
}
@Test
public void testCaseSensitiveFS() {
args.setFsCaseSensitive(true);
List<ClassNode> classes = loadFromSmaliFiles();
for (ClassNode cls : classes) {
assertThat(cls.getPackage(), is(Consts.DEFAULT_PACKAGE_NAME));
}
List<String> names = classes.stream().map(ClassNode::getShortName).collect(Collectors.toList());
assertThat(names, Matchers.containsInAnyOrder("A", "a"));
}
@Test
public void testWithDeobfuscation() {
enableDeobfuscation();
List<ClassNode> classes = loadFromSmaliFiles();
for (ClassNode cls : classes) {
assertThat(cls.getPackage(), not(emptyString()));
assertThat(cls.getPackage(), not(Consts.DEFAULT_PACKAGE_NAME));
}
long namesCount = classes.stream().map(cls -> cls.getShortName().toLowerCase()).distinct().count();
assertThat(namesCount, is(2L));
}
}
......@@ -2,10 +2,6 @@ package jadx.tests.integration.names;
import org.junit.jupiter.api.Test;
import jadx.api.JadxDecompiler;
import jadx.api.JadxInternalAccess;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.RootNode;
import jadx.tests.api.SmaliTest;
public class TestClassNameWithInvalidChar extends SmaliTest {
......@@ -16,21 +12,12 @@ public class TestClassNameWithInvalidChar extends SmaliTest {
@Test
public void test() {
JadxDecompiler d = loadSmaliFiles("names", "TestClassNameWithInvalidChar");
RootNode root = JadxInternalAccess.getRoot(d);
for (ClassNode cls : root.getClasses(false)) {
decompileAndCheckCls(d, cls);
}
loadFromSmaliFiles();
}
@Test
public void testWithDeobfuscation() {
enableDeobfuscation();
JadxDecompiler d = loadSmaliFiles("names", "TestClassNameWithInvalidChar");
RootNode root = JadxInternalAccess.getRoot(d);
for (ClassNode cls : root.getClasses(false)) {
decompileAndCheckCls(d, cls);
}
loadFromSmaliFiles();
}
}
.class public LA;
.super Ljava/lang/Object;
.class public La;
.super Ljava/lang/Object;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment