证书工具

This commit is contained in:
cheney
2022-10-13 10:21:45 +08:00
parent ee395aebee
commit f36b8c2ca3
8 changed files with 240 additions and 3 deletions
@@ -0,0 +1,73 @@
package com.sunyard.sydapi.test;
import com.sunyard.util.CertUtil;
import org.junit.Assert;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.security.NoSuchProviderException;
import java.security.cert.CertificateException;
public class TestCertUtil {
private static String readCert(String cert){
InputStream is = TestCertUtil.class.getResourceAsStream(cert);
byte[] data = new byte[0];
try {
data = new byte[is.available()];
is.read( data );
} catch (IOException e) {
e.printStackTrace();
}
return new String( data );
}
@Test
public void case1() throws CertificateException, NoSuchProviderException {
Assert.assertTrue(
CertUtil.isSM2Cert(readCert("/x509/c1.crt"))
);
}
@Test
public void case2() throws CertificateException, NoSuchProviderException {
Assert.assertTrue(
CertUtil.isSM2Cert(readCert("/x509/c2.crt"))
);
}
@Test
public void case3() throws CertificateException, NoSuchProviderException {
// 此证书是错误的
try {
CertUtil.isSM2Cert(readCert("/x509/c3.crt"));
} catch ( CertificateException e ) {
e.printStackTrace();
}
}
@Test
public void case4() throws CertificateException, NoSuchProviderException {
Assert.assertTrue(
CertUtil.isSM2Cert(readCert("/x509/c4.crt"))
);
}
@Test
public void case5() throws CertificateException, NoSuchProviderException {
Assert.assertFalse(
CertUtil.isSM2Cert(readCert("/x509/c5.crt"))
);
}
@Test
public void case6() throws CertificateException, NoSuchProviderException {
Assert.assertTrue(
CertUtil.isSM2Cert(readCert("/x509/c6.crt"))
);
}
}