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