View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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.InputStream;
26  import java.io.PrintWriter;
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.classfile.ClassParser;
34  import org.apache.bcel.classfile.JavaClass;
35  import org.apache.bcel.classfile.Method;
36  import org.apache.bcel.generic.EmptyVisitor;
37  import org.apache.bcel.generic.InstructionHandle;
38  import org.apache.bcel.generic.InstructionList;
39  import org.apache.bcel.generic.SWAP;
40  import org.apache.commons.lang3.StringUtils;
41  import org.apache.commons.lang3.SystemProperties;
42  import org.eclipse.jdt.internal.compiler.batch.Main;
43  import org.junit.jupiter.api.Test;
44  import org.junit.jupiter.api.io.TempDir;
45  
46  class VerifierMainTest extends AbstractTest {
47  
48      @TempDir
49      Path tempDir;
50  
51      @Test
52      void testSWAP() throws Exception {
53          final String[] argv = { "src/test/java/org/apache/bcel/data/SWAP.java", "-g", "-source", "1.4", "-target", "1.4", "-d", tempDir.toString() };
54          new Main(new PrintWriter(System.out), new PrintWriter(System.err), false/*systemExit*/, null/*options*/, null/*progress*/).compile(argv);
55          final String javaAgent = getJavaAgent();
56          final List<String> args = new ArrayList<>();
57          args.add("java");
58          if (javaAgent != null) {
59              args.add(javaAgent.replace("jacoco.exec", "jacoco_org.apache.bcel.data.SWAP.exec"));
60          }
61          args.add("-cp");
62          args.add(SystemProperties.getJavaClassPath());
63          args.add("org.apache.bcel.verifier.Verifier");
64          args.add("org/apache/bcel/data/SWAP.class");
65          final ProcessBuilder pb = new ProcessBuilder(args);
66          pb.redirectErrorStream(true);
67          final Process p = pb.start();
68          try (BufferedInputStream is = new BufferedInputStream(p.getInputStream())) {
69              final byte[] buff = new byte[2048];
70              final StringBuilder sb = new StringBuilder();
71              for (int len; (len = is.read(buff)) != -1;) {
72                  sb.append(new String(buff, 0, len));
73              }
74              final String output = sb.toString();
75              assertEquals(0, p.waitFor(), output);
76              assertEquals(0, StringUtils.countMatches(output, "VERIFIED_REJECTED"), output);
77              assertEquals(6, StringUtils.countMatches(output, "VERIFIED_OK"), output);
78          }
79          // Class has passed the JustIce verifier, but now we need to ensure that the SWAP instruction is in the compiled class.
80          final List<SWAP> swapInstructionsList = new ArrayList<>();
81          final EmptyVisitor swapCollector = new EmptyVisitor() {
82              @Override
83              public void visitSWAP(final SWAP obj) {
84                  swapInstructionsList.add(obj);
85                  super.visitSWAP(obj);
86              }
87          };
88          try (InputStream in = Files.newInputStream(tempDir.resolve("org/apache/bcel/data/SWAP.class"))) {
89              final ClassParser classParser = new ClassParser(in, "SWAP.class");
90              final JavaClass javaClass = classParser.parse();
91              final Method method = javaClass.getMethod(org.apache.bcel.data.SWAP.class.getMethod("getTestConstructor", Class.class));
92              final byte[] code = method.getCode().getCode();
93              final InstructionList instructionList = new InstructionList(code);
94              for (final InstructionHandle instructionHandle : instructionList) {
95                  instructionHandle.accept(swapCollector);
96              }
97          }
98          assertEquals(1, swapInstructionsList.size());
99      }
100 }