📄 certpathtest.java
字号:
} public void performTest() throws Exception { CertificateFactory cf = CertificateFactory.getInstance("X.509", "BC"); X509Certificate rootCert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(rootCertBin)); X509Certificate interCert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(interCertBin)); X509Certificate finalCert = (X509Certificate)cf.generateCertificate(new ByteArrayInputStream(finalCertBin)); //Testing CertPath generation from List List list = new ArrayList(); list.add(interCert); CertPath certPath1 = cf.generateCertPath(list); //Testing CertPath encoding as PkiPath byte[] encoded = certPath1.getEncoded("PkiPath"); //Testing CertPath generation from InputStream ByteArrayInputStream inStream = new ByteArrayInputStream(encoded); CertPath certPath2 = cf.generateCertPath(inStream, "PkiPath"); //Comparing both CertPathes if (!certPath2.equals(certPath1)) { fail("CertPath differ after encoding and decoding."); } encoded = certPath1.getEncoded("PKCS7"); //Testing CertPath generation from InputStream inStream = new ByteArrayInputStream(encoded); certPath2 = cf.generateCertPath(inStream, "PKCS7"); //Comparing both CertPathes if (!certPath2.equals(certPath1)) { fail("CertPath differ after encoding and decoding."); } encoded = certPath1.getEncoded("PEM"); //Testing CertPath generation from InputStream inStream = new ByteArrayInputStream(encoded); certPath2 = cf.generateCertPath(inStream, "PEM"); //Comparing both CertPathes if (!certPath2.equals(certPath1)) { fail("CertPath differ after encoding and decoding."); } // // empty list test // list = new ArrayList(); CertPath certPath = CertificateFactory.getInstance("X.509","BC").generateCertPath(list); if (certPath.getCertificates().size() != 0) { fail("list wrong size."); } // // exception tests // testExceptions(); } public String getName() { return "CertPath"; } public static void main( String[] args) { Security.addProvider(new BouncyCastleProvider()); runTest(new CertPathTest()); } private static class MyCertificate extends Certificate { private final byte[] encoding; public MyCertificate(String type, byte[] encoding) { super(type); // don't copy to allow null parameter in test this.encoding = encoding; } public byte[] getEncoded() throws CertificateEncodingException { // do copy to force NPE in test return (byte[])encoding.clone(); } public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { } public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { } public String toString() { return "[My test Certificate, type: " + getType() + "]"; } public PublicKey getPublicKey() { return new PublicKey() { public String getAlgorithm() { return "TEST"; } public byte[] getEncoded() { return new byte[] { (byte)1, (byte)2, (byte)3 }; } public String getFormat() { return "TEST_FORMAT"; } }; } } private static class MyCertPath extends CertPath { private final Vector certificates; private final Vector encodingNames; private final byte[] encoding; public MyCertPath(byte[] encoding) { super("MyEncoding"); this.encoding = encoding; certificates = new Vector(); certificates.add(new MyCertificate("MyEncoding", encoding)); encodingNames = new Vector(); encodingNames.add("MyEncoding"); } public List getCertificates() { return Collections.unmodifiableList(certificates); } public byte[] getEncoded() throws CertificateEncodingException { return (byte[])encoding.clone(); } public byte[] getEncoded(String encoding) throws CertificateEncodingException { if (getType().equals(encoding)) { return (byte[])this.encoding.clone(); } throw new CertificateEncodingException("Encoding not supported: " + encoding); } public Iterator getEncodings() { return Collections.unmodifiableCollection(encodingNames).iterator(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -