/*
 * 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.SecureRandom;

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


/**
 * Unit tests to test the Secure Random method.
 * 
 * Created on 15.02.2005
 * 
 * @author Werner Dittmann (werner@apache.org)
 * 
 */

public class OpenSSLTestSecureRandom extends TestCase {

    static {
        try {
            Class<?> c = Class
                    .forName("org.apache.security.juice.provider.JuiCEProviderOpenSSL");
            java.security.Security.addProvider((java.security.Provider) c
                    .newInstance());

        } catch (Throwable t) {
        }
    }
    
    public OpenSSLTestSecureRandom(String name) {
        super(name);
    }


    public void testSecureRandom() {
        SecureRandom rand = null;
        try {
            rand = SecureRandom.getInstance("SHA1PRNG", "JuiCE");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            fail(e.getMessage());            
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
            fail(e.getMessage());            
        }
        byte[] randBytes = new byte[32];
        rand.nextBytes(randBytes);
    }

    public void testSecureRandomSeed() {
        SecureRandom rand = null;
        try {
            rand = SecureRandom.getInstance("SHA1PRNG", "JuiCE");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            fail(e.getMessage());            
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
            fail(e.getMessage());            
        }
        byte[] randBytes = new byte[32];
        rand.nextBytes(randBytes);
        rand.setSeed(randBytes);
        rand.nextBytes(randBytes);
    }

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

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

}
