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.statics;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
24  
25  import java.time.Duration;
26  
27  import org.apache.bcel.Const;
28  import org.apache.bcel.Constants;
29  import org.apache.bcel.Repository;
30  import org.apache.bcel.generic.ClassGen;
31  import org.apache.bcel.generic.InstructionFactory;
32  import org.apache.bcel.generic.InstructionList;
33  import org.apache.bcel.generic.MethodGen;
34  import org.apache.bcel.generic.Type;
35  import org.apache.bcel.verifier.VerificationResult;
36  import org.apache.bcel.verifier.Verifier;
37  import org.apache.bcel.verifier.VerifierFactory;
38  import org.junit.jupiter.api.AfterEach;
39  import org.junit.jupiter.api.Test;
40  
41  class Pass2VerifierTest {
42  
43      @AfterEach
44      void afterEach() {
45          VerifierFactory.clear();
46          Repository.clearCache();
47      }
48  
49      /**
50       * Tests that a referenced exception class with a circular superclass hierarchy is rejected instead of looping forever.
51       */
52      @Test
53      void testCyclicExceptionSuperclassChainRejected() {
54          final ClassGen cgA = new ClassGen("Pass2CyclicA", "Pass2CyclicB", "Pass2CyclicA.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[0]);
55          final ClassGen cgB = new ClassGen("Pass2CyclicB", "Pass2CyclicA", "Pass2CyclicB.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[0]);
56          Repository.addClass(cgA.getJavaClass());
57          Repository.addClass(cgB.getJavaClass());
58          final String className = "Pass2CyclicX";
59          final ClassGen cg = new ClassGen(className, "java.lang.Object", "Pass2CyclicX.java", Const.ACC_PUBLIC | Const.ACC_SUPER, new String[0]);
60          final InstructionList il = new InstructionList();
61          il.append(InstructionFactory.createReturn(Type.VOID));
62          final MethodGen mg = new MethodGen(Const.ACC_PUBLIC | Const.ACC_STATIC, Type.VOID, Type.NO_ARGS, new String[0], "test", className, il,
63                  cg.getConstantPool());
64          mg.addException("Pass2CyclicA");
65          mg.setMaxStack();
66          mg.setMaxLocals();
67          cg.addMethod(mg.getMethod());
68          Repository.addClass(cg.getJavaClass());
69          final Verifier verifier = VerifierFactory.getVerifier(className);
70          assertEquals(VerificationResult.VR_OK, verifier.doPass1());
71          assertTimeoutPreemptively(Duration.ofSeconds(30), () -> assertEquals(VerificationResult.VERIFIED_REJECTED, verifier.doPass2().getStatus()));
72      }
73  
74      /**
75       * Tests that we do not break binary compatibility with BCEL-330.
76       */
77      @Test
78      void testReferenceToConstant() {
79          @SuppressWarnings("unused")
80          final short referenceToConstant = Constants.AALOAD;
81      }
82  }