1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
|
/*******************************************************************************
* Copyright (c) 2015 Jeff Martin.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the GNU Lesser General Public
* License v3.0 which accompanies this distribution, and is available at
* http://www.gnu.org/licenses/lgpl.html
*
* Contributors:
* Jeff Martin - initial API and implementation
******************************************************************************/
package cuchaz.enigma;
import cuchaz.enigma.analysis.*;
import cuchaz.enigma.analysis.index.EntryIndex;
import cuchaz.enigma.analysis.index.InheritanceIndex;
import cuchaz.enigma.analysis.index.JarIndex;
import cuchaz.enigma.translation.VoidTranslator;
import cuchaz.enigma.translation.representation.AccessFlags;
import cuchaz.enigma.translation.representation.entry.ClassEntry;
import cuchaz.enigma.translation.representation.entry.FieldEntry;
import cuchaz.enigma.translation.representation.entry.MethodDefEntry;
import cuchaz.enigma.translation.representation.entry.MethodEntry;
import org.junit.Test;
import java.util.Collection;
import java.util.List;
import java.util.jar.JarFile;
import static cuchaz.enigma.TestEntryFactory.*;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
public class TestJarIndexLoneClass {
private JarIndex index;
public TestJarIndexLoneClass()
throws Exception {
index = JarIndex.empty();
index.indexJar(new ParsedJar(new JarFile("build/test-obf/loneClass.jar")), s -> {});
}
@Test
public void obfEntries() {
assertThat(index.getEntryIndex().getClasses(), containsInAnyOrder(
newClass("cuchaz/enigma/inputs/Keep"),
newClass("a")
));
}
@Test
public void translationIndex() {
InheritanceIndex inheritanceIndex = index.getInheritanceIndex();
assertThat(inheritanceIndex.getParents(new ClassEntry("a")), is(empty()));
assertThat(inheritanceIndex.getParents(new ClassEntry("cuchaz/enigma/inputs/Keep")), is(empty()));
assertThat(inheritanceIndex.getAncestors(new ClassEntry("a")), is(empty()));
assertThat(inheritanceIndex.getAncestors(new ClassEntry("cuchaz/enigma/inputs/Keep")), is(empty()));
assertThat(inheritanceIndex.getChildren(new ClassEntry("a")), is(empty()));
assertThat(inheritanceIndex.getChildren(new ClassEntry("cuchaz/enigma/inputs/Keep")), is(empty()));
}
@Test
public void access() {
EntryIndex entryIndex = index.getEntryIndex();
assertThat(entryIndex.getFieldAccess(newField("a", "a", "Ljava/lang/String;")), is(AccessFlags.PRIVATE));
assertThat(entryIndex.getMethodAccess(newMethod("a", "a", "()Ljava/lang/String;")), is(AccessFlags.PUBLIC));
assertThat(entryIndex.getFieldAccess(newField("a", "b", "Ljava/lang/String;")), is(nullValue()));
assertThat(entryIndex.getFieldAccess(newField("a", "a", "LFoo;")), is(nullValue()));
}
@Test
public void classInheritance() {
IndexTreeBuilder treeBuilder = new IndexTreeBuilder(index);
ClassInheritanceTreeNode node = treeBuilder.buildClassInheritance(VoidTranslator.INSTANCE, newClass("a"));
assertThat(node, is(not(nullValue())));
assertThat(node.getObfClassName(), is("a"));
assertThat(node.getChildCount(), is(0));
}
@Test
public void methodInheritance() {
IndexTreeBuilder treeBuilder = new IndexTreeBuilder(index);
MethodEntry source = newMethod("a", "a", "()Ljava/lang/String;");
MethodInheritanceTreeNode node = treeBuilder.buildMethodInheritance(VoidTranslator.INSTANCE, source);
assertThat(node, is(not(nullValue())));
assertThat(node.getMethodEntry(), is(source));
assertThat(node.getChildCount(), is(0));
}
@Test
public void classImplementations() {
IndexTreeBuilder treeBuilder = new IndexTreeBuilder(index);
ClassImplementationsTreeNode node = treeBuilder.buildClassImplementations(VoidTranslator.INSTANCE, newClass("a"));
assertThat(node, is(nullValue()));
}
@Test
public void methodImplementations() {
IndexTreeBuilder treeBuilder = new IndexTreeBuilder(index);
MethodEntry source = newMethod("a", "a", "()Ljava/lang/String;");
List<MethodImplementationsTreeNode> nodes = treeBuilder.buildMethodImplementations(VoidTranslator.INSTANCE, source);
assertThat(nodes, hasSize(1));
assertThat(nodes.get(0).getMethodEntry(), is(source));
}
@Test
public void relatedMethodImplementations() {
Collection<MethodEntry> entries = index.getEntryResolver().resolveEquivalentMethods(newMethod("a", "a", "()Ljava/lang/String;"));
assertThat(entries, containsInAnyOrder(
newMethod("a", "a", "()Ljava/lang/String;")
));
}
@Test
@SuppressWarnings("unchecked")
public void fieldReferences() {
FieldEntry source = newField("a", "a", "Ljava/lang/String;");
Collection<EntryReference<FieldEntry, MethodDefEntry>> references = index.getReferenceIndex().getReferencesToField(source);
assertThat(references, containsInAnyOrder(
newFieldReferenceByMethod(source, "a", "<init>", "(Ljava/lang/String;)V"),
newFieldReferenceByMethod(source, "a", "a", "()Ljava/lang/String;")
));
}
@Test
public void behaviorReferences() {
assertThat(index.getReferenceIndex().getReferencesToMethod(newMethod("a", "a", "()Ljava/lang/String;")), is(empty()));
}
@Test
public void interfaces() {
assertThat(index.getInheritanceIndex().getParents(new ClassEntry("a")), is(empty()));
}
@Test
public void implementingClasses() {
assertThat(index.getInheritanceIndex().getChildren(new ClassEntry("a")), is(empty()));
}
@Test
public void isInterface() {
assertThat(index.getInheritanceIndex().isParent(new ClassEntry("a")), is(false));
}
@Test
public void testContains() {
EntryIndex entryIndex = index.getEntryIndex();
assertThat(entryIndex.hasClass(newClass("a")), is(true));
assertThat(entryIndex.hasClass(newClass("b")), is(false));
assertThat(entryIndex.hasField(newField("a", "a", "Ljava/lang/String;")), is(true));
assertThat(entryIndex.hasField(newField("a", "b", "Ljava/lang/String;")), is(false));
assertThat(entryIndex.hasField(newField("a", "a", "LFoo;")), is(false));
assertThat(entryIndex.hasMethod(newMethod("a", "a", "()Ljava/lang/String;")), is(true));
assertThat(entryIndex.hasMethod(newMethod("a", "b", "()Ljava/lang/String;")), is(false));
}
}
|