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.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertThrows;
25  import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
26  
27  import java.time.Duration;
28  
29  import org.apache.bcel.generic.Type;
30  import org.apache.bcel.verifier.exc.LocalVariableInfoInconsistentException;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests {@link LocalVariableInfo}.
35   */
36  class LocalVariableInfoTest {
37  
38      @Test
39      void testConflictingNameRejected() {
40          final LocalVariableInfo info = new LocalVariableInfo();
41          info.add("a", 0, 10, Type.INT);
42          assertThrows(LocalVariableInfoInconsistentException.class, () -> info.add("b", 5, 10, Type.INT));
43      }
44  
45      @Test
46      void testConflictingTypeRejected() {
47          final LocalVariableInfo info = new LocalVariableInfo();
48          info.add("a", 0, 10, Type.INT);
49          assertThrows(LocalVariableInfoInconsistentException.class, () -> info.add("a", 10, 5, Type.FLOAT));
50      }
51  
52      @Test
53      void testConsistentOverlapAccepted() {
54          final LocalVariableInfo info = new LocalVariableInfo();
55          info.add("a", 0, 10, Type.INT);
56          info.add("a", 5, 10, Type.INT);
57          assertEquals("a", info.getName(15));
58          assertNull(info.getName(16));
59      }
60  
61      @Test
62      void testLookups() {
63          final LocalVariableInfo info = new LocalVariableInfo();
64          info.add("a", 0, 10, Type.INT);
65          info.add("b", 20, 5, Type.FLOAT);
66          assertEquals("a", info.getName(0));
67          assertEquals("a", info.getName(10));
68          assertEquals(Type.INT, info.getType(5));
69          assertNull(info.getName(11));
70          assertNull(info.getType(19));
71          assertEquals("b", info.getName(20));
72          assertEquals(Type.FLOAT, info.getType(25));
73          assertNull(info.getName(26));
74      }
75  
76      /**
77       * A malicious class file can declare 65,535 maximum-length LocalVariableTable entries for the same slot; processing them must stay proportional to the
78       * number of entries, not to entries times offsets.
79       */
80      @Test
81      void testManyMaximumLengthEntriesFinishQuickly() {
82          assertTimeoutPreemptively(Duration.ofSeconds(10), () -> {
83              final LocalVariableInfo info = new LocalVariableInfo();
84              for (int i = 0; i < 65_535; i++) {
85                  info.add("dup", 0, 65_535, Type.INT);
86              }
87              assertEquals("dup", info.getName(65_535));
88              assertNull(info.getName(65_536));
89          });
90      }
91  }