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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import org.apache.bcel.AbstractTestCase;
24  import org.apache.bcel.Repository;
25  import org.apache.bcel.classfile.JavaClass;
26  import org.junit.jupiter.api.AfterEach;
27  
28  public abstract class AbstractVerifierTestCase {
29  
30      public static final String TEST_PACKAGE = AbstractVerifierTestCase.class.getPackage().getName() + ".tests.";
31  
32      @AfterEach
33      public void afterEach() {
34          AbstractTestCase.clear();
35      }
36  
37      /**
38       * Asserts that the verification of the given class is OK. If it isn't it throws an AssertionFailedError with the given
39       * message.
40       *
41       * @param className simple class name of the class to verify
42       * @param message message displayed if assertion fails
43       */
44      public void assertVerifyOK(final String className, final String message) throws ClassNotFoundException {
45          final String testClassName = TEST_PACKAGE + className;
46          assertTrue(doAllPasses(testClassName), message);
47      }
48  
49      /**
50       * Asserts that the verification of the given class is rejected. If it isn't it throws an AssertionFailedError with the
51       * given message.
52       *
53       * @param className simple class name of the class to verify
54       * @param message message displayed if assertion fails
55       */
56      public void assertVerifyRejected(final String className, final String message) throws ClassNotFoundException {
57          final String testClassname = TEST_PACKAGE + className;
58          assertFalse(doAllPasses(testClassname), message);
59      }
60  
61      /**
62       * Executes all the verification on the given class.
63       *
64       * @param className name of the class to verify
65       * @return false if the verification fails, true otherwise
66       */
67      public boolean doAllPasses(final String className) throws ClassNotFoundException {
68          final JavaClass jc = Repository.lookupClass(className);
69          final int nbMethods = jc.getMethods().length;
70  
71          final Verifier verifier = VerifierFactory.getVerifier(className);
72          VerificationResult result = verifier.doPass1();
73          if (result.getStatus() != VerificationResult.VERIFIED_OK) {
74              return false;
75          }
76  
77          result = verifier.doPass2();
78          if (result.getStatus() != VerificationResult.VERIFIED_OK) {
79              return false;
80          }
81  
82          for (int i = nbMethods; --i >= 0;) {
83              result = verifier.doPass3a(i);
84              if (result.getStatus() != VerificationResult.VERIFIED_OK) {
85                  return false;
86              }
87              result = verifier.doPass3b(i);
88              if (result.getStatus() != VerificationResult.VERIFIED_OK) {
89                  return false;
90              }
91          }
92  
93          return true;
94      }
95  
96  }