KeyStoreTest.java revision 46c6fad9fad8f3dbbc82516232a225f37d332ca7
1e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom/*
2e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * Copyright (C) 2010 The Android Open Source Project
3e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom *
4e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * Licensed under the Apache License, Version 2.0 (the "License");
5e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * you may not use this file except in compliance with the License.
6e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * You may obtain a copy of the License at
7e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom *
8e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom *      http://www.apache.org/licenses/LICENSE-2.0
9e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom *
10e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * Unless required by applicable law or agreed to in writing, software
11e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * distributed under the License is distributed on an "AS IS" BASIS,
12e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * See the License for the specific language governing permissions and
14e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom * limitations under the License.
15e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom */
16e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
174557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonpackage libcore.java.security;
18e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
19e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.ByteArrayInputStream;
20e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.ByteArrayOutputStream;
21e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.File;
22e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.FileInputStream;
23e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.io.FileOutputStream;
2428192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilsonimport java.io.InputStream;
2557f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstromimport java.io.OutputStream;
264557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Key;
274557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.KeyStore;
28e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.Builder;
29e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.Entry;
30e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.LoadStoreParameter;
31e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.PasswordProtection;
32e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.PrivateKeyEntry;
33e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.ProtectionParameter;
34e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.SecretKeyEntry;
35e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.TrustedCertificateEntry;
364557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.KeyStoreException;
374557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.NoSuchAlgorithmException;
384557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Provider;
394557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Security;
404557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.UnrecoverableKeyException;
41e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.cert.Certificate;
42e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.cert.X509Certificate;
43e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.ArrayList;
44e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Arrays;
45e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Collections;
46e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Date;
47e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.HashSet;
48e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.List;
49e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Set;
50e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.KeyGenerator;
51e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.SecretKey;
52e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport junit.framework.TestCase;
53e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
54e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrompublic class KeyStoreTest extends TestCase {
55e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
566a75005c0547634e5179829c61eb03209197cedaJesse Wilson    private static final PrivateKeyEntry PRIVATE_KEY;
576a75005c0547634e5179829c61eb03209197cedaJesse Wilson    private static final PrivateKeyEntry PRIVATE_KEY_2;
586a75005c0547634e5179829c61eb03209197cedaJesse Wilson    static {
596a75005c0547634e5179829c61eb03209197cedaJesse Wilson        try {
606a75005c0547634e5179829c61eb03209197cedaJesse Wilson            PRIVATE_KEY = TestKeyStore.getServer().getPrivateKey("RSA", "RSA");
616a75005c0547634e5179829c61eb03209197cedaJesse Wilson            PRIVATE_KEY_2 = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA");
626a75005c0547634e5179829c61eb03209197cedaJesse Wilson        } catch (Exception e) {
636a75005c0547634e5179829c61eb03209197cedaJesse Wilson            throw new RuntimeException(e);
646a75005c0547634e5179829c61eb03209197cedaJesse Wilson        }
656a75005c0547634e5179829c61eb03209197cedaJesse Wilson    }
666a75005c0547634e5179829c61eb03209197cedaJesse Wilson
67e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final SecretKey SECRET_KEY = generateSecretKey();
68e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final SecretKey SECRET_KEY_2 = generateSecretKey();
69e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
70e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static SecretKey generateSecretKey() {
71e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
72e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyGenerator kg = KeyGenerator.getInstance("DES");
73e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            return kg.generateKey();
74e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NoSuchAlgorithmException e) {
75e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throw new RuntimeException(e);
76e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
77e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
78e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
79e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_PRIVATE = "private";
80e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_CERTIFICATE = "certificate";
81e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_SECRET = "secret";
82e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
83e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_PRIVATE = "pRiVaTe";
84e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_CERTIFICATE = "cErTiFiCaTe";
85e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_SECRET = "sEcRet";
86e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
87e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_NO_PASSWORD_PRIVATE = "private-no-password";
88e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_NO_PASSWORD_SECRET = "secret-no-password";
89e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
90e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_STORE = "store password".toCharArray();
91e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_KEY = "key password".toCharArray();
92e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_BAD = "dummy".toCharArray();
93e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
94e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_STORE = new PasswordProtection(PASSWORD_STORE);
95e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_KEY = new PasswordProtection(PASSWORD_KEY);
96e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_BAD = new PasswordProtection(PASSWORD_BAD);
97e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
98a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom    public static List<KeyStore> keyStores() throws Exception {
99e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        List<KeyStore> keyStores = new ArrayList<KeyStore>();
100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider[] providers = Security.getProviders();
101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (Provider provider : providers) {
102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<Provider.Service> services = provider.getServices();
103e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            for (Provider.Service service : services) {
104e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String type = service.getType();
105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!type.equals("KeyStore")) {
106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    continue;
107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String algorithm = service.getAlgorithm();
109e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                KeyStore ks = KeyStore.getInstance(algorithm, provider);
110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(provider, ks.getProvider());
111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(algorithm, ks.getType());
112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!isUnsupported(ks)) {
113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStores.add(ks);
114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
115e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
116e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
117e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return keyStores;
118e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isSecretKeyEnabled(KeyStore ks) {
121e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // JKS key stores cannot store secret keys, neither can the RI's PKCS12
122e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("JKS")
123e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("CaseExactJKS")
124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || (ks.getType().equals("PKCS12"))));
125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isCertificateEnabled(KeyStore ks) {
128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // RI can't handle certificate in PKCS12, but BC can
129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("SunJSSE")));
130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isCaseSensitive(KeyStore ks) {
133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getType().equals("CaseExactJKS")
134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                || ks.getType().equals("BKS")
135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                || ks.getType().equals("BouncyCastle"));
136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isUnsupported(KeyStore ks) {
140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // Don't bother testing BC on RI
141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (StandardNames.IS_RI && ks.getProvider().getName().equals("BC"));
142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isNullPasswordAllowed(KeyStore ks) {
145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("JKS")
146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("CaseExactJKS")
147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("JCEKS")
148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("PKCS12")));
149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isKeyPasswordIgnored(KeyStore ks) {
152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // BouncyCastle's PKCS12 ignores the key password unlike the RI which requires it
153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC"));
154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
15646c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom    private static boolean isLoadStoreParameterSupported(KeyStore ks) {
15746c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom        // BouncyCastle's PKCS12 allows a JDKPKCS12StoreParameter
15846c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom        return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC"));
15946c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom    }
16046c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom
161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isSetKeyByteArrayUnimplemented(KeyStore ks) {
162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // All of BouncyCastle's
163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // KeyStore.setKeyEntry(String,byte[],char[]) implementations
164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // throw RuntimeException
165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getProvider().getName().equals("BC"));
166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void populate(KeyStore ks) throws Exception {
169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.load(null, null);
170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKey(ks);
171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isNullPasswordAllowed(ks)) {
172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ks.setKeyEntry(ALIAS_NO_PASSWORD_PRIVATE,
173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                           PRIVATE_KEY.getPrivateKey(),
174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                           null,
175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                           PRIVATE_KEY.getCertificateChain());
176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isCertificateEnabled(ks)) {
178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ks.setCertificateEntry(ALIAS_CERTIFICATE,
179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                   PRIVATE_KEY.getCertificate());
180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isSecretKeyEnabled(ks)) {
182e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setSecretKey(ks);
183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(ks)) {
184e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                ks.setKeyEntry(ALIAS_NO_PASSWORD_SECRET,
185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               SECRET_KEY,
186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               null,
187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               null);
188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks) throws Exception {
193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKey(ks, ALIAS_PRIVATE);
194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks, String alias) throws Exception {
196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKey(ks, alias, PRIVATE_KEY);
197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks,
199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     String alias,
200e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PrivateKeyEntry privateKey)
201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getPrivateKey(),
204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       PASSWORD_KEY,
205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getCertificateChain());
206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks) throws Exception {
209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKeyBytes(ks, ALIAS_PRIVATE);
210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks, String alias) throws Exception {
212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKeyBytes(ks, alias, PRIVATE_KEY);
213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks,
215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     String alias,
216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PrivateKeyEntry privateKey)
217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getPrivateKey().getEncoded(),
220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getCertificateChain());
221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks) throws Exception {
224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKey(ks, ALIAS_SECRET);
225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks, String alias) throws Exception {
227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKey(ks, alias, SECRET_KEY);
228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks, String alias, SecretKey key) throws Exception {
230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       key,
232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       PASSWORD_KEY,
233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       null);
234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks) throws Exception {
237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKeyBytes(ks, ALIAS_SECRET);
238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks, String alias) throws Exception {
240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKeyBytes(ks, alias, SECRET_KEY);
241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks, String alias, SecretKey key)
243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       key.getEncoded(),
246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       null);
247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks) throws Exception {
250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setCertificate(ks, ALIAS_CERTIFICATE);
251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks, String alias) throws Exception {
253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setCertificate(ks, alias, PRIVATE_KEY.getCertificate());
254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks, String alias, Certificate certificate)
256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setCertificateEntry(alias, certificate);
258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
260e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey(Key actual)
261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getPrivateKey(), actual);
263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey2(Key actual)
265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY_2.getPrivateKey(), actual);
267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey(Entry actual)
269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(PrivateKeyEntry.class, actual.getClass());
271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        PrivateKeyEntry privateKey = (PrivateKeyEntry) actual;
272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getPrivateKey(), privateKey.getPrivateKey());
273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getCertificate(), privateKey.getCertificate());
274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(Arrays.asList(PRIVATE_KEY.getCertificateChain()),
275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     Arrays.asList(privateKey.getCertificateChain()));
276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey(Key actual)
279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(SECRET_KEY, actual);
281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey2(Key actual)
283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(SECRET_KEY_2, actual);
285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey(Entry actual)
287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(SecretKeyEntry.class, actual.getClass());
289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(SECRET_KEY, ((SecretKeyEntry) actual).getSecretKey());
290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate(Certificate actual)
293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getCertificate(), actual);
295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate2(Certificate actual)
297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY_2.getCertificate(), actual);
299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate(Entry actual)
301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(TrustedCertificateEntry.class, actual.getClass());
303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(PRIVATE_KEY.getCertificate(),
304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     ((TrustedCertificateEntry) actual).getTrustedCertificate());
305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificateChain(Certificate[] actual)
308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(Arrays.asList(PRIVATE_KEY.getCertificateChain()),
310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     Arrays.asList(actual));
311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_create() throws Exception {
314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider[] providers = Security.getProviders();
315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (Provider provider : providers) {
316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<Provider.Service> services = provider.getServices();
317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            for (Provider.Service service : services) {
318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String type = service.getType();
319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!type.equals("KeyStore")) {
320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    continue;
321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String algorithm = service.getAlgorithm();
323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                KeyStore ks = KeyStore.getInstance(algorithm, provider);
324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(provider, ks.getProvider());
325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(algorithm, ks.getType());
326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getInstance() throws Exception {
331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null);
334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NullPointerException expected) {
336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type));
339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String providerName = StandardNames.SECURITY_PROVIDER_NAME;
341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, (String)null);
343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, providerName);
348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (Exception e) {
350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (e.getClass() != NullPointerException.class
351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                && e.getClass() != KeyStoreException.class) {
352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                throw e;
353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(type, (String)null);
357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type, providerName));
361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider provider = Security.getProvider(providerName);
363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, (Provider)null);
365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, provider);
370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NullPointerException expected) {
372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(type, (Provider)null);
375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            fail();
376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type, provider));
379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getDefaultType() throws Exception {
382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(type);
384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(type);
385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks);
386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(type, ks.getType());
387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getProvider() throws Exception {
390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks.getProvider());
392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(StandardNames.SECURITY_PROVIDER_NAME, ks.getProvider().getName());
393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNotNull(keyStore.getProvider());
396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getType() throws Exception {
400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(type);
402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks.getType());
403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(type, ks.getType());
404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNotNull(keyStore.getType());
407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getKey() throws Exception {
411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, null);
414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
421e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, null);
425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, PASSWORD_KEY);
434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey("", null));
443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey("", PASSWORD_KEY));
444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
465e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isKeyPasswordIgnored(keyStore)) {
466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_PRIVATE, null);
470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_SECRET, null);
481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with bad passwords
491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isKeyPasswordIgnored(keyStore)) {
492e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_PRIVATE, PASSWORD_BAD);
496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
497e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
498e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD);
503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
504e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
509e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificateChain() throws Exception {
511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateChain(null);
514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
518e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateChain(null);
524e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateChain(""));
532e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
534e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE));
539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificateChain(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE));
541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificate() throws Exception {
546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
547e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
548e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificate(null);
549e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
551e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
552e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
554e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificate(null);
559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
560e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
561e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
562e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
563e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
564e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificate(""));
567e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
568e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
569e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
571e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
573e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
574e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
575e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
577e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
578e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCreationDate() throws Exception {
587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
589a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                keyStore.getCreationDate(null);
590a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                fail();
591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        long before = System.currentTimeMillis();
595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // add 1000 since some key stores round of time to nearest second
597e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            long after = System.currentTimeMillis() + 1000;
598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
601e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCreationDate(null);
603e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
606e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCreationDate(""));
607e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
608e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
609e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Date date = keyStore.getCreationDate(ALIAS_CERTIFICATE);
611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNotNull(date);
612e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertTrue(before <= date.getTime());
613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertTrue(date.getTime() <= after);
614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCreationDate(ALIAS_CERTIFICATE));
616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE));
621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
623e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    Date date = keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE);
624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(before <= date.getTime());
625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(date.getTime() <= after);
626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setKeyEntry_Key() throws Exception {
632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null, null);
635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null, null);
646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, PASSWORD_KEY, null);
655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
661e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(ALIAS_PRIVATE,
664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PRIVATE_KEY.getPrivateKey(),
665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PASSWORD_KEY,
666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     null);
667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (IllegalArgumentException expected) {
669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
671e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setPrivateKey(keyStore);
678e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setSecretKey(keyStore);
683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
685e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, PASSWORD_KEY, null);
687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != KeyStoreException.class
690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
699e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2);
705e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    setSecretKey(keyStore, ALIAS_ALT_CASE_SECRET, SECRET_KEY_2);
712e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
713e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
718e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2);
719e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
722e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    setSecretKey(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2);
725e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
729e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
731e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
736e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(ALIAS_PRIVATE,
737e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PRIVATE_KEY.getPrivateKey(),
738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     null,
739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PRIVATE_KEY.getCertificateChain());
740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_PRIVATE,
744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                         PRIVATE_KEY.getPrivateKey(),
745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                         null,
746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                         PRIVATE_KEY.getCertificateChain());
747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class
751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != KeyStoreException.class) {
752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
756e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
757e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
758e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, null, null);
759e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, null));
760e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
761e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    try {
762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, null, null);
763e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        fail();
764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    } catch (Exception e) {
765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        if (e.getClass() != UnrecoverableKeyException.class
766e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            && e.getClass() != IllegalArgumentException.class
767e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            && e.getClass() != KeyStoreException.class) {
768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            throw e;
769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        }
770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
772e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
774e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
775e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setKeyEntry_array() throws Exception {
777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
778e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null);
780e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
782e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
784e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
788e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
790e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null);
791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class
796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != RuntimeException.class) {
797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
800e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
801e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
802e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
803e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isNullPasswordAllowed(keyStore)) {
804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // TODO Use EncryptedPrivateKeyInfo to protect keys if
805e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // password is required.
806e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
807e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
808e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSetKeyByteArrayUnimplemented(keyStore)) {
809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
810e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
811e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
812e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
813e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
815e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
816e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setPrivateKeyBytes(keyStore);
817e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
818e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
819e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
820e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
821e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setSecretKeyBytes(keyStore);
822e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
823e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY.getEncoded(), null);
826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
827e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
828e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
832e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
833e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isNullPasswordAllowed(keyStore)) {
834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // TODO Use EncryptedPrivateKeyInfo to protect keys if
835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // password is required.
836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSetKeyByteArrayUnimplemented(keyStore)) {
839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
844e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
845e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
846e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
848e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2);
849e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
850e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
851e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
852e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
853e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
854e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
855e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2);
856e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
857e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
858e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
859e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
860e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
861e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
862e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2);
863e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
864e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
865e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
866e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
867e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
868e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
869e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, SECRET_KEY_2);
870e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
871e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
872e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
875e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
876e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
877e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setCertificateEntry() throws Exception {
878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
879e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
880e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setCertificateEntry(null, null);
881e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
882e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
883e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
884e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
885e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
886e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
887e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
888e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
889e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
890e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setCertificateEntry(null, null);
891e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
892e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
893e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
894e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
895e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
896e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
897e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
898e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
899e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // Sort of delete by setting null.  Note that even though
900e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // certificiate is null, size doesn't change,
901e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // isCertificateEntry returns true, and it is still listed in aliases.
902e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
903e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
904e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
905e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    int size = keyStore.size();
906e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
907e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
908e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertEquals(size, keyStore.size());
909e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
910e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(Collections.list(keyStore.aliases()).contains(ALIAS_CERTIFICATE));
911e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (NullPointerException expectedSometimes) {
912e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertEquals("PKCS12", keyStore.getType());
913e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertEquals("BC", keyStore.getProvider().getName());
914e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
915e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
917e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
918a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                    fail();
919e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
920e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
921e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
922e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
923e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
924e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
925e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isCertificateEnabled(keyStore)) {
926e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
927e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
928e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
929e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
930e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
931e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
932e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
933e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setCertificate(keyStore);
934e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
935e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
937e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
938e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isCertificateEnabled(keyStore)) {
939e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
943e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
944e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
945e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
947e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
948e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setCertificate(keyStore,
949e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               ALIAS_ALT_CASE_CERTIFICATE,
950e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               PRIVATE_KEY_2.getCertificate());
951e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
952e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
954e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
955e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
956e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setCertificate(keyStore,
957e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               ALIAS_ALT_CASE_CERTIFICATE,
958e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               PRIVATE_KEY_2.getCertificate());
959e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
960e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
961e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
962e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
963e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
964e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_deleteEntry() throws Exception {
965e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
966e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
967e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(null);
968e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
969e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
970e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
971e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
972e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
973e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
974e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
975e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
976e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
977e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
978e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(null);
979e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
981e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
982e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
983e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
985e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.deleteEntry("");
987e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
988e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
989e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
990e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
991e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.deleteEntry(ALIAS_PRIVATE);
996e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
997e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_SECRET);
1001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1002e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1003e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_SECRET);
1004e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1005e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1006e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1007e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1008e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_CERTIFICATE);
1009e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1010e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1011e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_CERTIFICATE);
1012e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1013e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1014e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1015e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1016e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1017e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1018e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1019e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
1020e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1021e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_ALT_CASE_PRIVATE);
1022e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1023e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1024e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1025e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1026e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_ALT_CASE_SECRET);
1027e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1028e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_SECRET);
1030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1031e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1032e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1033e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_ALT_CASE_CERTIFICATE);
1035e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_CERTIFICATE);
1038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1039e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1041e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_aliases() throws Exception {
1044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1045e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1046e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.aliases();
1047e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1048e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1049e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1050e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1051e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1052e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1053e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1054e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(Collections.EMPTY_SET, new HashSet(Collections.list(keyStore.aliases())));
1055e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1056e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1057e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1058e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1059e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<String> expected = new HashSet<String>();
1060e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            expected.add(ALIAS_PRIVATE);
1061e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1062e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_NO_PASSWORD_PRIVATE);
1063e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1064e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1065e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_SECRET);
1066e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore)) {
1067e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    expected.add(ALIAS_NO_PASSWORD_SECRET);
1068e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1069e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1070e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1071e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_CERTIFICATE);
1072e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1073e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected, new HashSet<String>(Collections.list(keyStore.aliases())));
1074e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1075e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1076e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1077e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_containsAlias() throws Exception {
1078e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1079e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1080e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.containsAlias(null);
1081e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1082e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1083e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1084e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1085e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1086e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1087e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1088e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1089e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1090e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.containsAlias(null);
1091e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1092e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1093e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1094e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1095e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.containsAlias(""));
1096e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1097e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1098e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1099e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.containsAlias(""));
1101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(keyStore.containsAlias(ALIAS_PRIVATE));
1103e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore), keyStore.containsAlias(ALIAS_SECRET));
1104e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore), keyStore.containsAlias(ALIAS_CERTIFICATE));
1105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_PRIVATE));
1108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1109e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_SECRET));
1110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
1111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_CERTIFICATE));
1112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1115e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_size() throws Exception {
1116e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1117e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1118e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.aliases();
1119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1121e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1122e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1123e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(0, keyStore.size());
1127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            int expected = 1;
1132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore)) {
1138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    expected++;
1139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected, keyStore.size());
1145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_isKeyEntry() throws Exception {
1149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isKeyEntry(null);
1152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isKeyEntry(null);
1162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(""));
1167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(""));
1172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(keyStore.isKeyEntry(ALIAS_PRIVATE));
1174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore), keyStore.isKeyEntry(ALIAS_SECRET));
1175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(ALIAS_CERTIFICATE));
1176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isKeyEntry(ALIAS_ALT_CASE_PRIVATE));
1179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isKeyEntry(ALIAS_ALT_CASE_SECRET));
1181e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(ALIAS_ALT_CASE_CERTIFICATE));
1182e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1184e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_isCertificateEntry() throws Exception {
1186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isCertificateEntry(null);
1189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.isCertificateEntry(null);
1200e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (NullPointerException expected) {
1202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertFalse(keyStore.isCertificateEntry(null));
1205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(""));
1208e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(""));
1213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_PRIVATE));
1215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_SECRET));
1216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore),
1217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
1218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_PRIVATE));
1220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_SECRET));
1221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
1222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE));
1223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificateAlias() throws Exception {
1227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateAlias(null);
1230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateAlias(null));
1238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<String> expected = new HashSet<String>();
1243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            expected.add(ALIAS_PRIVATE);
1244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_NO_PASSWORD_PRIVATE);
1246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_CERTIFICATE);
1249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            String actual = keyStore.getCertificateAlias(PRIVATE_KEY.getCertificate());
1251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(expected.contains(actual));
1252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateAlias(PRIVATE_KEY_2.getCertificate()));
1253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(File expected, char[] storePassword, KeyStore actual)
1257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(actual.getType(), actual.getProvider());
125957f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        InputStream is = new FileInputStream(expected);
126057f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        ks.load(is, storePassword);
126157f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        is.close();
1262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEqualsKeyStores(ks, actual);
1263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(KeyStore expected,
1266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      ByteArrayOutputStream actual, char[] storePassword)
1267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(expected.getType(), expected.getProvider());
1269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.load(new ByteArrayInputStream(actual.toByteArray()), storePassword);
1270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEqualsKeyStores(expected, ks);
1271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1273e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(KeyStore expected, KeyStore actual)
1274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(expected.size(), actual.size());
1276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (String alias : Collections.list(actual.aliases())) {
1277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (alias.equals(ALIAS_NO_PASSWORD_PRIVATE)
1278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    || alias.equals(ALIAS_NO_PASSWORD_SECRET)) {
1279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(expected.getKey(alias, null),
1280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                             actual.getKey(alias, null));
1281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(expected.getKey(alias, PASSWORD_KEY),
1283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                             actual.getKey(alias, PASSWORD_KEY));
1284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected.getCertificate(alias), actual.getCertificate(alias));
1286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_store_OutputStream() throws Exception {
1290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null, null);
1293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
1301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(out, null);
1303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(keyStore, out, null);
1304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.store(out, null);
1307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != IllegalArgumentException.class
1309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
1310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
1319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(out, null);
1321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(keyStore, out, null);
1322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.store(out, null);
1325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != IllegalArgumentException.class
1327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
1328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
1337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.store(out, PASSWORD_STORE);
1338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEqualsKeyStores(keyStore, out, PASSWORD_STORE);
1339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
1344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.store(out, PASSWORD_STORE);
1345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEqualsKeyStores(keyStore, out, PASSWORD_STORE);
1346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_store_LoadStoreParameter() throws Exception {
1350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null);
1353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null);
1362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (UnsupportedOperationException expected) {
136446c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                assertFalse(isLoadStoreParameterSupported(keyStore));
136546c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom            } catch (IllegalArgumentException expected) {
136646c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                // its supported, but null causes an exception
136746c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                assertTrue(isLoadStoreParameterSupported(keyStore));
1368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_load_InputStream() throws Exception {
1373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(0, keyStore.size());
1376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, PASSWORD_STORE);
1380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(0, keyStore.size());
1381e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // test_KeyStore_store_OutputStream effectively tests load as well as store
1384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_load_LoadStoreParameter() throws Exception {
1387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null);
1389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(0, keyStore.size());
1390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.load(new LoadStoreParameter() {
1395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        public ProtectionParameter getProtectionParameter() {
1396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            return null;
1397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        }
1398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    });
1399a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                fail();
1400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (UnsupportedOperationException expected) {
1401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getEntry() throws Exception {
1406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, null);
1409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, null);
1420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1421e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, PARAM_KEY);
1425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getEntry("", null));
1429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getEntry("", PARAM_KEY));
1430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
1432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
1433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
1435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
1437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getEntry(ALIAS_CERTIFICATE, null));
1440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_CERTIFICATE, null));
1442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
1446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
1447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
1448e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
1450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
1452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1453e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
1455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
1456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
1459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
1463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
1465e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else if (isKeyPasswordIgnored(keyStore)) {
1466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, null));
1467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_PRIVATE, null);
1470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
1474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_SECRET, null);
1481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
1485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with bad passwords
1491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isKeyPasswordIgnored(keyStore)) {
1492e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
1493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD);
1496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1497e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
1498e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_SECRET, PARAM_BAD);
1503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1504e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
1505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1509e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setEntry() throws Exception {
1511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1512e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, null);
1515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1518e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1524e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, null);
1526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1532e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1534e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, PARAM_KEY);
1535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry("", null, PARAM_KEY);
1544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1547e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1548e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1549e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1551e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1552e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
1553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1554e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.setEntry(ALIAS_PRIVATE, PRIVATE_KEY, PARAM_KEY);
1555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
1557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(SECRET_KEY), PARAM_KEY);
1560e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1561e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1562e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1563e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, SECRET_KEY, PASSWORD_KEY, null);
1564e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1565e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
1566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1567e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1568e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1569e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_CERTIFICATE,
1571e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                  new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()),
1572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                  null);
1573e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1574e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1575e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
1577e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()),
1578e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
1579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
1581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1589e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
1590e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2, PARAM_KEY);
1593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1597e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_SECRET,
1600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new SecretKeyEntry(SECRET_KEY_2),
1601e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
1602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1603e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1606e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1607e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1608e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1609e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE,
1610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new TrustedCertificateEntry(PRIVATE_KEY_2.getCertificate()),
1611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
1612e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1617e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, PRIVATE_KEY_2, PARAM_KEY);
1619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1623e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_SECRET,
1626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new SecretKeyEntry(SECRET_KEY_2),
1627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
1628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE,
1636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new TrustedCertificateEntry(PRIVATE_KEY_2.getCertificate()),
1637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
1638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
1639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null/non-null passwords
1648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_PRIVATE, PRIVATE_KEY, null);
1650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != UnrecoverableKeyException.class
1653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
1654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1658e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(SECRET_KEY), null);
1661e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    fail();
1662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class
1665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != KeyStoreException.class) {
1666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1671e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
1672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
1673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()),
1674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
1675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    try {
1678e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        keyStore.setEntry(ALIAS_CERTIFICATE,
1679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                          new TrustedCertificateEntry(PRIVATE_KEY.getCertificate()),
1680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                          PARAM_KEY);
1681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        fail();
1682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    } catch (KeyStoreException expected) {
1683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1685e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_entryInstanceOf() throws Exception {
1690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, null);
1693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1699e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, null);
1703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1705e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, Entry.class);
1708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1712e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf("", null);
1713e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", Entry.class));
1718e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1719e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1722e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", Entry.class));
1725e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", PrivateKeyEntry.class));
1726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", SecretKeyEntry.class));
1727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", TrustedCertificateEntry.class));
1728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1729e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
1730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertTrue(keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class));
1731e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class));
1732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class));
1733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore),
1735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class));
1736e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class));
1737e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class));
1738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore),
1740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_CERTIFICATE,
1741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  TrustedCertificateEntry.class));
1742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class));
1743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class));
1744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, PrivateKeyEntry.class));
1748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, SecretKeyEntry.class));
1749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE,
1750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 TrustedCertificateEntry.class));
1751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, SecretKeyEntry.class));
1754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, PrivateKeyEntry.class));
1755e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET,
1756e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 TrustedCertificateEntry.class));
1757e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1758e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
1759e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE,
1760e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  TrustedCertificateEntry.class));
1761e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE,
1762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 PrivateKeyEntry.class));
1763e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, SecretKeyEntry.class));
1764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1766e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1767e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_Builder() throws Exception {
1768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore, null);
1772e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1774e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1775e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1778e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore.getType(),
1780e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    keyStore.getProvider(),
1781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
1782a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                fail();
1783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1784e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1787e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1788e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1789e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(null,
1790e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
1791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
1792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
1793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore.getType(),
1798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    keyStore.getProvider(),
1799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
1800e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
1801e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1802e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1803e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1804e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1805e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1806e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1807e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1808e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Builder builder = Builder.newInstance(keyStore, PARAM_STORE);
1809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1810e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                builder.getProtectionParameter(null);
1811e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1812e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1813e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore, builder.getKeyStore());
1815e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1816e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                builder.getProtectionParameter(null);
1817e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                fail();
1818e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1819e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1820e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
1821e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1822e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1823e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            File file = File.createTempFile("keystore", keyStore.getProvider().getName());
1826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
182757f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                OutputStream os = new FileOutputStream(file);
182857f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                keyStore.store(os, PASSWORD_STORE);
182957f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                os.close();
1830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder builder = Builder.newInstance(keyStore.getType(),
1831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      keyStore.getProvider(),
1832e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      file,
1833e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      PARAM_STORE);
1834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(keyStore.getType(), builder.getKeyStore().getType());
1835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
1836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
1837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(file, PASSWORD_STORE, keyStore);
1838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } finally {
1839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                file.delete();
1840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1844e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Builder builder = Builder.newInstance(keyStore.getType(),
1845e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  keyStore.getProvider(),
1846e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  PARAM_STORE);
1847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore.getType(), builder.getKeyStore().getType());
1848e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
1849e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
1850e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1851e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1852e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1853e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_cacerts_bks() throws Exception {
1854e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (StandardNames.IS_RI) {
1855e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            return;
1856e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1857e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance("BKS");
185828192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilson        InputStream in = new FileInputStream(System.getProperty(
185928192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilson                "javax.net.ssl.trustStore", "/etc/security/cacerts.bks"));
186028192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilson        ks.load(in, null);
186157f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        in.close();
1862e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (String alias : Collections.list(ks.aliases())) {
1863cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom            try {
1864cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assert(ks.isCertificateEntry(alias));
1865cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                Certificate c = ks.getCertificate(alias);
1866cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertTrue(c instanceof X509Certificate);
1867cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                X509Certificate cert = (X509Certificate) c;
1868cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertEquals(cert.getSubjectUniqueID(), cert.getIssuerUniqueID());
1869cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertNotNull(cert.getPublicKey());
1870cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom            } catch (Exception e) {
1871cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                throw new Exception("alias=" + alias, e);
1872cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom            }
1873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1875e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom}
1876