KeyStoreTest.java revision 6256280922cc8a6622a156afeb7f43a31576d43f
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;
24a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Rootimport java.io.IOException;
2528192ac5dbb128c63d914fab324d15757fe98fdaJesse Wilsonimport java.io.InputStream;
2657f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstromimport java.io.OutputStream;
274557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Key;
284557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.KeyStore;
29e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.Builder;
30e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.Entry;
31e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.LoadStoreParameter;
32e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.PasswordProtection;
33e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.PrivateKeyEntry;
34e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.ProtectionParameter;
35e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.SecretKeyEntry;
36e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.KeyStore.TrustedCertificateEntry;
374557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.KeyStoreException;
384557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.NoSuchAlgorithmException;
394557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Provider;
404557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.Security;
414557728efb66c455a52b7669a8eefef7a9e54854Jesse Wilsonimport java.security.UnrecoverableKeyException;
42e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.cert.Certificate;
43e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.security.cert.X509Certificate;
44e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.ArrayList;
45e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Arrays;
46e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Collections;
47e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Date;
486256280922cc8a6622a156afeb7f43a31576d43fKenny Rootimport java.util.Enumeration;
49e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.HashSet;
50e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.List;
51e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport java.util.Set;
52e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.KeyGenerator;
53e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport javax.crypto.SecretKey;
54e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstromimport junit.framework.TestCase;
55e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
56e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrompublic class KeyStoreTest extends TestCase {
57e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
58003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static PrivateKeyEntry PRIVATE_KEY;
59003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static PrivateKeyEntry PRIVATE_KEY_2;
606a75005c0547634e5179829c61eb03209197cedaJesse Wilson
61003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static SecretKey SECRET_KEY;
62003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static SecretKey SECRET_KEY_2;
63e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
64e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_PRIVATE = "private";
65e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_CERTIFICATE = "certificate";
66e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_SECRET = "secret";
67e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
68e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_PRIVATE = "pRiVaTe";
696256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static final String ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE = "PrIvAtE-no-password";
70e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_CERTIFICATE = "cErTiFiCaTe";
71e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_ALT_CASE_SECRET = "sEcRet";
72e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
733d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root    private static final String ALIAS_UNICODE_PRIVATE = "\u6400\u7902\u3101\u8c02\u5002\u8702\udd01";
746256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static final String ALIAS_UNICODE_NO_PASSWORD_PRIVATE = "\u926c\u0967\uc65b\ubc78";
753d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root    private static final String ALIAS_UNICODE_CERTIFICATE = "\u5402\udd01\u7902\u8702\u3101\u5f02\u3101\u5402\u5002\u8702\udd01";
763d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root    private static final String ALIAS_UNICODE_SECRET = "\ue224\ud424\ud224\ue124\ud424\ue324";
773d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root
78e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_NO_PASSWORD_PRIVATE = "private-no-password";
79e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final String ALIAS_NO_PASSWORD_SECRET = "secret-no-password";
80e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
81e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_STORE = "store password".toCharArray();
82e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_KEY = "key password".toCharArray();
83e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final char[] PASSWORD_BAD = "dummy".toCharArray();
84e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
85e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_STORE = new PasswordProtection(PASSWORD_STORE);
86e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_KEY = new PasswordProtection(PASSWORD_KEY);
87e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static final ProtectionParameter PARAM_BAD = new PasswordProtection(PASSWORD_BAD);
88e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
89003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static PrivateKeyEntry getPrivateKey() {
90003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        if (PRIVATE_KEY == null) {
91003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            PRIVATE_KEY = TestKeyStore.getServer().getPrivateKey("RSA", "RSA");
92003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
93003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        return PRIVATE_KEY;
94003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
95003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
96003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static PrivateKeyEntry getPrivateKey2() {
97003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        if (PRIVATE_KEY_2 == null) {
98003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            PRIVATE_KEY_2 = TestKeyStore.getClientCertificate().getPrivateKey("RSA", "RSA");
99003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
100003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        return PRIVATE_KEY_2;
101003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
102003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
103003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static SecretKey getSecretKey() {
104003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        if (SECRET_KEY == null) {
105003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            SECRET_KEY = generateSecretKey();
106003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
107003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        return SECRET_KEY;
108003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
109003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
110003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static SecretKey getSecretKey2() {
111003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        if (SECRET_KEY_2 == null) {
112003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            SECRET_KEY_2 = generateSecretKey();
113003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
114003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        return SECRET_KEY_2;
115003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
116003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
117003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    private static SecretKey generateSecretKey() {
118003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        try {
119003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            KeyGenerator kg = KeyGenerator.getInstance("DES");
120003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            return kg.generateKey();
121003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        } catch (NoSuchAlgorithmException e) {
122003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            throw new RuntimeException(e);
123003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        }
124003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom    }
125003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom
126a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom    public static List<KeyStore> keyStores() throws Exception {
127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        List<KeyStore> keyStores = new ArrayList<KeyStore>();
128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider[] providers = Security.getProviders();
129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (Provider provider : providers) {
130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<Provider.Service> services = provider.getServices();
131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            for (Provider.Service service : services) {
132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String type = service.getType();
133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!type.equals("KeyStore")) {
134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    continue;
135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String algorithm = service.getAlgorithm();
137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                KeyStore ks = KeyStore.getInstance(algorithm, provider);
138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(provider, ks.getProvider());
139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(algorithm, ks.getType());
140e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!isUnsupported(ks)) {
141e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStores.add(ks);
142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
145e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return keyStores;
146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isSecretKeyEnabled(KeyStore ks) {
149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // JKS key stores cannot store secret keys, neither can the RI's PKCS12
150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("JKS")
151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("CaseExactJKS")
1526256280922cc8a6622a156afeb7f43a31576d43fKenny Root                  || (ks.getType().equals("PKCS12"))
1536256280922cc8a6622a156afeb7f43a31576d43fKenny Root                  || (ks.getType().equals("AndroidKeyStore"))));
154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isCertificateEnabled(KeyStore ks) {
157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // RI can't handle certificate in PKCS12, but BC can
158e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("SunJSSE")));
159e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
160e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
161e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isCaseSensitive(KeyStore ks) {
162e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getType().equals("CaseExactJKS")
163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                || ks.getType().equals("BKS")
1646256280922cc8a6622a156afeb7f43a31576d43fKenny Root                || ks.getType().equals("BouncyCastle")
1656256280922cc8a6622a156afeb7f43a31576d43fKenny Root                || ks.getType().equals("AndroidKeyStore"));
166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
167e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isUnsupported(KeyStore ks) {
170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // Don't bother testing BC on RI
171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (StandardNames.IS_RI && ks.getProvider().getName().equals("BC"));
172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isNullPasswordAllowed(KeyStore ks) {
175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (!(ks.getType().equals("JKS")
176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("CaseExactJKS")
177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("JCEKS")
178e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                  || ks.getType().equals("PKCS12")));
179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1806256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static boolean isKeyPasswordSupported(KeyStore ks) {
1816256280922cc8a6622a156afeb7f43a31576d43fKenny Root        return !ks.getType().equals("AndroidKeyStore");
1826256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
183e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isKeyPasswordIgnored(KeyStore ks) {
184e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // BouncyCastle's PKCS12 ignores the key password unlike the RI which requires it
185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC"));
186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
18846c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom    private static boolean isLoadStoreParameterSupported(KeyStore ks) {
18946c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom        // BouncyCastle's PKCS12 allows a JDKPKCS12StoreParameter
19046c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom        return (ks.getType().equals("PKCS12") && ks.getProvider().getName().equals("BC"));
19146c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom    }
19246c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom
1936256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static boolean isPersistentStorage(KeyStore ks) {
1946256280922cc8a6622a156afeb7f43a31576d43fKenny Root        return ks.getType().equalsIgnoreCase("AndroidKeyStore");
1956256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
1966256280922cc8a6622a156afeb7f43a31576d43fKenny Root
1976256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static boolean isLoadStoreUnsupported(KeyStore ks) {
1986256280922cc8a6622a156afeb7f43a31576d43fKenny Root        return ks.getType().equalsIgnoreCase("AndroidKeyStore");
1996256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
2006256280922cc8a6622a156afeb7f43a31576d43fKenny Root
201e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    private static boolean isSetKeyByteArrayUnimplemented(KeyStore ks) {
202e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // All of BouncyCastle's
203e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // KeyStore.setKeyEntry(String,byte[],char[]) implementations
204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // throw RuntimeException
205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        return (ks.getProvider().getName().equals("BC"));
206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
208347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    private static boolean hasDefaultContents(KeyStore ks) {
209347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        // AndroidCAStore exposes CA cert files via the KeyStore
210347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        // interface, so it does start out empty like other KeyStores
211347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        return (ks.getType().equals("AndroidCAStore"));
212347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    }
213347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
214347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    private static boolean isReadOnly(KeyStore ks) {
215347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        // AndroidCAStore is read only, throwing
216347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        // UnsupportedOperationException on write operations
217347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        return (ks.getType().equals("AndroidCAStore"));
218347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    }
219347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void populate(KeyStore ks) throws Exception {
2216256280922cc8a6622a156afeb7f43a31576d43fKenny Root        clearKeyStore(ks);
2226256280922cc8a6622a156afeb7f43a31576d43fKenny Root        if (isKeyPasswordSupported(ks)) {
2236256280922cc8a6622a156afeb7f43a31576d43fKenny Root            setPrivateKey(ks);
224347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        }
225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isNullPasswordAllowed(ks)) {
226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ks.setKeyEntry(ALIAS_NO_PASSWORD_PRIVATE,
227003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                           getPrivateKey().getPrivateKey(),
228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                           null,
229003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                           getPrivateKey().getCertificateChain());
230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isCertificateEnabled(ks)) {
232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ks.setCertificateEntry(ALIAS_CERTIFICATE,
233003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                   getPrivateKey().getCertificate());
234e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (isSecretKeyEnabled(ks)) {
236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setSecretKey(ks);
237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(ks)) {
238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                ks.setKeyEntry(ALIAS_NO_PASSWORD_SECRET,
239003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                               getSecretKey(),
240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               null,
241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               null);
242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2466256280922cc8a6622a156afeb7f43a31576d43fKenny Root    private static void clearKeyStore(KeyStore ks) throws Exception {
2476256280922cc8a6622a156afeb7f43a31576d43fKenny Root        ks.load(null, null);
2486256280922cc8a6622a156afeb7f43a31576d43fKenny Root        if (isReadOnly(ks)) {
2496256280922cc8a6622a156afeb7f43a31576d43fKenny Root            try {
2506256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKey(ks);
2516256280922cc8a6622a156afeb7f43a31576d43fKenny Root                fail(ks.toString());
2526256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } catch (UnsupportedOperationException e) {
2536256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
2546256280922cc8a6622a156afeb7f43a31576d43fKenny Root            return;
2556256280922cc8a6622a156afeb7f43a31576d43fKenny Root        }
2566256280922cc8a6622a156afeb7f43a31576d43fKenny Root        if (isPersistentStorage(ks)) {
2576256280922cc8a6622a156afeb7f43a31576d43fKenny Root            Enumeration<String> aliases = ks.aliases();
2586256280922cc8a6622a156afeb7f43a31576d43fKenny Root            while (aliases.hasMoreElements()) {
2596256280922cc8a6622a156afeb7f43a31576d43fKenny Root                String alias = aliases.nextElement();
2606256280922cc8a6622a156afeb7f43a31576d43fKenny Root                ks.deleteEntry(alias);
2616256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
2626256280922cc8a6622a156afeb7f43a31576d43fKenny Root        }
2636256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
2646256280922cc8a6622a156afeb7f43a31576d43fKenny Root
2656256280922cc8a6622a156afeb7f43a31576d43fKenny Root    public static void setPrivateKeyNoPassword(KeyStore ks, String alias, PrivateKeyEntry privateKey)
2666256280922cc8a6622a156afeb7f43a31576d43fKenny Root            throws Exception {
2676256280922cc8a6622a156afeb7f43a31576d43fKenny Root        ks.setKeyEntry(alias, privateKey.getPrivateKey(), null, privateKey.getCertificateChain());
2686256280922cc8a6622a156afeb7f43a31576d43fKenny Root    }
269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks) throws Exception {
270e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKey(ks, ALIAS_PRIVATE);
271e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
272e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks, String alias) throws Exception {
273003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setPrivateKey(ks, alias, getPrivateKey());
274e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
275e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKey(KeyStore ks,
276e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     String alias,
277e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PrivateKeyEntry privateKey)
278e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
279e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getPrivateKey(),
281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       PASSWORD_KEY,
282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getCertificateChain());
283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks) throws Exception {
286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setPrivateKeyBytes(ks, ALIAS_PRIVATE);
287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks, String alias) throws Exception {
289003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setPrivateKeyBytes(ks, alias, getPrivateKey());
290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setPrivateKeyBytes(KeyStore ks,
292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     String alias,
293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PrivateKeyEntry privateKey)
294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getPrivateKey().getEncoded(),
297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       privateKey.getCertificateChain());
298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks) throws Exception {
301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKey(ks, ALIAS_SECRET);
302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks, String alias) throws Exception {
304003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setSecretKey(ks, alias, getSecretKey());
305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKey(KeyStore ks, String alias, SecretKey key) throws Exception {
307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       key,
309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       PASSWORD_KEY,
310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       null);
311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks) throws Exception {
314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setSecretKeyBytes(ks, ALIAS_SECRET);
315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks, String alias) throws Exception {
317003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setSecretKeyBytes(ks, alias, getSecretKey());
318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setSecretKeyBytes(KeyStore ks, String alias, SecretKey key)
320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setKeyEntry(alias,
322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       key.getEncoded(),
323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                       null);
324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks) throws Exception {
327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        setCertificate(ks, ALIAS_CERTIFICATE);
328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks, String alias) throws Exception {
330003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        setCertificate(ks, alias, getPrivateKey().getCertificate());
331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void setCertificate(KeyStore ks, String alias, Certificate certificate)
333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.setCertificateEntry(alias, certificate);
335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey(Key actual)
338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
339003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getPrivateKey(), actual);
340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey2(Key actual)
342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
343003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey2().getPrivateKey(), actual);
344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertPrivateKey(Entry actual)
346e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
347347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        assertNotNull(actual);
348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(PrivateKeyEntry.class, actual.getClass());
349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        PrivateKeyEntry privateKey = (PrivateKeyEntry) actual;
350003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getPrivateKey(), privateKey.getPrivateKey());
351003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getCertificate(), privateKey.getCertificate());
352003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()),
353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     Arrays.asList(privateKey.getCertificateChain()));
354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey(Key actual)
357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
358003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getSecretKey(), actual);
359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey2(Key actual)
361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
362003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getSecretKey2(), actual);
363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertSecretKey(Entry actual)
365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(SecretKeyEntry.class, actual.getClass());
367003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getSecretKey(), ((SecretKeyEntry) actual).getSecretKey());
368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate(Certificate actual)
371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
372003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getCertificate(), actual);
373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate2(Certificate actual)
375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
376003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey2().getCertificate(), actual);
377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificate(Entry actual)
379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertSame(TrustedCertificateEntry.class, actual.getClass());
381003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(getPrivateKey().getCertificate(),
382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     ((TrustedCertificateEntry) actual).getTrustedCertificate());
383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public static void assertCertificateChain(Certificate[] actual)
386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception {
387003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom        assertEquals(Arrays.asList(getPrivateKey().getCertificateChain()),
388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                     Arrays.asList(actual));
389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
391e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_create() throws Exception {
392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider[] providers = Security.getProviders();
393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (Provider provider : providers) {
394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<Provider.Service> services = provider.getServices();
395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            for (Provider.Service service : services) {
396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String type = service.getType();
397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (!type.equals("KeyStore")) {
398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    continue;
399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                String algorithm = service.getAlgorithm();
401e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                KeyStore ks = KeyStore.getInstance(algorithm, provider);
402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(provider, ks.getProvider());
403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(algorithm, ks.getType());
404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getInstance() throws Exception {
409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null);
4125ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NullPointerException expected) {
414e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
415e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
416e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type));
417e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
418e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String providerName = StandardNames.SECURITY_PROVIDER_NAME;
419e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, (String)null);
4215ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, providerName);
4265ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (Exception e) {
428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (e.getClass() != NullPointerException.class
429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                && e.getClass() != KeyStoreException.class) {
430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                throw e;
431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(type, (String)null);
4355ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type, providerName));
439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        Provider provider = Security.getProvider(providerName);
441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, (Provider)null);
4435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
445e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
447e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(null, provider);
4485ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
449e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (NullPointerException expected) {
450e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
451e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        try {
452e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            KeyStore.getInstance(type, (Provider)null);
4535ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            fail(type);
454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        } catch (IllegalArgumentException expected) {
455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(KeyStore.getInstance(type, provider));
457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
458e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
459e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getDefaultType() throws Exception {
460e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
461e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(type);
462e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(type);
463e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks);
464e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(type, ks.getType());
465e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
466e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
467e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getProvider() throws Exception {
468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks.getProvider());
470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(StandardNames.SECURITY_PROVIDER_NAME, ks.getProvider().getName());
471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNotNull(keyStore.getProvider());
474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getType() throws Exception {
478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        String type = KeyStore.getDefaultType();
479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(type);
480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(ks.getType());
481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertNotNull(type, ks.getType());
482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNotNull(keyStore.getType());
485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
488e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getKey() throws Exception {
489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, null);
4925ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
497e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
498e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, null);
5035ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
504e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
508e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
509e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
510e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
511e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getKey(null, PASSWORD_KEY);
5125ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
513e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
514e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
515e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
516e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
517e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
518e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey("", null));
521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey("", PASSWORD_KEY));
522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
524347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
525347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
5276256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
5286256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
5296256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
5306256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
5316256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
5326256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
533347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
534347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
535347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } else {
536347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
537347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
541347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
5436256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, PASSWORD_KEY));
544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
545e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
5466256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
5476256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
5486256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
5496256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
5506256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
5516256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
552e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
554e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
557e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
5586256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
560e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
561347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (isReadOnly(keyStore)) {
562347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getKey(ALIAS_PRIVATE, null));
5636256280922cc8a6622a156afeb7f43a31576d43fKenny Root                } else if (isKeyPasswordSupported(keyStore)) {
564347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    try {
565347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                        keyStore.getKey(ALIAS_PRIVATE, null);
5665ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
567347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    } catch (Exception e) {
568347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                        if (e.getClass() != UnrecoverableKeyException.class
569347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                            && e.getClass() != IllegalArgumentException.class) {
570347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                            throw e;
571347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                        }
572e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
573e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
574e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
575347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
576347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, null));
577347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isSecretKeyEnabled(keyStore)) {
578e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_SECRET, null);
5805ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
583e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
589e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with bad passwords
590347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
591347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, null));
5926256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
5946256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore)) {
595e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_PRIVATE, PASSWORD_BAD);
5975ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
598e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
599e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
601347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
602347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD));
603347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isSecretKeyEnabled(keyStore)) {
604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getKey(ALIAS_SECRET, PASSWORD_BAD);
6065ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
607e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
608e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
609e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
612e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificateChain() throws Exception {
614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
615e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
616e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateChain(null);
6175ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
623e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateChain(null);
6275ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateChain(""));
635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
637347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
638347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getCertificateChain(ALIAS_PRIVATE));
6396256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore)) {
640347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
6416256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isNullPasswordAllowed(keyStore)) {
6426256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
643347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
646347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) {
647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE));
648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
649e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificateChain(keyStore.getCertificateChain(ALIAS_ALT_CASE_PRIVATE));
650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificate() throws Exception {
655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
657e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificate(null);
6585ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
659e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
660e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
661e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
662e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
663e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
664e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
665e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificate(null);
6685ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
669e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
670e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
671e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class) {
672e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
673e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
674e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
675e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificate(""));
676e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
677e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
678347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) {
679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
685347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) {
686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
687e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
688e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
689e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
690e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
691e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
692e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
693e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCreationDate() throws Exception {
696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
698a5c608e59f9d574ea4bc65e9dff44aae2f34fd26Brian Carlstrom                keyStore.getCreationDate(null);
6995ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        long before = System.currentTimeMillis();
704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
7056256280922cc8a6622a156afeb7f43a31576d43fKenny Root            populate(keyStore);
7066256280922cc8a6622a156afeb7f43a31576d43fKenny Root
707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // add 1000 since some key stores round of time to nearest second
708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            long after = System.currentTimeMillis() + 1000;
709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
712e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCreationDate(null);
7135ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
714e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
715e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
716e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCreationDate(""));
717e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
718e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
719347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (!isReadOnly(keyStore) && isCertificateEnabled(keyStore)) {
720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Date date = keyStore.getCreationDate(ALIAS_CERTIFICATE);
721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNotNull(date);
7226256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("date should be after start time: " + date.getTime() + " >= " + before,
7236256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        before <= date.getTime());
7246256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("date should be before expiry time: " + date.getTime() + " <= " + after,
7256256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        date.getTime() <= after);
726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
727e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCreationDate(ALIAS_CERTIFICATE));
728e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
729e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
730e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
731347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore) || isCaseSensitive(keyStore)) {
732e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE));
733e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
734e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    Date date = keyStore.getCreationDate(ALIAS_ALT_CASE_CERTIFICATE);
736e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(before <= date.getTime());
737e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertTrue(date.getTime() <= after);
738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setKeyEntry_Key() throws Exception {
744e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null, null);
7475ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
753e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
754347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
755347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
756347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.setKeyEntry(null, null, null, null);
7575ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
758347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
759347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
760347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
761347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
763e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null, null);
7665ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
767e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
768e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
769e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
770e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
771e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
772e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
773e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
774e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, PASSWORD_KEY, null);
7755ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
778e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
779e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
780e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
782e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
783e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(ALIAS_PRIVATE,
784003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                     getPrivateKey().getPrivateKey(),
785e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     PASSWORD_KEY,
786e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     null);
7875ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
7886256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } catch (Exception e) {
7896256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (e.getClass() != IllegalArgumentException.class
7906256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        && e.getClass() != KeyStoreException.class) {
7916256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    throw e;
7926256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
7976256280922cc8a6622a156afeb7f43a31576d43fKenny Root            clearKeyStore(keyStore);
798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
799e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
8006256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
8016256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8026256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
8036256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
8046256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
8056256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
806347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
807347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
808003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null);
8095ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
810347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
811347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
812347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
813347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
8146256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
8156256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKey(keyStore);
8166256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8176256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
8186256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
8196256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
8206256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKeyNoPassword(keyStore, ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey());
8216256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
8226256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
8236256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setSecretKey(keyStore);
827e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
828e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
830003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null);
8315ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
832e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
833e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != KeyStoreException.class
834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
838e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
843e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
844347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
845347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
846347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
847347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
848347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
849347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isCaseSensitive(keyStore)) {
8506256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
8516256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8526256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
8536256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
8546256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8556256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
8566256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
8576256280922cc8a6622a156afeb7f43a31576d43fKenny Root
8586256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
8596256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
8606256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
8616256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE,
8626256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            getPrivateKey2());
8636256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
8646256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
8656256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
866e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
867e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
868e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
869e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
870003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    setSecretKey(keyStore, ALIAS_ALT_CASE_SECRET, getSecretKey2());
871e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
872e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
8756256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
8766256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8776256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
8786256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
8796256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
8806256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
8816256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
8826256280922cc8a6622a156afeb7f43a31576d43fKenny Root
8836256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
8846256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
8856256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
8866256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKey(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2());
8876256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, null));
8886256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
8896256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
8906256280922cc8a6622a156afeb7f43a31576d43fKenny Root
891e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
892e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
893e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
894003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    setSecretKey(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2());
895e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
896e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
897e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
898e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
899e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
900e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
901e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
902e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
903347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
904347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
905347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.setKeyEntry(ALIAS_PRIVATE,
906003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                         getPrivateKey().getPrivateKey(),
907347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                                         null,
908003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                         getPrivateKey().getCertificateChain());
9095ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
910347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
911347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
912347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
913347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
914e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
915e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
917e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(ALIAS_PRIVATE,
918003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                     getPrivateKey().getPrivateKey(),
919e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                     null,
920003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                     getPrivateKey().getCertificateChain());
921e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
922e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
923e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
924e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_PRIVATE,
925003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                         getPrivateKey().getPrivateKey(),
926e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                         null,
927003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                         getPrivateKey().getCertificateChain());
9285ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
929e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
930e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
931e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class
932e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != KeyStoreException.class) {
933e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
934e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
935e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
937e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
938e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
939003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null);
940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, null));
941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    try {
943003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                        keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), null, null);
9445ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
945e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    } catch (Exception e) {
946e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        if (e.getClass() != UnrecoverableKeyException.class
947e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            && e.getClass() != IllegalArgumentException.class
948e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            && e.getClass() != KeyStoreException.class) {
949e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            throw e;
950e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        }
951e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
952e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
954e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
955e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
956e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
957e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setKeyEntry_array() throws Exception {
958e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
959e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
960e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null);
9615ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
962e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
963e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
964e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
965e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
966e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
967e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
968e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
969347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
970347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
971347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.setKeyEntry(null, null, null);
9725ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
973347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
974347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
975347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
976347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
977347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
978e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
979e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setKeyEntry(null, null, null);
9815ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
982e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
983e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != IllegalArgumentException.class
985e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class
986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != RuntimeException.class) {
987e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
988e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
989e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
990e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
991e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isNullPasswordAllowed(keyStore)) {
994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // TODO Use EncryptedPrivateKeyInfo to protect keys if
995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // password is required.
996e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
997e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSetKeyByteArrayUnimplemented(keyStore)) {
999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1001e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
10026256280922cc8a6622a156afeb7f43a31576d43fKenny Root            clearKeyStore(keyStore);
1003e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1004e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
10056256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
10066256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
10076256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
10086256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
10096256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
10106256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1011347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1012347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1013347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    setPrivateKeyBytes(keyStore);
10145ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1015347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1016347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1017347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1018347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
10196256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
10206256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKeyBytes(keyStore);
10216256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
10226256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
10236256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
10246256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
10256256280922cc8a6622a156afeb7f43a31576d43fKenny Root                setPrivateKeyNoPassword(keyStore, ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey());
10266256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
10276256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
10286256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1031e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setSecretKeyBytes(keyStore);
1032e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1033e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1035003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey().getEncoded(), null);
10365ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
1038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1039e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1041e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isNullPasswordAllowed(keyStore)) {
1044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // TODO Use EncryptedPrivateKeyInfo to protect keys if
1045e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                // password is required.
1046e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1047e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1048e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSetKeyByteArrayUnimplemented(keyStore)) {
1049e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1050e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1051e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1052e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1053e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1054347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1055347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
1056347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
1057347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1058347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1059347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isCaseSensitive(keyStore)) {
10606256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
10616256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
10626256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
10636256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
10646256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
10656256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
10666256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
10676256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
10686256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
10696256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
10706256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE,
10716256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            getPrivateKey2());
10726256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
10736256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
10746256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
1075e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1076e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1077e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1078e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1079003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2());
1080e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1081e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1082e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1083e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
10846256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
10856256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
10866256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
10876256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getPrivateKey2());
10886256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
10896256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
10906256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
10916256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
10926256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
10936256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
10946256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    setPrivateKeyNoPassword(keyStore, ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE,
10956256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            getPrivateKey2());
10966256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
10976256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
10986256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
1099e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1103003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    setSecretKeyBytes(keyStore, ALIAS_ALT_CASE_PRIVATE, getSecretKey2());
1104e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
1106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1108e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1109e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setCertificateEntry() throws Exception {
1112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1113e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1114e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setCertificateEntry(null, null);
11155ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1116e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1117e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1118e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1121e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1122347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1123e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setCertificateEntry(null, null);
11265ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1130e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1134347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1135347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1136347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1137347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
11385ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1139347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1140347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1141347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1142347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1143347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // Sort of delete by setting null.  Note that even though
1145347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            // certificate is null, size doesn't change,
1146e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // isCertificateEntry returns true, and it is still listed in aliases.
1147e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1148e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1149e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    int size = keyStore.size();
1151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
11526256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getType(), keyStore.getCertificate(ALIAS_CERTIFICATE));
11536256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertEquals(keyStore.getType(), size, keyStore.size());
11546256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertTrue(keyStore.getType(), keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
11556256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertTrue(keyStore.getType(),
11566256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            Collections.list(keyStore.aliases()).contains(ALIAS_CERTIFICATE));
1157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (NullPointerException expectedSometimes) {
11586256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    if (!("PKCS12".equalsIgnoreCase(keyStore.getType()) &&
11596256280922cc8a6622a156afeb7f43a31576d43fKenny Root                                "BC".equalsIgnoreCase(keyStore.getProvider().getName()))
11606256280922cc8a6622a156afeb7f43a31576d43fKenny Root                            && !"AndroidKeyStore".equalsIgnoreCase(keyStore.getType())) {
11616256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        throw expectedSometimes;
11626256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    }
1163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1165e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1166e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setCertificateEntry(ALIAS_CERTIFICATE, null);
11675ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1168e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
1169e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1170e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1171e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1172e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1173e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1174e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isCertificateEnabled(keyStore)) {
1175e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1176e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1177e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
11786256280922cc8a6622a156afeb7f43a31576d43fKenny Root            clearKeyStore(keyStore);
1179e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1180e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1181347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1182347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1183347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    setCertificate(keyStore);
11845ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1185347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1186347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1187347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1188347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            setCertificate(keyStore);
1190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1193e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1194e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (!isCertificateEnabled(keyStore)) {
1195e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                continue;
1196e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1197e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1198e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1199e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1200347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
12016256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
12026256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1203347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isCaseSensitive(keyStore)) {
1204e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1205e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1206e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setCertificate(keyStore,
1207e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               ALIAS_ALT_CASE_CERTIFICATE,
1208003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                               getPrivateKey2().getCertificate());
1209e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1213e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                setCertificate(keyStore,
1215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                               ALIAS_ALT_CASE_CERTIFICATE,
1216003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                               getPrivateKey2().getCertificate());
1217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
1218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
1219e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1220e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1222e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_deleteEntry() throws Exception {
1223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(null);
12265ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1234347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1235347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1236347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.deleteEntry(null);
12375ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1238347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1239347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1240347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1241347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1242347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1244e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(null);
12465ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1249e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.deleteEntry("");
1254e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1259347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1260347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1261347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.deleteEntry(ALIAS_PRIVATE);
1262347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException e) {
1263347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1264347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1265347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1266347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
12686256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
12696256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
12706256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
12716256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.deleteEntry(ALIAS_PRIVATE);
12726256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
12736256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
12746256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
12756256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
12766256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
12776256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.deleteEntry(ALIAS_NO_PASSWORD_PRIVATE);
12786256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNull(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
12796256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1280e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1281e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1282e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1283e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_SECRET);
1284e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1285e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_SECRET);
1287e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1288e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_CERTIFICATE);
1292e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
1293e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1294e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.deleteEntry(ALIAS_CERTIFICATE);
1295e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1296e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1297e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1300347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCaseSensitive(keyStore)) {
13046256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
13056256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
13066256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.deleteEntry(ALIAS_ALT_CASE_PRIVATE);
13076256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
13086256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
13096256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
13106256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
13116256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.deleteEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE);
13126256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
13136256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
1314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_ALT_CASE_SECRET);
1318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
1319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_SECRET);
1321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_ALT_CASE_CERTIFICATE);
1326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
1327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
1328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.deleteEntry(ALIAS_CERTIFICATE);
1329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_aliases() throws Exception {
1335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1336e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.aliases();
13385ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
13456256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
13466256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNotNull("Should be able to query size: " + keyStore.getType(),
13476256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.aliases());
13486256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
13496256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have more than one alias already: " + keyStore.getType(),
13506256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.aliases().hasMoreElements());
1351347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
13526256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have no aliases:" + keyStore.getType(), Collections.EMPTY_SET,
13530647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                        new HashSet(Collections.list(keyStore.aliases())));
1354347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1357e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1359347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<String> expected = new HashSet<String>();
13616256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
13626256280922cc8a6622a156afeb7f43a31576d43fKenny Root                expected.add(ALIAS_PRIVATE);
13636256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1365e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_NO_PASSWORD_PRIVATE);
1366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_SECRET);
1369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore)) {
1370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    expected.add(ALIAS_NO_PASSWORD_SECRET);
1371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_CERTIFICATE);
1375e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
13766256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
13776256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertNotNull("Should be able to query size: " + keyStore.getType(),
13786256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.aliases());
13796256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
1380347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertTrue(keyStore.aliases().hasMoreElements());
1381347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
1382347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertEquals(expected, new HashSet<String>(Collections.list(keyStore.aliases())));
1383347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_containsAlias() throws Exception {
1388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1389e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.containsAlias(null);
13915ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1393e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1394e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1395e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1396e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1397e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1398e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1399e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1400e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.containsAlias(null);
14015ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1402e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1403e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1404e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.containsAlias(""));
1406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1410347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.containsAlias(""));
1412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1413347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1414347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.containsAlias(ALIAS_PRIVATE));
1415347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1416347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
14176256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
14186256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue(keyStore.containsAlias(ALIAS_PRIVATE));
14196256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isNullPasswordAllowed(keyStore)) {
14206256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue(keyStore.containsAlias(ALIAS_NO_PASSWORD_PRIVATE));
14216256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore), keyStore.containsAlias(ALIAS_SECRET));
1423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore), keyStore.containsAlias(ALIAS_CERTIFICATE));
1424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1425e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1426e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_PRIVATE));
1427e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_SECRET));
1429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
1430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.containsAlias(ALIAS_ALT_CASE_CERTIFICATE));
1431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_size() throws Exception {
1435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1437e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.aliases();
14385ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1441e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1442e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1443e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1444e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
14456256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
14466256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should successfully query size: " + keyStore.getType(),
14476256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() >= 0);
14486256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
14496256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
14506256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1451347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
14526256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
1453347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1454e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1455e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1456e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1457e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1458347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (hasDefaultContents(keyStore)) {
14596256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
14606256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1461347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1462347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1463347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
14646256280922cc8a6622a156afeb7f43a31576d43fKenny Root            int expected = 0;
14656256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
14666256280922cc8a6622a156afeb7f43a31576d43fKenny Root                expected++;
14676256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1468e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1469e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1470e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
1472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1473e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore)) {
1474e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    expected++;
1475e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1476e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1477e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1478e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected++;
1479e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1480e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected, keyStore.size());
1481e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1482e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1483e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1484e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_isKeyEntry() throws Exception {
1485e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1486e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1487e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isKeyEntry(null);
14885ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1489e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1490e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1491e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1492e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1494e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1495e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1496e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1497e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isKeyEntry(null);
14985ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1499e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1500e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1501e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1502e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(""));
1503e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1504e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1505e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1506e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1507e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1508347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            assertFalse(keyStore.isKeyEntry(""));
1509347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1510347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.isKeyEntry(ALIAS_PRIVATE));
1511347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1512347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
15136256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
15146256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue(keyStore.isKeyEntry(ALIAS_PRIVATE));
15156256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
15166256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
15176256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue(keyStore.isKeyEntry(ALIAS_NO_PASSWORD_PRIVATE));
15186256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1519e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore), keyStore.isKeyEntry(ALIAS_SECRET));
1520e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(ALIAS_CERTIFICATE));
1521e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1522e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
1523e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isKeyEntry(ALIAS_ALT_CASE_PRIVATE));
1524e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
1525e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.isKeyEntry(ALIAS_ALT_CASE_SECRET));
1526e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isKeyEntry(ALIAS_ALT_CASE_CERTIFICATE));
1527e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1528e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1529e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1530e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_isCertificateEntry() throws Exception {
1531e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1532e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1533e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.isCertificateEntry(null);
15345ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1535e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1536e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1537e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1538e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1539e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1540e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1541e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1542e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1543e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1544e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.isCertificateEntry(null);
15455ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1546e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (NullPointerException expected) {
1547e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1548e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1549e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertFalse(keyStore.isCertificateEntry(null));
1550e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1551e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1552e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(""));
1553e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1554e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1555e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1556e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1557347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1558e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(""));
1559e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
15606256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
15616256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertFalse(keyStore.isCertificateEntry(ALIAS_PRIVATE));
15626256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
15636256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
15646256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertFalse(keyStore.isCertificateEntry(ALIAS_NO_PASSWORD_PRIVATE));
15656256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1566e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_SECRET));
1567347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            assertEquals(isCertificateEnabled(keyStore) && !isReadOnly(keyStore),
15680647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                    keyStore.isCertificateEntry(ALIAS_CERTIFICATE));
1569e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1570e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_PRIVATE));
1571e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.isCertificateEntry(ALIAS_ALT_CASE_SECRET));
1572347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            assertEquals(!isCaseSensitive(keyStore)
15730647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                    && isCertificateEnabled(keyStore)
15740647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                    && !isReadOnly(keyStore),
15750647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson                    keyStore.isCertificateEntry(ALIAS_ALT_CASE_CERTIFICATE));
1576e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1577e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1578e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1579e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getCertificateAlias() throws Exception {
1580e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1581e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1582e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getCertificateAlias(null);
15835ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1584e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1585e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1586e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1587e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1588e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1589e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1590e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getCertificateAlias(null));
1591e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1592e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1593e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1594e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1595347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1596e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Set<String> expected = new HashSet<String>();
15976256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
15986256280922cc8a6622a156afeb7f43a31576d43fKenny Root                expected.add(ALIAS_PRIVATE);
15996256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1600e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1601e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_NO_PASSWORD_PRIVATE);
1602e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1603e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
1604e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                expected.add(ALIAS_CERTIFICATE);
1605e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1606003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            String actual = keyStore.getCertificateAlias(getPrivateKey().getCertificate());
1607347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            assertEquals(!isReadOnly(keyStore), expected.contains(actual));
1608003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom            assertNull(keyStore.getCertificateAlias(getPrivateKey2().getCertificate()));
1609e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1610e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1611e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1612e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(File expected, char[] storePassword, KeyStore actual)
1613e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1614e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(actual.getType(), actual.getProvider());
161557f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        InputStream is = new FileInputStream(expected);
161657f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        ks.load(is, storePassword);
161757f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom        is.close();
1618e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEqualsKeyStores(ks, actual);
1619e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1620e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1621e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(KeyStore expected,
1622e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      ByteArrayOutputStream actual, char[] storePassword)
1623e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1624e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        KeyStore ks = KeyStore.getInstance(expected.getType(), expected.getProvider());
1625e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        ks.load(new ByteArrayInputStream(actual.toByteArray()), storePassword);
1626e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEqualsKeyStores(expected, ks);
1627e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1628e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1629e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void assertEqualsKeyStores(KeyStore expected, KeyStore actual)
1630e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            throws Exception{
1631e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        assertEquals(expected.size(), actual.size());
1632e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (String alias : Collections.list(actual.aliases())) {
1633e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (alias.equals(ALIAS_NO_PASSWORD_PRIVATE)
1634e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    || alias.equals(ALIAS_NO_PASSWORD_SECRET)) {
1635e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(expected.getKey(alias, null),
1636e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                             actual.getKey(alias, null));
1637e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1638e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(expected.getKey(alias, PASSWORD_KEY),
1639e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                             actual.getKey(alias, PASSWORD_KEY));
1640e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1641e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(expected.getCertificate(alias), actual.getCertificate(alias));
1642e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1643e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1644e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1645e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_store_OutputStream() throws Exception {
1646e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1647e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1648e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null, null);
16495ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1650e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1651e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1652e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1653e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1654e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1655e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1656e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
16576256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
1658347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1659347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.store(out, null);
16605ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1661347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
1662347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1663347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1664347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1665347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1666e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isNullPasswordAllowed(keyStore)) {
1667e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(out, null);
1668e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(keyStore, out, null);
1669347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1670347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1671347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1672347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            try {
1673347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                keyStore.store(out, null);
16745ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1675347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } catch (Exception e) {
1676347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (e.getClass() != IllegalArgumentException.class
1677347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    && e.getClass() != NullPointerException.class) {
1678347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    throw e;
1679e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1680e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1681e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1682e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1683e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1684e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1685347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
1686e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
16876256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
1688347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1689347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.store(out, null);
16905ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
16916256280922cc8a6622a156afeb7f43a31576d43fKenny Root                } catch (UnsupportedOperationException expected) {
1692347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1693347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isNullPasswordAllowed(keyStore)) {
1694e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(out, null);
1695e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(keyStore, out, null);
1696e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1697e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1698e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.store(out, null);
16995ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1700e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1701e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != IllegalArgumentException.class
1702e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != NullPointerException.class) {
1703e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1704e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1705e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1706e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1707e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1708e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1709e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1710e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1711e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
17126256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
1713347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1714347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.store(out, PASSWORD_STORE);
17155ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1716347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException e) {
1717347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1718347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1719347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1720e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.store(out, PASSWORD_STORE);
1721e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEqualsKeyStores(keyStore, out, PASSWORD_STORE);
1722e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1723e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1724e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1725e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1726e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            ByteArrayOutputStream out = new ByteArrayOutputStream();
17276256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
1728347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
1729347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    keyStore.store(out, PASSWORD_STORE);
17305ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1731347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException e) {
1732347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1733347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
1734347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1735e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.store(out, PASSWORD_STORE);
1736e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEqualsKeyStores(keyStore, out, PASSWORD_STORE);
1737e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1738e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1739e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1740e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_store_LoadStoreParameter() throws Exception {
1741e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1742e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1743e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null);
17445ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1745e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (KeyStoreException expected) {
1746e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1747e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1748e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1749e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1750e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1751e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1752e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.store(null);
17535ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1754e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (UnsupportedOperationException expected) {
175546c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                assertFalse(isLoadStoreParameterSupported(keyStore));
175646c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom            } catch (IllegalArgumentException expected) {
175746c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                // its supported, but null causes an exception
175846c6fad9fad8f3dbbc82516232a225f37d332ca7Brian Carlstrom                assertTrue(isLoadStoreParameterSupported(keyStore));
1759e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1760e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1761e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1762e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1763e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_load_InputStream() throws Exception {
1764e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1765e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
17666256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
17676256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should be able to query size: " + keyStore.getType(),
17686256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() >= 0);
17696256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
17706256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
17716256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1772347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
17736256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
1774347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1775e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1776e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1777e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
17786256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore)) {
17796256280922cc8a6622a156afeb7f43a31576d43fKenny Root                continue;
17806256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
1781e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, PASSWORD_STORE);
17826256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
17836256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should be able to query size: " + keyStore.getType(),
17846256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() >= 0);
17856256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
17866256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
17876256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1788347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
17896256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
1790347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1791e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1792e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1793e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        // test_KeyStore_store_OutputStream effectively tests load as well as store
1794e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1795e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1796e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_load_LoadStoreParameter() throws Exception {
1797e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1798e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null);
17996256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isPersistentStorage(keyStore)) {
18006256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should be able to query size: " + keyStore.getType(),
18016256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() >= 0);
18026256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (hasDefaultContents(keyStore)) {
18036256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertTrue("Should have non-empty store: " + keyStore.getType(),
18046256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        keyStore.size() > 0);
1805347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else {
18066256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertEquals("Should have empty store: " + keyStore.getType(), 0, keyStore.size());
1807347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
1808e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1809e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1810e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1811e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1812e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.load(new LoadStoreParameter() {
1813e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        public ProtectionParameter getProtectionParameter() {
1814e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                            return null;
1815e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        }
1816e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    });
18175ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1818e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (UnsupportedOperationException expected) {
1819e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1820e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1821e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1822e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1823e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_getEntry() throws Exception {
1824e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1825e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1826e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, null);
18275ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1828e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1829e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1830e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1831e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1832e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1833e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
1834e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1835e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1836e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1837e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, null);
18385ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1839e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1840e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1841e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1842e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.getEntry(null, PARAM_KEY);
18435ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1844e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1845e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1846e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getEntry("", null));
1847e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getEntry("", PARAM_KEY));
1848e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1849e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
1850347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1851347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
1852e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
18536256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
18546256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_KEY));
18556256280922cc8a6622a156afeb7f43a31576d43fKenny Root                } else if (isNullPasswordAllowed(keyStore)) {
18566256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
18576256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
1858347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1859347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertSecretKey(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
1860347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } else {
1861347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_KEY));
1862347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1863347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                if (isCertificateEnabled(keyStore)) {
1864347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertCertificate(keyStore.getEntry(ALIAS_CERTIFICATE, null));
1865347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } else {
1866347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    assertNull(keyStore.getEntry(ALIAS_CERTIFICATE, null));
1867347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
1868e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1869e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1870e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
1871347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
1872e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
1873e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
1874e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1875e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_ALT_CASE_PRIVATE, PARAM_KEY));
1876e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
1877e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getEntry(ALIAS_ALT_CASE_SECRET, PARAM_KEY));
1878e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1879e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1880347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isCaseSensitive(keyStore) || isReadOnly(keyStore)) {
1881e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
1882e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
1883e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
1884e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getEntry(ALIAS_ALT_CASE_CERTIFICATE, null));
1885e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1886e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1887e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1888e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null passwords
1889347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1890347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
1891347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isNullPasswordAllowed(keyStore)) {
1892e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_NO_PASSWORD_PRIVATE, null));
18936256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
1894e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, null));
18956256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordIgnored(keyStore)) {
1896e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1897e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_PRIVATE, null);
18985ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1899e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1900e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1901e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
1902e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1903e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1904e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1905e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1906347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1907347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_SECRET, null));
1908347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isSecretKeyEnabled(keyStore)) {
1909e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1910e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_SECRET, null);
19115ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1912e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
1913e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
1914e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class) {
1915e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
1916e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
1917e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1918e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1919e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1920e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with bad passwords
1921347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1922347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
19236256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore) && isKeyPasswordIgnored(keyStore)) {
1924e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD));
19256256280922cc8a6622a156afeb7f43a31576d43fKenny Root            } else if (isKeyPasswordSupported(keyStore)) {
1926e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1927e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_PRIVATE, PARAM_BAD);
19285ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1929e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
1930e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1931e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1932347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
1933347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getEntry(ALIAS_SECRET, PARAM_BAD));
1934347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isSecretKeyEnabled(keyStore)) {
1935e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
1936e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.getEntry(ALIAS_SECRET, PARAM_BAD);
19375ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
1938e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (UnrecoverableKeyException expected) {
1939e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1940e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1941e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1942e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
1943e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1944a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root    public static class FakeProtectionParameter implements ProtectionParameter {
1945a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root    }
1946a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root
1947e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_setEntry() throws Exception {
1948e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1949e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1950e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1951e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, null);
19525ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1953e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1954e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1955e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1956e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1957e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
1958e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
1959e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1960a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            try {
1961a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), new FakeProtectionParameter());
19626256280922cc8a6622a156afeb7f43a31576d43fKenny Root                fail("Should not accept unknown ProtectionParameter: " + keyStore.getProvider());
1963a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            } catch (KeyStoreException expected) {
1964a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            }
1965a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root        }
1966a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root
1967a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root        for (KeyStore keyStore : keyStores()) {
1968a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            keyStore.load(null, null);
1969a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root
1970e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
1971e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1972e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, null);
19735ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1974e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1975e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1976e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1977e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1978e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1979e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1980e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1981e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(null, null, PARAM_KEY);
19825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1983e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (Exception e) {
1984e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (e.getClass() != NullPointerException.class
1985e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    && e.getClass() != KeyStoreException.class) {
1986e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    throw e;
1987e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
1988e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1989e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
1990e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry("", null, PARAM_KEY);
19915ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
1992e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
1993e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
1994e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
1995e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1996e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
19976256280922cc8a6622a156afeb7f43a31576d43fKenny Root            clearKeyStore(keyStore);
1998e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
1999e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
2000e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
2001347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
2002347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                try {
2003003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY);
20045ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
2005347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                } catch (UnsupportedOperationException expected) {
2006347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
2007347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
2008347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
20096256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
20106256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), PARAM_KEY);
20116256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
20126256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_PRIVATE));
20136256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
20146256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
20156256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.setEntry(ALIAS_NO_PASSWORD_PRIVATE, getPrivateKey(), null);
20166256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
20176256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_NO_PASSWORD_PRIVATE));
20186256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
2019e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
2020e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2021003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY);
2022e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2023e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
2024e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
2025003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setKeyEntry(ALIAS_SECRET, getSecretKey(), PASSWORD_KEY, null);
20265ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
2027e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
2028e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2029e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2030e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
2031e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertNull(keyStore.getCertificate(ALIAS_CERTIFICATE));
2032e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.setEntry(ALIAS_CERTIFICATE,
2033003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                  new TrustedCertificateEntry(getPrivateKey().getCertificate()),
2034e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                  null);
2035e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2036e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
2037e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
2038e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
2039003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new TrustedCertificateEntry(getPrivateKey().getCertificate()),
2040e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
20415ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
2042e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (KeyStoreException expected) {
2043e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2044e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
20456256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isKeyPasswordSupported(keyStore)) {
20466256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.setEntry(ALIAS_UNICODE_PRIVATE, getPrivateKey(), PARAM_KEY);
20476256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_PRIVATE, PASSWORD_KEY));
20486256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore.getCertificateChain(ALIAS_UNICODE_PRIVATE));
20496256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
20506256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isNullPasswordAllowed(keyStore)) {
20516256280922cc8a6622a156afeb7f43a31576d43fKenny Root                keyStore.setEntry(ALIAS_UNICODE_NO_PASSWORD_PRIVATE, getPrivateKey(), null);
20526256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertPrivateKey(keyStore.getKey(ALIAS_UNICODE_NO_PASSWORD_PRIVATE, null));
20536256280922cc8a6622a156afeb7f43a31576d43fKenny Root                assertCertificateChain(keyStore
20546256280922cc8a6622a156afeb7f43a31576d43fKenny Root                        .getCertificateChain(ALIAS_UNICODE_NO_PASSWORD_PRIVATE));
20556256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
20563d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root            if (isSecretKeyEnabled(keyStore)) {
20573d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                assertNull(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY));
20583d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                keyStore.setEntry(ALIAS_UNICODE_SECRET, new SecretKeyEntry(getSecretKey()), PARAM_KEY);
20593d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                assertSecretKey(keyStore.getKey(ALIAS_UNICODE_SECRET, PASSWORD_KEY));
20603d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root            } else {
20613d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                try {
20623d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    keyStore.setKeyEntry(ALIAS_UNICODE_SECRET, getSecretKey(), PASSWORD_KEY, null);
20635ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
20643d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                } catch (KeyStoreException expected) {
20653d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                }
20663d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root            }
2067e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2068e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2069e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2070e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
2071e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2072347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
2073347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
2074347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
2075347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2076347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2077347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } else if (isCaseSensitive(keyStore)) {
20786256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isKeyPasswordSupported(keyStore)) {
20796256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
20806256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
20816256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY);
20826256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
20836256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
20846256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
20856256280922cc8a6622a156afeb7f43a31576d43fKenny Root
20866256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isNullPasswordAllowed(keyStore)) {
20876256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
20886256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
20896256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.setEntry(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, getPrivateKey2(), null);
20906256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey(keyStore.getKey(ALIAS_NO_PASSWORD_PRIVATE, null));
20916256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_NO_PASSWORD_PRIVATE, null));
20926256280922cc8a6622a156afeb7f43a31576d43fKenny Root                }
2093e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2094e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
2095e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2096e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2097e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_SECRET,
2098003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new SecretKeyEntry(getSecretKey2()),
2099e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
2100e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2101e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2102e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2103e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2104e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
2105e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2106e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertNull(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
2107e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE,
2108003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new TrustedCertificateEntry(
2109003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                              getPrivateKey2().getCertificate()),
2110e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
2111e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2112e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
21133d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE,
21143d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                      new TrustedCertificateEntry(
21153d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                              getPrivateKey().getCertificate()),
21163d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                      null);
21173d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE));
2118e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2119e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } else {
2120e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
2121e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
2122003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                keyStore.setEntry(ALIAS_ALT_CASE_PRIVATE, getPrivateKey2(), PARAM_KEY);
2123e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_PRIVATE, PASSWORD_KEY));
2124e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertPrivateKey2(keyStore.getKey(ALIAS_ALT_CASE_PRIVATE, PASSWORD_KEY));
2125e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2126e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isSecretKeyEnabled(keyStore)) {
2127e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2128e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2129e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_SECRET,
2130003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new SecretKeyEntry(getSecretKey2()),
2131e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
2132e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_SECRET, PASSWORD_KEY));
2133e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertSecretKey2(keyStore.getKey(ALIAS_ALT_CASE_SECRET, PASSWORD_KEY));
2134e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2135e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2136e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isCertificateEnabled(keyStore)) {
2137e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2138e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
2139e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_ALT_CASE_CERTIFICATE,
2140003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new TrustedCertificateEntry(
2141003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                              getPrivateKey2().getCertificate()),
2142e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      null);
2143e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_CERTIFICATE));
2144e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate2(keyStore.getCertificate(ALIAS_ALT_CASE_CERTIFICATE));
21453d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    keyStore.setEntry(ALIAS_UNICODE_CERTIFICATE,
21463d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                      new TrustedCertificateEntry(
21473d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                              getPrivateKey().getCertificate()),
21483d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                                      null);
21493d91ee9d8d6b757ff2bcb1dcbf30caa10ff6bc31Kenny Root                    assertCertificate(keyStore.getCertificate(ALIAS_UNICODE_CERTIFICATE));
2150e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2151e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2152e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2153e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2154e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2155e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2156e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2157e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test with null/non-null passwords
21585ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            if (isReadOnly(keyStore)) {
21595ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                try {
21605ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null);
21615ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
21625ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                } catch (UnsupportedOperationException expected) {
2163e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2164e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                try {
2165003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                    keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
21665ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
21675ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                } catch (UnsupportedOperationException expected) {
21685ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                }
21695ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                try {
21705ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
21715ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                                      new TrustedCertificateEntry(getPrivateKey().getCertificate()),
21725ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                                      null);
21735ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
21745ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                } catch (UnsupportedOperationException expected) {
21755ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                }
21765ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                continue;
21775ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            }
21785ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
21795ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null);
21805ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                assertPrivateKey(keyStore.getKey(ALIAS_PRIVATE, null));
21815ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            } else {
21825ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                try {
21835ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    keyStore.setEntry(ALIAS_PRIVATE, getPrivateKey(), null);
21845ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    fail(keyStore.getType());
2185e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } catch (Exception e) {
2186e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    if (e.getClass() != UnrecoverableKeyException.class
2187e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != IllegalArgumentException.class
2188e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        && e.getClass() != KeyStoreException.class) {
2189e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        throw e;
2190e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
2191e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2192e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
21935ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom            if (isSecretKeyEnabled(keyStore)) {
21945ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
21955ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
21965ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    assertSecretKey(keyStore.getKey(ALIAS_SECRET, null));
21975ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                } else {
21985ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    try {
21995ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        keyStore.setEntry(ALIAS_SECRET, new SecretKeyEntry(getSecretKey()), null);
22005ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
22015ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    } catch (Exception e) {
22025ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        if (e.getClass() != UnrecoverableKeyException.class
22035ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                            && e.getClass() != IllegalArgumentException.class
22045ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                            && e.getClass() != KeyStoreException.class) {
22055ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                            throw e;
22065ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        }
22075ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                    }
2208347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
2209347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
2210e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            if (isCertificateEnabled(keyStore)) {
2211e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                if (isNullPasswordAllowed(keyStore) || isKeyPasswordIgnored(keyStore)) {
2212e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    keyStore.setEntry(ALIAS_CERTIFICATE,
2213003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                      new TrustedCertificateEntry(getPrivateKey().getCertificate()),
2214e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                      PARAM_KEY);
2215e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    assertCertificate(keyStore.getCertificate(ALIAS_CERTIFICATE));
2216e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                } else {
2217e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    try {
2218e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                        keyStore.setEntry(ALIAS_CERTIFICATE,
2219003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                          new TrustedCertificateEntry(
2220003f7a4d100cd1527d94bac81a4a3c5a8216c6eeBrian Carlstrom                                                  getPrivateKey().getCertificate()),
2221e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                          PARAM_KEY);
22225ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
2223e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    } catch (KeyStoreException expected) {
2224e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                    }
2225e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                }
2226e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2227e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2228e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
2229e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2230e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_entryInstanceOf() throws Exception {
2231e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2232e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2233e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, null);
22345ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2235e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2236e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2237e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2238e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2239e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2240e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2241e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2242e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2243e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, null);
22445ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2245e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2246e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2247e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2248e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf(null, Entry.class);
22495ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2250e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2251e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2252e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2253e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                keyStore.entryInstanceOf("", null);
22545ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2255e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2256e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2257e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2258e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", Entry.class));
2259e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2260e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2261e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2262e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
2263e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2264e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test odd inputs
2265e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", Entry.class));
2266e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", PrivateKeyEntry.class));
2267e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", SecretKeyEntry.class));
2268e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf("", TrustedCertificateEntry.class));
2269e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2270347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            if (isReadOnly(keyStore)) {
2271347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class));
2272347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class));
2273347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class));
2274347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2275347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class));
2276347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class));
2277347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class));
2278347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2279347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE,
2280347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                                                     TrustedCertificateEntry.class));
2281347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class));
2282347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class));
2283347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                continue;
2284347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            }
2285347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2286e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case sensitive
22876256280922cc8a6622a156afeb7f43a31576d43fKenny Root            assertEquals(isKeyPasswordSupported(keyStore),
22886256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.entryInstanceOf(ALIAS_PRIVATE, PrivateKeyEntry.class));
2289e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, SecretKeyEntry.class));
2290e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_PRIVATE, TrustedCertificateEntry.class));
2291e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
22926256280922cc8a6622a156afeb7f43a31576d43fKenny Root            assertEquals(isNullPasswordAllowed(keyStore),
22936256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE, PrivateKeyEntry.class));
22946256280922cc8a6622a156afeb7f43a31576d43fKenny Root            assertFalse(keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE, SecretKeyEntry.class));
22956256280922cc8a6622a156afeb7f43a31576d43fKenny Root            assertFalse(keyStore.entryInstanceOf(ALIAS_NO_PASSWORD_PRIVATE,
22966256280922cc8a6622a156afeb7f43a31576d43fKenny Root                    TrustedCertificateEntry.class));
22976256280922cc8a6622a156afeb7f43a31576d43fKenny Root
2298e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isSecretKeyEnabled(keyStore),
2299e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_SECRET, SecretKeyEntry.class));
2300e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, PrivateKeyEntry.class));
2301e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_SECRET, TrustedCertificateEntry.class));
2302e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2303e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(isCertificateEnabled(keyStore),
2304e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_CERTIFICATE,
2305e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  TrustedCertificateEntry.class));
2306e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, PrivateKeyEntry.class));
2307e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_CERTIFICATE, SecretKeyEntry.class));
2308e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2309e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            // test case insensitive
2310e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore),
2311e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, PrivateKeyEntry.class));
2312e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE, SecretKeyEntry.class));
2313e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_PRIVATE,
2314e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 TrustedCertificateEntry.class));
2315e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2316e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isSecretKeyEnabled(keyStore),
2317e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, SecretKeyEntry.class));
2318e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET, PrivateKeyEntry.class));
2319e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_SECRET,
2320e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 TrustedCertificateEntry.class));
2321e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2322e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(!isCaseSensitive(keyStore) && isCertificateEnabled(keyStore),
2323e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                         keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE,
2324e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  TrustedCertificateEntry.class));
2325e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE,
2326e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                 PrivateKeyEntry.class));
2327e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertFalse(keyStore.entryInstanceOf(ALIAS_ALT_CASE_CERTIFICATE, SecretKeyEntry.class));
2328e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2329e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
2330e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2331e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    public void test_KeyStore_Builder() throws Exception {
2332e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2333e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2334e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2335e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore, null);
23365ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2337e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2338e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2339e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2340e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2341e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2342e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2343e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore.getType(),
2344e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    keyStore.getProvider(),
2345e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
23465ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2347e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2348e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2349e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2350e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2351e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2352e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2353e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(null,
2354e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
2355e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
2356e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
23575ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2358e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2359e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2360e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2361e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder.newInstance(keyStore.getType(),
2362e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    keyStore.getProvider(),
2363e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null,
2364e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                    null);
23655ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2366e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2367e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2368e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2369e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2370e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2371e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            keyStore.load(null, null);
2372e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Builder builder = Builder.newInstance(keyStore, PARAM_STORE);
2373e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2374e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                builder.getProtectionParameter(null);
23755ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2376e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2377e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2378e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore, builder.getKeyStore());
2379e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2380e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                builder.getProtectionParameter(null);
23815ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                fail(keyStore.getType());
2382e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } catch (NullPointerException expected) {
2383e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2384e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
2385e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2386e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2387e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
2388e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            populate(keyStore);
2389347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2390e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            File file = File.createTempFile("keystore", keyStore.getProvider().getName());
2391347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            OutputStream os = null;
2392e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            try {
2393347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                os = new FileOutputStream(file);
23946256280922cc8a6622a156afeb7f43a31576d43fKenny Root                if (isLoadStoreUnsupported(keyStore) || isReadOnly(keyStore)) {
2395347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    try {
2396347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                        keyStore.store(os, PASSWORD_STORE);
23975ab96b6746a5a8f9f4e3902379c0b6f062c0d2e0Brian Carlstrom                        fail(keyStore.getType());
2398347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    } catch (UnsupportedOperationException expected) {
2399347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    }
2400347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                    continue;
2401347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                }
2402347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
240357f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                keyStore.store(os, PASSWORD_STORE);
240457f2cc03ff2cf5d2f6413c5410680b4908d7301dBrian Carlstrom                os.close();
2405e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                Builder builder = Builder.newInstance(keyStore.getType(),
2406e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      keyStore.getProvider(),
2407e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      file,
2408e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                      PARAM_STORE);
2409e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(keyStore.getType(), builder.getKeyStore().getType());
2410e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
2411e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
2412e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                assertEqualsKeyStores(file, PASSWORD_STORE, keyStore);
2413e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            } finally {
2414a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                try {
2415a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                    if (os != null) {
2416a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                        os.close();
2417a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                    }
2418a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                } catch (IOException ignored) {
2419a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root                }
2420e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                file.delete();
2421e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            }
2422e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2423e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2424e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (KeyStore keyStore : keyStores()) {
24256256280922cc8a6622a156afeb7f43a31576d43fKenny Root            if (isLoadStoreUnsupported(keyStore)) {
24266256280922cc8a6622a156afeb7f43a31576d43fKenny Root                continue;
24276256280922cc8a6622a156afeb7f43a31576d43fKenny Root            }
2428e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            Builder builder = Builder.newInstance(keyStore.getType(),
2429e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  keyStore.getProvider(),
2430e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom                                                  PARAM_STORE);
2431e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore.getType(), builder.getKeyStore().getType());
2432e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(keyStore.getProvider(), builder.getKeyStore().getProvider());
2433e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            assertEquals(PARAM_STORE, builder.getProtectionParameter(""));
2434e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2435e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
2436e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom
2437347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom    public void test_KeyStore_cacerts() throws Exception {
2438e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        if (StandardNames.IS_RI) {
2439e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom            return;
2440e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2441347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        KeyStore ks = KeyStore.getInstance("AndroidCAStore");
2442347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        assertEquals("AndroidCAStore", ks.getType());
2443347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        assertEquals("HarmonyJSSE", ks.getProvider().getName());
2444347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2445347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom        ks.load(null, null);
2446e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        for (String alias : Collections.list(ks.aliases())) {
2447347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            Certificate c = null;
2448cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom            try {
2449347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                c = ks.getCertificate(alias);
2450347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNotNull(c);
2451347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertTrue(ks.isCertificateEntry(alias));
2452347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertTrue(ks.entryInstanceOf(alias, TrustedCertificateEntry.class));
2453347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertEquals(alias, ks.getCertificateAlias(c));
2454347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2455cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertTrue(c instanceof X509Certificate);
2456cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                X509Certificate cert = (X509Certificate) c;
2457cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertEquals(cert.getSubjectUniqueID(), cert.getIssuerUniqueID());
2458cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom                assertNotNull(cert.getPublicKey());
2459347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2460347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertTrue(ks.containsAlias(alias));
2461347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNotNull(ks.getCreationDate(alias));
2462347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNotNull(ks.getEntry(alias, null));
2463347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2464347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertFalse(ks.isKeyEntry(alias));
2465347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(ks.getKey(alias, null));
2466347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                assertNull(ks.getCertificateChain(alias));
2467347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom
2468347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom            } catch (Throwable t) {
2469347b2a604114602da9bc4ae040278f74d11c2f51Brian Carlstrom                throw new Exception("alias=" + alias + " cert=" + c, t);
2470cc555b2c2df6d1dec46a6c7a1e42e4db741b6c49Brian Carlstrom            }
2471e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom        }
2472e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom    }
24730647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson
24740647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson    // http://b/857840: want JKS key store
24750647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson    public void testDefaultKeystore() {
24760647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        String type = KeyStore.getDefaultType();
2477a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root        assertEquals(StandardNames.KEY_STORE_ALGORITHM, type);
24780647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson
24790647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        try {
24800647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            KeyStore store = KeyStore.getInstance(KeyStore.getDefaultType());
24810647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            assertNotNull("Keystore must not be null", store);
24820647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        } catch (Exception ex) {
24830647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            throw new RuntimeException(ex);
24840647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        }
24850647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson
24860647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        try {
2487a3cb41e5a975dfe5ede995e8fbe6ff86ae5f6615Kenny Root            KeyStore store = KeyStore.getInstance(StandardNames.KEY_STORE_ALGORITHM);
24880647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            assertNotNull("Keystore must not be null", store);
24890647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        } catch (Exception ex) {
24900647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson            throw new RuntimeException(ex);
24910647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson        }
24920647bfed6eda99ad77b2dfe8e3696e3fabfaf3ccJesse Wilson    }
2493e3a187163504f00c98bd75cbd8bcbdde123ae2cdBrian Carlstrom}
2494