diff options
| author | 2014-09-08 00:23:46 -0400 | |
|---|---|---|
| committer | 2014-09-08 00:23:46 -0400 | |
| commit | a68dc42b6a835bd513e9d617c9892e85f321ddb6 (patch) | |
| tree | 61bf840bcb88f285f2812652ceed54c507c34f72 /test/cuchaz/enigma/TestJarIndexInheritanceTree.java | |
| parent | added some basic tests for the deobufscator and the jar index (diff) | |
| download | enigma-fork-a68dc42b6a835bd513e9d617c9892e85f321ddb6.tar.gz enigma-fork-a68dc42b6a835bd513e9d617c9892e85f321ddb6.tar.xz enigma-fork-a68dc42b6a835bd513e9d617c9892e85f321ddb6.zip | |
added some tests for a small inheritance hierarchy
Diffstat (limited to '')
| -rw-r--r-- | test/cuchaz/enigma/TestJarIndexInheritanceTree.java | 255 |
1 files changed, 255 insertions, 0 deletions
diff --git a/test/cuchaz/enigma/TestJarIndexInheritanceTree.java b/test/cuchaz/enigma/TestJarIndexInheritanceTree.java new file mode 100644 index 0000000..5ded5df --- /dev/null +++ b/test/cuchaz/enigma/TestJarIndexInheritanceTree.java | |||
| @@ -0,0 +1,255 @@ | |||
| 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 | ******************************************************************************/ | ||
| 11 | package cuchaz.enigma; | ||
| 12 | |||
| 13 | import static cuchaz.enigma.EntryFactory.newBehaviorReferenceByConstructor; | ||
| 14 | import static cuchaz.enigma.EntryFactory.newBehaviorReferenceByMethod; | ||
| 15 | import static cuchaz.enigma.EntryFactory.newClass; | ||
| 16 | import static cuchaz.enigma.EntryFactory.newFieldReferenceByConstructor; | ||
| 17 | import static cuchaz.enigma.EntryFactory.newFieldReferenceByMethod; | ||
| 18 | import static org.hamcrest.MatcherAssert.assertThat; | ||
| 19 | import static org.hamcrest.Matchers.contains; | ||
| 20 | import static org.hamcrest.Matchers.containsInAnyOrder; | ||
| 21 | import static org.hamcrest.Matchers.empty; | ||
| 22 | import static org.hamcrest.Matchers.is; | ||
| 23 | import static org.hamcrest.Matchers.nullValue; | ||
| 24 | |||
| 25 | import java.util.Collection; | ||
| 26 | import java.util.Set; | ||
| 27 | import java.util.jar.JarFile; | ||
| 28 | |||
| 29 | import org.junit.Test; | ||
| 30 | |||
| 31 | import cuchaz.enigma.analysis.Access; | ||
| 32 | import cuchaz.enigma.analysis.EntryReference; | ||
| 33 | import cuchaz.enigma.analysis.JarIndex; | ||
| 34 | import cuchaz.enigma.analysis.TranslationIndex; | ||
| 35 | import cuchaz.enigma.mapping.BehaviorEntry; | ||
| 36 | import cuchaz.enigma.mapping.ClassEntry; | ||
| 37 | import cuchaz.enigma.mapping.ConstructorEntry; | ||
| 38 | import cuchaz.enigma.mapping.FieldEntry; | ||
| 39 | import cuchaz.enigma.mapping.MethodEntry; | ||
| 40 | |||
| 41 | public class TestJarIndexInheritanceTree | ||
| 42 | { | ||
| 43 | private JarIndex m_index; | ||
| 44 | |||
| 45 | private ClassEntry m_baseClass = new ClassEntry( "none/a" ); | ||
| 46 | private ClassEntry m_subClassA = new ClassEntry( "none/b" ); | ||
| 47 | private ClassEntry m_subClassAA = new ClassEntry( "none/d" ); | ||
| 48 | private ClassEntry m_subClassB = new ClassEntry( "none/c" ); | ||
| 49 | private FieldEntry m_nameField = new FieldEntry( m_baseClass, "a" ); | ||
| 50 | private FieldEntry m_numThingsField = new FieldEntry( m_subClassB, "a" ); | ||
| 51 | |||
| 52 | public TestJarIndexInheritanceTree( ) | ||
| 53 | throws Exception | ||
| 54 | { | ||
| 55 | m_index = new JarIndex(); | ||
| 56 | m_index.indexJar( new JarFile( "build/libs/testInheritanceTree.obf.jar" ), false ); | ||
| 57 | } | ||
| 58 | |||
| 59 | @Test | ||
| 60 | public void obfEntries( ) | ||
| 61 | { | ||
| 62 | assertThat( m_index.getObfClassEntries(), containsInAnyOrder( | ||
| 63 | newClass( "cuchaz/enigma/inputs/Keep" ), | ||
| 64 | m_baseClass, | ||
| 65 | m_subClassA, | ||
| 66 | m_subClassAA, | ||
| 67 | m_subClassB | ||
| 68 | ) ); | ||
| 69 | } | ||
| 70 | |||
| 71 | @Test | ||
| 72 | public void translationIndex( ) | ||
| 73 | { | ||
| 74 | TranslationIndex index = m_index.getTranslationIndex(); | ||
| 75 | |||
| 76 | // base class | ||
| 77 | assertThat( index.getSuperclassName( m_baseClass.getName() ), is( nullValue() ) ); | ||
| 78 | assertThat( index.getAncestry( m_baseClass.getName() ), is( empty() ) ); | ||
| 79 | assertThat( index.getSubclassNames( m_baseClass.getName() ), containsInAnyOrder( | ||
| 80 | m_subClassA.getName(), | ||
| 81 | m_subClassB.getName() | ||
| 82 | ) ); | ||
| 83 | |||
| 84 | // subclass a | ||
| 85 | assertThat( index.getSuperclassName( m_subClassA.getName() ), is( m_baseClass.getName() ) ); | ||
| 86 | assertThat( index.getAncestry( m_subClassA.getName() ), contains( m_baseClass.getName() ) ); | ||
| 87 | assertThat( index.getSubclassNames( m_subClassA.getName() ), contains( m_subClassAA.getName() ) ); | ||
| 88 | |||
| 89 | // subclass aa | ||
| 90 | assertThat( index.getSuperclassName( m_subClassAA.getName() ), is( m_subClassA.getName() ) ); | ||
| 91 | assertThat( index.getAncestry( m_subClassAA.getName() ), contains( | ||
| 92 | m_subClassA.getName(), | ||
| 93 | m_baseClass.getName() | ||
| 94 | ) ); | ||
| 95 | assertThat( index.getSubclassNames( m_subClassAA.getName() ), is( empty() ) ); | ||
| 96 | |||
| 97 | // subclass b | ||
| 98 | assertThat( index.getSuperclassName( m_subClassB.getName() ), is( m_baseClass.getName() ) ); | ||
| 99 | assertThat( index.getAncestry( m_subClassB.getName() ), contains( m_baseClass.getName() ) ); | ||
| 100 | assertThat( index.getSubclassNames( m_subClassB.getName() ), is( empty() ) ); | ||
| 101 | } | ||
| 102 | |||
| 103 | @Test | ||
| 104 | public void access( ) | ||
| 105 | { | ||
| 106 | assertThat( m_index.getAccess( m_nameField ), is( Access.Private ) ); | ||
| 107 | assertThat( m_index.getAccess( m_numThingsField ), is( Access.Private ) ); | ||
| 108 | } | ||
| 109 | |||
| 110 | @Test | ||
| 111 | public void isImplemented( ) | ||
| 112 | { | ||
| 113 | // getName() | ||
| 114 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_baseClass, "a", "()Ljava/lang/String;" ) ), is( true ) ); | ||
| 115 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassA, "a", "()Ljava/lang/String;" ) ), is( false ) ); | ||
| 116 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassAA, "a", "()Ljava/lang/String;" ) ), is( true ) ); | ||
| 117 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassB, "a", "()Ljava/lang/String;" ) ), is( false ) ); | ||
| 118 | |||
| 119 | // doBaseThings() | ||
| 120 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_baseClass, "a", "()V" ) ), is( true ) ); | ||
| 121 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassA, "a", "()V" ) ), is( false ) ); | ||
| 122 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassAA, "a", "()V" ) ), is( true ) ); | ||
| 123 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassB, "a", "()V" ) ), is( true ) ); | ||
| 124 | |||
| 125 | // doBThings() | ||
| 126 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_baseClass, "b", "()V" ) ), is( false ) ); | ||
| 127 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassA, "b", "()V" ) ), is( false ) ); | ||
| 128 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassAA, "b", "()V" ) ), is( false ) ); | ||
| 129 | assertThat( m_index.isMethodImplemented( new MethodEntry( m_subClassB, "b", "()V" ) ), is( true ) ); | ||
| 130 | } | ||
| 131 | |||
| 132 | @Test | ||
| 133 | public void relatedMethodImplementations( ) | ||
| 134 | { | ||
| 135 | Set<MethodEntry> entries; | ||
| 136 | |||
| 137 | // getName() | ||
| 138 | entries = m_index.getRelatedMethodImplementations( new MethodEntry( m_baseClass, "a", "()Ljava/lang/String;" ) ); | ||
| 139 | assertThat( entries, containsInAnyOrder( | ||
| 140 | new MethodEntry( m_baseClass, "a", "()Ljava/lang/String;" ), | ||
| 141 | new MethodEntry( m_subClassAA, "a", "()Ljava/lang/String;" ) | ||
| 142 | ) ); | ||
| 143 | entries = m_index.getRelatedMethodImplementations( new MethodEntry( m_subClassAA, "a", "()Ljava/lang/String;" ) ); | ||
| 144 | assertThat( entries, containsInAnyOrder( | ||
| 145 | new MethodEntry( m_baseClass, "a", "()Ljava/lang/String;" ), | ||
| 146 | new MethodEntry( m_subClassAA, "a", "()Ljava/lang/String;" ) | ||
| 147 | ) ); | ||
| 148 | |||
| 149 | // doBaseThings() | ||
| 150 | entries = m_index.getRelatedMethodImplementations( new MethodEntry( m_baseClass, "a", "()V" ) ); | ||
| 151 | assertThat( entries, containsInAnyOrder( | ||
| 152 | new MethodEntry( m_baseClass, "a", "()V" ), | ||
| 153 | new MethodEntry( m_subClassAA, "a", "()V" ), | ||
| 154 | new MethodEntry( m_subClassB, "a", "()V" ) | ||
| 155 | ) ); | ||
| 156 | entries = m_index.getRelatedMethodImplementations( new MethodEntry( m_subClassAA, "a", "()V" ) ); | ||
| 157 | assertThat( entries, containsInAnyOrder( | ||
| 158 | new MethodEntry( m_baseClass, "a", "()V" ), | ||
| 159 | new MethodEntry( m_subClassAA, "a", "()V" ), | ||
| 160 | new MethodEntry( m_subClassB, "a", "()V" ) | ||
| 161 | ) ); | ||
| 162 | entries = m_index.getRelatedMethodImplementations( new MethodEntry( m_subClassB, "a", "()V" ) ); | ||
| 163 | assertThat( entries, containsInAnyOrder( | ||
| 164 | new MethodEntry( m_baseClass, "a", "()V" ), | ||
| 165 | new MethodEntry( m_subClassAA, "a", "()V" ), | ||
| 166 | new MethodEntry( m_subClassB, "a", "()V" ) | ||
| 167 | ) ); | ||
| 168 | |||
| 169 | // doBThings | ||
| 170 | entries = m_index.getRelatedMethodImplementations( new MethodEntry( m_subClassB, "b", "()V" ) ); | ||
| 171 | assertThat( entries, containsInAnyOrder( | ||
| 172 | new MethodEntry( m_subClassB, "b", "()V" ) | ||
| 173 | ) ); | ||
| 174 | } | ||
| 175 | |||
| 176 | @Test | ||
| 177 | @SuppressWarnings( "unchecked" ) | ||
| 178 | public void fieldReferences( ) | ||
| 179 | { | ||
| 180 | Collection<EntryReference<FieldEntry,BehaviorEntry>> references; | ||
| 181 | |||
| 182 | // name | ||
| 183 | references = m_index.getFieldReferences( m_nameField ); | ||
| 184 | assertThat( references, containsInAnyOrder( | ||
| 185 | newFieldReferenceByConstructor( m_nameField, m_baseClass.getName(), "(Ljava/lang/String;)V" ), | ||
| 186 | newFieldReferenceByMethod( m_nameField, m_baseClass.getName(), "a", "()Ljava/lang/String;" ) | ||
| 187 | ) ); | ||
| 188 | |||
| 189 | // numThings | ||
| 190 | references = m_index.getFieldReferences( m_numThingsField ); | ||
| 191 | assertThat( references, containsInAnyOrder( | ||
| 192 | newFieldReferenceByConstructor( m_numThingsField, m_subClassB.getName(), "()V" ), | ||
| 193 | newFieldReferenceByMethod( m_numThingsField, m_subClassB.getName(), "b", "()V" ) | ||
| 194 | ) ); | ||
| 195 | } | ||
| 196 | |||
| 197 | @Test | ||
| 198 | @SuppressWarnings( "unchecked" ) | ||
| 199 | public void behaviorReferences( ) | ||
| 200 | { | ||
| 201 | BehaviorEntry source; | ||
| 202 | Collection<EntryReference<BehaviorEntry,BehaviorEntry>> references; | ||
| 203 | |||
| 204 | // baseClass constructor | ||
| 205 | source = new ConstructorEntry( m_baseClass, "(Ljava/lang/String;)V" ); | ||
| 206 | references = m_index.getBehaviorReferences( source ); | ||
| 207 | assertThat( references, containsInAnyOrder( | ||
| 208 | newBehaviorReferenceByConstructor( source, m_subClassA.getName(), "(Ljava/lang/String;)V" ), | ||
| 209 | newBehaviorReferenceByConstructor( source, m_subClassB.getName(), "()V" ) | ||
| 210 | ) ); | ||
| 211 | |||
| 212 | // subClassA constructor | ||
| 213 | source = new ConstructorEntry( m_subClassA, "(Ljava/lang/String;)V" ); | ||
| 214 | references = m_index.getBehaviorReferences( source ); | ||
| 215 | assertThat( references, containsInAnyOrder( | ||
| 216 | newBehaviorReferenceByConstructor( source, m_subClassAA.getName(), "()V" ) | ||
| 217 | ) ); | ||
| 218 | |||
| 219 | // baseClass.getName() | ||
| 220 | source = new MethodEntry( m_baseClass, "a", "()Ljava/lang/String;" ); | ||
| 221 | references = m_index.getBehaviorReferences( source ); | ||
| 222 | assertThat( references, containsInAnyOrder( | ||
| 223 | newBehaviorReferenceByMethod( source, m_subClassAA.getName(), "a", "()Ljava/lang/String;" ) | ||
| 224 | ) ); | ||
| 225 | |||
| 226 | // subclassAA.getName() | ||
| 227 | source = new MethodEntry( m_subClassAA, "a", "()Ljava/lang/String;" ); | ||
| 228 | references = m_index.getBehaviorReferences( source ); | ||
| 229 | assertThat( references, containsInAnyOrder( | ||
| 230 | newBehaviorReferenceByMethod( source, m_subClassAA.getName(), "a", "()V" ) | ||
| 231 | ) ); | ||
| 232 | } | ||
| 233 | |||
| 234 | @Test | ||
| 235 | public void containsEntries( ) | ||
| 236 | { | ||
| 237 | // classes | ||
| 238 | assertThat( m_index.containsObfClass( m_baseClass ), is( true ) ); | ||
| 239 | assertThat( m_index.containsObfClass( m_subClassA ), is( true ) ); | ||
| 240 | assertThat( m_index.containsObfClass( m_subClassAA ), is( true ) ); | ||
| 241 | assertThat( m_index.containsObfClass( m_subClassB ), is( true ) ); | ||
| 242 | |||
| 243 | // fields | ||
| 244 | assertThat( m_index.containsObfField( m_nameField ), is( true ) ); | ||
| 245 | assertThat( m_index.containsObfField( m_numThingsField ), is( true ) ); | ||
| 246 | |||
| 247 | // methods | ||
| 248 | assertThat( m_index.containsObfMethod( new MethodEntry( m_baseClass, "a", "()Ljava/lang/String;" ) ), is( true ) ); | ||
| 249 | assertThat( m_index.containsObfMethod( new MethodEntry( m_baseClass, "a", "()V" ) ), is( true ) ); | ||
| 250 | assertThat( m_index.containsObfMethod( new MethodEntry( m_subClassAA, "a", "()Ljava/lang/String;" ) ), is( true ) ); | ||
| 251 | assertThat( m_index.containsObfMethod( new MethodEntry( m_subClassAA, "a", "()V" ) ), is( true ) ); | ||
| 252 | assertThat( m_index.containsObfMethod( new MethodEntry( m_subClassB, "a", "()V" ) ), is( true ) ); | ||
| 253 | assertThat( m_index.containsObfMethod( new MethodEntry( m_subClassB, "b", "()V" ) ), is( true ) ); | ||
| 254 | } | ||
| 255 | } | ||