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 }
100 final boolean timedOut = executor.isFailure(exitValue) && watchdog.killedProcess();
101 if (timedOut) {
102 return "Command timed out.";
103 }
104
105
106 return errStream.toString();
107 }
108
109
110
111
112 @Test
113 void testB303() {
114 testVerify("issue303/example", "A");
115 }
116
117
118
119
120 @Test
121 void testB307() {
122 testVerify("issue307/example", "A");
123 }
124
125
126
127
128 @Test
129 void testB308() {
130 testVerify("issue308", "Hello");
131 }
132
133
134
135
136 @Test
137 void testB309() {
138 testVerify("issue309", "Hello");
139 }
140
141
142
143
144 @Test
145 void testB310() {
146 testVerify("issue310", "Hello");
147 }
148
149
150
151
152 @Test
153 void testB311() {
154 testVerify("issue311", "Hello");
155 }
156
157
158
159
160
161 @Test
162 void testB312() {
163 testVerify("issue312", "Hello");
164 }
165
166
167
168
169 @Test
170 void testB313() {
171 testVerify("issue313", "Hello");
172 }
173
174
175
176
177 @Test
178 void testB337() {
179 testVerify("issue337/example", "A");
180 }
181
182
183
184
185
186
187
188
189 private void testVerify(final String directory, final String className) {
190 final String baseDir = "target/test-classes";
191 final String testDir = baseDir + (directory.isEmpty() ? "" : File.separator + directory);
192
193 final File origFile = new File(testDir, className + ".classx");
194 final File testFile = new File(testDir, className + JavaClass.EXTENSION);
195
196 if (!origFile.renameTo(testFile)) {
197 fail("Failed to rename orig file");
198 }
199
200 String result;
201 try {
202 result = run(buildVerifyCommand(className, testDir));
203 } catch (final Exception e) {
204 result = e.getMessage();
205 }
206
207 if (!testFile.renameTo(origFile)) {
208 fail("Failed to rename test file");
209 }
210
211 assertTrue(result.isEmpty(), result);
212 }
213 }