/*
 * Copyright 2002-2004 The Apache Software Foundation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * imitations under the License.
 */

package test;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.MessageDigest;
import java.util.Arrays;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * Unit tests to test the various Digests.
 * 
 * The class generates digestes with the openSSL functions and compares these
 * results with digests generated by the BouncyCastle implementation.
 * 
 * Created on 01.10.2005
 * 
 * @author Werner Dittmann (werner@apache.org)
 * 
 */

public class OpenSSLTestDigests extends TestCase {

    MessageDigest mdJuice = null;

    MessageDigest mdBC = null;

    byte[] plainData = null;

    byte[] hashJuice = null;

    byte[] hashBC = null;

    public OpenSSLTestDigests(String name) {
        super(name);
        try {
            Class<?> c = Class
                    .forName("org.apache.security.juice.provider.JuiCEProviderOpenSSL");
            java.security.Security.addProvider((java.security.Provider) c
                    .newInstance());
            c = Class
                    .forName("org.bouncycastle.jce.provider.BouncyCastleProvider");
            java.security.Security.addProvider((java.security.Provider) c
                    .newInstance());
        } catch (Throwable t) {
        }
    }

    public void setUp() {

        plainData = new byte[1023];
        for (int i = 0; i < plainData.length; i++) {
            plainData[i] = (byte) (i + 1);
        }
    }

    private void setDigests(String cipherName) {

        try {
            /*
             * initialize ciphers: use the openSSL implementation and the
             * standard BC implementation (as reference)
             */
            mdJuice = MessageDigest.getInstance(cipherName, "JuiCE");
            assertNotNull("Message digest not found: " + cipherName, mdJuice);
            mdBC = MessageDigest.getInstance(cipherName, "BC");
            /*
             * Check if cipher providers are correct, i.e the correct provider
             * was chosen.
             */
            assertEquals("Wrong class of openSSL provider", mdJuice
                    .getProvider().getClass().getName(),
                    "org.apache.security.juice.provider.JuiCEProviderOpenSSL");

            assertEquals("Wrong class of BC provider", mdBC.getProvider()
                    .getClass().getName(),
                    "org.bouncycastle.jce.provider.BouncyCastleProvider");

        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage());
        } catch (NoSuchProviderException e) {
            throw new RuntimeException(e.getMessage());
        }
    }

    void digest() {
        try {
            hashJuice = mdJuice.digest(plainData);
            hashBC = mdBC.digest(plainData);
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    void performTest() {
        digest();
        assertTrue("hash data mismatch", Arrays.equals(hashJuice, hashBC));
    }

    /**
     * Test public encryption using openSSL, private decryption using BC.
     * 
     */
    public void testSHA1() {
        setDigests("SHA1");
        performTest();
    }

    public void testSHA256() {
        try {
            setDigests("SHA256");
        } catch (RuntimeException e) {
            System.out.println(e.getMessage() + ", skipping test");
            return;
        }
        performTest();
    }

    public void testSHA512() {
        try {
            setDigests("SHA512");
        } catch (RuntimeException e) {
            System.out.println(e.getMessage() + ", skipping test");
            return;
        }
        performTest();
    }

    public void testMD2() {
        setDigests("MD2");
        performTest();
    }

    public void testMD4() {
        setDigests("MD4");
        performTest();
    }

    public void testMD5() {
        setDigests("MD5");
        performTest();
    }

    public void testRIPEMD160() {
        setDigests("RIPEMD160");
        performTest();
    }

    /**
     * JUnit suite <p/>
     * 
     * @return a junit test suite
     */
    public static Test suite() {
        return new TestSuite(OpenSSLTestDigests.class);
    }

    /**
     * @param args
     */
    public static void main(String[] args) {
        junit.textui.TestRunner.run(suite());
    }
}
