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  package org.apache.bcel.classfile;
20  
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertThrowsExactly;
23  
24  import java.io.DataInput;
25  import java.util.Map;
26  import java.util.Optional;
27  
28  import org.apache.bcel.AbstractTest;
29  import org.apache.bcel.Repository;
30  import org.apache.bcel.generic.Type;
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   *  Tests signatures, including primitives, generics, and bad signatures for exceptions.
35   */
36  class SignatureTest extends AbstractTest {
37  
38      @Test
39      void testBadSignatures() throws Exception {
40          assertThrowsExactly(IllegalArgumentException.class, () -> Signature.translate("<"));
41          assertThrowsExactly(IllegalArgumentException.class, () -> Signature.translate("<>"));
42      }
43  
44      /**
45       * Deeply nested attacker-supplied signatures must fail fast instead of overflowing the stack.
46       */
47      @Test
48      void testDeeplyNestedSignature() {
49          final StringBuilder sb = new StringBuilder();
50          for (int i = 0; i < 100_000; i++) {
51              sb.append("A<");
52          }
53          final String deep = sb.toString();
54          assertThrowsExactly(IllegalArgumentException.class, () -> Signature.translate(deep));
55      }
56  
57      @Test
58      void testMap() throws Exception {
59          final JavaClass jc = Repository.lookupClass(Map.class);
60          final Signature classSignature = (Signature) findAttribute("Signature", jc.getAttributes());
61          final String translatedSignature = Signature.translate(classSignature.getSignature());
62          assertEquals("<K, V>java.lang.Object", translatedSignature);
63          testMethod(jc, "(java.lang.Object)V", Map.class, "get", Object.class);
64          testMethod(jc, "(K, V)V", Map.class, "put", Object.class, Object.class);
65      }
66  
67      void testMethod(final JavaClass jc, final String expected, final Class<?> clazz, final String methodName, final Class<?>... paramTypes) throws Exception {
68          final Method method = jc.getMethod(clazz.getMethod(methodName, paramTypes));
69          final String methodSignature = Optional.ofNullable(method.getGenericSignature()).orElse(method.getSignature());
70          final String translatedMethodSignature = Signature.translate(methodSignature);
71          assertEquals(expected, translatedMethodSignature);
72      }
73  
74      @Test
75      void testString() throws Exception {
76          final JavaClass jc = Repository.lookupClass(String.class);
77          final Signature classSignature = (Signature) findAttribute("Signature", jc.getAttributes());
78          final String translatedSignature = Signature.translate(classSignature.getSignature());
79          assertEquals("java.lang.Object", translatedSignature);
80          testMethod(jc, "()java.lang.String", String.class, "trim");
81  
82      }
83  
84      @Test
85      void testType() throws Exception {
86          assertEquals("(I)I", Type.getSignature(Math.class.getMethod("abs", int.class)));
87          assertEquals("(J)J", Type.getSignature(Math.class.getMethod("abs", long.class)));
88          assertEquals("(D)D", Type.getSignature(Math.class.getMethod("abs", double.class)));
89          assertEquals("(F)F", Type.getSignature(Math.class.getMethod("abs", float.class)));
90          assertEquals("(Ljava/lang/String;)[Ljava/lang/String;", Type.getSignature(String.class.getMethod("split", String.class)));
91          assertEquals("(I)C", Type.getSignature(String.class.getMethod("charAt", int.class)));
92          assertEquals("()Ljava/lang/String;", Type.getSignature(String.class.getMethod("trim")));
93          assertEquals("()V", Type.getSignature(Object.class.getMethod("notify")));
94          assertEquals("(Ljava/lang/Object;)Z", Type.getSignature(Object.class.getMethod("equals", Object.class)));
95          assertEquals("()B", Type.getSignature(DataInput.class.getMethod("readByte")));
96          assertEquals("()S", Type.getSignature(DataInput.class.getMethod("readShort")));
97      }
98  }