FileDocCategorySizeDatePackage
X509KeyManagerTest.javaAPI DocAndroid 1.5 API51202Wed May 06 22:41:06 BST 2009tests.api.javax.net.ssl

X509KeyManagerTest

public class X509KeyManagerTest extends TestCase
Tests for X509KeyManager class constructors and methods.

Fields Summary
private X509KeyManager
manager
private KeyManagerFactory
factory
private String
keyType
private String
client
private String
server
private String
type
private KeyStore
keyTest
private X509Certificate[]
cert
private PrivateKey[]
keys
private String
password
String
certificate
ByteArrayInputStream
certArray
byte[]
keyBytes
String
certificate2
ByteArrayInputStream
certArray2
byte[]
key2Bytes
String
certificate3
ByteArrayInputStream
certArray3
byte[]
key3Bytes
Constructors Summary
Methods Summary
private booleancompareC(java.lang.String[] ar)

        if (ar.length != 3) {
            return false;
        }
        for (int i = 0; i < ar.length; i++) {
            if (!ar[i].toLowerCase().equals("clientkey_01") && !ar[i].toLowerCase().equals("clientkey_02") && !ar[i].toLowerCase().equals("clientkey_03")) {
                return false;
            }
        }
        return true;
    
voidinit(java.lang.String name)

      keyType = name;
      try {
          CertificateFactory cf = CertificateFactory.getInstance("X.509");
          KeyFactory kf = KeyFactory.getInstance("RSA");
          keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
          keyTest.load(null, "1234".toCharArray());
          if (keyType.equals(client)) {
              keys = new PrivateKey[3];
              keys[0] = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
              keys[1] = kf.generatePrivate(new PKCS8EncodedKeySpec(key2Bytes));
              keys[2] = kf.generatePrivate(new PKCS8EncodedKeySpec(key3Bytes));
              cert = new X509Certificate[3];
              cert[0] = (X509Certificate) cf.generateCertificate(certArray);
              cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
              cert[2] = (X509Certificate) cf.generateCertificate(certArray3);
              keyTest.setKeyEntry("clientKey_01", keys[0], password.toCharArray(), new X509Certificate[] {cert[0]});
              keyTest.setKeyEntry("clientKey_02", keys[1], password.toCharArray(), new X509Certificate[] {cert[0], cert[1]});
              keyTest.setKeyEntry("clientKey_03", keys[2], password.toCharArray(), new X509Certificate[] {cert[0], cert[2]});
              keyTest.setCertificateEntry("clientAlias_01", cert[0]);
              keyTest.setCertificateEntry("clientAlias_02", cert[0]);
              keyTest.setCertificateEntry("clientAlias_03", cert[1]);
          } else if (keyType.equals(server)) {
              keys = new PrivateKey[1];
              keys[0] = kf.generatePrivate(new PKCS8EncodedKeySpec(keyBytes));
              cert = new X509Certificate[1];
              cert[0] = (X509Certificate) cf.generateCertificate(certArray3);
              keyTest.setKeyEntry("serverKey_00", keys[0], password.toCharArray(), new X509Certificate[] {cert[0]});
              keyTest.setCertificateEntry("serverAlias_00", cert[0]);
          }
      } catch (Exception ex) {
          ex.printStackTrace();
          throw new IllegalArgumentException(ex.getMessage());
      }
      try {
        factory.init(keyTest, "1234".toCharArray());
      } catch (Exception e) {
        fail("Could't init the KeyManagerFactory");
      }
      manager = (X509KeyManager) factory.getKeyManagers()[0];
    
protected voidsetUp()

    
    /*
     * The same key in PEM format.
     * The DER version of this key was created using
     * 
     * openssl pkcs8 -topk8 -nocrypt -in key1.pem
     *         -inform PEM -out key1.der -outform DER
     * 
     * -----BEGIN RSA PRIVATE KEY-----
     * Proc-Type: 4,ENCRYPTED
     * DEK-Info: DES-EDE3-CBC,0EE6B33EC2D92297
     * 
     * r7lbWwtlmubgMG020XiOStqgrvPkP1hTrbOV7Gh2IVNTyXWyA8UriQlPyqBQNzy2
     * 5+Z+JUqzYoLCGY0fQ95ck+ya/wHJQX4OSKFOZwQKpU7pEY9wN1YPa7U9ZnyCPGtB
     * +ejvHuIMJhE5wq9Y1iEDIlON++onWTf4T36Sz3OQ8gEJbnx3x+UjcCINooj7kOeM
     * giCi5yJEOJaf4fkRioUh6S7cm/msTH3ID33rrvTjk7cD8mGzzTy4hWyKaK4K9GbC
     * dOvSORM9mVwTWMUdu1wJ5uyadwBhpSIhC/qpP8Je60nFy8YJlzB2FaMUpAuIOM7B
     * EVN2uAMDNOpGzcOJPbLig8smk2lA4+y1T3gFd9paskSjD9B8+/3KuagWEEQQL7T4
     * YK3xtjzXwEp6OdG2QjD4ZcK5D0MKuYPF3PszwzlCnBG/On6wIvIiTPWBn/G2u59D
     * gJPV7V3Jipn0iYYN+i7T5TNoT7Vko8s3BRpVSrlFUFFhtQPad6NcxGNNH5L1g3fF
     * +dp4TnG64PCQZtuu6I6gfuMXztOwQtEpxxHo9WktlCpwL0tT/tpx+zOVbLvgusjB
     * QKYCIplbSI7VtpOfcJ3kTTAWSOGZli4FayB/Dplf/FXN6ZwwASw09ioVQc/CFdLk
     * Xw05elxV8/AFvm+/VkUHK5JJSp32WMgAJA+XrUsOb5lw1Tl3Hlj9KHALp+Pt/i7N
     * +LPnxrpuTry31APt8aRup/pWOLa+f97Hz+arp4wJa5LK+GtTTtoI4+QZp5qzR/jy
     * oM+DoKtK+1WsCU7teJwEWXV/ayo1TEFEhcY0F7IAPCzDlG3XOFmulQ==
     * -----END RSA PRIVATE KEY-----
     */

    
       
        String defAlg = KeyManagerFactory.getDefaultAlgorithm();
        try {
            factory = KeyManagerFactory.getInstance(defAlg);
        } catch (NoSuchAlgorithmException e) {
            fail("could not get default KeyManagerFactory");
        }
    
public voidtest_chooseClientAlias()

tests
X509KeyManager#chooseClientAlias(String[] keyType, Principal[] issuers, Socket socket)

        String[] ar = {client};
        init(client);
        assertNull(manager.chooseClientAlias(null, null, new Socket()));
        assertNull(manager.chooseClientAlias(new String[0], null, new Socket()));
        assertNull(manager.chooseClientAlias(ar, null, new Socket()));
        String res = manager.chooseClientAlias(new String[]{type}, null, null);
        assertNotNull(res);
        assertEquals("clientkey_03", res.toLowerCase().toLowerCase());
    
public voidtest_chooseServerAlias()

tests
X509KeyManager#chooseServerAlias(String keyType, Principal[] issuers, Socket socket)

        init(server);
        assertNull(manager.chooseServerAlias(null, null, new Socket()));
        assertNull(manager.chooseServerAlias("", null, new Socket()));
        String res = manager.chooseServerAlias(type, null, null);
        assertNotNull(res);
        assertEquals("serverkey_00", res.toLowerCase());
        res = manager.chooseServerAlias(type, null, new Socket());
        assertNotNull(res);
        assertEquals("serverkey_00", res.toLowerCase());
    
public voidtest_getCertificateChain()

tests
X509KeyManager#getCertificateChain(String alias)

        init(server);
        assertNull("Not NULL for NULL parameter", manager.getCertificateChain(null));
        assertNull("Not NULL for empty parameter",manager.getCertificateChain(""));
        assertNull("Not NULL for clientAlias_01 parameter", manager.getCertificateChain("clientAlias_01"));
        assertNull("Not NULL for serverAlias_00 parameter", manager.getCertificateChain("serverAlias_00"));
    
public voidtest_getClientAliases()

tests
X509KeyManager#getClientAliases(String keyType, Principal[] issuers)

        init(client);
        assertNull(manager.getClientAliases(null, null));
        assertNull(manager.getClientAliases("", null));
        String[] resArray = manager.getClientAliases(type, null);
        assertNotNull(resArray);
        assertTrue("Incorrect result", compareC(resArray));
    
public voidtest_getPrivateKey()

tests
X509KeyManager#getPrivateKey(String alias)

        init(client);
        assertNull("Not NULL for NULL parameter", manager.getPrivateKey(null));
        assertNull("Not NULL for serverAlias_00 parameter", manager.getPrivateKey("serverAlias_00"));
        assertNull("Not NULL for clientAlias_02 parameter", manager.getPrivateKey("clientAlias_02"));
    
public voidtest_getServerAliases()

tests
X509KeyManager#getServerAliases(String keyType, Principal[] issuers)

        init(server);
        assertNull(manager.getServerAliases(null, null));
        assertNull(manager.getServerAliases("", null));
        String[] resArray = manager.getServerAliases(type, null);
        assertNotNull(resArray);
        assertEquals("Incorrect length", 1, resArray.length);
        assertEquals("Incorrect aliase", "serverkey_00", resArray[0].toLowerCase());