package org.jscep.client.verification;

import static org.junit.Assert.assertTrue;

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.MessageDigest;
import java.security.Security;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.security.auth.x500.X500Principal;

import org.jscep.util.X509Certificates;
import org.junit.experimental.theories.DataPoints;
import org.junit.experimental.theories.Theories;
import org.junit.experimental.theories.Theory;
import org.junit.runner.RunWith;

@RunWith(Theories.class)
public class MessageDigestCertificateVerifierTest {
    @DataPoints
    public static MessageDigest[] getMessageDigests() throws Exception {
        List<MessageDigest> digests = new ArrayList<MessageDigest>();

        Set<String> algorithms = Security.getAlgorithms("MessageDigest");
        for (String algorithm : algorithms) {
            digests.add(MessageDigest.getInstance(algorithm));
        }

        return digests.toArray(new MessageDigest[0]);
    }

    @Theory
    public void testVerify(MessageDigest digest) throws Exception {
        X500Principal subject = new X500Principal("CN=example");
        KeyPair keyPair = KeyPairGenerator.getInstance("RSA").generateKeyPair();
        X509Certificate cert = X509Certificates.createEphemeral(subject,
                keyPair);

        byte[] expected = digest.digest(cert.getEncoded());

        CertificateVerifier verifier = new MessageDigestCertificateVerifier(
                digest, expected);
        assertTrue(verifier.verify(cert));

        // For backwards compatibility, make sure that the MessageDigestCertificateVerifier
        // works with the fingerprint over TBSCertificate.
        byte[] tbsExpected = digest.digest(cert.getTBSCertificate());

        CertificateVerifier tbsVerifier = new MessageDigestCertificateVerifier(
                digest, tbsExpected);
        assertTrue(tbsVerifier.verify(cert));
    }

}

@@ -1629,29 +1629,31 @@
 ify(cert));%0A
-%0A
     
+%7D%0A%0A
     // For b
@@ -1728,20 +1728,16 @@
 erifier%0A
-    
     // w
@@ -1791,23 +1791,360 @@
     
-    byte%5B%5D tbsE
+@Theory%0A    public void testVerifyTbsCertificate(MessageDigest digest) throws Exception %7B%0A        X500Principal subject = new X500Principal(%22CN=example%22);%0A        KeyPair keyPair = KeyPairGenerator.getInstance(%22RSA%22).generateKeyPair();%0A        X509Certificate cert = X509Certificates.createEphemeral(subject,%0A                keyPair);%0A%0A        byte%5B%5D e
 xpec
@@ -2219,20 +2219,17 @@
 erifier 
-tbsV
+v
 erifier 
@@ -2296,12 +2296,9 @@
 st, 
-tbsE
+e
 xpec
@@ -2326,12 +2326,9 @@
 rue(
-tbsV
+v
 erif
@@ -2352,11 +2352,10 @@
 ;%0A    %7D%0A
-%0A
 %7D%0A

split the tests