FileDocCategorySizeDatePackage
DSAKeyPairGeneratorTest.javaAPI DocAndroid 1.5 API5377Wed May 06 22:41:06 BST 2009tests.security.interfaces

DSAKeyPairGeneratorTest

public class DSAKeyPairGeneratorTest extends junit.framework.TestCase

Fields Summary
private final BigInteger
p
private final BigInteger
q
private final BigInteger
g
Constructors Summary
Methods Summary
public voidtest_DSAKeyPairGenerator01()

tests
java.security.interfaces.DSAKeyPairGenerator #initialize(DSAParams params, SecureRandom random)

        DSAParams dsaParams = new DSAParameterSpec(p, q, g);
        SecureRandom random = null;
        MyDSA dsa = new MyDSA(dsaParams);
        try {
            random = SecureRandom.getInstance("SHA1PRNG");
        } catch (Exception e) {
            fail("Unexpected exception for SecureRandom: " + e);
        }
        
        try {
            dsa.initialize(dsaParams, random);
        } catch (Exception e) {
            fail("Unexpected exception: " + e);
        }
        
        try {
            dsa.initialize(dsaParams, null);
            fail("InvalidParameterException was not thrown");
        } catch (InvalidParameterException ipe) {
            //expected
        } catch (Exception e) {
            fail(e + " was thrown instead of InvalidParameterException");
        }
        try {
            dsa.initialize(null, random);
            fail("InvalidParameterException was not thrown");
        } catch (InvalidParameterException ipe) {
            //expected
        } catch (Exception e) {
            fail(e + " was thrown instead of InvalidParameterException");
        }
    
public voidtest_DSAKeyPairGenerator02()

tests
java.security.interfaces.DSAKeyPairGenerator #initialize(int modlen, boolean genParams, SecureRandom randomm)

        int[] invalidLen = {-1, 0, 511, 513, 650, 1023, 1025};
        DSAParams dsaParams = new DSAParameterSpec(p, q, g);
        SecureRandom random = null;
        MyDSA dsa = new MyDSA(null);
        try {
            random = SecureRandom.getInstance("SHA1PRNG");
        } catch (Exception e) {
            fail("Unexpected exception for SecureRandom: " + e);
        }
        
        //exception case
        try {
            dsa.initialize(520, false, random);
            fail("InvalidParameterException was not thrown");
        } catch (InvalidParameterException ipe) {
            String str = ipe.getMessage();
            if (!str.equals("there are not precomputed parameters")) {
                fail("Incorrect exception's message: " + str);
            }
        } catch (Exception e) {
            fail(e + " was thrown instead of InvalidParameterException");
        }
        
        //exception case
        for (int i = 0; i < invalidLen.length; i++) {
            try {
                dsa.initialize(invalidLen[i], true, random);
                fail("InvalidParameterException was not thrown");
            } catch (InvalidParameterException ipe) {
                String str = ipe.getMessage();
                if (!str.equals("Incorrect modlen")) {
                    fail("Incorrect exception's message: " + str);
                }
            } catch (Exception e) {
                fail(e + " was thrown instead of InvalidParameterException");
            }            
        }
        
        //positive case
        dsa = new MyDSA(dsaParams);
        try {
            dsa.initialize(520, true, random);
        } catch (Exception e) {
            fail(e + " was thrown for subcase 1");
        }
        
        //positive case
        try {
            dsa.initialize(520, false, random);
        } catch (Exception e) {
            fail(e + " was thrown for subcase 1");
        }