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;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  
24  import java.io.File;
25  import java.io.IOException;
26  import java.net.URISyntaxException;
27  import java.util.Enumeration;
28  import java.util.jar.JarEntry;
29  import java.util.jar.JarFile;
30  
31  import org.apache.bcel.classfile.JavaClass;
32  import org.apache.bcel.classfile.Utility;
33  import org.apache.bcel.verifier.input.FieldVerifierChildClass;
34  import org.apache.bcel.verifier.input.StaticFieldVerifierChildClass;
35  import org.apache.commons.lang3.Strings;
36  import org.junit.jupiter.api.AfterEach;
37  import org.junit.jupiter.api.Test;
38  
39  class VerifierTest {
40  
41      private static File getJarFile(final Class<?> clazz) throws URISyntaxException {
42          return new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI());
43      }
44  
45      private static void testDefaultMethodValidation(final String className, final String... excludes) throws ClassNotFoundException {
46          if (Strings.CS.endsWithAny(className, excludes)) {
47              return;
48          }
49          final Verifier verifier = VerifierFactory.getVerifier(className);
50          VerificationResult result = verifier.doPass1();
51  
52          assertEquals(VerificationResult.VERIFIED_OK, result.getStatus(), "Pass 1 verification of " + className + " failed: " + result.getMessage());
53  
54          result = verifier.doPass2();
55  
56          assertEquals(VerificationResult.VERIFIED_OK, result.getStatus(), "Pass 2 verification of " + className + " failed: " + result.getMessage());
57  
58          if (result == VerificationResult.VR_OK) {
59              final JavaClass jc = org.apache.bcel.Repository.lookupClass(className);
60              for (int i = 0; i < jc.getMethods().length; i++) {
61                  result = verifier.doPass3a(i);
62                  assertEquals(VerificationResult.VR_OK, result, "Pass 3a, method number " + i + " ['" + jc.getMethods()[i] + "']:\n" + result);
63                  result = verifier.doPass3b(i);
64                  assertEquals(VerificationResult.VR_OK, result, "Pass 3b, method number " + i + " ['" + jc.getMethods()[i] + "']:\n" + result);
65              }
66          }
67      }
68  
69      private static void testJarFile(final File file, final String... excludes) throws IOException, ClassNotFoundException {
70          try (JarFile jarFile = new JarFile(file)) {
71              final Enumeration<JarEntry> entries = jarFile.entries();
72              while (entries.hasMoreElements()) {
73                  final JarEntry jarEntry = entries.nextElement();
74                  String entryName = jarEntry.getName();
75                  if (entryName.endsWith(JavaClass.EXTENSION)) {
76                      entryName = entryName.replaceFirst("\\.class$", "");
77                      entryName = Utility.compactClassName(entryName, false);
78                      testDefaultMethodValidation(entryName, excludes);
79                  }
80              }
81          }
82      }
83  
84      @AfterEach
85      public void afterEach() {
86          VerifierFactory.clear();
87      }
88  
89      @Test
90      void testArrayUtils() throws ClassNotFoundException {
91          testDefaultMethodValidation("org.apache.commons.lang.ArrayUtils");
92      }
93  
94      @Test
95      void testCollection() throws ClassNotFoundException {
96          testDefaultMethodValidation("java.util.Collection");
97      }
98  
99      @Test
100     void testCommonsLang2() throws IOException, URISyntaxException, ClassNotFoundException {
101         testJarFile(getJarFile(org.apache.commons.lang.StringUtils.class), "ArrayUtils", "SerializationUtils");
102     }
103 
104     @Test
105     void testDefinitionImpl() throws ClassNotFoundException {
106         testDefaultMethodValidation("com.ibm.wsdl.DefinitionImpl");
107     }
108 
109     @Test
110     void testJvmOpCodes() throws ClassNotFoundException {
111         testDefaultMethodValidation("org.apache.bcel.verifier.tests.JvmOpCodes");
112     }
113 
114     @Test
115     void testObjectInputStreamPass2() {
116         // Exercise NestHost diagnostics without depending on JDK-specific bytecode and unsupported pass 3 instructions.
117         final Verifier verifier = VerifierFactory.getVerifier("java.io.ObjectInputStream");
118         assertEquals(VerificationResult.VR_OK, verifier.doPass1());
119         assertEquals(VerificationResult.VR_OK, verifier.doPass2());
120     }
121 
122     @Test
123     void testPackagePrivateField() throws ClassNotFoundException {
124         testDefaultMethodValidation(FieldVerifierChildClass.class.getName());
125     }
126 
127     @Test
128     void testPackagePrivateStaticField() throws ClassNotFoundException {
129         testDefaultMethodValidation(StaticFieldVerifierChildClass.class.getName());
130     }
131 
132     @Test
133     void testWSDL() throws IOException, URISyntaxException, ClassNotFoundException {
134         testJarFile(getJarFile(javax.wsdl.Port.class), "WSDLReaderImpl", "DefinitionImpl");
135     }
136 }