FileDocCategorySizeDatePackage
KeyFactoryTest.javaAPI DocAndroid 1.5 API20950Wed May 06 22:41:06 BST 2009org.apache.harmony.security.tests.java.security

KeyFactoryTest

public class KeyFactoryTest extends TestCase

Fields Summary
Provider
provider
boolean
exceptionThrown
Provider
existingProvider
private static final String
TEST_PROVIDER_NAME
private static final String
TEST_KEYFACTORY_NAME
Constructors Summary
Methods Summary
private voidcheckException(java.lang.String message, java.lang.Exception thrown, java.lang.Class expected)

        if (thrown == null) {
            if (!exceptionThrown) {
                fail(message + ", expected " + expected.getName());
            }
        } else if (expected == thrown.getClass()) {
            exceptionThrown = true;
            // ok
        } else {
            exceptionThrown = true;
            fail(message + ", unexpected exception: " + thrown + ", expected: " + expected.getName());
        }
    
protected voidsetUp()

        super.setUp();
        exceptionThrown = false;
        
        Provider[] providers = Security.getProviders();
        if (providers.length == 0) {
            fail("no providers found");
        }
        
        existingProvider = providers[0];
        
        provider = new TestKeyFactoryProvider();
        Security.addProvider(provider);
    
protected voidtearDown()

        super.tearDown();
        Security.removeProvider(provider.getName());
    
public voidtestGeneratePrivate()

        KeyFactory factory = null;
        try {
            factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        }
        
        assertNotNull(factory);
        
        try {
            TestPrivateKey key = new TestPrivateKey();
            TestPrivateKeySpec keySpec = new TestPrivateKeySpec(key);
            PrivateKey privateKey = factory.generatePrivate(keySpec);
            assertNotNull(privateKey);
            assertTrue(Arrays.equals(key.getEncoded(), privateKey.getEncoded()));
        } catch (InvalidKeySpecException e) {
            fail("unexpected exception: " + e);
        }
        
        KeySpec[] keySpecs = {
                new TestPublicKeySpec(new TestPublicKey()),
                null,
                new DSAPublicKeySpec(null, null, null, null)
        };
        
        Class[] exceptions = {
                InvalidKeySpecException.class,
                NullPointerException.class,
                InvalidKeySpecException.class
        };
        
        for (int i = 0; i < keySpecs.length; i++) {
            KeySpec keySpec = keySpecs[i];
            exceptionThrown = false;
            String message = "generatePrivate(" +
                (keySpec == null ? "null" : keySpec.toString()) + ")";
            try {
                factory.generatePrivate(keySpec);
            } catch (Exception e) {
                checkException(message, e, exceptions[i]);
            } finally {
                checkException(message, null, exceptions[i]);
            }
        }
    
public voidtestGeneratePublic()

        KeyFactory factory = null;
        try {
            factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        }
        
        assertNotNull(factory);
        
        try {
            TestPublicKey key = new TestPublicKey();
            TestPublicKeySpec keySpec = new TestPublicKeySpec(key);
            PublicKey publicKey = factory.generatePublic(keySpec);
            assertNotNull(publicKey);
            assertTrue(Arrays.equals(key.encoded, publicKey.getEncoded()));
        } catch (InvalidKeySpecException e) {
            fail("unexpected exception: " + e);
        }
        
        KeySpec[] keySpecs = {
                new TestPrivateKeySpec(new TestPrivateKey()),
                null,
                new DSAPublicKeySpec(null, null, null, null)
        };
        
        Class[] exceptions = {
                InvalidKeySpecException.class,
                NullPointerException.class,
                InvalidKeySpecException.class
        };
        
        for (int i = 0; i < keySpecs.length; i++) {
            KeySpec keySpec = keySpecs[i];
            String message = "generatePublic(" +
                (keySpec == null ? "null" : keySpec.toString()) + ")";
            
            try {
                PublicKey generatePublic = factory.generatePublic(keySpec);
                assertNotNull(generatePublic);
            } catch (Exception e) {
                checkException(message, e, exceptions[i]);
            } finally {
                checkException(message, null, exceptions[i]);
            }
        }
    
public voidtestGetInstanceString()

        try {
            KeyFactory factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
            assertNotNull(factory);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        }
        
        String[] parameters = {
                "UnknownKeyFactory",
                null
        };
        
        Class[] exceptions = {
                NoSuchAlgorithmException.class,
                NullPointerException.class
        };
        
        for (int i = 0; i < parameters.length; i++) {
            String algorithm = parameters[i];
            exceptionThrown = false;
            String message = "getInstance(" + (algorithm == null ? "null" : "\"" + algorithm + "\"") + ")";
            try {
                KeyFactory.getInstance(algorithm);
            } catch (Exception e) {
                checkException(message, e, exceptions[i]);
            } finally {
                checkException(message, null, exceptions[i]);
            }
        }
        
    
public voidtestGetInstanceStringProvider()

        try {
            KeyFactory factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME, provider);
            assertNotNull(factory);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        }
        
        String[] algorithms = {
                "UnknownKeyFactory",
                null,
                TEST_KEYFACTORY_NAME,
                TEST_KEYFACTORY_NAME
        };
        
        Provider[] providers = {
                provider,
                provider,
                existingProvider,
                null
        };
        
        Class[] exceptions = {
                NoSuchAlgorithmException.class,
                NullPointerException.class,
                NoSuchAlgorithmException.class,
                IllegalArgumentException.class
        };
        
        for (int i = 0; i < algorithms.length; i++) {
            String algorithm = algorithms[i];
            Provider provider = providers[i];
            String message = "getInstance(" +
                (algorithm == null ? "null" : "\"" + algorithm + "\"") +
                ", " +
                (provider == null ? "null" : "provider");
            exceptionThrown = false;
            try {
                KeyFactory.getInstance(algorithm, provider);
            } catch (Exception e) {
                checkException(message, e, exceptions[i]);
            } finally {
                checkException(message, null, exceptions[i]);
            }
            
        }
    
public voidtestGetInstanceStringString()

        try {
            KeyFactory factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME, TEST_PROVIDER_NAME);
            assertNotNull(factory);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        } catch (NoSuchProviderException e) {
            fail("unexpected exception: " + e);
        }
        
        String[][] combinations = {
                { "UnknownKeyFactory", TEST_PROVIDER_NAME},
                { TEST_KEYFACTORY_NAME, "UnknownProvider"},
                { TEST_KEYFACTORY_NAME, existingProvider.getName() },
                { null, TEST_PROVIDER_NAME },
                { TEST_KEYFACTORY_NAME, null },
                { null, null}
        };
        
        Class[] exceptions = {
                NoSuchAlgorithmException.class,
                NoSuchProviderException.class,
                NoSuchAlgorithmException.class,
                NullPointerException.class,
                IllegalArgumentException.class,
                IllegalArgumentException.class
        };
        
        for (int i = 0; i < combinations.length; i++) {
            String[] combination = combinations[i];
            String message = "getInstance(\"" + combination[0] + "\", \"" + combination[1] + "\")";
            exceptionThrown = false;
            try {
                KeyFactory.getInstance(combination[0], combination[1]);
            } catch (Exception e) {
                checkException(message, e, exceptions[i]);
            } finally {
                checkException(message, null, exceptions[i]);
            }
        }
    
public voidtestGetKeySpec()

        KeyFactory factory = null;
        try {
            factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        }
        
        assertNotNull(factory);
        
        {
            Key[] keys = {
                    new TestPrivateKey(),
                    new TestPublicKey(),
                    new TestPrivateKey(new byte[] { 42, 41, 40 }),
                    new TestPublicKey(new byte[] { 40, 41, 42 })
            };
            
            Class[] keySpecs = {
                    TestPrivateKeySpec.class,
                    TestPublicKeySpec.class,
                    TestPrivateKeySpec.class,
                    TestPublicKeySpec.class,
            };
            
            for (int i = 0; i < keys.length; i++) {
                Key key = keys[i];
                Class keySpec = keySpecs[i];
                String message = "getKeySpec(" + key.toString() + ", " + keySpec.toString() + ")";
                try {
                    KeySpec spec = factory.getKeySpec(key, keySpec);
                    assertNotNull(spec);
                    assertTrue(spec.getClass() == keySpec);
                } catch (InvalidKeySpecException e) {
                    fail("unexpected exception: " + e);
                }
            }
        }
        
        {
            Key[] keys = {
                    new AnotherKey(),
                    null,
                    new TestPrivateKey(),
                    null,
            };
            
            Class[] keySpecs = {
                    KeySpec.class,
                    TestPrivateKeySpec.class,
                    null,
                    null,
            };
            
            Class[] exceptions = {
                    InvalidKeySpecException.class,
                    NullPointerException.class,
                    InvalidKeySpecException.class,
                    NullPointerException.class
            };
            
            for (int i = 0; i < keys.length; i++) {
                Key key = keys[i];
                Class keySpec = keySpecs[i];
                exceptionThrown = false;
                String message = "getKeySpec(" +
                    (key == null ? "null" : key.toString()) +
                    ", " +
                    (keySpec == null ? "null" : keySpec.toString()) + ")";
                try {
                    factory.getKeySpec(key, keySpec);
                } catch (Exception e) {
                    checkException(message, e, exceptions[i]);
                } finally {
                    checkException(message, null, exceptions[i]);
                }
                
            }
        }
    
public voidtestTranslateKey()

        KeyFactory factory = null;
        try {
            factory = KeyFactory.getInstance(TEST_KEYFACTORY_NAME);
        } catch (NoSuchAlgorithmException e) {
            fail("unexpected exception: " + e);
        }
        
        assertNotNull(factory);
        
        {
            Key[] keys = {
                    new TestPrivateKey(),
                    new TestPublicKey()
            };
            
            Class[] translated = {
                    TestPublicKey.class,
                    TestPrivateKey.class
            };
            
            for (int i = 0; i < keys.length; i++) {
                Key key = keys[i];
                Class translate = translated[i];
                try {
                    Key translateKey = factory.translateKey(key);
                    assertNotNull(translateKey);
                    assertEquals(translate, translateKey.getClass());
                } catch (InvalidKeyException e) {
                    fail("unexpected exception: " + e);
                }
            }
        }
        
        {
            Key[] keys = {
                    new AnotherKey(),
                    null
            };
            
            Class[] exceptions = {
                    InvalidKeyException.class,
                    NullPointerException.class
            };
            
            for (int i = 0; i < keys.length; i++) {
                Key key = keys[i];
                String message = "translateKey(" +
                    (key == null ? "null" : key.toString()) + ")";
                exceptionThrown = false;
                try {
                    factory.translateKey(key);
                } catch (Exception e) {
                    checkException(message, e, exceptions[i]);
                } finally {
                    checkException(message, null, exceptions[i]);
                }
            }
        }