001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.security.tests.java.security;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023: import java.security.KeyPair;
024: import java.security.KeyPairGenerator;
025: import java.security.KeyStore;
026: import java.security.PrivateKey;
027: import java.security.Provider;
028: import java.security.PublicKey;
029: import java.security.SecureRandom;
030: import java.security.Security;
031: import java.security.UnrecoverableKeyException;
032: import java.security.cert.CertificateFactory;
033: import java.security.cert.X509Certificate;
034: import java.util.Arrays;
035: import java.util.Enumeration;
036: import java.util.HashSet;
037: import java.util.Set;
038:
039: import tests.support.Support_TestProvider;
040: import tests.support.resource.Support_Resources;
041:
042: public class KeyStore2Test extends junit.framework.TestCase {
043: static PrivateKey privateKey;
044: static {
045: try {
046: KeyPairGenerator keyPairGenerator = KeyPairGenerator
047: .getInstance("DSA");
048:
049: SecureRandom secureRandom = new SecureRandom();
050: keyPairGenerator.initialize(1024, secureRandom);
051: KeyPair keyPair = keyPairGenerator.genKeyPair();
052: privateKey = keyPair.getPrivate();
053: } catch (Exception e) {
054: fail("initialization failed: " + e);
055: }
056: }
057:
058: final char[] pssWord = { 'a', 'b', 'c' };
059: private Provider support_TestProvider;
060:
061: // creating a certificate
062: String certificate = "-----BEGIN CERTIFICATE-----\n"
063: + "MIICZTCCAdICBQL3AAC2MA0GCSqGSIb3DQEBAgUAMF8xCzAJBgNVBAYTAlVTMSAw\n"
064: + "HgYDVQQKExdSU0EgRGF0YSBTZWN1cml0eSwgSW5jLjEuMCwGA1UECxMlU2VjdXJl\n"
065: + "IFNlcnZlciBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eTAeFw05NzAyMjAwMDAwMDBa\n"
066: + "Fw05ODAyMjAyMzU5NTlaMIGWMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZv\n"
067: + "cm5pYTESMBAGA1UEBxMJUGFsbyBBbHRvMR8wHQYDVQQKExZTdW4gTWljcm9zeXN0\n"
068: + "ZW1zLCBJbmMuMSEwHwYDVQQLExhUZXN0IGFuZCBFdmFsdWF0aW9uIE9ubHkxGjAY\n"
069: + "BgNVBAMTEWFyZ29uLmVuZy5zdW4uY29tMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCB\n"
070: + "iQKBgQCofmdY+PiUWN01FOzEewf+GaG+lFf132UpzATmYJkA4AEA/juW7jSi+LJk\n"
071: + "wJKi5GO4RyZoyimAL/5yIWDV6l1KlvxyKslr0REhMBaD/3Z3EsLTTEf5gVrQS6sT\n"
072: + "WMoSZAyzB39kFfsB6oUXNtV8+UKKxSxKbxvhQn267PeCz5VX2QIDAQABMA0GCSqG\n"
073: + "SIb3DQEBAgUAA34AXl3at6luiV/7I9MN5CXYoPJYI8Bcdc1hBagJvTMcmlqL2uOZ\n"
074: + "H9T5hNMEL9Tk6aI7yZPXcw/xI2K6pOR/FrMp0UwJmdxX7ljV6ZtUZf7pY492UqwC\n"
075: + "1777XQ9UEZyrKJvF5ntleeO0ayBqLGVKCWzWZX9YsXCpv47FNLZbupE=\n"
076: + "-----END CERTIFICATE-----\n";
077:
078: ByteArrayInputStream certArray = new ByteArrayInputStream(
079: certificate.getBytes());
080:
081: String certificate2 = "-----BEGIN CERTIFICATE-----\n"
082: + "MIICZzCCAdCgAwIBAgIBGzANBgkqhkiG9w0BAQUFADBhMQswCQYDVQQGEwJVUzEY\n"
083: + "MBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsT\n"
084: + "A1BLSTEcMBoGA1UEAxMTRG9EIFBLSSBNZWQgUm9vdCBDQTAeFw05ODA4MDMyMjAy\n"
085: + "MjlaFw0wODA4MDQyMjAyMjlaMGExCzAJBgNVBAYTAlVTMRgwFgYDVQQKEw9VLlMu\n"
086: + "IEdvdmVybm1lbnQxDDAKBgNVBAsTA0RvRDEMMAoGA1UECxMDUEtJMRwwGgYDVQQD\n"
087: + "ExNEb0QgUEtJIE1lZCBSb290IENBMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKB\n"
088: + "gQDbrM/J9FrJSX+zxFUbsI9Vw5QbguVBIa95rwW/0M8+sM0r5gd+DY6iubm6wnXk\n"
089: + "CSvbfQlFEDSKr4WYeeGp+d9WlDnQdtDFLdA45tCi5SHjnW+hGAmZnld0rz6wQekF\n"
090: + "5xQaa5A6wjhMlLOjbh27zyscrorMJ1O5FBOWnEHcRv6xqQIDAQABoy8wLTAdBgNV\n"
091: + "HQ4EFgQUVrmYR6m9701cHQ3r5kXyG7zsCN0wDAYDVR0TBAUwAwEB/zANBgkqhkiG\n"
092: + "9w0BAQUFAAOBgQDVX1Y0YqC7vekeZjVxtyuC8Mnxbrz6D109AX07LEIRzNYzwZ0w\n"
093: + "MTImSp9sEzWW+3FueBIU7AxGys2O7X0qmN3zgszPfSiocBuQuXIYQctJhKjF5KVc\n"
094: + "VGQRYYlt+myhl2vy6yPzEVCjiKwMEb1Spu0irCf+lFW2hsdjvmSQMtZvOw==\n"
095: + "-----END CERTIFICATE-----\n";
096:
097: ByteArrayInputStream certArray2 = new ByteArrayInputStream(
098: certificate2.getBytes());
099:
100: String certificate3 = "-----BEGIN CERTIFICATE-----\n"
101: + "MIIDXDCCAsWgAwIBAgIBSjANBgkqhkiG9w0BAQUFADBWMQswCQYDVQQGEwJVUzEY\n"
102: + "MBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsT\n"
103: + "A1BLSTERMA8GA1UEAxMITWVkIENBLTEwHhcNOTgwODAyMTgwMjQwWhcNMDEwODAy\n"
104: + "MTgwMjQwWjB0MQswCQYDVQQGEwJVUzEYMBYGA1UEChMPVS5TLiBHb3Zlcm5tZW50\n"
105: + "MQwwCgYDVQQLEwNEb0QxDDAKBgNVBAsTA1BLSTENMAsGA1UECxMEVVNBRjEgMB4G\n"
106: + "A1UEAxMXR3VtYnkuSm9zZXBoLjAwMDAwMDUwNDQwgZ8wDQYJKoZIhvcNAQEBBQAD\n"
107: + "gY0AMIGJAoGBALT/R7bPqs1c1YqXAg5HNpZLgW2HuAc7RCaP06cE4R44GBLw/fQc\n"
108: + "VRNLn5pgbTXsDnjiZVd8qEgYqjKFQka4/tNhaF7No2tBZB+oYL/eP0IWtP+h/W6D\n"
109: + "KR5+UvIIdgmx7k3t9jp2Q51JpHhhKEb9WN54trCO9Yu7PYU+LI85jEIBAgMBAAGj\n"
110: + "ggEaMIIBFjAWBgNVHSAEDzANMAsGCWCGSAFlAgELAzAfBgNVHSMEGDAWgBQzOhTo\n"
111: + "CWdhiGUkIOx5cELXppMe9jAdBgNVHQ4EFgQUkLBJl+ayKgzOp/wwBX9M1lSkCg4w\n"
112: + "DgYDVR0PAQH/BAQDAgbAMAwGA1UdEwEB/wQCMAAwgZ0GA1UdHwSBlTCBkjCBj6CB\n"
113: + "jKCBiYaBhmxkYXA6Ly9kcy0xLmNoYW1iLmRpc2EubWlsL2NuJTNkTWVkJTIwQ0El\n"
114: + "MmQxJTJjb3UlM2RQS0klMmNvdSUzZERvRCUyY28lM2RVLlMuJTIwR292ZXJubWVu\n"
115: + "dCUyY2MlM2RVUz9jZXJ0aWZpY2F0ZVJldm9jYXRpb25MaXN0JTNiYmluYXJ5MA0G\n"
116: + "CSqGSIb3DQEBBQUAA4GBAFjapuDHMvIdUeYRyEYdShBR1JZC20tJ3MQnyBQveddz\n"
117: + "LGFDGpIkRAQU7T/5/ne8lMexyxViC21xOlK9LdbJCbVyywvb9uEm/1je9wieQQtr\n"
118: + "kjykuB+WB6qTCIslAO/eUmgzfzIENvnH8O+fH7QTr2PdkFkiPIqBJYHvw7F3XDqy\n"
119: + "-----END CERTIFICATE-----\n";
120:
121: ByteArrayInputStream certArray3 = new ByteArrayInputStream(
122: certificate3.getBytes());
123:
124: private byte[] creatCertificate() throws Exception {
125: ByteArrayOutputStream out = null;
126:
127: CertificateFactory cf = CertificateFactory.getInstance("X.509");
128: X509Certificate cert[] = new X509Certificate[2];
129: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
130: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
131: KeyStore keyTest = KeyStore.getInstance(KeyStore
132: .getDefaultType());
133: keyTest.load(null, null);
134: // alias 1
135: keyTest.setCertificateEntry("alias1", cert[0]);
136:
137: // alias 2
138: keyTest.setKeyEntry("alias2", privateKey, pssWord, cert);
139:
140: // alias 3
141: keyTest.setCertificateEntry("alias3", cert[1]);
142:
143: out = new ByteArrayOutputStream();
144: keyTest.store(out, pssWord);
145: out.close();
146:
147: return out.toByteArray();
148: }
149:
150: /**
151: * @tests java.security.KeyStore#aliases()
152: */
153: public void test_aliases() throws Exception {
154: // Test for method java.util.Enumeration
155: // java.security.KeyStore.aliases()
156: // NOT COMPATIBLE WITH PCS#12
157: CertificateFactory cf = CertificateFactory.getInstance("X.509");
158: X509Certificate cert[] = new X509Certificate[2];
159: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
160: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
161: KeyStore keyTest = KeyStore.getInstance(KeyStore
162: .getDefaultType());
163: keyTest.load(null, null);
164:
165: // KeyStore keyTest =
166: // KeyStore.getInstance(KeyStore.getDefaultType());
167: // alias 1
168: keyTest.setCertificateEntry("alias1", cert[0]);
169:
170: // alias 2
171: keyTest.setCertificateEntry("alias2", cert[0]);
172:
173: // alias 3
174: keyTest.setCertificateEntry("alias3", cert[0]);
175:
176: // obtaining the aliase
177: Enumeration aliase = keyTest.aliases();
178: Set alia = new HashSet();
179: int i = 0;
180: while (aliase.hasMoreElements()) {
181: alia.add(aliase.nextElement());
182: i++;
183: }
184: assertTrue("the alias names were returned wrong", i == 3
185: && alia.contains("alias1") && alia.contains("alias2")
186: && alia.contains("alias3"));
187: }
188:
189: /**
190: * @tests java.security.KeyStore#containsAlias(java.lang.String)
191: */
192: public void test_containsAliasLjava_lang_String() throws Exception {
193: // Test for method boolean
194: // java.security.KeyStore.containsAlias(java.lang.String)
195: CertificateFactory cf = CertificateFactory.getInstance("X.509");
196: X509Certificate cert[] = new X509Certificate[2];
197: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
198: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
199: KeyStore keyTest = KeyStore.getInstance(KeyStore
200: .getDefaultType());
201: keyTest.load(null, null);
202:
203: // alias 1
204: keyTest.setCertificateEntry("alias1", cert[0]);
205:
206: // alias 2
207: keyTest.setCertificateEntry("alias2", cert[0]);
208:
209: assertTrue("alias1 does not exist", keyTest
210: .containsAlias("alias1"));
211: assertTrue("alias2 does not exist", keyTest
212: .containsAlias("alias2"));
213: assertFalse("alias3 exists", keyTest.containsAlias("alias3"));
214: }
215:
216: /**
217: * @tests java.security.KeyStore#getCertificate(java.lang.String)
218: */
219: public void test_getCertificateLjava_lang_String() throws Exception {
220: // Test for method java.security.cert.Certificate
221: // java.security.KeyStore.getCertificate(java.lang.String)
222: CertificateFactory cf = CertificateFactory.getInstance("X.509");
223: X509Certificate cert[] = new X509Certificate[2];
224: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
225: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
226: KeyStore keyTest = KeyStore.getInstance(KeyStore
227: .getDefaultType());
228: keyTest.load(null, null);
229:
230: // alias 1
231: PublicKey pub = cert[0].getPublicKey();
232: keyTest.setCertificateEntry("alias1", cert[0]);
233:
234: java.security.cert.Certificate certRes = keyTest
235: .getCertificate("alias1");
236: assertTrue(
237: "the public key of the certificate from getCertificate() "
238: + "did not equal the original certificate",
239: certRes.getPublicKey() == pub);
240:
241: // alias 2
242: keyTest.setCertificateEntry("alias2", cert[0]);
243:
244: // testing for a certificate chain
245: java.security.cert.Certificate cert2 = keyTest
246: .getCertificate("alias2");
247: assertTrue("the certificate for alias2 is supposed to exist",
248: cert2 != null && cert2.equals(cert[0]));
249:
250: }
251:
252: /**
253: * @tests java.security.KeyStore#getCertificateAlias(java.security.cert.Certificate)
254: */
255: public void test_getCertificateAliasLjava_security_cert_Certificate()
256: throws Exception {
257: // Test for method java.lang.String
258: // java.security.KeyStore.getCertificateAlias(java.security.cert.Certificate)
259: CertificateFactory cf = CertificateFactory.getInstance("X.509");
260: X509Certificate cert[] = new X509Certificate[2];
261: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
262: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
263: KeyStore keyTest = KeyStore.getInstance(KeyStore
264: .getDefaultType());
265: keyTest.load(null, null);
266:
267: // certificate entry
268: keyTest.setCertificateEntry("alias1", cert[1]);
269: String alias = keyTest.getCertificateAlias(cert[1]);
270: assertTrue("certificate entry - the alias returned for this "
271: + "certificate was wrong", alias.equals("alias1"));
272:
273: // key entry
274:
275: keyTest.setKeyEntry("alias2", privateKey, pssWord, cert);
276: alias = keyTest.getCertificateAlias(cert[0]);
277: assertTrue("key entry - the alias returned for this "
278: + "certificate was wrong", alias.equals("alias2"));
279:
280: // testing case with a nonexistent certificate
281: X509Certificate cert2 = (X509Certificate) cf
282: .generateCertificate(certArray3);
283: String aliasNull = keyTest.getCertificateAlias(cert2);
284: assertNull("the alias returned for the nonexist certificate "
285: + "was NOT null", aliasNull);
286: }
287:
288: /**
289: * @tests java.security.KeyStore#getCertificateChain(java.lang.String)
290: */
291: public void test_getCertificateChainLjava_lang_String()
292: throws Exception {
293: // Test for method java.security.cert.Certificate []
294: // java.security.KeyStore.getCertificateChain(java.lang.String)
295: // creatCertificate();
296: CertificateFactory cf = CertificateFactory.getInstance("X.509");
297: X509Certificate cert[] = new X509Certificate[2];
298: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
299: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
300: KeyStore keyTest = KeyStore.getInstance(KeyStore
301: .getDefaultType());
302: keyTest.load(null, null);
303:
304: // alias 1
305: keyTest.setCertificateEntry("alias1", cert[0]);
306:
307: // alias 2
308: keyTest.setKeyEntry("alias2", privateKey, pssWord, cert);
309:
310: java.security.cert.Certificate[] certRes = keyTest
311: .getCertificateChain("alias2");
312: assertTrue("there are more than two certificate returned "
313: + "from getCertificateChain", certRes.length == 2);
314: assertTrue(
315: "the certificates returned from getCertificateChain "
316: + "is not correct",
317: cert[0].getPublicKey() == certRes[0].getPublicKey()
318: && cert[1].getPublicKey() == certRes[1]
319: .getPublicKey());
320: java.security.cert.Certificate[] certResNull = keyTest
321: .getCertificateChain("alias1");
322: assertNull("the certificate chain returned from "
323: + "getCertificateChain is NOT null", certResNull);
324: }
325:
326: /**
327: * @tests java.security.KeyStore#getInstance(java.lang.String)
328: */
329: public void test_getInstanceLjava_lang_String() throws Exception {
330: // Test for method java.security.KeyStore
331: // java.security.KeyStore.getInstance(java.lang.String)
332: KeyStore keyTest = KeyStore.getInstance(KeyStore
333: .getDefaultType());
334: assertTrue("the method getInstance did not obtain "
335: + "the correct type", keyTest.getType().equals(
336: KeyStore.getDefaultType()));
337: }
338:
339: /**
340: * @tests java.security.KeyStore#getInstance(java.lang.String,
341: * java.lang.String)
342: */
343: public void test_getInstanceLjava_lang_StringLjava_lang_String()
344: throws Exception {
345: // Test for method java.security.KeyStore
346: // java.security.KeyStore.getInstance(java.lang.String,
347: // java.lang.String)
348: KeyStore keyTest = KeyStore.getInstance("PKCS#12/Netscape",
349: "TestProvider");
350: assertTrue("the method getInstance did not obtain the "
351: + "correct provider and type", keyTest.getProvider()
352: .getName().equals("TestProvider")
353: && keyTest.getType().equals("PKCS#12/Netscape"));
354: }
355:
356: /**
357: * @tests java.security.KeyStore#getKey(java.lang.String, char[])
358: */
359: public void test_getKeyLjava_lang_String$C() throws Exception {
360:
361: // Test for method java.security.Key
362: // java.security.KeyStore.getKey(java.lang.String, char [])
363: // creatCertificate();
364: CertificateFactory cf = CertificateFactory.getInstance("X.509");
365: X509Certificate cert[] = new X509Certificate[2];
366: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
367: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
368: KeyStore keyTest = KeyStore.getInstance(KeyStore
369: .getDefaultType());
370: keyTest.load(null, null);
371:
372: keyTest.setKeyEntry("alias2", privateKey, pssWord, cert);
373: PrivateKey returnedKey = (PrivateKey) keyTest.getKey("alias2",
374: pssWord);
375: byte[] retB = returnedKey.getEncoded();
376: byte[] priB = privateKey.getEncoded();
377: boolean equality = Arrays.equals(retB, priB);
378: equality &= returnedKey.getAlgorithm().equals(
379: privateKey.getAlgorithm());
380: equality &= returnedKey.getFormat().equals(
381: privateKey.getFormat());
382: assertTrue("the private key returned from getKey for a "
383: + "key entry did not equal the original key", equality);
384:
385: try {
386: keyTest.getKey("alias2", "wrong".toCharArray());
387: fail("Should have thrown UnrecoverableKeyException");
388: } catch (UnrecoverableKeyException e) {
389: // expected
390: }
391:
392: keyTest.setCertificateEntry("alias1", cert[1]);
393: assertNull("the private key returned from getKey for "
394: + "a certificate entry is not null", keyTest.getKey(
395: "alias1", pssWord));
396: }
397:
398: /**
399: * @tests java.security.KeyStore#getProvider()
400: */
401: public void test_getProvider() throws Exception {
402: // Test for method java.security.Provider
403: // java.security.KeyStore.getProvider()
404: KeyStore keyTest = KeyStore.getInstance("PKCS#12/Netscape",
405: "TestProvider");
406: Provider provKeyStore = keyTest.getProvider();
407: assertEquals("the provider should be TestProvider",
408: "TestProvider", provKeyStore.getName());
409: }
410:
411: /**
412: * @tests java.security.KeyStore#getType()
413: */
414: public void test_getType() throws Exception {
415: // Test for method java.lang.String java.security.KeyStore.getType()
416: KeyStore keyTest = KeyStore.getInstance("PKCS#12/Netscape",
417: "TestProvider");
418: assertEquals(
419: "type should be PKCS#12/Netscape for provider TestProvider",
420: "PKCS#12/Netscape", keyTest.getType());
421: }
422:
423: /**
424: * @tests java.security.KeyStore#isCertificateEntry(java.lang.String)
425: */
426: public void test_isCertificateEntryLjava_lang_String()
427: throws Exception {
428: // Test for method boolean
429: // java.security.KeyStore.isCertificateEntry(java.lang.String)
430: CertificateFactory cf = CertificateFactory.getInstance("X.509");
431: X509Certificate cert[] = new X509Certificate[2];
432: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
433: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
434: KeyStore keyTest = KeyStore.getInstance(KeyStore
435: .getDefaultType());
436: keyTest.load(null, null);
437: // alias 1
438: keyTest.setCertificateEntry("alias1", cert[0]);
439:
440: // alias 2
441: keyTest.setKeyEntry("alias2", privateKey, pssWord, cert);
442:
443: assertTrue(
444: "isCertificateEntry method returns false for a certificate",
445: keyTest.isCertificateEntry("alias1"));
446: assertFalse(
447: "isCertificateEntry method returns true for noncertificate",
448: keyTest.isCertificateEntry("alias2"));
449: }
450:
451: /**
452: * @tests java.security.KeyStore#isKeyEntry(java.lang.String)
453: */
454: public void test_isKeyEntryLjava_lang_String() throws Exception {
455: // Test for method boolean
456: // java.security.KeyStore.isKeyEntry(java.lang.String)
457: CertificateFactory cf = CertificateFactory.getInstance("X.509");
458: X509Certificate cert[] = new X509Certificate[2];
459: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
460: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
461: KeyStore keyTest = KeyStore.getInstance(KeyStore
462: .getDefaultType());
463: keyTest.load(null, null);
464: // alias 1
465: keyTest.setCertificateEntry("alias1", cert[0]);
466:
467: // alias 2
468: keyTest.setKeyEntry("alias2", privateKey, pssWord, cert);
469:
470: assertTrue("isKeyEntry method returns false for a certificate",
471: keyTest.isKeyEntry("alias2"));
472: assertFalse(
473: "isKeyEntry method returns true for noncertificate",
474: keyTest.isKeyEntry("alias1"));
475: }
476:
477: /**
478: * @tests java.security.KeyStore#load(java.io.InputStream, char[])
479: */
480: public void test_loadLjava_io_InputStream$C() throws Exception {
481: // Test for method void java.security.KeyStore.load(java.io.InputStream,
482: // char [])
483: byte[] keyStore = creatCertificate();
484: KeyStore keyTest = KeyStore.getInstance(KeyStore
485: .getDefaultType());
486: InputStream in = new ByteArrayInputStream(keyStore);
487: keyTest.load(in, pssWord);
488: in.close();
489: assertTrue("alias1 is not a certificate", keyTest
490: .isCertificateEntry("alias1"));
491: assertTrue("alias2 is not a keyEntry", keyTest
492: .isKeyEntry("alias2"));
493: assertTrue("alias3 is not a certificate", keyTest
494: .isCertificateEntry("alias3"));
495:
496: // test with null password
497: keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
498: in = new ByteArrayInputStream(keyStore);
499: keyTest.load(in, null);
500: in.close();
501: assertTrue("alias1 is not a certificate", keyTest
502: .isCertificateEntry("alias1"));
503: assertTrue("alias2 is not a keyEntry", keyTest
504: .isKeyEntry("alias2"));
505: assertTrue("alias3 is not a certificate", keyTest
506: .isCertificateEntry("alias3"));
507:
508: keyTest = KeyStore.getInstance(KeyStore.getDefaultType());
509: InputStream v1in = Support_Resources.getStream("hyts_ks.bks");
510: char[] pass = "abcdef".toCharArray();
511: keyTest.load(v1in, pass);
512: v1in.close();
513: keyTest.getKey("mykey", pass);
514: }
515:
516: /**
517: * @tests java.security.KeyStore#setCertificateEntry(java.lang.String,
518: * java.security.cert.Certificate)
519: */
520: public void test_setCertificateEntryLjava_lang_StringLjava_security_cert_Certificate()
521: throws Exception {
522: // Test for method void
523: // java.security.KeyStore.setCertificateEntry(java.lang.String,
524: // java.security.cert.Certificate)
525: CertificateFactory cf = CertificateFactory.getInstance("X.509");
526: X509Certificate cert = (X509Certificate) cf
527: .generateCertificate(certArray);
528: KeyStore keyTest = KeyStore.getInstance(KeyStore
529: .getDefaultType());
530: keyTest.load(null, null);
531:
532: PublicKey pub = cert.getPublicKey();
533: keyTest.setCertificateEntry("alias1", cert);
534: assertTrue(
535: "the entry specified by the alias alias1 is not a certificate",
536: keyTest.isCertificateEntry("alias1"));
537: java.security.cert.Certificate resultCert = keyTest
538: .getCertificate("alias1");
539: assertTrue(
540: "the public key of the certificate from getCertificate() did not equal the original certificate",
541: resultCert.getPublicKey() == pub);
542: }
543:
544: /**
545: * @tests java.security.KeyStore#setKeyEntry(java.lang.String, byte[],
546: * java.security.cert.Certificate[])
547: */
548: public void test_setKeyEntryLjava_lang_String$B$Ljava_security_cert_Certificate()
549: throws Exception {
550:
551: // fail("Test hangs - requires a full math implementation ??");
552:
553: // Test for method void
554: // java.security.KeyStore.setKeyEntry(java.lang.String, byte [],
555: // java.security.cert.Certificate [])
556:
557: CertificateFactory cf = CertificateFactory.getInstance("X.509");
558: X509Certificate cert[] = new X509Certificate[2];
559: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
560: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
561: KeyStore keyTest = KeyStore.getInstance(KeyStore
562: .getDefaultType());
563: keyTest.load(null, null);
564:
565: // set the same alias as keyEntry
566: keyTest.setKeyEntry("alias2", privateKey.getEncoded(), cert);
567: assertTrue(
568: "the entry specified by the alias alias2 is not a keyEntry",
569: keyTest.isKeyEntry("alias2"));
570: }
571:
572: /**
573: * @tests java.security.KeyStore#setKeyEntry(java.lang.String,
574: * java.security.Key, char[], java.security.cert.Certificate[])
575: */
576: public void test_setKeyEntryLjava_lang_StringLjava_security_Key$C$Ljava_security_cert_Certificate()
577: throws Exception {
578:
579: // Test for method void
580: // java.security.KeyStore.setKeyEntry(java.lang.String,
581: // java.security.Key, char [], java.security.cert.Certificate [])
582:
583: CertificateFactory cf = CertificateFactory.getInstance("X.509");
584: X509Certificate cert[] = new X509Certificate[2];
585: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
586: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
587: KeyStore keyTest = KeyStore.getInstance(KeyStore
588: .getDefaultType());
589: keyTest.load(null, null);
590:
591: keyTest.setKeyEntry("alias3", privateKey, pssWord, cert);
592: assertTrue(
593: "the entry specified by the alias alias3 is not a keyEntry",
594: keyTest.isKeyEntry("alias3"));
595: }
596:
597: /**
598: * @tests java.security.KeyStore#size()
599: */
600: public void test_size() throws Exception {
601: // Test for method int java.security.KeyStore.size()
602:
603: CertificateFactory cf = CertificateFactory.getInstance("X.509");
604: X509Certificate cert[] = new X509Certificate[2];
605: cert[0] = (X509Certificate) cf.generateCertificate(certArray);
606: cert[1] = (X509Certificate) cf.generateCertificate(certArray2);
607: KeyStore keyTest = KeyStore.getInstance(KeyStore
608: .getDefaultType());
609: keyTest.load(null, null);
610: // alias 1
611: keyTest.setCertificateEntry("alias1", cert[0]);
612:
613: // alias 2
614: keyTest.setKeyEntry("alias2", privateKey, pssWord, cert);
615:
616: // alias 3
617: keyTest.setCertificateEntry("alias3", cert[1]);
618:
619: assertEquals("the size of the keyStore is not 3", 3, keyTest
620: .size());
621: }
622:
623: /**
624: * Sets up the fixture, for example, open a network connection. This method
625: * is called before a test is executed.
626: */
627: protected void setUp() throws Exception {
628: super .setUp();
629: support_TestProvider = new Support_TestProvider();
630: Security.addProvider(support_TestProvider);
631: }
632:
633: protected void tearDown() throws Exception {
634: super.tearDown();
635: Security.removeProvider(support_TestProvider.getName());
636: }
637: }
|