summaryrefslogtreecommitdiff
path: root/test/cuchaz
diff options
context:
space:
mode:
authorGravatar jeff2014-09-14 23:56:43 -0400
committerGravatar jeff2014-09-14 23:56:43 -0400
commit72e918a5134c2bf747a476284bcfa1bd2ef2fa21 (patch)
tree2fd256d6a8bbe38b7b9fe1892f444a8c29de08ef /test/cuchaz
parentadded test to check constructor references (diff)
downloadenigma-fork-72e918a5134c2bf747a476284bcfa1bd2ef2fa21.tar.gz
enigma-fork-72e918a5134c2bf747a476284bcfa1bd2ef2fa21.tar.xz
enigma-fork-72e918a5134c2bf747a476284bcfa1bd2ef2fa21.zip
added tests to check constructor tokens
fixed a bug with constructor tokens too
Diffstat (limited to 'test/cuchaz')
-rw-r--r--test/cuchaz/enigma/TestJarIndexConstructorReferences.java29
-rw-r--r--test/cuchaz/enigma/TestTokensConstructors.java152
-rw-r--r--test/cuchaz/enigma/TokenChecker.java71
-rw-r--r--test/cuchaz/enigma/inputs/constructors/Caller.java15
-rw-r--r--test/cuchaz/enigma/inputs/constructors/DefaultConstructable.java6
-rw-r--r--test/cuchaz/enigma/inputs/constructors/SubClass.java3
-rw-r--r--test/cuchaz/enigma/inputs/constructors/SubSubClass.java2
7 files changed, 266 insertions, 12 deletions
diff --git a/test/cuchaz/enigma/TestJarIndexConstructorReferences.java b/test/cuchaz/enigma/TestJarIndexConstructorReferences.java
index 38882a0..c069188 100644
--- a/test/cuchaz/enigma/TestJarIndexConstructorReferences.java
+++ b/test/cuchaz/enigma/TestJarIndexConstructorReferences.java
@@ -18,10 +18,13 @@ import static org.hamcrest.Matchers.containsInAnyOrder;
18import static org.hamcrest.Matchers.empty; 18import static org.hamcrest.Matchers.empty;
19import static org.hamcrest.Matchers.is; 19import static org.hamcrest.Matchers.is;
20 20
21import java.io.File;
22import java.util.Collection;
21import java.util.jar.JarFile; 23import java.util.jar.JarFile;
22 24
23import org.junit.Test; 25import org.junit.Test;
24 26
27import cuchaz.enigma.analysis.EntryReference;
25import cuchaz.enigma.analysis.JarIndex; 28import cuchaz.enigma.analysis.JarIndex;
26import cuchaz.enigma.mapping.BehaviorEntry; 29import cuchaz.enigma.mapping.BehaviorEntry;
27import cuchaz.enigma.mapping.ClassEntry; 30import cuchaz.enigma.mapping.ClassEntry;
@@ -30,17 +33,19 @@ import cuchaz.enigma.mapping.ConstructorEntry;
30public class TestJarIndexConstructorReferences 33public class TestJarIndexConstructorReferences
31{ 34{
32 private JarIndex m_index; 35 private JarIndex m_index;
33 36
34 private ClassEntry m_baseClass = new ClassEntry( "none/a" ); 37 private ClassEntry m_baseClass = new ClassEntry( "none/a" );
35 private ClassEntry m_subClass = new ClassEntry( "none/c" ); 38 private ClassEntry m_subClass = new ClassEntry( "none/d" );
36 private ClassEntry m_subsubClass = new ClassEntry( "none/d" ); 39 private ClassEntry m_subsubClass = new ClassEntry( "none/e" );
40 private ClassEntry m_defaultClass = new ClassEntry( "none/c" );
37 private ClassEntry m_callerClass = new ClassEntry( "none/b" ); 41 private ClassEntry m_callerClass = new ClassEntry( "none/b" );
38 42
39 public TestJarIndexConstructorReferences( ) 43 public TestJarIndexConstructorReferences( )
40 throws Exception 44 throws Exception
41 { 45 {
46 File jarFile = new File( "build/libs/testConstructors.obf.jar" );
42 m_index = new JarIndex(); 47 m_index = new JarIndex();
43 m_index.indexJar( new JarFile( "build/libs/testConstructors.obf.jar" ), false ); 48 m_index.indexJar( new JarFile( jarFile ), false );
44 } 49 }
45 50
46 @Test 51 @Test
@@ -51,6 +56,7 @@ public class TestJarIndexConstructorReferences
51 m_baseClass, 56 m_baseClass,
52 m_subClass, 57 m_subClass,
53 m_subsubClass, 58 m_subsubClass,
59 m_defaultClass,
54 m_callerClass 60 m_callerClass
55 ) ); 61 ) );
56 } 62 }
@@ -60,7 +66,8 @@ public class TestJarIndexConstructorReferences
60 public void baseDefault( ) 66 public void baseDefault( )
61 { 67 {
62 BehaviorEntry source = new ConstructorEntry( m_baseClass, "()V" ); 68 BehaviorEntry source = new ConstructorEntry( m_baseClass, "()V" );
63 assertThat( m_index.getBehaviorReferences( source ), containsInAnyOrder( 69 Collection<EntryReference<BehaviorEntry,BehaviorEntry>> references = m_index.getBehaviorReferences( source );
70 assertThat( references, containsInAnyOrder(
64 newBehaviorReferenceByMethod( source, m_callerClass.getName(), "a", "()V" ), 71 newBehaviorReferenceByMethod( source, m_callerClass.getName(), "a", "()V" ),
65 newBehaviorReferenceByConstructor( source, m_subClass.getName(), "()V" ), 72 newBehaviorReferenceByConstructor( source, m_subClass.getName(), "()V" ),
66 newBehaviorReferenceByConstructor( source, m_subClass.getName(), "(III)V" ) 73 newBehaviorReferenceByConstructor( source, m_subClass.getName(), "(III)V" )
@@ -126,4 +133,14 @@ public class TestJarIndexConstructorReferences
126 newBehaviorReferenceByMethod( source, m_callerClass.getName(), "f", "()V" ) 133 newBehaviorReferenceByMethod( source, m_callerClass.getName(), "f", "()V" )
127 ) ); 134 ) );
128 } 135 }
136
137 @Test
138 @SuppressWarnings( "unchecked" )
139 public void defaultConstructable( )
140 {
141 BehaviorEntry source = new ConstructorEntry( m_defaultClass, "()V" );
142 assertThat( m_index.getBehaviorReferences( source ), containsInAnyOrder(
143 newBehaviorReferenceByMethod( source, m_callerClass.getName(), "g", "()V" )
144 ) );
145 }
129} 146}
diff --git a/test/cuchaz/enigma/TestTokensConstructors.java b/test/cuchaz/enigma/TestTokensConstructors.java
new file mode 100644
index 0000000..2409153
--- /dev/null
+++ b/test/cuchaz/enigma/TestTokensConstructors.java
@@ -0,0 +1,152 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma;
12
13import static cuchaz.enigma.EntryFactory.newBehaviorReferenceByConstructor;
14import static cuchaz.enigma.EntryFactory.newBehaviorReferenceByMethod;
15import static cuchaz.enigma.EntryFactory.newConstructor;
16import static org.hamcrest.MatcherAssert.assertThat;
17import static org.hamcrest.Matchers.containsInAnyOrder;
18import static org.hamcrest.Matchers.is;
19import static org.hamcrest.Matchers.nullValue;
20
21import java.io.File;
22
23import org.junit.Test;
24
25import cuchaz.enigma.mapping.BehaviorEntry;
26
27public class TestTokensConstructors extends TokenChecker
28{
29 public TestTokensConstructors( )
30 throws Exception
31 {
32 super( new File( "build/libs/testConstructors.obf.jar" ) );
33 }
34
35 @Test
36 public void baseDeclarations( )
37 {
38 assertThat( getDeclarationToken( newConstructor( "none/a", "()V" ) ), is( "a" ) );
39 assertThat( getDeclarationToken( newConstructor( "none/a", "(I)V" ) ), is( "a" ) );
40 }
41
42 @Test
43 public void subDeclarations( )
44 {
45 assertThat( getDeclarationToken( newConstructor( "none/d", "()V" ) ), is( "d" ) );
46 assertThat( getDeclarationToken( newConstructor( "none/d", "(I)V" ) ), is( "d" ) );
47 assertThat( getDeclarationToken( newConstructor( "none/d", "(II)V" ) ), is( "d" ) );
48 assertThat( getDeclarationToken( newConstructor( "none/d", "(III)V" ) ), is( "d" ) );
49 }
50
51 @Test
52 public void subsubDeclarations( )
53 {
54 assertThat( getDeclarationToken( newConstructor( "none/e", "(I)V" ) ), is( "e" ) );
55 }
56
57 @Test
58 public void defaultDeclarations( )
59 {
60 assertThat( getDeclarationToken( newConstructor( "none/c", "()V" ) ), nullValue() );
61 }
62
63 @Test
64 public void baseDefaultReferences( )
65 {
66 BehaviorEntry source = newConstructor( "none/a", "()V" );
67 assertThat(
68 getReferenceTokens( newBehaviorReferenceByMethod( source, "none/b", "a", "()V" ) ),
69 containsInAnyOrder( "a" )
70 );
71 assertThat(
72 getReferenceTokens( newBehaviorReferenceByConstructor( source, "none/d", "()V" ) ),
73 containsInAnyOrder( "super" ) // implicit call, decompiled to "super"
74 );
75 assertThat(
76 getReferenceTokens( newBehaviorReferenceByConstructor( source, "none/d", "(III)V" ) ),
77 containsInAnyOrder( "super" ) // implicit call, decompiled to "super"
78 );
79 }
80
81 @Test
82 public void baseIntReferences( )
83 {
84 BehaviorEntry source = newConstructor( "none/a", "(I)V" );
85 assertThat(
86 getReferenceTokens( newBehaviorReferenceByMethod( source, "none/b", "b", "()V" ) ),
87 containsInAnyOrder( "a" )
88 );
89 }
90
91 @Test
92 public void subDefaultReferences( )
93 {
94 BehaviorEntry source = newConstructor( "none/d", "()V" );
95 assertThat(
96 getReferenceTokens( newBehaviorReferenceByMethod( source, "none/b", "c", "()V" ) ),
97 containsInAnyOrder( "d" )
98 );
99 assertThat(
100 getReferenceTokens( newBehaviorReferenceByConstructor( source, "none/d", "(I)V" ) ),
101 containsInAnyOrder( "this" )
102 );
103 }
104
105 @Test
106 public void subIntReferences( )
107 {
108 BehaviorEntry source = newConstructor( "none/d", "(I)V" );
109 assertThat(
110 getReferenceTokens( newBehaviorReferenceByMethod( source, "none/b", "d", "()V" ) ),
111 containsInAnyOrder( "d" )
112 );
113 assertThat(
114 getReferenceTokens( newBehaviorReferenceByConstructor( source, "none/d", "(II)V" ) ),
115 containsInAnyOrder( "this" )
116 );
117 assertThat(
118 getReferenceTokens( newBehaviorReferenceByConstructor( source, "none/e", "(I)V" ) ),
119 containsInAnyOrder( "super" )
120 );
121 }
122
123 @Test
124 public void subIntIntReferences( )
125 {
126 BehaviorEntry source = newConstructor( "none/d", "(II)V" );
127 assertThat(
128 getReferenceTokens( newBehaviorReferenceByMethod( source, "none/b", "e", "()V" ) ),
129 containsInAnyOrder( "d" )
130 );
131 }
132
133 @Test
134 public void subsubIntReferences( )
135 {
136 BehaviorEntry source = newConstructor( "none/e", "(I)V" );
137 assertThat(
138 getReferenceTokens( newBehaviorReferenceByMethod( source, "none/b", "f", "()V" ) ),
139 containsInAnyOrder( "e" )
140 );
141 }
142
143 @Test
144 public void defaultConstructableReferences( )
145 {
146 BehaviorEntry source = newConstructor( "none/c", "()V" );
147 assertThat(
148 getReferenceTokens( newBehaviorReferenceByMethod( source, "none/b", "g", "()V" ) ),
149 containsInAnyOrder( "c" )
150 );
151 }
152}
diff --git a/test/cuchaz/enigma/TokenChecker.java b/test/cuchaz/enigma/TokenChecker.java
new file mode 100644
index 0000000..07ab9ec
--- /dev/null
+++ b/test/cuchaz/enigma/TokenChecker.java
@@ -0,0 +1,71 @@
1/*******************************************************************************
2 * Copyright (c) 2014 Jeff Martin.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the GNU Public License v3.0
5 * which accompanies this distribution, and is available at
6 * http://www.gnu.org/licenses/gpl.html
7 *
8 * Contributors:
9 * Jeff Martin - initial API and implementation
10 ******************************************************************************/
11package cuchaz.enigma;
12
13import java.io.File;
14import java.io.IOException;
15import java.util.Collection;
16import java.util.List;
17
18import com.beust.jcommander.internal.Lists;
19import com.strobel.decompiler.languages.java.ast.CompilationUnit;
20
21import cuchaz.enigma.analysis.EntryReference;
22import cuchaz.enigma.analysis.SourceIndex;
23import cuchaz.enigma.analysis.Token;
24import cuchaz.enigma.analysis.TreeDumpVisitor;
25import cuchaz.enigma.mapping.Entry;
26
27public class TokenChecker
28{
29 private Deobfuscator m_deobfuscator;
30
31 protected TokenChecker( File jarFile )
32 throws IOException
33 {
34 m_deobfuscator = new Deobfuscator( jarFile );
35 }
36
37 protected String getDeclarationToken( Entry entry )
38 {
39 // decompile the class
40 CompilationUnit tree = m_deobfuscator.getSourceTree( entry.getClassName() );
41 // DEBUG
42 //tree.acceptVisitor( new TreeDumpVisitor( new File( "tree." + entry.getClassName().replace( '/', '.' ) + ".txt" ) ), null );
43 String source = m_deobfuscator.getSource( tree );
44 SourceIndex index = m_deobfuscator.getSourceIndex( tree, source );
45
46 // get the token value
47 Token token = index.getDeclarationToken( entry );
48 if( token == null )
49 {
50 return null;
51 }
52 return source.substring( token.start, token.end );
53 }
54
55 @SuppressWarnings( "unchecked" )
56 protected Collection<String> getReferenceTokens( EntryReference<? extends Entry,? extends Entry> reference )
57 {
58 // decompile the class
59 CompilationUnit tree = m_deobfuscator.getSourceTree( reference.context.getClassName() );
60 String source = m_deobfuscator.getSource( tree );
61 SourceIndex index = m_deobfuscator.getSourceIndex( tree, source );
62
63 // get the token values
64 List<String> values = Lists.newArrayList();
65 for( Token token : index.getReferenceTokens( (EntryReference<Entry,Entry>)reference ) )
66 {
67 values.add( source.substring( token.start, token.end ) );
68 }
69 return values;
70 }
71}
diff --git a/test/cuchaz/enigma/inputs/constructors/Caller.java b/test/cuchaz/enigma/inputs/constructors/Caller.java
index f356b1b..b218619 100644
--- a/test/cuchaz/enigma/inputs/constructors/Caller.java
+++ b/test/cuchaz/enigma/inputs/constructors/Caller.java
@@ -20,28 +20,35 @@ public class Caller
20 // c()V 20 // c()V
21 public void callSubDefault( ) 21 public void callSubDefault( )
22 { 22 {
23 // none/c.<init>()V 23 // none/d.<init>()V
24 System.out.println( new SubClass() ); 24 System.out.println( new SubClass() );
25 } 25 }
26 26
27 // d()V 27 // d()V
28 public void callSubInt( ) 28 public void callSubInt( )
29 { 29 {
30 // none/c.<init>(I)V 30 // none/d.<init>(I)V
31 System.out.println( new SubClass( 6 ) ); 31 System.out.println( new SubClass( 6 ) );
32 } 32 }
33 33
34 // e()V 34 // e()V
35 public void callSubIntInt( ) 35 public void callSubIntInt( )
36 { 36 {
37 // none/c.<init>(II)V 37 // none/d.<init>(II)V
38 System.out.println( new SubClass( 4, 2 ) ); 38 System.out.println( new SubClass( 4, 2 ) );
39 } 39 }
40 40
41 // f()V 41 // f()V
42 public void callSubSubInt( ) 42 public void callSubSubInt( )
43 { 43 {
44 // none/d.<init>(I)V 44 // none/e.<init>(I)V
45 System.out.println( new SubSubClass( 3 ) ); 45 System.out.println( new SubSubClass( 3 ) );
46 } 46 }
47
48 // g()V
49 public void callDefaultConstructable()
50 {
51 // none/c.<init>()V
52 System.out.println( new DefaultConstructable() );
53 }
47} 54}
diff --git a/test/cuchaz/enigma/inputs/constructors/DefaultConstructable.java b/test/cuchaz/enigma/inputs/constructors/DefaultConstructable.java
new file mode 100644
index 0000000..6cfd35e
--- /dev/null
+++ b/test/cuchaz/enigma/inputs/constructors/DefaultConstructable.java
@@ -0,0 +1,6 @@
1package cuchaz.enigma.inputs.constructors;
2
3public class DefaultConstructable
4{
5 // only default constructor
6}
diff --git a/test/cuchaz/enigma/inputs/constructors/SubClass.java b/test/cuchaz/enigma/inputs/constructors/SubClass.java
index 2235de3..6ef7732 100644
--- a/test/cuchaz/enigma/inputs/constructors/SubClass.java
+++ b/test/cuchaz/enigma/inputs/constructors/SubClass.java
@@ -1,6 +1,6 @@
1package cuchaz.enigma.inputs.constructors; 1package cuchaz.enigma.inputs.constructors;
2 2
3// none/c extends none/a 3// none/d extends none/a
4public class SubClass extends BaseClass 4public class SubClass extends BaseClass
5{ 5{
6 // <init>()V 6 // <init>()V
@@ -24,6 +24,7 @@ public class SubClass extends BaseClass
24 this( a + b ); 24 this( a + b );
25 } 25 }
26 26
27 // <init>(III)V
27 public SubClass( int a, int b, int c ) 28 public SubClass( int a, int b, int c )
28 { 29 {
29 // none/a.<init>()V 30 // none/a.<init>()V
diff --git a/test/cuchaz/enigma/inputs/constructors/SubSubClass.java b/test/cuchaz/enigma/inputs/constructors/SubSubClass.java
index a9445d8..76a0f1f 100644
--- a/test/cuchaz/enigma/inputs/constructors/SubSubClass.java
+++ b/test/cuchaz/enigma/inputs/constructors/SubSubClass.java
@@ -1,6 +1,6 @@
1package cuchaz.enigma.inputs.constructors; 1package cuchaz.enigma.inputs.constructors;
2 2
3// none/d extends none/c 3// none/e extends none/d
4public class SubSubClass extends SubClass 4public class SubSubClass extends SubClass
5{ 5{
6 // <init>(I)V 6 // <init>(I)V