KeyStore3Test.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.security.tests.java.security; 19 20import java.io.ByteArrayInputStream; 21import java.io.FileNotFoundException; 22import java.io.IOException; 23import java.io.InputStream; 24import java.io.OutputStream; 25import java.io.UnsupportedEncodingException; 26import java.security.Key; 27import java.security.KeyPair; 28import java.security.KeyPairGenerator; 29import java.security.KeyStore; 30import java.security.KeyStoreException; 31import java.security.KeyStoreSpi; 32import java.security.NoSuchAlgorithmException; 33import java.security.Provider; 34import java.security.UnrecoverableKeyException; 35import java.security.KeyStore.Builder; 36import java.security.KeyStore.PasswordProtection; 37import java.security.KeyStore.ProtectionParameter; 38import java.security.cert.Certificate; 39import java.security.cert.CertificateException; 40import java.security.cert.CertificateFactory; 41import java.util.Date; 42import java.util.Enumeration; 43 44import junit.framework.TestCase; 45 46public class KeyStore3Test extends TestCase { 47 48 private KeyStore mockKeyStore; 49 50 private KeyPair keyPair; 51 52 private Certificate certificate; 53 54 public KeyStore3Test() throws Exception { 55 KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA"); 56 keyPair = keyPairGenerator.generateKeyPair(); 57 58 String certificateData = "-----BEGIN CERTIFICATE-----\n" 59 + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n" 60 + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n" 61 + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n" 62 + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n" 63 + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n" 64 + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n" 65 + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n" 66 + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n" 67 + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n" 68 + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n" 69 + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n" 70 + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n" 71 + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n" 72 + "-----END CERTIFICATE-----\n"; 73 74 ByteArrayInputStream certArray; 75 { 76 try { 77 certArray = new ByteArrayInputStream(certificateData.getBytes("UTF-8")); 78 } catch (UnsupportedEncodingException e) { 79 throw new RuntimeException(e.getMessage()); 80 } 81 } 82 CertificateFactory cf = CertificateFactory.getInstance("X.509"); 83 certificate = cf.generateCertificate(certArray); 84 } 85 86 public void test_load() throws Exception { 87 // No exception should be thrown out. 88 mockKeyStore.load(null); 89 } 90 91 public void test_store() throws Exception { 92 try { 93 mockKeyStore.store(null); 94 fail("should throw KeyStoreException: not initialized"); 95 } catch (KeyStoreException e) { 96 // expected 97 } 98 99 // No exception should be thrown out. 100 mockKeyStore.load(null, null); 101 mockKeyStore.store(null); 102 } 103 104 public void test_setKeyEntry_null() throws Exception { 105 mockKeyStore.load(null, null); 106 // No exception should be thrown out. 107 mockKeyStore.setKeyEntry(null, null, null, null); 108 } 109 110 public void test_setKeyEntry_key_is_null() throws Exception { 111 mockKeyStore.load(null, null); 112 // No exception should be thrown out. 113 mockKeyStore.setKeyEntry("Alias", null, null, new Certificate[]{certificate}); 114 } 115 116 public void test_setKeyEntry_key_is_private() throws Exception { 117 mockKeyStore.load(null, null); 118 Key key = keyPair.getPrivate(); 119 try { 120 mockKeyStore.setKeyEntry("Alias", key, null, null); 121 fail("should throw IllegalArgumentException"); 122 } catch (IllegalArgumentException e) { 123 // expected 124 } 125 126 try { 127 mockKeyStore.setKeyEntry("Alias", key, null, 128 new Certificate[0]); 129 fail("should throw IllegalArgumentException"); 130 } catch (IllegalArgumentException e) { 131 // expected 132 } 133 134 mockKeyStore.setKeyEntry("Alias", key, null, new Certificate[]{certificate}); 135 } 136 137 public void test_setKeyEntry_key_is_public() throws Exception 138 { 139 mockKeyStore.load(null, null); 140 Key key = keyPair.getPublic(); 141 mockKeyStore.setKeyEntry("Alias1", key, null, null); 142 mockKeyStore.setKeyEntry("Alias2", key, null, 143 new Certificate[0]); 144 mockKeyStore.setKeyEntry("Alias3", key, null, new Certificate[]{certificate}); 145 } 146 147 public void test_setCertificateEntry_null() throws Exception { 148 mockKeyStore.load(null, null); 149 150 mockKeyStore.setCertificateEntry(null, null); 151 152 mockKeyStore.setCertificateEntry(null, certificate); 153 154 mockKeyStore.setCertificateEntry("Alias", null); 155 } 156 157 public void test_store_null() throws Exception { 158 mockKeyStore.load(null, null); 159 mockKeyStore.store(null, null); 160 } 161 162 public void test_getKeyStore() throws KeyStoreException, 163 NoSuchAlgorithmException, CertificateException, 164 FileNotFoundException, IOException { 165 166 String alias = "BKS"; 167 char[] pwd = new char[] { '1', '2', '3', '4', '5', '6' }; 168 InputStream fis = KeyStore2Test.class 169 .getResourceAsStream("builderimpl.ks"); 170 KeyStore ks = KeyStore.getInstance(alias); 171 ks.load(fis, pwd); 172 Builder b = Builder.newInstance(ks, new PasswordProtection(pwd)); 173 KeyStore firstKeyStore = b.getKeyStore(); 174 ProtectionParameter firstProtParameter = b 175 .getProtectionParameter(alias); 176 assertSame(firstKeyStore, b.getKeyStore()); 177 assertSame(firstProtParameter, b.getProtectionParameter(alias)); 178 179 b = Builder.newInstance(alias, ks.getProvider(), 180 new KeyStore.PasswordProtection(pwd)); 181 firstKeyStore = b.getKeyStore(); 182 firstProtParameter = b.getProtectionParameter(alias); 183 assertNotSame(firstKeyStore, b.getKeyStore()); 184 assertSame(firstProtParameter, b.getProtectionParameter(alias)); 185 } 186 187 protected void setUp() throws Exception { 188 super.setUp(); 189 mockKeyStore = new MyKeyStore(new MyKeyStoreSpi(), null, "MyKeyStore"); 190 } 191 192 private static class MyKeyStore extends KeyStore { 193 194 public MyKeyStore(KeyStoreSpi keyStoreSpi, Provider provider, 195 String type) { 196 super(keyStoreSpi, provider, type); 197 } 198 } 199 200 private static class MyKeyStoreSpi extends KeyStoreSpi { 201 202 public Enumeration<String> engineAliases() { 203 return null; 204 } 205 206 public boolean engineContainsAlias(String arg0) { 207 return false; 208 } 209 210 public void engineDeleteEntry(String arg0) throws KeyStoreException { 211 } 212 213 public Certificate engineGetCertificate(String arg0) { 214 return null; 215 } 216 217 public String engineGetCertificateAlias(Certificate arg0) { 218 return null; 219 } 220 221 public Certificate[] engineGetCertificateChain(String arg0) { 222 return null; 223 } 224 225 public Date engineGetCreationDate(String arg0) { 226 return null; 227 } 228 229 public Key engineGetKey(String arg0, char[] arg1) 230 throws NoSuchAlgorithmException, UnrecoverableKeyException { 231 return null; 232 } 233 234 public boolean engineIsCertificateEntry(String arg0) { 235 return false; 236 } 237 238 public boolean engineIsKeyEntry(String arg0) { 239 return false; 240 } 241 242 public void engineLoad(InputStream arg0, char[] arg1) 243 throws IOException, NoSuchAlgorithmException, 244 CertificateException { 245 return; 246 } 247 248 public void engineSetCertificateEntry(String arg0, Certificate arg1) 249 throws KeyStoreException { 250 return; 251 } 252 253 public void engineSetKeyEntry(String arg0, byte[] arg1, 254 Certificate[] arg2) throws KeyStoreException { 255 return; 256 } 257 258 public void engineSetKeyEntry(String arg0, Key arg1, char[] arg2, 259 Certificate[] arg3) throws KeyStoreException { 260 return; 261 } 262 263 public int engineSize() { 264 return 0; 265 } 266 267 public void engineStore(KeyStore.LoadStoreParameter param){ 268 return; 269 } 270 271 public void engineStore(OutputStream arg0, char[] arg1) 272 throws IOException, NoSuchAlgorithmException, 273 CertificateException { 274 return; 275 } 276 } 277 278} 279 280