You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/11/10 21:34:20 UTC

[commons-bcel] branch master updated: code coverage and unit tests on the verifier (#166)

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-bcel.git


The following commit(s) were added to refs/heads/master by this push:
     new e0d0cbb8 code coverage and unit tests on the verifier (#166)
e0d0cbb8 is described below

commit e0d0cbb89ab1c56ae34715565b0b072b6b192f58
Author: nbauma109 <nb...@users.noreply.github.com>
AuthorDate: Thu Nov 10 22:34:15 2022 +0100

    code coverage and unit tests on the verifier (#166)
---
 pom.xml                                            | 14 ++++
 .../bcel/classfile/JDKClassDumpTestCase.java       | 10 ++-
 .../apache/bcel/generic/EmptyVisitorTestCase.java  |  8 +-
 .../java/org/apache/bcel/generic/LargeJump.java    | 97 ++++++++++++++++++++++
 .../org/apache/bcel/verifier/VerifierTestCase.java | 97 ++++++++++++++++++++--
 5 files changed, 215 insertions(+), 11 deletions(-)

diff --git a/pom.xml b/pom.xml
index caf01980..43950216 100644
--- a/pom.xml
+++ b/pom.xml
@@ -475,6 +475,20 @@
       <version>1.3</version>
       <scope>test</scope>
     </dependency>
+    <!-- START very old jars for Justice verifier -->
+    <dependency>
+      <groupId>commons-lang</groupId>
+      <artifactId>commons-lang</artifactId>
+      <version>2.0</version>
+      <scope>test</scope>
+    </dependency>
+    <dependency>
+      <groupId>wsdl4j</groupId>
+      <artifactId>wsdl4j</artifactId>
+      <version>1.5.3</version>
+      <scope>test</scope>
+    </dependency>
+    <!-- END very old jars for Justice verifier -->
   </dependencies>
 
   <profiles>
diff --git a/src/test/java/org/apache/bcel/classfile/JDKClassDumpTestCase.java b/src/test/java/org/apache/bcel/classfile/JDKClassDumpTestCase.java
index 723eb1ee..e92e1c69 100644
--- a/src/test/java/org/apache/bcel/classfile/JDKClassDumpTestCase.java
+++ b/src/test/java/org/apache/bcel/classfile/JDKClassDumpTestCase.java
@@ -29,6 +29,8 @@ import java.util.Enumeration;
 import java.util.jar.JarEntry;
 import java.util.jar.JarFile;
 
+import org.apache.bcel.generic.JavaHome;
+import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.MethodSource;
 
@@ -54,7 +56,7 @@ public class JDKClassDumpTestCase {
     }
 
     private void testJar(final Path path) throws Exception {
-        try (final JarFile jar = new JarFile(path.toFile())) {
+        try (JarFile jar = new JarFile(path.toFile())) {
             System.out.println("Parsing " + jar.getName());
             final Enumeration<JarEntry> en = jar.entries();
             while (en.hasMoreElements()) {
@@ -75,5 +77,9 @@ public class JDKClassDumpTestCase {
     public void testPerformance(final Path path) throws Exception {
         assertDoesNotThrow(() -> testJar(path));
     }
-
+    
+    @Test
+    public void testPerformanceJmod() throws Exception {
+        JavaHome.streamModulePath().forEach(path -> assertDoesNotThrow(() -> testJar(path)));
+    }
 }
diff --git a/src/test/java/org/apache/bcel/generic/EmptyVisitorTestCase.java b/src/test/java/org/apache/bcel/generic/EmptyVisitorTestCase.java
index cf43293e..462818b3 100644
--- a/src/test/java/org/apache/bcel/generic/EmptyVisitorTestCase.java
+++ b/src/test/java/org/apache/bcel/generic/EmptyVisitorTestCase.java
@@ -16,8 +16,6 @@
  */
 package org.apache.bcel.generic;
 
-import static org.junit.jupiter.api.Assertions.fail;
-
 import org.apache.bcel.classfile.Code;
 import org.apache.bcel.classfile.JavaClass;
 import org.apache.bcel.classfile.Method;
@@ -25,6 +23,8 @@ import org.apache.bcel.util.SyntheticRepository;
 import org.junit.jupiter.params.ParameterizedTest;
 import org.junit.jupiter.params.provider.ValueSource;
 
+import static org.junit.jupiter.api.Assertions.fail;
+
 class EmptyVisitorTestCase {
 
     /*
@@ -49,7 +49,9 @@ class EmptyVisitorTestCase {
         "java.awt.GradientPaintContext",                 // contains instruction DREM
         "java.util.concurrent.atomic.DoubleAccumulator", // contains instruction DUP2_X1
         "java.util.Hashtable",                           // contains instruction FNEG
-        "javax.swing.text.html.CSS"                      // contains instruction DUP2_X2
+        "javax.swing.text.html.CSS",                     // contains instruction DUP2_X2
+        "org.apache.bcel.generic.LargeJump",             // contains instruction GOTO_W
+        "org.apache.commons.lang.SerializationUtils"     // contains instruction JSR
     // @formatter:on
     })
     public void test(final String className) throws ClassNotFoundException {
diff --git a/src/test/java/org/apache/bcel/generic/LargeJump.java b/src/test/java/org/apache/bcel/generic/LargeJump.java
new file mode 100644
index 00000000..e03aed6a
--- /dev/null
+++ b/src/test/java/org/apache/bcel/generic/LargeJump.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+package org.apache.bcel.generic;
+
+class LargeJump {
+    public static void methodWithLargeJump(final int i) {
+        for (; i == 0;) {
+            try {
+                x();
+            } finally {
+                switch (i) {
+                    case 1:
+                        try {
+                            x();
+                        } finally {
+                            switch (i) {
+                                case 1:
+                                    try {
+                                        x();
+                                    } finally {
+                                        switch (i) {
+                                            case 1:
+                                                try {
+                                                    x();
+                                                } finally {
+                                                    switch (i) {
+                                                        case 1:
+                                                            try {
+                                                                x();
+                                                            } finally {
+                                                                switch (i) {
+                                                                    case 1:
+                                                                        try {
+                                                                            x();
+                                                                        } finally {
+                                                                            switch (i) {
+                                                                                case 1:
+                                                                                    try {
+                                                                                        x();
+                                                                                    } finally {
+                                                                                        switch (i) {
+                                                                                            case 1:
+                                                                                                try {
+                                                                                                    x();
+                                                                                                } finally {
+                                                                                                    switch (i) {
+                                                                                                        case 1:
+                                                                                                            try {
+                                                                                                                x();
+                                                                                                            } finally {
+                                                                                                                switch (i) {
+                                                                                                                    case 1:
+                                                                                                                        try {
+                                                                                                                            x();
+                                                                                                                        } finally {
+                                                                                                                            switch (i) {
+                                                                                                                                case 1:
+                                                                                                                            }
+                                                                                                                        }
+                                                                                                                }
+                                                                                                            }
+                                                                                                    }
+                                                                                                }
+                                                                                        }
+                                                                                    }
+                                                                            }
+                                                                        }
+                                                                }
+                                                            }
+                                                    }
+                                                }
+                                        }
+                                    }
+                            }
+                        }
+                }
+            }
+        }
+    }
+
+    private static void x() {
+    }
+}
diff --git a/src/test/java/org/apache/bcel/verifier/VerifierTestCase.java b/src/test/java/org/apache/bcel/verifier/VerifierTestCase.java
index 285283eb..874f885b 100644
--- a/src/test/java/org/apache/bcel/verifier/VerifierTestCase.java
+++ b/src/test/java/org/apache/bcel/verifier/VerifierTestCase.java
@@ -18,9 +18,22 @@
 package org.apache.bcel.verifier;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import java.io.File;
+import java.io.IOException;
+import java.net.URISyntaxException;
+import java.util.Enumeration;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
 
-import java.util.Collection;
-
+import org.apache.bcel.classfile.JavaClass;
+import org.apache.bcel.classfile.NestHost;
+import org.apache.bcel.classfile.Utility;
+import org.apache.bcel.verifier.exc.AssertionViolatedException;
+import org.apache.bcel.verifier.statics.StringRepresentation;
+import org.apache.commons.lang3.StringUtils;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Test;
 
@@ -31,10 +44,10 @@ public class VerifierTestCase {
         VerifierFactory.clear();
     }
 
-    @Test
-    public void testDefaultMethodValidation() {
-        final String className = Collection.class.getName();
-
+    private static void testDefaultMethodValidation(final String className, final String... excludes) throws ClassNotFoundException {
+        if (StringUtils.endsWithAny(className, excludes)) {
+            return;
+        }
         final Verifier verifier = VerifierFactory.getVerifier(className);
         VerificationResult result = verifier.doPass1();
 
@@ -43,5 +56,77 @@ public class VerifierTestCase {
         result = verifier.doPass2();
 
         assertEquals(VerificationResult.VERIFIED_OK, result.getStatus(), "Pass 2 verification of " + className + " failed: " + result.getMessage());
+
+        if (result == VerificationResult.VR_OK) {
+            final JavaClass jc = org.apache.bcel.Repository.lookupClass(className);
+            for (int i = 0; i < jc.getMethods().length; i++) {
+                result = verifier.doPass3a(i);
+                assertEquals(VerificationResult.VR_OK, result, "Pass 3a, method number " + i + " ['" + jc.getMethods()[i] + "']:\n" + result);
+                result = verifier.doPass3b(i);
+                assertEquals(VerificationResult.VR_OK, result, "Pass 3b, method number " + i + " ['" + jc.getMethods()[i] + "']:\n" + result);
+            }
+        }
+    }
+
+    private static File getJarFile(final Class<?> clazz) throws URISyntaxException {
+        return new File(clazz.getProtectionDomain().getCodeSource().getLocation().toURI());
+    }
+
+    private static void testJarFile(final File file, final String... excludes) throws IOException, ClassNotFoundException {
+        try (JarFile jarFile = new JarFile(file)) {
+            final Enumeration<JarEntry> entries = jarFile.entries();
+            while (entries.hasMoreElements()) {
+                final JarEntry jarEntry = entries.nextElement();
+                String entryName = jarEntry.getName();
+                if (entryName.endsWith(".class")) {
+                    entryName = entryName.replaceFirst("\\.class$", "");
+                    entryName = Utility.compactClassName(entryName, false);
+                    testDefaultMethodValidation(entryName, excludes);
+                }
+            }
+        }
+    }
+
+    private static void testNestHostWithJavaVersion(final String className) throws ClassNotFoundException {
+        final String version = System.getProperty("java.version");
+        assertNotNull(version);
+        try {
+            testDefaultMethodValidation(className);
+            assertTrue(version.startsWith("1."));
+        } catch (final AssertionViolatedException e) {
+            assertFalse(version.startsWith("1."));
+            final StringBuilder expectedMessage = new StringBuilder();
+            expectedMessage.append("INTERNAL ERROR: Please adapt '");
+            expectedMessage.append(StringRepresentation.class);
+            expectedMessage.append("' to deal with objects of class '");
+            expectedMessage.append(NestHost.class);
+            expectedMessage.append("'.");
+            assertEquals(expectedMessage.toString(), e.getCause().getMessage());
+        }
+    }
+
+    @Test
+    public void testCollection() throws ClassNotFoundException {
+        testDefaultMethodValidation("java.util.Collection");
+    }
+    
+    @Test
+    public void testArrayUtils() throws ClassNotFoundException {
+        testNestHostWithJavaVersion("org.apache.commons.lang.ArrayUtils");
+    }
+    
+    @Test
+    public void testDefinitionImpl() throws ClassNotFoundException {
+        testNestHostWithJavaVersion("com.ibm.wsdl.DefinitionImpl");
+    }
+    
+    @Test
+    public void testCommonsLang1() throws IOException, URISyntaxException, ClassNotFoundException {
+        testJarFile(getJarFile(org.apache.commons.lang.StringUtils.class), "ArrayUtils", "SerializationUtils");
+    }
+
+    @Test
+    public void testWSDL() throws IOException, URISyntaxException, ClassNotFoundException {
+        testJarFile(getJarFile(javax.wsdl.Port.class), "WSDLReaderImpl",  "DefinitionImpl");
     }
 }