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