• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.apache.harmony.security.tests.java.security;
2 
3 import junit.framework.TestCase;
4 
5 import org.apache.harmony.security.tests.support.MyProvider;
6 import org.apache.harmony.security.tests.support.TestKeyStoreSpi;
7 import org.apache.harmony.security.tests.support.cert.MyCertificate;
8 
9 import java.io.ByteArrayInputStream;
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.InputStream;
13 import java.io.OutputStream;
14 import java.security.Key;
15 import java.security.KeyStore;
16 import java.security.KeyStoreException;
17 import java.security.NoSuchAlgorithmException;
18 import java.security.NoSuchProviderException;
19 import java.security.Provider;
20 import java.security.Security;
21 import java.security.UnrecoverableEntryException;
22 import java.security.UnrecoverableKeyException;
23 import java.security.KeyStore.Entry;
24 import java.security.KeyStore.ProtectionParameter;
25 import java.security.cert.Certificate;
26 import java.security.cert.CertificateException;
27 
28 public class KeyStore4Test extends TestCase {
29 
30     Provider provider = new MyProvider();
31     KeyStore keyStore;
32     KeyStore uninitialized;
33     KeyStore failing;
34 
35     public static final String KEY_STORE_TYPE = "TestKeyStore";
36 
setUp()37     protected void setUp() throws Exception{
38         super.setUp();
39 
40         Security.addProvider(new MyProvider());
41 
42         try {
43             keyStore = KeyStore.getInstance(KEY_STORE_TYPE);
44             keyStore.load(null, "PASSWORD".toCharArray());
45         } catch (KeyStoreException e) {
46             fail("test class not available");
47         }
48 
49         try {
50             uninitialized = KeyStore.getInstance(KEY_STORE_TYPE);
51         } catch (KeyStoreException e) {
52             fail("test keystore not available");
53         }
54 
55     }
56 
57     @Override
tearDown()58     protected void tearDown() throws Exception {
59         super.tearDown();
60 
61         Security.removeProvider(provider.getName());
62     }
63 
testGetInstanceString()64     public void testGetInstanceString() {
65         try {
66             KeyStore ks = KeyStore.getInstance("TestKeyStore");
67             assertNotNull("keystore is null", ks);
68             assertEquals("KeyStore is not of expected Type", "TestKeyStore", ks.getType());
69         } catch (KeyStoreException e) {
70             fail("unexpected exception: " + e);
71         }
72 
73         try {
74             KeyStore.getInstance("UnknownKeyStore");
75             fail("expected KeyStoreException");
76         } catch (KeyStoreException e) {
77             // ok
78         }
79 
80         try {
81             KeyStore.getInstance(null);
82             fail("expected NullPointerException");
83         } catch (NullPointerException e) {
84             // ok
85         } catch (KeyStoreException e) {
86             fail("unexpected exception: " + e);
87         }
88     }
89 
testGetInstanceStringString()90     public void testGetInstanceStringString() {
91         try {
92             KeyStore ks = KeyStore.getInstance("TestKeyStore", provider.getName());
93             assertNotNull("keystore is null", ks);
94             assertEquals("KeyStore is not of expected type", "TestKeyStore", ks.getType());
95         } catch (KeyStoreException e) {
96             fail("unexpected exception: " + e);
97         } catch (NoSuchProviderException e) {
98             fail("unexpected exception: " + e);
99         }
100 
101         try {
102             KeyStore.getInstance("UnknownKeyStore", provider.getName());
103             fail("expected KeyStoreException");
104         } catch (KeyStoreException e) {
105             // ok
106         } catch (NoSuchProviderException e) {
107             fail("unexpected exception: " + e);
108         }
109 
110         try {
111             KeyStore.getInstance("TestKeyStore", (String)null);
112             fail("expected IllegalArgumentException");
113         } catch (KeyStoreException e) {
114             fail("unexpected exception: " + e);
115         } catch (NoSuchProviderException e) {
116             fail("unexpected exception: " + e);
117         } catch (IllegalArgumentException e) {
118             // ok
119         }
120 
121         try {
122             KeyStore.getInstance("TestKeyStore", "");
123             fail("expected IllegalArgumentException");
124         } catch (KeyStoreException e) {
125             fail("unexpected exception: " + e);
126         } catch (NoSuchProviderException e) {
127             fail("unexpected exception: " + e);
128         } catch (IllegalArgumentException e) {
129             // ok
130         }
131 
132         try {
133             KeyStore.getInstance(null, provider.getName());
134             fail("expected KeyStoreException");
135         } catch (KeyStoreException e) {
136             // ok
137         } catch (NoSuchProviderException e) {
138             fail("unexpected exception: " + e);
139         } catch (NullPointerException e) {
140             // also ok
141         }
142 
143         try {
144             KeyStore.getInstance("TestKeyStore", "UnknownProvider");
145             fail("expected NoSuchProviderException");
146         } catch (NoSuchProviderException e) {
147             // ok
148         } catch (KeyStoreException e) {
149             fail("unexpected exception: " + e);
150         }
151     }
152 
testGetInstanceStringProvider()153     public void testGetInstanceStringProvider() {
154         try {
155             KeyStore ks = KeyStore.getInstance("TestKeyStore", provider);
156             assertNotNull("KeyStore is null", ks);
157             assertEquals("KeyStore is not of expected type", "TestKeyStore", ks.getType());
158         } catch (KeyStoreException e) {
159             fail("unexpected exception: " + e);
160         }
161 
162         try {
163             KeyStore.getInstance("UnknownKeyStore", provider);
164             fail("expected KeyStoreException");
165         } catch (KeyStoreException e) {
166             // ok;
167         }
168 
169         try {
170             KeyStore.getInstance("TestKeyStore", (Provider)null);
171             fail("expected IllegalArgumentException");
172         } catch (KeyStoreException e) {
173             fail("unexpected exception: " + e);
174         } catch (IllegalArgumentException e) {
175             // ok
176         }
177 
178         try {
179             KeyStore.getInstance(null, provider);
180             fail("expected NullPointerException");
181         } catch (KeyStoreException e) {
182             fail("unexpected exception: " + e);
183         } catch (NullPointerException e) {
184             // ok
185         }
186     }
187 
188 
testGetKey()189     public void testGetKey() {
190         try {
191             Key key = keyStore.getKey("keyalias", null);
192             assertNotNull(key);
193         } catch (KeyStoreException e) {
194             fail("unexpected exception: " + e);
195         } catch (NoSuchAlgorithmException e) {
196             fail("unexpected exception: " + e);
197         } catch (UnrecoverableKeyException e) {
198             fail("unexpected exception: " + e);
199         }
200 
201         try {
202             keyStore.getKey("certalias", null);
203             fail("expected NoSuchAlgorithmException");
204         } catch (KeyStoreException e) {
205             fail("unexpected exception: " + e);
206         } catch (NoSuchAlgorithmException e) {
207             // ok
208         } catch (UnrecoverableKeyException e) {
209             fail("unexpected exception: " + e);
210         }
211 
212         try {
213             uninitialized.getKey("keyalias", null);
214             fail("expected KeyStoreException");
215         } catch (KeyStoreException e) {
216             // ok
217         } catch (NoSuchAlgorithmException e) {
218             fail("unexpected exception: " + e);
219         } catch (UnrecoverableKeyException e) {
220             fail("unexpected exception: " + e);
221         }
222 
223         try {
224             keyStore.getKey("unknownalias", null);
225             fail("expected NoSuchAlgorithmException");
226         } catch (KeyStoreException e) {
227             fail("unexpected exception: " + e);
228         } catch (NoSuchAlgorithmException e) {
229             // ok
230         } catch (UnrecoverableKeyException e) {
231             fail("unexpected exception: " + e);
232         }
233 
234         try {
235             keyStore.getKey("unknownalias", "PASSWORD".toCharArray());
236             fail("expected UnrecoverableKeyException");
237         } catch (UnrecoverableKeyException e) {
238             // ok
239         } catch (KeyStoreException e) {
240             fail("unexpected exception: " + e);
241         } catch (NoSuchAlgorithmException e) {
242             fail("unexpected exception: " + e);
243         }
244 
245     }
246 
247 
testGetCertificateAlias()248     public void testGetCertificateAlias() {
249         try {
250             String alias = keyStore.getCertificateAlias(TestKeyStoreSpi.CERT);
251             assertNotNull("alias is null", alias);
252             assertEquals("alias is not expected", "certalias", alias);
253         } catch (KeyStoreException e) {
254             fail("unexpected exception: " + e);
255         }
256 
257         try {
258             uninitialized.getCertificateAlias(TestKeyStoreSpi.CERT);
259             fail("expected KeyStoreException");
260         } catch (KeyStoreException e) {
261             // ok
262         }
263 
264         try {
265             keyStore.getCertificateAlias(null);
266             fail("expected NullPointerException");
267         } catch (KeyStoreException e) {
268             fail("unexpected exception: " + e);
269         } catch (NullPointerException e) {
270             // ok
271         }
272 
273         try {
274             String certificateAlias = keyStore.getCertificateAlias(new MyCertificate("dummy", null));
275             assertNull("alias was not null", certificateAlias);
276         } catch (KeyStoreException e) {
277             fail("unexpected exception: " + e);
278         }
279     }
280 
testStoreOutputStreamCharArray()281     public void testStoreOutputStreamCharArray() {
282         OutputStream os = new ByteArrayOutputStream();
283         char[] password = "PASSWORD".toCharArray();
284 
285         try {
286             keyStore.store(os, password);
287         } catch (KeyStoreException e) {
288             fail("unexpected exception: " + e);
289         } catch (NoSuchAlgorithmException e) {
290             fail("unexpected exception: " + e);
291         } catch (CertificateException e) {
292             fail("unexpected exception: " + e);
293         } catch (IOException e) {
294             fail("unexpected exception: " + e);
295         }
296 
297         try {
298             keyStore.store(os, null);
299             fail("expected NoSuchAlgorithmException");
300         } catch (KeyStoreException e) {
301             fail("unexpected exception: " + e);
302         } catch (NoSuchAlgorithmException e) {
303             // ok
304         } catch (CertificateException e) {
305             fail("unexpected exception: " + e);
306         } catch (IOException e) {
307             // ok
308         }
309 
310         try {
311             keyStore.store(os, "".toCharArray());
312             fail("expected CertificateException");
313         } catch (KeyStoreException e) {
314             fail("unexpected exception: " + e);
315         } catch (NoSuchAlgorithmException e) {
316             fail("unexpected exception: " + e);
317         } catch (CertificateException e) {
318             // ok
319         } catch (IOException e) {
320             fail("unexpected exception: " + e);
321         }
322 
323         try {
324             keyStore.store(null, null);
325             fail("expected IOException");
326         } catch (KeyStoreException e) {
327             fail("unexpected exception: " + e);
328         } catch (NoSuchAlgorithmException e) {
329             fail("unexpected exception: " + e);
330         } catch (CertificateException e) {
331             fail("unexpected exception: " + e);
332         } catch (IOException e) {
333             // ok
334         }
335 
336         try {
337             uninitialized.store(null, null);
338             fail("expected KeyStoreException");
339         } catch (KeyStoreException e) {
340             // ok
341         } catch (NoSuchAlgorithmException e) {
342             fail("unexpected exception: " + e);
343         } catch (CertificateException e) {
344             fail("unexpected exception: " + e);
345         } catch (IOException e) {
346             fail("unexpected exception: " + e);
347         }
348 
349 
350 
351 
352     }
353 
testStoreLoadStoreParameter()354     public void testStoreLoadStoreParameter() {
355         try {
356             keyStore.store(new KeyStore.LoadStoreParameter() {
357 
358                 public ProtectionParameter getProtectionParameter() {
359                     return new KeyStore.PasswordProtection("PASSWORD".toCharArray());
360                 }});
361         } catch (NoSuchAlgorithmException e) {
362             fail("unexpected exception: " + e);
363         } catch (CertificateException e) {
364             fail("unexpected exception: " + e);
365         } catch (IOException e) {
366             fail("unexpected exception: " + e);
367         } catch (KeyStoreException e) {
368             fail("unexpected exception: " + e);
369         }
370 
371         try {
372             keyStore.store(null);
373             fail("expected IOException");
374         } catch (KeyStoreException e) {
375             fail("unexpected exception: " + e);
376         } catch (NoSuchAlgorithmException e) {
377             fail("unexpected exception: " + e);
378         } catch (CertificateException e) {
379             fail("unexpected exception: " + e);
380         } catch (IOException e) {
381             // ok
382         }
383 
384         try {
385             keyStore.store(new KeyStore.LoadStoreParameter() {
386 
387                 public ProtectionParameter getProtectionParameter() {
388                     return null;
389                 }});
390             fail("expected UnsupportedOperationException");
391         } catch (KeyStoreException e) {
392             fail("unexpected exception: " + e);
393         } catch (NoSuchAlgorithmException e) {
394             fail("unexpected exception: " + e);
395         } catch (CertificateException e) {
396             fail("unexpected exception: " + e);
397         } catch (IOException e) {
398             fail("unexpected exception: " + e);
399         } catch (UnsupportedOperationException e) {
400             // ok
401         }
402 
403         try {
404             keyStore.store(new KeyStore.LoadStoreParameter() {
405 
406                 public ProtectionParameter getProtectionParameter() {
407                     return new KeyStore.PasswordProtection("".toCharArray());
408                 }});
409         } catch (KeyStoreException e) {
410             fail("unexpected exception: " + e);
411         } catch (NoSuchAlgorithmException e) {
412             fail("unexpected exception: " + e);
413         } catch (CertificateException e) {
414             // ok
415         } catch (IOException e) {
416             fail("unexpected exception: " + e);
417         }
418 
419         try {
420             keyStore.store(new KeyStore.LoadStoreParameter() {
421 
422                 public ProtectionParameter getProtectionParameter() {
423                     return new KeyStore.PasswordProtection(null);
424                 }} );
425         } catch (KeyStoreException e) {
426             fail("unexpected exception: " + e);
427         } catch (NoSuchAlgorithmException e) {
428             // ok
429         } catch (CertificateException e) {
430             fail("unexpected exception: " + e);
431         } catch (IOException e) {
432             fail("unexpected exception: " + e);
433         }
434 
435         try {
436             uninitialized.store(null);
437             fail("expected KeyStoreException");
438         } catch (KeyStoreException e) {
439             // ok
440         } catch (NoSuchAlgorithmException e) {
441             fail("unexpected exception: " + e);
442         } catch (CertificateException e) {
443             fail("unexpected exception: " + e);
444         } catch (IOException e) {
445             fail("unexpected exception: " + e);
446         }
447     }
448 
testLoadInputStreamCharArray()449     public void testLoadInputStreamCharArray() {
450         InputStream is = new ByteArrayInputStream("DATA".getBytes());
451         char[] password = "PASSWORD".toCharArray();
452         try {
453             keyStore.load(is, password);
454             assertTrue(keyStore.containsAlias("keyalias"));
455         } catch (NoSuchAlgorithmException e) {
456             fail("unexpected exception: " + e);
457         } catch (CertificateException e) {
458             fail("unexpected exception: " + e);
459         } catch (IOException e) {
460             fail("unexpected exception: " + e);
461         } catch (KeyStoreException e) {
462             fail("unexpected exception: " + e);
463         }
464 
465         try {
466             keyStore.load(new ByteArrayInputStream("".getBytes()), password);
467             fail("expected IOException");
468         } catch (NoSuchAlgorithmException e) {
469             fail("unexpected exception: " + e);
470         } catch (CertificateException e) {
471             fail("unexpected exception: " + e);
472         } catch (IOException e) {
473             // ok
474         }
475 
476         try {
477             keyStore.load(is, null);
478             fail("expected NoSuchAlgorithmException");
479         } catch (NoSuchAlgorithmException e) {
480             // ok
481         } catch (CertificateException e) {
482             fail("unexpected exception: " + e);
483         } catch (IOException e) {
484             fail("unexpected exception: " + e);
485         }
486 
487         try {
488             keyStore.load(is, new char[] {});
489             fail("expected CertificateException");
490         } catch (NoSuchAlgorithmException e) {
491             fail("unexpected exception: " + e);
492         } catch (CertificateException e) {
493             // ok
494         } catch (IOException e) {
495             fail("unexpected exception: " + e);
496         }
497     }
498 
testLoadLoadStoreParameter()499     public void testLoadLoadStoreParameter() {
500         try {
501             keyStore.load(null);
502             fail("expected NoSuchAlgorithmException");
503         } catch (NoSuchAlgorithmException e) {
504             // ok
505         } catch (CertificateException e) {
506             fail("unexpected exception: " + e);
507         } catch (IOException e) {
508             fail("unexpected exception: " + e);
509         }
510 
511         try {
512             keyStore.load(new KeyStore.LoadStoreParameter() {
513 
514                 public ProtectionParameter getProtectionParameter() {
515                     return new KeyStore.PasswordProtection("PASSWORD".toCharArray());
516                 }
517 
518             });
519         } catch (NoSuchAlgorithmException e) {
520             fail("unexpected exception: " + e);
521         } catch (CertificateException e) {
522             fail("unexpected exception: " + e);
523         } catch (IOException e) {
524             fail("unexpected exception: " + e);
525         }
526 
527         try {
528             keyStore.load(new KeyStore.LoadStoreParameter() {
529 
530                 public ProtectionParameter getProtectionParameter() {
531                     return null;
532                 }
533 
534             });
535             fail("expected NoSuchAlgorithmException");
536         } catch (NoSuchAlgorithmException e) {
537             // ok
538         } catch (CertificateException e) {
539             fail("unexpected exception: " + e);
540         } catch (IOException e) {
541             fail("unexpected exception: " + e);
542         }
543 
544         try {
545             keyStore.load(new KeyStore.LoadStoreParameter() {
546 
547                 public ProtectionParameter getProtectionParameter() {
548                     return new KeyStore.ProtectionParameter() {};
549                 }
550 
551             });
552             fail("expected CertificateException");
553         } catch (NoSuchAlgorithmException e) {
554             fail("unexpected exception: " + e);
555         } catch (CertificateException e) {
556             // ok
557         } catch (IOException e) {
558             fail("unexpected exception: " + e);
559         }
560     }
561 
testGetEntry()562     public void testGetEntry() {
563         try {
564             Entry entry = keyStore.getEntry("certalias", null);
565             assertNotNull("entry is null", entry);
566             assertTrue("entry is not cert entry", entry instanceof KeyStore.TrustedCertificateEntry);
567         } catch (NoSuchAlgorithmException e) {
568             fail("unexpected exception: " + e);
569         } catch (UnrecoverableEntryException e) {
570             fail("unexpected exception: " + e);
571         } catch (KeyStoreException e) {
572             fail("unexpected exception: " + e);
573         }
574 
575         try {
576             Entry entry = keyStore.getEntry("certalias", new KeyStore.ProtectionParameter() {});
577             assertNotNull(entry);
578         } catch (NoSuchAlgorithmException e) {
579             fail("unexpected exception: " + e);
580         } catch (UnrecoverableEntryException e) {
581             fail("unexpected exception: " + e);
582         } catch (KeyStoreException e) {
583             fail("unexpected exception: " + e);
584         } catch (UnsupportedOperationException e) {
585             // ok
586         }
587 
588         try {
589             Entry entry = keyStore.getEntry("keyalias", new KeyStore.PasswordProtection(new char[] {} ));
590             assertNotNull(entry);
591             assertTrue(entry instanceof KeyStore.SecretKeyEntry);
592         } catch (NoSuchAlgorithmException e) {
593             fail("unexpected exception: " + e);
594         } catch (UnrecoverableEntryException e) {
595             fail("unexpected exception: " + e);
596         } catch (KeyStoreException e) {
597             fail("unexpected exception: " + e);
598         }
599 
600         try {
601             keyStore.getEntry("unknownalias", new KeyStore.PasswordProtection(new char[] {}));
602             fail("expected NoSuchAlgorithmException");
603         } catch (NoSuchAlgorithmException e) {
604             // ok
605         } catch (UnrecoverableEntryException e) {
606             fail("unexpected exception: " + e);
607         } catch (KeyStoreException e) {
608             fail("unexpected exception: " + e);
609         } catch (UnsupportedOperationException e) {
610             // also ok
611         }
612 
613         try {
614             keyStore.getEntry(null, new KeyStore.ProtectionParameter() {});
615             fail("expected NullPointerException");
616         } catch (NoSuchAlgorithmException e) {
617             fail("unexpected exception: " + e);
618         } catch (UnrecoverableEntryException e) {
619             fail("unexpected exception: " + e);
620         } catch (KeyStoreException e) {
621             fail("unexpected exception: " + e);
622         } catch (NullPointerException e) {
623             // ok
624         }
625     }
626 
627 
628 
testGetType()629     public void testGetType() {
630         assertEquals(KEY_STORE_TYPE, keyStore.getType());
631     }
632 
testGetProvider()633     public void testGetProvider() {
634         assertNotNull(keyStore.getProvider());
635         assertEquals("not equal", provider, keyStore.getProvider());
636     }
637 
638 }
639