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