1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.bcel.verifier;
19
20 import static org.junit.jupiter.api.Assertions.assertTrue;
21 import static org.junit.jupiter.api.Assertions.fail;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.File;
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.List;
29
30 import org.apache.bcel.classfile.JavaClass;
31 import org.apache.commons.exec.CommandLine;
32 import org.apache.commons.exec.DefaultExecuteResultHandler;
33 import org.apache.commons.exec.DefaultExecutor;
34 import org.apache.commons.exec.ExecuteException;
35 import org.apache.commons.exec.ExecuteWatchdog;
36 import org.apache.commons.exec.PumpStreamHandler;
37 import org.apache.commons.lang3.ArrayUtils;
38 import org.junit.jupiter.api.Test;
39
40
41
42
43
44 public class VerifyBadClassesTestCase {
45
46 private List<String> buildVerifyCommand(final String className, final String testDir) {
47 final List<String> command = new ArrayList<>();
48 command.add("java");
49 command.add("-ea");
50
51 command.add("-classpath");
52 command.add(System.getProperty("java.class.path") + ":" + testDir);
53
54 command.add("org.apache.bcel.verifier.Verifier");
55 command.add(className);
56
57 return command;
58 }
59
60
61
62
63
64
65
66
67
68
69 private String run(final List<String> command) throws ExecuteException, IOException {
70
71
72 final long timeout = 30 * 1000;
73
74 final String[] args = command.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
75 final CommandLine cmdLine = new CommandLine(args[0]);
76 cmdLine.addArguments(Arrays.copyOfRange(args, 1, args.length));
77
78 final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
79 final DefaultExecutor executor = new DefaultExecutor();
80
81 final ExecuteWatchdog watchdog = new ExecuteWatchdog(timeout);
82 executor.setWatchdog(watchdog);
83
84 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
85 final ByteArrayOutputStream errStream = new ByteArrayOutputStream();
86 final PumpStreamHandler streamHandler = new PumpStreamHandler(outStream, errStream);
87 executor.setStreamHandler(streamHandler);
88 executor.execute(cmdLine, resultHandler);
89
90 int exitValue = -1;
91 try {
92 resultHandler.waitFor();
93 exitValue = resultHandler.getExitValue();
94 } catch (final InterruptedException e) {
95
96 }
97 final boolean timedOut = executor.isFailure(exitValue) && watchdog.killedProcess();
98 if (timedOut) {
99 return "Command timed out.";
100 }
101
102
103 return errStream.toString();
104 }
105
106
107
108
109 @Test
110 public void testB303() {
111 testVerify("issue303/example", "A");
112 }
113
114
115
116
117 @Test
118 public void testB307() {
119 testVerify("issue307/example", "A");
120 }
121
122
123
124
125 @Test
126 public void testB308() {
127 testVerify("issue308", "Hello");
128 }
129
130
131
132
133 @Test
134 public void testB309() {
135 testVerify("issue309", "Hello");
136 }
137
138
139
140
141 @Test
142 public void testB310() {
143 testVerify("issue310", "Hello");
144 }
145
146
147
148
149 @Test
150 public void testB311() {
151 testVerify("issue311", "Hello");
152 }
153
154
155
156
157
158 @Test
159 public void testB312() {
160 testVerify("issue312", "Hello");
161 }
162
163
164
165
166 @Test
167 public void testB313() {
168 testVerify("issue313", "Hello");
169 }
170
171
172
173
174 @Test
175 public void testB337() {
176 testVerify("issue337/example", "A");
177 }
178
179
180
181
182
183
184
185
186 private void testVerify(final String directory, final String className) {
187 final String baseDir = "target/test-classes";
188 final String testDir = baseDir + (directory.isEmpty() ? "" : File.separator + directory);
189
190 final File origFile = new File(testDir, className + ".classx");
191 final File testFile = new File(testDir, className + JavaClass.EXTENSION);
192
193 if (!origFile.renameTo(testFile)) {
194 fail("Failed to rename orig file");
195 }
196
197 String result;
198 try {
199 result = run(buildVerifyCommand(className, testDir));
200 } catch (final Exception e) {
201 result = e.getMessage();
202 }
203
204 if (!testFile.renameTo(origFile)) {
205 fail("Failed to rename test file");
206 }
207
208 assertTrue(result.isEmpty(), result);
209 }
210 }