FileDocCategorySizeDatePackage
KeyPairGeneratorSpecTest.javaAPI DocAndroid 5.1 API6313Thu Mar 12 22:22:30 GMT 2015android.security

KeyPairGeneratorSpecTest

public class KeyPairGeneratorSpecTest extends android.test.AndroidTestCase

Fields Summary
private static final String
TEST_ALIAS_1
private static final X500Principal
TEST_DN_1
private static final long
NOW_MILLIS
private static final BigInteger
SERIAL_1
private static final Date
NOW
private static final Date
NOW_PLUS_10_YEARS
Constructors Summary
Methods Summary
public voidtestBuilder_Success()

        KeyPairGeneratorSpec spec = new KeyPairGeneratorSpec.Builder(getContext())
                .setAlias(TEST_ALIAS_1)
                .setKeyType("RSA")
                .setKeySize(1024)
                .setSubject(TEST_DN_1)
                .setSerialNumber(SERIAL_1)
                .setStartDate(NOW)
                .setEndDate(NOW_PLUS_10_YEARS)
                .setEncryptionRequired()
                .build();

        assertEquals("Context should be the one specified", getContext(), spec.getContext());

        assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias());

        assertEquals("Key algorithm should be the one specified", "RSA", spec.getKeyType());

        assertEquals("Key size should be the one specified", 1024, spec.getKeySize());

        assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN());

        assertEquals("startDate should be the one specified", NOW, spec.getStartDate());

        assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate());

        assertEquals("encryption flag should be on", KeyStore.FLAG_ENCRYPTED, spec.getFlags());
    
public voidtestConstructor_EndBeforeStart_Failure()

        try {
            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1,
                    NOW_PLUS_10_YEARS, NOW, 0);
            fail("Should throw IllegalArgumentException when end is before start");
        } catch (IllegalArgumentException success) {
        }
    
public voidtestConstructor_NullContext_Failure()

        try {
            new KeyPairGeneratorSpec(null, TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1, NOW,
                    NOW_PLUS_10_YEARS, 0);
            fail("Should throw IllegalArgumentException when context is null");
        } catch (IllegalArgumentException success) {
        }
    
public voidtestConstructor_NullEndDate_Failure()

        try {
            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1,
                    NOW, null, 0);
            fail("Should throw IllegalArgumentException when keystoreAlias is null");
        } catch (IllegalArgumentException success) {
        }
    
public voidtestConstructor_NullKeystoreAlias_Failure()

        try {
            new KeyPairGeneratorSpec(getContext(), null, "RSA", 1024, null, TEST_DN_1, SERIAL_1, NOW,
                    NOW_PLUS_10_YEARS, 0);
            fail("Should throw IllegalArgumentException when keystoreAlias is null");
        } catch (IllegalArgumentException success) {
        }
    
public voidtestConstructor_NullSerial_Failure()

        try {
            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, null, NOW,
                    NOW_PLUS_10_YEARS, 0);
            fail("Should throw IllegalArgumentException when startDate is null");
        } catch (IllegalArgumentException success) {
        }
    
public voidtestConstructor_NullStartDate_Failure()

        try {
            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1, SERIAL_1,
                    null, NOW_PLUS_10_YEARS, 0);
            fail("Should throw IllegalArgumentException when startDate is null");
        } catch (IllegalArgumentException success) {
        }
    
public voidtestConstructor_NullSubjectDN_Failure()

        try {
            new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, null, SERIAL_1, NOW,
                    NOW_PLUS_10_YEARS, 0);
            fail("Should throw IllegalArgumentException when subjectDN is null");
        } catch (IllegalArgumentException success) {
        }
    
public voidtestConstructor_Success()


         
        KeyPairGeneratorSpec spec =
                new KeyPairGeneratorSpec(getContext(), TEST_ALIAS_1, "RSA", 1024, null, TEST_DN_1,
                        SERIAL_1, NOW, NOW_PLUS_10_YEARS, 0);

        assertEquals("Context should be the one specified", getContext(), spec.getContext());

        assertEquals("Alias should be the one specified", TEST_ALIAS_1, spec.getKeystoreAlias());

        assertEquals("Key algorithm should be the one specified", "RSA", spec.getKeyType());

        assertEquals("Key size should be the one specified", 1024, spec.getKeySize());

        assertEquals("subjectDN should be the one specified", TEST_DN_1, spec.getSubjectDN());

        assertEquals("startDate should be the one specified", NOW, spec.getStartDate());

        assertEquals("endDate should be the one specified", NOW_PLUS_10_YEARS, spec.getEndDate());