LuciferYang opened a new pull request, #259:
URL: https://github.com/apache/commons-crypto/pull/259

   There are some issues with the reentrancy of 
`CryptoRandomFactory.getCryptoRandom(properties)` method.
   
   For the following test case:
   
   ```java
   @Test
   public void testReentrancyOfExceptionInInitializerErrorRandom() throws 
GeneralSecurityException, IOException {
       final Properties properties = new Properties();
       String classes = 
ExceptionInInitializerErrorRandom.class.getName().concat(",")
               .concat(CryptoRandomFactory.RandomProvider.JAVA.getClassName());
       properties.setProperty(CryptoRandomFactory.CLASSES_KEY, classes);
       try (final CryptoRandom random = 
CryptoRandomFactory.getCryptoRandom(properties)) {
           assertEquals(JavaCryptoRandom.class.getName(), 
random.getClass().getName());
       }
       try (final CryptoRandom random = 
CryptoRandomFactory.getCryptoRandom(properties)) {
           assertEquals(JavaCryptoRandom.class.getName(), 
random.getClass().getName());
       }
       try (final CryptoRandom random = 
CryptoRandomFactory.getCryptoRandom(properties)) {
           assertEquals(JavaCryptoRandom.class.getName(), 
random.getClass().getName());
       }
   } 
   ```
   The second call to `CryptoRandomFactory.getCryptoRandom(properties)` will 
result in the following error:
   
   ```java
   java.lang.NoClassDefFoundError: Could not initialize class 
org.apache.commons.crypto.random.ExceptionInInitializerErrorRandom
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:534)
        at java.base/java.lang.Class.forName(Class.java:513)
        at 
org.apache.commons.crypto.utils.ReflectionUtils.getClassByNameOrNull(ReflectionUtils.java:93)
        at 
org.apache.commons.crypto.utils.ReflectionUtils.getClassByName(ReflectionUtils.java:64)
        at 
org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:189)
        at 
org.apache.commons.crypto.random.CryptoRandomFactoryTest.testReentrancyOfExceptionInInitializerErrorRandom(CryptoRandomFactoryTest.java:99)
        at java.base/java.lang.reflect.Method.invoke(Method.java:580)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
        at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
   Caused by: java.lang.ExceptionInInitializerError: Exception 
java.lang.IllegalStateException: java.security.GeneralSecurityException: 
ExceptionInInitializerErrorRandom init failed [in thread "main"]
        at 
org.apache.commons.crypto.random.ExceptionInInitializerErrorRandom.<clinit>(ExceptionInInitializerErrorRandom.java:32)
        at java.base/java.lang.Class.forName0(Native Method)
        at java.base/java.lang.Class.forName(Class.java:534)
        at java.base/java.lang.Class.forName(Class.java:513)
        at 
org.apache.commons.crypto.utils.ReflectionUtils.getClassByNameOrNull(ReflectionUtils.java:93)
        at 
org.apache.commons.crypto.utils.ReflectionUtils.getClassByName(ReflectionUtils.java:64)
        at 
org.apache.commons.crypto.random.CryptoRandomFactory.getCryptoRandom(CryptoRandomFactory.java:189)
        at 
org.apache.commons.crypto.random.CryptoRandomFactoryTest.testReentrancyOfExceptionInInitializerErrorRandom(CryptoRandomFactoryTest.java:96)
        ... 3 more
   ```
   
   This is due to the `Class.forName` method. If the first initialization 
fails, subsequent identical calls will not attempt to initialize the class 
again, but will throw `NoClassDefFoundError`.
   
   So, this PR provides an additional layer of fault tolerance for 
ExceptionInInitializerError, which is wrapped by NoClassDefFoundError, and it 
also includes corresponding test cases to repeatedly test the 
`CryptoRandomFactory.getCryptoRandom(properties)` method.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to