1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.bcel.verifier.statics;
21
22 import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
23 import static org.junit.jupiter.api.Assertions.assertEquals;
24 import static org.junit.jupiter.api.Assertions.assertFalse;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.junit.jupiter.api.Assertions.assertTrue;
27 import static org.junit.jupiter.api.Assertions.fail;
28
29 import java.io.ByteArrayInputStream;
30 import java.io.ByteArrayOutputStream;
31 import java.io.DataInputStream;
32 import java.io.DataOutputStream;
33 import java.io.IOException;
34 import java.io.InputStream;
35 import java.nio.file.Files;
36 import java.nio.file.Path;
37 import java.nio.file.Paths;
38 import java.util.ArrayList;
39 import java.util.Arrays;
40 import java.util.List;
41
42 import org.apache.bcel.Const;
43 import org.apache.bcel.Repository;
44 import org.apache.bcel.classfile.Attribute;
45 import org.apache.bcel.classfile.ClassFormatException;
46 import org.apache.bcel.classfile.ClassParser;
47 import org.apache.bcel.classfile.Constant;
48 import org.apache.bcel.classfile.ConstantPool;
49 import org.apache.bcel.classfile.JavaClass;
50 import org.apache.bcel.classfile.MethodParameter;
51 import org.apache.bcel.classfile.Module;
52 import org.apache.bcel.classfile.ModuleMainClass;
53 import org.apache.bcel.classfile.ModulePackages;
54 import org.apache.bcel.classfile.NestHost;
55 import org.apache.bcel.classfile.Node;
56 import org.apache.bcel.classfile.StackMapType;
57 import org.apache.bcel.generic.ClassGen;
58 import org.apache.bcel.generic.ConstantPoolGen;
59 import org.apache.bcel.verifier.VerificationResult;
60 import org.apache.bcel.verifier.Verifier;
61 import org.apache.bcel.verifier.VerifierFactory;
62 import org.junit.jupiter.api.AfterEach;
63 import org.junit.jupiter.api.Test;
64
65
66
67
68
69
70
71
72 class StringRepresentationTest {
73
74 private static final Path MODULE_INFO_PATH = Paths.get("src/test/resources/jpms/java17/commons-io/module-info.class");
75
76 private static Attribute findAttribute(final JavaClass javaClass, final String name) {
77 for (final Attribute attribute : javaClass.getAttributes()) {
78 if (name.equals(attribute.getName())) {
79 return attribute;
80 }
81 }
82 fail("Missing attribute: " + name);
83 return null;
84 }
85
86 private static int findConstantIndex(final ConstantPool pool, final byte tag) {
87 for (int i = 1; i < pool.getLength(); i++) {
88 final Constant constant = pool.getConstant(i);
89 if (constant != null && constant.getTag() == tag) {
90 return i;
91 }
92 }
93 fail("Missing constant tag: " + tag);
94 return -1;
95 }
96
97 private static JavaClass parseModuleInfo() throws IOException {
98 try (InputStream inputStream = Files.newInputStream(MODULE_INFO_PATH)) {
99 return new ClassParser(inputStream, MODULE_INFO_PATH.toString()).parse();
100 }
101 }
102
103 @AfterEach
104 void afterEach() {
105 VerifierFactory.clear();
106 }
107
108 @Test
109 void testMalformedModulePackages() throws IOException {
110 final JavaClass moduleInfo = parseModuleInfo();
111 final ConstantPoolGen pool = new ConstantPoolGen(moduleInfo.getConstantPool());
112 final int nameIndex = pool.addUtf8("ModulePackages");
113 final ModulePackages modulePackages = new ModulePackages(nameIndex, 4, new int[] { moduleInfo.getClassNameIndex() }, pool.getConstantPool());
114 assertThrows(ClassFormatException.class, modulePackages::toString);
115 assertEquals("<<ModulePackages>>", new StringRepresentation(modulePackages).toString());
116 }
117
118 @Test
119 void testMalformedNestHost() {
120 final ConstantPoolGen pool = new ConstantPoolGen();
121 final int nameIndex = pool.addUtf8("NestHost");
122 final NestHost nestHost = new NestHost(nameIndex, 2, nameIndex, pool.getConstantPool());
123 assertThrows(ClassFormatException.class, nestHost::toString);
124 assertEquals("<<NestHost>>", new StringRepresentation(nestHost).toString());
125 }
126
127 @Test
128 void testMalformedStackMapType() {
129 final ConstantPoolGen pool = new ConstantPoolGen();
130 final int utf8Index = pool.addUtf8("NotAClassConstant");
131 final StackMapType type = new StackMapType(Const.ITEM_Object, utf8Index, pool.getConstantPool());
132 assertThrows(ClassFormatException.class, type::toString);
133 assertEquals("<<StackMapType>>", new StringRepresentation(type).toString());
134 }
135
136 @Test
137 void testMethodParameter() {
138 final MethodParameter parameter = new MethodParameter();
139 assertEquals(parameter.toString(), new StringRepresentation(parameter).toString());
140 }
141
142 @Test
143 void testModuleAttribute() throws IOException {
144 final Attribute module = findAttribute(parseModuleInfo(), "Module");
145 assertEquals(module.toString(), new StringRepresentation(module).toString());
146 }
147
148 @Test
149 void testModuleEntries() throws IOException {
150 final Module module = (Module) findAttribute(parseModuleInfo(), "Module");
151 final List<Node> entries = new ArrayList<>();
152 assertTrue(module.getRequiresTable().length > 0);
153 assertTrue(module.getExportsTable().length > 0);
154 entries.addAll(Arrays.asList(module.getRequiresTable()));
155 entries.addAll(Arrays.asList(module.getExportsTable()));
156 assertFalse(entries.isEmpty(), "The module descriptor is expected to have entries");
157 for (final Node entry : entries) {
158 assertEquals(entry.toString(), new StringRepresentation(entry).toString());
159 }
160 }
161
162 @Test
163 void testModuleOpensAndProvides() throws IOException {
164 final JavaClass moduleInfo = parseModuleInfo();
165 final Module module = (Module) findAttribute(moduleInfo, "Module");
166 final ConstantPoolGen pool = new ConstantPoolGen(moduleInfo.getConstantPool());
167 final int serviceIndex = pool.addClass("example.Service");
168 final int implementationIndex = pool.addClass("example.ServiceImpl");
169 final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
170 try (DataOutputStream output = new DataOutputStream(bytes)) {
171 output.writeShort(module.getNameIndex());
172 output.writeInt(28);
173 output.writeShort(findConstantIndex(moduleInfo.getConstantPool(), Const.CONSTANT_Module));
174 output.writeShort(0);
175 output.writeShort(0);
176 output.writeShort(0);
177 output.writeShort(0);
178 output.writeShort(1);
179 output.writeShort(findConstantIndex(moduleInfo.getConstantPool(), Const.CONSTANT_Package));
180 output.writeShort(0);
181 output.writeShort(0);
182 output.writeShort(0);
183 output.writeShort(1);
184 output.writeShort(serviceIndex);
185 output.writeShort(1);
186 output.writeShort(implementationIndex);
187 }
188
189 final Module populated;
190 try (DataInputStream input = new DataInputStream(new ByteArrayInputStream(bytes.toByteArray()))) {
191 populated = (Module) Attribute.readAttribute(input, pool.getConstantPool());
192 }
193 assertEquals(1, populated.getOpensTable().length);
194 assertEquals(1, populated.getProvidesTable().length);
195 for (final Node entry : Arrays.asList(populated.getOpensTable()[0], populated.getProvidesTable()[0])) {
196 assertEquals(entry.toString(), new StringRepresentation(entry).toString());
197 }
198 }
199
200 @Test
201 void testNestHost() {
202 final ConstantPoolGen pool = new ConstantPoolGen();
203 final int nameIndex = pool.addUtf8("NestHost");
204 final int hostIndex = pool.addClass("example.Host");
205 final NestHost nestHost = new NestHost(nameIndex, 2, hostIndex, pool.getConstantPool());
206 assertEquals("NestHost: example.Host", new StringRepresentation(nestHost).toString());
207 assertEquals(nestHost.toString(), new StringRepresentation(nestHost).toString());
208 }
209
210 @Test
211 void testOtherModuleDescriptorAttributes() throws IOException {
212 final JavaClass moduleInfo = parseModuleInfo();
213 final ConstantPoolGen pool = new ConstantPoolGen(moduleInfo.getConstantPool());
214 final int packagesNameIndex = pool.addUtf8("ModulePackages");
215 final int mainClassNameIndex = pool.addUtf8("ModuleMainClass");
216 final int mainClassIndex = pool.addClass("example.Main");
217 final ConstantPool constantPool = pool.getConstantPool();
218 final ModulePackages modulePackages = new ModulePackages(packagesNameIndex, 4,
219 new int[] { findConstantIndex(moduleInfo.getConstantPool(), Const.CONSTANT_Package) }, constantPool);
220 final ModuleMainClass moduleMainClass = new ModuleMainClass(mainClassNameIndex, 2, mainClassIndex, constantPool);
221 for (final Node node : Arrays.asList(modulePackages, moduleMainClass)) {
222 assertEquals(node.toString(), new StringRepresentation(node).toString());
223 }
224 }
225
226 @Test
227 void testStackMapTypes() {
228 final ConstantPoolGen pool = new ConstantPoolGen();
229 final int classIndex = pool.addClass("example.Value");
230 for (byte tag = Const.ITEM_Bogus; tag <= Const.ITEM_NewObject; tag++) {
231 final StackMapType type = new StackMapType(tag, tag == Const.ITEM_Object ? classIndex : 0, pool.getConstantPool());
232 assertEquals(type.toString(), new StringRepresentation(type).toString(), "Verification type tag " + tag);
233 }
234 }
235
236 @Test
237 void testVerifierOnAModuleDescriptor() throws IOException, ClassNotFoundException {
238 final JavaClass moduleInfo = parseModuleInfo();
239 final JavaClass previous = Repository.addClass(moduleInfo);
240 try {
241 final Verifier verifier = VerifierFactory.getVerifier(moduleInfo.getClassName());
242 verifier.flush();
243 final VerificationResult result = assertDoesNotThrow(verifier::doPass2);
244
245 assertEquals(VerificationResult.VERIFIED_REJECTED, result.getStatus());
246 assertEquals("Superclass of 'module-info' missing but not java.lang.Object itself!", result.getMessage());
247 assertTrue(Arrays.stream(verifier.getMessages())
248 .anyMatch(message -> message.contains("Module:") && message.contains("is unknown and will therefore be ignored")));
249 } finally {
250 Repository.removeClass(moduleInfo);
251 if (previous != null) {
252 Repository.addClass(previous);
253 }
254 }
255 }
256
257 @Test
258 void testVerifierOnNestMember() throws IOException, ClassNotFoundException {
259 final ClassGen generator = new ClassGen("example.Host$Member", "java.lang.Object", "Host.java",
260 Const.ACC_PUBLIC | Const.ACC_SUPER, new String[0]);
261 generator.setMajor(Const.MAJOR_11);
262 final ConstantPoolGen pool = generator.getConstantPool();
263 final int nameIndex = pool.addUtf8("NestHost");
264 final int hostIndex = pool.addClass("example.Host");
265 generator.addAttribute(new NestHost(nameIndex, 2, hostIndex, pool.getConstantPool()));
266
267 final JavaClass member;
268 try (InputStream input = new ByteArrayInputStream(generator.getJavaClass().getBytes())) {
269 member = new ClassParser(input, "Host$Member.class").parse();
270 }
271 final JavaClass previous = Repository.addClass(member);
272 try {
273 final Verifier verifier = VerifierFactory.getVerifier(member.getClassName());
274 verifier.flush();
275 assertEquals(VerificationResult.VR_OK, assertDoesNotThrow(verifier::doPass2));
276 assertTrue(Arrays.stream(verifier.getMessages()).anyMatch(message -> message.contains("NestHost: example.Host")
277 && message.contains("is unknown and will therefore be ignored")));
278 } finally {
279 Repository.removeClass(member);
280 if (previous != null) {
281 Repository.addClass(previous);
282 }
283 }
284 }
285 }