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;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23
24 import java.io.BufferedInputStream;
25 import java.io.File;
26 import java.io.InputStream;
27 import java.nio.file.Files;
28 import java.nio.file.Path;
29 import java.util.ArrayList;
30 import java.util.List;
31
32 import org.apache.bcel.AbstractTest;
33 import org.apache.bcel.Const;
34 import org.apache.bcel.classfile.ClassParser;
35 import org.apache.bcel.classfile.JavaClass;
36 import org.apache.bcel.classfile.Method;
37 import org.apache.bcel.generic.ClassGen;
38 import org.apache.bcel.generic.EmptyVisitor;
39 import org.apache.bcel.generic.ILOAD;
40 import org.apache.bcel.generic.InstructionConst;
41 import org.apache.bcel.generic.InstructionHandle;
42 import org.apache.bcel.generic.InstructionList;
43 import org.apache.bcel.generic.MethodGen;
44 import org.apache.bcel.generic.SWAP;
45 import org.apache.bcel.generic.Type;
46 import org.apache.commons.lang3.StringUtils;
47 import org.apache.commons.lang3.SystemProperties;
48 import org.junit.jupiter.api.Test;
49 import org.junit.jupiter.api.io.TempDir;
50
51 class VerifierMainTest extends AbstractTest {
52
53 @TempDir
54 Path tempDir;
55
56 @Test
57 void testSWAP() throws Exception {
58 final String className = "org.apache.bcel.data.SWAP";
59 final Path classFile = tempDir.resolve("org/apache/bcel/data/SWAP.class");
60 final ClassGen classGen = new ClassGen(className, "java.lang.Object", "SWAP.java", Const.ACC_PUBLIC | Const.ACC_SUPER, null);
61 classGen.setMajor(Const.MAJOR_1_4);
62 classGen.setMinor(Const.MINOR_1_4);
63 classGen.addEmptyConstructor(Const.ACC_PUBLIC);
64 final InstructionList instructions = new InstructionList();
65 try {
66 instructions.append(new ILOAD(0));
67 instructions.append(new ILOAD(1));
68 instructions.append(InstructionConst.SWAP);
69 instructions.append(InstructionConst.ISUB);
70 instructions.append(InstructionConst.IRETURN);
71 final MethodGen methodGen = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.INT, new Type[] { Type.INT, Type.INT },
72 new String[] { "first", "second" }, "swap", className, instructions, classGen.getConstantPool());
73 methodGen.setMaxStack();
74 methodGen.setMaxLocals();
75 classGen.addMethod(methodGen.getMethod());
76 } finally {
77 instructions.dispose();
78 }
79 classGen.getJavaClass().dump(classFile.toFile());
80 final String javaAgent = getJavaAgent();
81 final List<String> args = new ArrayList<>();
82 args.add(SystemProperties.getJavaHome() + File.separator + "bin" + File.separator + "java");
83 if (javaAgent != null) {
84 args.add(javaAgent.replace("jacoco.exec", "jacoco_org.apache.bcel.data.SWAP.exec"));
85 }
86 args.add("-cp");
87 args.add(tempDir + File.pathSeparator + SystemProperties.getJavaClassPath());
88 args.add("org.apache.bcel.verifier.Verifier");
89 args.add("org/apache/bcel/data/SWAP.class");
90 final ProcessBuilder pb = new ProcessBuilder(args);
91 pb.redirectErrorStream(true);
92 final Process p = pb.start();
93 try (BufferedInputStream is = new BufferedInputStream(p.getInputStream())) {
94 final byte[] buff = new byte[2048];
95 final StringBuilder sb = new StringBuilder();
96 for (int len; (len = is.read(buff)) != -1;) {
97 sb.append(new String(buff, 0, len));
98 }
99 final String output = sb.toString();
100 assertEquals(0, p.waitFor(), output);
101 assertEquals(0, StringUtils.countMatches(output, "VERIFIED_REJECTED"), output);
102 assertEquals(6, StringUtils.countMatches(output, "VERIFIED_OK"), output);
103 }
104
105 final List<SWAP> swapInstructionsList = new ArrayList<>();
106 final EmptyVisitor swapCollector = new EmptyVisitor() {
107 @Override
108 public void visitSWAP(final SWAP obj) {
109 swapInstructionsList.add(obj);
110 super.visitSWAP(obj);
111 }
112 };
113 try (InputStream in = Files.newInputStream(classFile)) {
114 final ClassParser classParser = new ClassParser(in, "SWAP.class");
115 final JavaClass javaClass = classParser.parse();
116 for (final Method method : javaClass.getMethods()) {
117 final InstructionList instructionList = new InstructionList(method.getCode().getCode());
118 try {
119 for (final InstructionHandle instructionHandle : instructionList) {
120 instructionHandle.accept(swapCollector);
121 }
122 } finally {
123 instructionList.dispose();
124 }
125 }
126 }
127 assertEquals(1, swapInstructionsList.size());
128 }
129 }