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