summaryrefslogtreecommitdiff
path: root/src/test
diff options
context:
space:
mode:
authorGravatar Runemoro2020-03-09 06:04:08 -0400
committerGravatar GitHub2020-03-09 10:04:08 +0000
commit58c0aeb15a65324de08a914dfa62cc68a516a4e3 (patch)
treef45e8141c0864692051149a478c5a0a6bbe68686 /src/test
parentMade Enigma gui translatable (#193) (diff)
downloadenigma-fork-58c0aeb15a65324de08a914dfa62cc68a516a4e3.tar.gz
enigma-fork-58c0aeb15a65324de08a914dfa62cc68a516a4e3.tar.xz
enigma-fork-58c0aeb15a65324de08a914dfa62cc68a516a4e3.zip
CFR support (#192)
* Add decompiler API * Add CFR support
Diffstat (limited to 'src/test')
-rw-r--r--src/test/java/cuchaz/enigma/TestDeobfed.java51
-rw-r--r--src/test/java/cuchaz/enigma/TestDeobfuscator.java10
-rw-r--r--src/test/java/cuchaz/enigma/TestInnerClasses.java13
-rw-r--r--src/test/java/cuchaz/enigma/TestSourceIndex.java12
-rw-r--r--src/test/java/cuchaz/enigma/TokenChecker.java27
5 files changed, 54 insertions, 59 deletions
diff --git a/src/test/java/cuchaz/enigma/TestDeobfed.java b/src/test/java/cuchaz/enigma/TestDeobfed.java
index 3ee86cb..c88b0eb 100644
--- a/src/test/java/cuchaz/enigma/TestDeobfed.java
+++ b/src/test/java/cuchaz/enigma/TestDeobfed.java
@@ -13,6 +13,9 @@ package cuchaz.enigma;
13 13
14import cuchaz.enigma.analysis.ClassCache; 14import cuchaz.enigma.analysis.ClassCache;
15import cuchaz.enigma.analysis.index.JarIndex; 15import cuchaz.enigma.analysis.index.JarIndex;
16import cuchaz.enigma.source.Decompiler;
17import cuchaz.enigma.source.Decompilers;
18import cuchaz.enigma.source.SourceSettings;
16import org.junit.BeforeClass; 19import org.junit.BeforeClass;
17import org.junit.Test; 20import org.junit.Test;
18 21
@@ -68,31 +71,29 @@ public class TestDeobfed {
68 @Test 71 @Test
69 public void decompile() { 72 public void decompile() {
70 EnigmaProject project = new EnigmaProject(enigma, classCache, index); 73 EnigmaProject project = new EnigmaProject(enigma, classCache, index);
74 Decompiler decompiler = Decompilers.PROCYON.create(project.getClassCache(), new SourceSettings(false, false));
71 75
72 CompiledSourceTypeLoader typeLoader = new CompiledSourceTypeLoader(project.getClassCache()); 76 decompiler.getSource("a");
73 SourceProvider sourceProvider = new SourceProvider(SourceProvider.createSettings(), typeLoader); 77 decompiler.getSource("b");
74 78 decompiler.getSource("c");
75 sourceProvider.getSources("a"); 79 decompiler.getSource("d");
76 sourceProvider.getSources("b"); 80 decompiler.getSource("d$1");
77 sourceProvider.getSources("c"); 81 decompiler.getSource("e");
78 sourceProvider.getSources("d"); 82 decompiler.getSource("f");
79 sourceProvider.getSources("d$1"); 83 decompiler.getSource("g");
80 sourceProvider.getSources("e"); 84 decompiler.getSource("g$a");
81 sourceProvider.getSources("f"); 85 decompiler.getSource("g$a$a");
82 sourceProvider.getSources("g"); 86 decompiler.getSource("g$b");
83 sourceProvider.getSources("g$a"); 87 decompiler.getSource("g$b$a");
84 sourceProvider.getSources("g$a$a"); 88 decompiler.getSource("h");
85 sourceProvider.getSources("g$b"); 89 decompiler.getSource("h$a");
86 sourceProvider.getSources("g$b$a"); 90 decompiler.getSource("h$a$a");
87 sourceProvider.getSources("h"); 91 decompiler.getSource("h$b");
88 sourceProvider.getSources("h$a"); 92 decompiler.getSource("h$b$a");
89 sourceProvider.getSources("h$a$a"); 93 decompiler.getSource("h$b$a$a");
90 sourceProvider.getSources("h$b"); 94 decompiler.getSource("h$b$a$b");
91 sourceProvider.getSources("h$b$a"); 95 decompiler.getSource("i");
92 sourceProvider.getSources("h$b$a$a"); 96 decompiler.getSource("i$a");
93 sourceProvider.getSources("h$b$a$b"); 97 decompiler.getSource("i$b");
94 sourceProvider.getSources("i");
95 sourceProvider.getSources("i$a");
96 sourceProvider.getSources("i$b");
97 } 98 }
98} 99}
diff --git a/src/test/java/cuchaz/enigma/TestDeobfuscator.java b/src/test/java/cuchaz/enigma/TestDeobfuscator.java
index 2a6fec4..6619d26 100644
--- a/src/test/java/cuchaz/enigma/TestDeobfuscator.java
+++ b/src/test/java/cuchaz/enigma/TestDeobfuscator.java
@@ -11,13 +11,15 @@
11 11
12package cuchaz.enigma; 12package cuchaz.enigma;
13 13
14import cuchaz.enigma.source.Decompiler;
15import cuchaz.enigma.source.Decompilers;
16import cuchaz.enigma.source.SourceSettings;
14import org.junit.Test; 17import org.junit.Test;
15 18
16import java.io.IOException; 19import java.io.IOException;
17import java.nio.file.Paths; 20import java.nio.file.Paths;
18 21
19public class TestDeobfuscator { 22public class TestDeobfuscator {
20
21 private EnigmaProject openProject() throws IOException { 23 private EnigmaProject openProject() throws IOException {
22 Enigma enigma = Enigma.create(); 24 Enigma enigma = Enigma.create();
23 return enigma.openJar(Paths.get("build/test-obf/loneClass.jar"), ProgressListener.none()); 25 return enigma.openJar(Paths.get("build/test-obf/loneClass.jar"), ProgressListener.none());
@@ -32,10 +34,8 @@ public class TestDeobfuscator {
32 @Test 34 @Test
33 public void decompileClass() throws Exception { 35 public void decompileClass() throws Exception {
34 EnigmaProject project = openProject(); 36 EnigmaProject project = openProject();
37 Decompiler decompiler = Decompilers.PROCYON.create(project.getClassCache(), new SourceSettings(false, false));
35 38
36 CompiledSourceTypeLoader typeLoader = new CompiledSourceTypeLoader(project.getClassCache()); 39 decompiler.getSource("a").asString();
37 SourceProvider sourceProvider = new SourceProvider(SourceProvider.createSettings(), typeLoader);
38
39 sourceProvider.writeSourceToString(sourceProvider.getSources("a"));
40 } 40 }
41} 41}
diff --git a/src/test/java/cuchaz/enigma/TestInnerClasses.java b/src/test/java/cuchaz/enigma/TestInnerClasses.java
index 18e4936..85c72f8 100644
--- a/src/test/java/cuchaz/enigma/TestInnerClasses.java
+++ b/src/test/java/cuchaz/enigma/TestInnerClasses.java
@@ -13,6 +13,9 @@ package cuchaz.enigma;
13 13
14import cuchaz.enigma.analysis.ClassCache; 14import cuchaz.enigma.analysis.ClassCache;
15import cuchaz.enigma.analysis.index.JarIndex; 15import cuchaz.enigma.analysis.index.JarIndex;
16import cuchaz.enigma.source.Decompiler;
17import cuchaz.enigma.source.Decompilers;
18import cuchaz.enigma.source.SourceSettings;
16import cuchaz.enigma.translation.representation.entry.ClassEntry; 19import cuchaz.enigma.translation.representation.entry.ClassEntry;
17import org.junit.Test; 20import org.junit.Test;
18 21
@@ -32,15 +35,13 @@ public class TestInnerClasses {
32 private static final ClassEntry ClassTreeLevel1 = newClass("f$a"); 35 private static final ClassEntry ClassTreeLevel1 = newClass("f$a");
33 private static final ClassEntry ClassTreeLevel2 = newClass("f$a$a"); 36 private static final ClassEntry ClassTreeLevel2 = newClass("f$a$a");
34 private static final ClassEntry ClassTreeLevel3 = newClass("f$a$a$a"); 37 private static final ClassEntry ClassTreeLevel3 = newClass("f$a$a$a");
35 private JarIndex index; 38 private final JarIndex index;
36 private SourceProvider sourceProvider; 39 private final Decompiler decompiler;
37 40
38 public TestInnerClasses() throws Exception { 41 public TestInnerClasses() throws Exception {
39 ClassCache classCache = ClassCache.of(Paths.get("build/test-obf/innerClasses.jar")); 42 ClassCache classCache = ClassCache.of(Paths.get("build/test-obf/innerClasses.jar"));
40 index = classCache.index(ProgressListener.none()); 43 index = classCache.index(ProgressListener.none());
41 44 decompiler = Decompilers.PROCYON.create(classCache, new SourceSettings(false, false));
42 CompiledSourceTypeLoader typeLoader = new CompiledSourceTypeLoader(classCache);
43 sourceProvider = new SourceProvider(SourceProvider.createSettings(), typeLoader);
44 } 45 }
45 46
46 @Test 47 @Test
@@ -79,6 +80,6 @@ public class TestInnerClasses {
79 } 80 }
80 81
81 private void decompile(ClassEntry classEntry) { 82 private void decompile(ClassEntry classEntry) {
82 sourceProvider.getSources(classEntry.getName()); 83 decompiler.getSource(classEntry.getName());
83 } 84 }
84} 85}
diff --git a/src/test/java/cuchaz/enigma/TestSourceIndex.java b/src/test/java/cuchaz/enigma/TestSourceIndex.java
index b1d1182..b201608 100644
--- a/src/test/java/cuchaz/enigma/TestSourceIndex.java
+++ b/src/test/java/cuchaz/enigma/TestSourceIndex.java
@@ -12,9 +12,8 @@
12package cuchaz.enigma; 12package cuchaz.enigma;
13 13
14import com.google.common.collect.Sets; 14import com.google.common.collect.Sets;
15import com.strobel.decompiler.languages.java.ast.CompilationUnit;
16import cuchaz.enigma.analysis.ClassCache; 15import cuchaz.enigma.analysis.ClassCache;
17import cuchaz.enigma.analysis.SourceIndex; 16import cuchaz.enigma.source.*;
18import cuchaz.enigma.analysis.index.JarIndex; 17import cuchaz.enigma.analysis.index.JarIndex;
19import cuchaz.enigma.translation.representation.entry.ClassEntry; 18import cuchaz.enigma.translation.representation.entry.ClassEntry;
20import org.junit.Test; 19import org.junit.Test;
@@ -53,8 +52,7 @@ public class TestSourceIndex {
53 ClassCache classCache = project.getClassCache(); 52 ClassCache classCache = project.getClassCache();
54 JarIndex index = project.getJarIndex(); 53 JarIndex index = project.getJarIndex();
55 54
56 CompiledSourceTypeLoader typeLoader = new CompiledSourceTypeLoader(classCache); 55 Decompiler decompiler = Decompilers.PROCYON.create(classCache, new SourceSettings(false, false));
57 SourceProvider sourceProvider = new SourceProvider(SourceProvider.createSettings(), typeLoader);
58 56
59 // get all classes that aren't inner classes 57 // get all classes that aren't inner classes
60 Set<ClassEntry> classEntries = Sets.newHashSet(); 58 Set<ClassEntry> classEntries = Sets.newHashSet();
@@ -66,10 +64,8 @@ public class TestSourceIndex {
66 64
67 for (ClassEntry obfClassEntry : classEntries) { 65 for (ClassEntry obfClassEntry : classEntries) {
68 try { 66 try {
69 CompilationUnit tree = sourceProvider.getSources(obfClassEntry.getName()); 67 Source source = decompiler.getSource(obfClassEntry.getName());
70 String source = sourceProvider.writeSourceToString(tree); 68 source.index();
71
72 SourceIndex.buildIndex(source, tree, true);
73 } catch (Throwable t) { 69 } catch (Throwable t) {
74 throw new Error("Unable to index " + obfClassEntry, t); 70 throw new Error("Unable to index " + obfClassEntry, t);
75 } 71 }
diff --git a/src/test/java/cuchaz/enigma/TokenChecker.java b/src/test/java/cuchaz/enigma/TokenChecker.java
index 1dde034..48d0c83 100644
--- a/src/test/java/cuchaz/enigma/TokenChecker.java
+++ b/src/test/java/cuchaz/enigma/TokenChecker.java
@@ -12,10 +12,10 @@
12package cuchaz.enigma; 12package cuchaz.enigma;
13 13
14import com.google.common.collect.Lists; 14import com.google.common.collect.Lists;
15import com.strobel.decompiler.languages.java.ast.CompilationUnit;
16import cuchaz.enigma.analysis.ClassCache; 15import cuchaz.enigma.analysis.ClassCache;
17import cuchaz.enigma.analysis.EntryReference; 16import cuchaz.enigma.analysis.EntryReference;
18import cuchaz.enigma.analysis.SourceIndex; 17import cuchaz.enigma.source.SourceIndex;
18import cuchaz.enigma.source.*;
19import cuchaz.enigma.analysis.Token; 19import cuchaz.enigma.analysis.Token;
20import cuchaz.enigma.translation.representation.entry.Entry; 20import cuchaz.enigma.translation.representation.entry.Entry;
21 21
@@ -25,43 +25,40 @@ import java.util.Collection;
25import java.util.List; 25import java.util.List;
26 26
27public class TokenChecker { 27public class TokenChecker {
28 28 private final Decompiler decompiler;
29 private SourceProvider sourceProvider;
30 29
31 protected TokenChecker(Path path) throws IOException { 30 protected TokenChecker(Path path) throws IOException {
32 ClassCache classCache = ClassCache.of(path); 31 ClassCache classCache = ClassCache.of(path);
33 32 decompiler = Decompilers.PROCYON.create(classCache, new SourceSettings(false, false));
34 CompiledSourceTypeLoader typeLoader = new CompiledSourceTypeLoader(classCache);
35 sourceProvider = new SourceProvider(SourceProvider.createSettings(), typeLoader);
36 } 33 }
37 34
38 protected String getDeclarationToken(Entry<?> entry) { 35 protected String getDeclarationToken(Entry<?> entry) {
39 // decompile the class 36 // decompile the class
40 CompilationUnit tree = sourceProvider.getSources(entry.getContainingClass().getFullName()); 37 Source source = decompiler.getSource(entry.getContainingClass().getFullName());
41 // DEBUG 38 // DEBUG
42 // tree.acceptVisitor( new TreeDumpVisitor( new File( "tree." + entry.getClassName().replace( '/', '.' ) + ".txt" ) ), null ); 39 // tree.acceptVisitor( new TreeDumpVisitor( new File( "tree." + entry.getClassName().replace( '/', '.' ) + ".txt" ) ), null );
43 String source = sourceProvider.writeSourceToString(tree); 40 String string = source.asString();
44 SourceIndex index = SourceIndex.buildIndex(source, tree, true); 41 SourceIndex index = source.index();
45 42
46 // get the token value 43 // get the token value
47 Token token = index.getDeclarationToken(entry); 44 Token token = index.getDeclarationToken(entry);
48 if (token == null) { 45 if (token == null) {
49 return null; 46 return null;
50 } 47 }
51 return source.substring(token.start, token.end); 48 return string.substring(token.start, token.end);
52 } 49 }
53 50
54 @SuppressWarnings("unchecked") 51 @SuppressWarnings("unchecked")
55 protected Collection<String> getReferenceTokens(EntryReference<? extends Entry<?>, ? extends Entry<?>> reference) { 52 protected Collection<String> getReferenceTokens(EntryReference<? extends Entry<?>, ? extends Entry<?>> reference) {
56 // decompile the class 53 // decompile the class
57 CompilationUnit tree = sourceProvider.getSources(reference.context.getContainingClass().getFullName()); 54 Source source = decompiler.getSource(reference.context.getContainingClass().getFullName());
58 String source = sourceProvider.writeSourceToString(tree); 55 String string = source.asString();
59 SourceIndex index = SourceIndex.buildIndex(source, tree, true); 56 SourceIndex index = source.index();
60 57
61 // get the token values 58 // get the token values
62 List<String> values = Lists.newArrayList(); 59 List<String> values = Lists.newArrayList();
63 for (Token token : index.getReferenceTokens((EntryReference<Entry<?>, Entry<?>>) reference)) { 60 for (Token token : index.getReferenceTokens((EntryReference<Entry<?>, Entry<?>>) reference)) {
64 values.add(source.substring(token.start, token.end)); 61 values.add(string.substring(token.start, token.end));
65 } 62 }
66 return values; 63 return values;
67 } 64 }