Commits

Anonymous committed 309e456

Add test for X509CertificateFactory#generateCertificates

Change-Id: I461881d515201829eae08046574824dbda30d460

Comments (0)

Files changed (7)

luni/src/test/java/libcore/java/security/cert/X509CertificateTest.java

 
 import java.io.BufferedInputStream;
 import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
 import java.io.IOException;
 import java.security.PublicKey;
 import java.security.Security;
 import java.security.SignatureException;
+import java.security.cert.Certificate;
+import java.security.cert.CertificateException;
 import java.security.cert.CertificateExpiredException;
 import java.security.cert.CertificateFactory;
 import java.security.cert.CertificateNotYetValidException;
 
     private static final String CERT_UNSUPPORTED = "x509/cert-unsupported.der";
 
+    private static final String CERTS_X509_PEM = "x509/certs.pem";
+
+    private static final String CERTS_X509_DER = "x509/certs.der";
+
+    private static final String CERTS_PKCS7_PEM = "x509/certs-pk7.pem";
+
+    private static final String CERTS_PKCS7_DER = "x509/certs-pk7.der";
+
     private final X509Certificate getCertificate(CertificateFactory f, String name)
             throws Exception {
         final InputStream is = Support_Resources.getStream(name);
         }
     }
 
+    private final Collection<? extends X509Certificate> getCertificates(CertificateFactory f, String name)
+            throws Exception {
+        final InputStream is = Support_Resources.getStream(name);
+        assertNotNull("File does not exist: " + name, is);
+        try {
+            return (Collection<? extends X509Certificate>) f.generateCertificates(is);
+        } finally {
+            try {
+                is.close();
+            } catch (IOException ignored) {
+            }
+        }
+    }
+
     private PublicKey getRsaCertificatePublicKey() throws Exception {
         final InputStream ris = Support_Resources.getStream("x509/cert-rsa-pubkey.der");
         try {
                 hasUnsupportedCriticalExtension(f);
                 getEncoded(f);
                 verify(f);
+                generateCertificates_X509_PEM(f);
+                generateCertificates_X509_DER(f);
+                generateCertificates_PKCS7_PEM(f);
+                generateCertificates_PKCS7_DER(f);
+                generateCertificates_Empty(f);
             } catch (Throwable e) {
                 out.append("Error encountered checking " + p.getName() + "\n");
                 e.printStackTrace(out);
         assertEquals(Arrays.toString(cBytes), Arrays.toString(c.getEncoded()));
     }
 
+    private void generateCertificates_X509_DER(CertificateFactory f) throws Exception {
+        /* DER-encoded list of certificates */
+        Collection<? extends X509Certificate> certs = getCertificates(f, CERTS_X509_DER);
+        assertNotNull(certs);
+        assertEquals(2, certs.size());
+    }
+
+    private void generateCertificates_X509_PEM(CertificateFactory f) throws Exception {
+        /* PEM-encoded list of certificates */
+        Collection<? extends X509Certificate> certs = getCertificates(f, CERTS_X509_PEM);
+        assertNotNull(certs);
+        assertEquals(2, certs.size());
+    }
+
+    private void generateCertificates_PKCS7_PEM(CertificateFactory f) throws Exception {
+        /* PEM-encoded PKCS7 bag of certificates */
+        Collection<? extends X509Certificate> certs = getCertificates(f, CERTS_PKCS7_PEM);
+        assertNotNull(certs);
+        if ("BC".equals(f.getProvider().getName())) {
+            // Bouncycastle is broken
+            assertEquals(0, certs.size());
+        } else {
+            assertEquals(2, certs.size());
+        }
+    }
+
+    private void generateCertificates_PKCS7_DER(CertificateFactory f) throws Exception {
+        /* DER-encoded PKCS7 bag of certificates */
+        Collection<? extends X509Certificate> certs = getCertificates(f, CERTS_PKCS7_DER);
+        assertNotNull(certs);
+        assertEquals(2, certs.size());
+    }
+
+    private void generateCertificates_Empty(CertificateFactory f) throws Exception {
+        final InputStream is = new ByteArrayInputStream(new byte[0]);
+
+        final Collection<? extends Certificate> certs;
+        try {
+            certs = f.generateCertificates(is);
+            // DRLCertFactory is broken
+            if ("DRLCertFactory".equals(f.getProvider().getName())) {
+                fail("should throw when no certificates present");
+            }
+        } catch (CertificateException e) {
+            if ("DRLCertFactory".equals(f.getProvider().getName())) {
+                return;
+            }
+            throw e;
+        }
+        assertNotNull(certs);
+        assertEquals(0, certs.size());
+    }
+
     @Override
     protected void setUp() throws Exception {
         super.setUp();

luni/src/test/java/libcore/java/security/cert/certs.der

Empty file added.

support/src/test/java/tests/resources/x509/certs-pk7.der

Binary file added.

support/src/test/java/tests/resources/x509/certs-pk7.pem

+-----BEGIN PKCS7-----
+MIIIBg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ggEeAoGBAImYpx8uzo8B+Q3hxZN46+A80MRrbA18klMPdOVBoMInEe2w
+cxZatUOzqzxqb5V3Qd7lc9wYnTVy3BvDGbVxZ22J8GdtbJ046ZeoZkaIavohekr0
+87VGczxIjO5jxCcTRpFVjB5TPR8m6UlojxRo5y4K0yQ2ciV9lWxXpyBUAHyFAhUA
+7hFB9CwOX6cnV0Ehn6n9mZwsf4sCgYBcX2t572fhu3oWasXVbkA90GcN4RsNEHbL
+iFVk95r0LViFyrKvuYTCEt6J1SVBX+PZ+wU5h0SkI/Ya2RTo0KWkJBkFAcEy36e8
+lJCJMOlBQZwfWt2F1vTgnFPWpBLH2GuYwIB2qEBN9ehLvyPGIbozmSuQ5rE++jK1
+yUzcO9eg6AOBhAACgYAd7BJdvhiNsLKw8RcloBUYYB5jZeQYzOlR8xONopRWzcQX
+nmRug0Y/Zk6gCkGCnZoWYViB2x5TbkQY3l9zT6WT8sfRVoidUUUyNRpgXG4pydSj
+XOCEqD1WS/XMx9VsYsjrkq/6zBxDzcputrYdkS45Yiua+ruyyNwXaghPyKQssqMa
+MBgwCQYDVR0TBAIwADALBgNVHQ8EBAMCADcwCQYHKoZIzjgEAwMvADAsAhRG6gnv
+MAvWYlUjXdBOXVIqUbIelQIUB7T9ah0mDUbOmjJvsDqX2ioJsqmhADEA
+-----END PKCS7-----

support/src/test/java/tests/resources/x509/certs.der

Binary file added.

support/src/test/java/tests/resources/x509/certs.pem

+-----BEGIN CERTIFICATE-----
+MIIEvDCCBCWgAwIBAgIJALlhQ+HW8x5vMA0GCSqGSIb3DQEBBQUAMGAxCzAJBgNV
+BAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRIwEAYDVQQHEwlTYW4gTWF0ZW8x
+FzAVBgNVBAoTDkdlbml1cy5jb20gSW5jMQ8wDQYDVQQLEwZOZXRPcHMwHhcNMTMw
+MTAyMDAwMzEyWhcNMjIxMjMxMDAwMzEyWjBgMQswCQYDVQQGEwJVUzETMBEGA1UE
+C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f49n7BTmHRKQj9hrZFOjQpaQkGQUBwTLfp7yUkIkw6UFBnB9a3YXW
+9OCcU9akEsfYa5jAgHaoQE316Eu/I8YhujOZK5DmsT76MrXJTNw716DoA4GEAAKB
+gB3sEl2+GI2wsrDxFyWgFRhgHmNl5BjM6VHzE42ilFbNxBeeZG6DRj9mTqAKQYKd
+mhZhWIHbHlNuRBjeX3NPpZPyx9FWiJ1RRTI1GmBcbinJ1KNc4ISoPVZL9czH1Wxi
+yOuSr/rMHEPNym62th2RLjliK5r6u7LI3BdqCE/IpCyyoxowGDAJBgNVHRMEAjAA
+MAsGA1UdDwQEAwIANzAJBgcqhkjOOAQDAy8AMCwCFEbqCe8wC9ZiVSNd0E5dUipR
+sh6VAhQHtP1qHSYNRs6aMm+wOpfaKgmyqQ==
+-----END CERTIFICATE-----

support/src/test/java/tests/resources/x509/create.sh

 rm -f privkey.pem
 rm -f dsapriv.pem
 rm -f ecpriv.pem
+
+rm -f certs.pem
+
+cat cert-rsa.der cert-dsa.der > certs.der
+openssl x509 -inform d -in cert-rsa.der > certs.pem
+openssl x509 -inform d -in cert-dsa.der >> certs.pem
+
+openssl crl2pkcs7 -certfile certs.pem -nocrl > certs-pk7.pem
+openssl crl2pkcs7 -certfile certs.pem -nocrl -outform d > certs-pk7.der