tiebin-zhang / powermock

Automatically exported from code.google.com/p/powermock
Apache License 2.0
0 stars 0 forks source link

ClassFormatError #353

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
I got ClassFormatError in my test using Powermock 1.4.10. The problem class is 
javax.security.auth.x500.X500Principal. I simplified the test code as:

@PrepareForTest(EncryptUsage.class)
@RunWith(PowerMockRunner.class)
public class TestEncryptedFileUtil {

    @Test
    public void constructorTest(){
        try{
            EncryptUsage efu = new EncryptUsage();
            assertTrue("EncryptedFileUtil instance not null", efu != null);
        }catch(Throwable t){
            t.printStackTrace();
            fail("Got error: " + t.getMessage());
        }
    }
}

public class EncryptUsage{

    public EncryptUsage() throws Exception{
        byte[] myCipher = "theCipher".getBytes();
        DESKeySpec sks = new DESKeySpec(myCipher);
        SecretKeyFactory skf = SecretKeyFactory.getInstance("DES");
        skf.generateSecret(sks);
    }

}

The error is:
java.lang.ClassFormatError
    at java.lang.ClassLoader.defineClass(ClassLoader.java:222)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:158)
    at org.powermock.core.classloader.MockClassLoader.loadUnmockedClass(MockClassLoader.java)
    at org.powermock.core.classloader.MockClassLoader.loadModifiedClass(MockClassLoader.java:152)
    at org.powermock.core.classloader.DeferSupportingClassLoader.loadClass(DeferSupportingClassLoader.java:65)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:561)
    at com.ibm.security.validator.SimpleValidator.engineValidate(Unknown Source)
    at com.ibm.security.validator.Validator.validate(Unknown Source)
    at com.ibm.security.validator.Validator.validate(Unknown Source)
    at javax.crypto.a.a(Unknown Source)
    at javax.crypto.a.a(Unknown Source)
    at javax.crypto.a.a(Unknown Source)
    at javax.crypto.a.a(Unknown Source)
    at javax.crypto.b.b(Unknown Source)
    at javax.crypto.b.a(Unknown Source)
    at javax.crypto.b.b(Unknown Source)
    at javax.crypto.b.a(Unknown Source)
    at javax.crypto.SecretKeyFactory.getInstance(Unknown Source)
    at powerMockTest.EncryptUsage.<init>(EncryptUsage.java:11)
    at powerMockTest.TestEncryptedFileUtil.constructorTest(TestEncryptedFileUtil.java:18)

What's wrong? Looking forward to your reply. Thank you in advance.

Original issue reported on code.google.com by xinweic...@gmail.com on 28 Oct 2011 at 9:54

GoogleCodeExporter commented 9 years ago
Try to use @PowerMockIgnore("javax.security.auth.*")

Original comment by johan.ha...@gmail.com on 31 Oct 2011 at 7:18

GoogleCodeExporter commented 9 years ago
Thank you for the quick replying. But after adding @PowerMockIgnore, I got:
JUnit version 4.8.2
.java.security.NoSuchAlgorithmException: DES SecretKeyFactory not available
        at javax.crypto.SecretKeyFactory.<init>(DashoA13*..)
        at javax.crypto.SecretKeyFactory.getInstance(DashoA13*..)
        at powerMockTest.EncryptUsage.<init>(EncryptUsage.java:11)
        at powerMockTest.TestEncryptedFileUtil.constructorTest(TestEncryptedFileUtil.java:20)
...

Original comment by xinweic...@gmail.com on 31 Oct 2011 at 11:21

GoogleCodeExporter commented 9 years ago
since I do test the encryption part, the defer loading of security package 
doesn't help here.

Knowing nothing about what's inside powermock, I still tried to debug its code 
until reach the calling to javaassit's (3.15-GA) CtClass.toBytecode for the 
class javax.security.auth.x500.X500Principal. The following 
ClassLoader.defineClass failed with ClassFormatError, and actually before that, 
calling CtClass.toClass failed with the same error. The byte[] are correct, and 
the separate test of javaassit's CtClass has no problem.

Then I stopped. Johan, please help take a look. Thank you very much.

Original comment by xinweic...@gmail.com on 3 Nov 2011 at 5:29

GoogleCodeExporter commented 9 years ago
I appreciate your help in trying to figure things out, I know it's not always 
easy. Unfortunately I don't have the time needed to investigate such complex 
things. I'm maintaining PowerMock totally in my spare time and it takes a great 
amount time as it is. Sorry :/

Original comment by johan.ha...@gmail.com on 3 Nov 2011 at 7:59

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I had the same issue with PomerMockRunner and the same error:
XYZ SecretKeyFactory not available

adding

@PowerMockIgnore( {..... , "javax.crypto.*" })

solved the issue for me, and the SecretKeyFactory.getInstance() starting 
working for me. 

Original comment by com...@gmail.com on 26 Sep 2012 at 9:50

GoogleCodeExporter commented 9 years ago
Thanks for sharing

Original comment by johan.ha...@gmail.com on 27 Sep 2012 at 6:08