summaryrefslogtreecommitdiff
path: root/security/src/test/impl/java/org/apache/harmony/security/tests/java/security/KeyStore_Impl1Test.java
blob: a05c599ff5674c55c5aaafbf929a674bc30f7919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

/**
 * @author Vera Y. Petrashkova
 */

package org.apache.harmony.security.tests.java.security;

import java.io.ByteArrayOutputStream;
import java.security.Key;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.KeyStoreSpi;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.Provider;
import java.security.UnrecoverableEntryException;
import java.security.UnrecoverableKeyException;
import java.security.cert.Certificate;
import java.util.Arrays;

import org.apache.harmony.security.tests.support.KeyStoreTestSupport;
import org.apache.harmony.security.tests.support.MyKeyStoreSpi;
import org.apache.harmony.security.tests.support.MyLoadStoreParams;
import org.apache.harmony.security.tests.support.SpiEngUtils;
import org.apache.harmony.security.tests.support.TestKeyPair;

import junit.framework.TestCase;

/**
 * Tests for <code>KeyStore</code> constructor and methods
 */

public class KeyStore_Impl1Test extends TestCase {

    public static final String srvKeyStore = KeyStoreTestSupport.srvKeyStore;
    public static String[] validValues = KeyStoreTestSupport.validValues;

    private static final String[] aliases = { "", "alias", "Alias", "ALIAS",
            "new alias", "another alias", "ADDITIONAL", "THE SAME ALIAS" };

    private static String[] invalidValues = SpiEngUtils.invalidValues;

    public static String defaultType = KeyStoreTestSupport.defaultType;
    public static boolean JKSSupported = KeyStoreTestSupport.JKSSupported;
    public static String defaultProviderName = KeyStoreTestSupport.defaultProviderName;
    public static Provider defaultProvider = KeyStoreTestSupport.defaultProvider;

    private static String NotSupportMsg = "Default KeyStore type is not supported";

    public KeyStore[] createKS() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStore[] kpg = new KeyStore[3];

        kpg[0] = KeyStore.getInstance(defaultType);
        kpg[1] = KeyStore.getInstance(defaultType, defaultProvider);
        kpg[2] = KeyStore.getInstance(defaultType, defaultProviderName);
        return kpg;
    }

    /**
     * Test for <code>getInstance(String type)</code> method
     * Assertion:
     * returns KeyStoreException object
     */
    public void testKeyStore03() throws KeyStoreException {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStore ks;
        for (int i = 0; i < validValues.length; i++) {
            ks = KeyStore.getInstance(validValues[i]);
            assertEquals("Incorrect type", ks.getType(), validValues[i]);
        }
    }

    /**
     * Test for <code>getInstance(String type, String provider)</code> method
     * Assertion: throws IllegalArgumentException when provider is null or empty
     */
    public void testKeyStore04() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        String provider = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                KeyStore.getInstance(validValues[i], provider);
                fail("IllegalArgumentException must be thrown when provider is null (type: "
                        .concat(validValues[i]).concat(" )"));
            } catch (IllegalArgumentException e) {
            }
            try {
                KeyStore.getInstance(validValues[i], "");
                fail("IllegalArgumentException must be thrown when provider is empty (type: "
                        .concat(validValues[i]).concat(" )"));
            } catch (IllegalArgumentException e) {
            }
        }
    }

    /**
     * Test for <code>getInstance(String type, String provider)</code> method
     * Assertion: throws NoSuchProviderException when provider is not available
     */
    public void testKeyStore05() throws KeyStoreException {
        assertTrue(NotSupportMsg, JKSSupported);
        for (int i = 0; i < validValues.length; i++) {
            for (int j = 1; j < invalidValues.length; j++) {
                try {
                    KeyStore.getInstance(validValues[i], invalidValues[j]);
                    fail("NoSuchProviderException must be thrown (type: "
                            .concat(validValues[i]).concat("  provider: ")
                            .concat(invalidValues[j]).concat(" )"));
                } catch (NoSuchProviderException e) {
                }
            }
        }
    }

    /**
     * Test for <code>getInstance(String type, String provider)</code> method
     * Assertion:
     * throws NullPointerException when type is null
     * throws KeyStoreException when type is not available
     */
    public void testKeyStore06() throws NoSuchProviderException {
        assertTrue(NotSupportMsg, JKSSupported);
        try {
            KeyStore.getInstance(null, defaultProviderName);
            fail("KeyStoreException must be thrown  when type is null");
        } catch (KeyStoreException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                KeyStore.getInstance(invalidValues[i], defaultProviderName);
                fail("KeyStoreException must be thrown (type: ".concat(
                        invalidValues[i]).concat("  provider: ").concat(
                        defaultProviderName).concat(" )"));
            } catch (KeyStoreException e) {
            }
        }
    }

    /**
     * Test for <code>getInstance(String type, String provider)</code> method
     * Assertion: returns KeyStore object
     */
    public void testKeyStore07() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStore ks;
        for (int i = 0; i < validValues.length; i++) {
            ks = KeyStore.getInstance(validValues[i], defaultProviderName);
            assertEquals("Incorrect type", ks.getType(), validValues[i]);
            assertEquals("Incorrect provider", ks.getProvider().getName(),
                    defaultProviderName);
        }
    }

    /**
     * Test for <code>getInstance(String type, Provider provider)</code> method
     * Assertion: throws IllegalArgumentException when provider is null
     */
    public void testKeyStore08() throws KeyStoreException {
        assertTrue(NotSupportMsg, JKSSupported);
        Provider provider = null;
        for (int i = 0; i < validValues.length; i++) {
            try {
                KeyStore.getInstance(validValues[i], provider);
                fail("IllegalArgumentException must be thrown when provider is null (type: "
                        .concat(validValues[i]).concat(" )"));
            } catch (IllegalArgumentException e) {
            }
        }
    }

    /**
     * Test for <code>getInstance(String type, Provider provider)</code>
     * method
     * Assertions:
     * throws NullPointerException when type is null
     * throws KeyStoreException when type is not available
     */
    public void testKeyStore09() {
        assertTrue(NotSupportMsg, JKSSupported);
        try {
            KeyStore.getInstance(null, defaultProvider);
            fail("KeyStoreException must be thrown when type is null");
        } catch (KeyStoreException e) {
        } catch (NullPointerException e) {
        }
        for (int i = 0; i < invalidValues.length; i++) {
            try {
                KeyStore.getInstance(invalidValues[i], defaultProvider);
                fail("KeyStoreException must be thrown when type is null (type: "
                        .concat(invalidValues[i]).concat(" provider: ").concat(
                                defaultProvider.getName()).concat(" )"));
            } catch (KeyStoreException e) {
            }
        }
    }

    /**
     * Test for <code>getInstance(String type, Provider provider)</code>
     * method
     * Assertion: returns KeyStore object
     */
    public void testKeyStore10() throws KeyStoreException {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStore ks;
        for (int i = 0; i < validValues.length; i++) {
            ks = KeyStore.getInstance(validValues[i], defaultProvider);
            assertEquals("Incorrect type", ks.getType(), validValues[i]);
            assertEquals("Incorrect provider", ks.getProvider(),
                    defaultProvider);
        }
    }

    /**
     * Test for methods:
     * <code>getKey(String alias, char[] password)</code>
     * <code>getCertificateChain(String alias)</code>
     * <code>getCertificate(String alias)</code>
     * <code>getCreationDate(String alias)</code>
     * <code>setKeyEntry(String alias, Key key, char[] password, Certificate[] chain)</code>
     * <code>setKeyEntry(String alias, byte[] key, Certificate[] chain)</code>
     * <code>setCertificateEntry(String alias, Certificate cert)</code>
     * <code>deleteEntry(String alias)</code>
     * <code>Enumeration aliases()</code>
     * <code>containsAlias(String alias)</code>
     * <code>size()</code>
     * <code>isKeyEntry(String alias)</code>
     * <code>isCertificateEntry(String alias)</code>
     * <code>getCertificateAlias(Certificate cert)</code>
     * <code>store(OutputStream stream, char[] password)</code>
     * Assertion: throws KeyStoreException when KeyStore was not initialized
     */
    public void testKeyStore11() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        String msgF = "KeyStoreException must be thrown because KeyStore was not initialized";
        KeyStore[] kss = createKS();
        assertNotNull("KeyStore objects were not created", kss);
        for (int i = 0; i < kss.length; i++) {
            try {
                kss[i].getKey("", new char[1]);
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].getCertificateChain("");
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].getCertificate("");
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].getCreationDate("");
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].aliases();
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].containsAlias("");
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].size();
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].setKeyEntry("", null, new char[0], new Certificate[0]);
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].setKeyEntry("", new byte[0], new Certificate[0]);
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].setCertificateEntry("", null);
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].deleteEntry("");
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].isKeyEntry("");
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].isCertificateEntry("");
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].getCertificateAlias(null);
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            ByteArrayOutputStream ba = new ByteArrayOutputStream();
            try {
                kss[i].store(ba, new char[0]);
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].store(new MyLoadStoreParams(
                        new KeyStore.PasswordProtection(new char[0])));
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(
                    new KeyStoreTestSupport.MCertificate("type", new byte[0]));
            try {
                kss[i].setEntry("aaa", entry, null);
                fail(msgF);
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].getEntry("aaa", null);
                fail(msgF);
            } catch (KeyStoreException e) {
            }
        }
    }

    /**
     * Test for
     * <code>setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter params)</code>
     * <code>containsAlias(String alias)</code>
     * <code>getEntry(String alias)</code>
     * <code>getCertificate(String alias)</code>
     * <code>isCertificateEntry(String alias)</code>
     * <code>isKeyEntry(String alias)</code>
     * methods Assertions: setEntry(..) throws NullPointerException when alias
     * or entry is null;
     * <p/>
     * containsAlias(..), getEntry(..), isCertificateEntry(..), isKeyEntry(...)
     * throw NullPointerException when alias is null;
     * <p/>
     * setEntry(..) stores Entry and getEntry(..) returns it when
     * KeyStore.TrustedCertificateEntry is used; getCertificate(...) returns
     * used trusted certificate.
     */
    public void testEntry01() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStoreTestSupport.MCertificate trust = new KeyStoreTestSupport.MCertificate(
                "type", new byte[0]);
        KeyStore.TrustedCertificateEntry entry = new KeyStore.TrustedCertificateEntry(
                trust);
        KeyStore[] kss = createKS();
        assertNotNull("KeyStore objects were not created", kss);

        for (int i = 0; i < kss.length; i++) {
            kss[i].load(null, null);
            try {
                kss[i].setEntry(null, entry, null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            try {
                kss[i].setEntry("ZZZ", null, null);
                fail("NullPointerException should be thrown when entry is null");
            } catch (NullPointerException e) {
            }
            for (int j = 0; j < aliases.length; j++) {
                kss[i].setEntry(aliases[j], entry, null);
            }
        }
        for (int i = 0; i < kss.length; i++) {
            try {
                kss[i].containsAlias(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            try {
                kss[i].isCertificateEntry(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            try {
                kss[i].isKeyEntry(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            try {
                kss[i].getEntry(null, null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            KeyStore.Entry en;
            for (int j = 0; j < aliases.length; j++) {
                assertFalse("Incorrect alias", kss[i].containsAlias("Bad"
                        .concat(aliases[j])));
                assertTrue("Incorrect alias", kss[i].containsAlias(aliases[j]));
                assertTrue("Not CertificateEntry", kss[i]
                        .isCertificateEntry(aliases[j]));
                assertFalse("Incorrect KeyEntry", kss[i].isKeyEntry(aliases[j]));
                en = kss[i].getEntry(aliases[j], null);
                assertTrue("Incorrect Entry",
                        en instanceof KeyStore.TrustedCertificateEntry);
                assertEquals("Incorrect certificate",
                        ((KeyStore.TrustedCertificateEntry) en)
                                .getTrustedCertificate(), entry
                        .getTrustedCertificate());
                assertEquals("Incorrect certificate", kss[i]
                        .getCertificate(aliases[j]), trust);
            }
        }
    }


    /**
     * Test for
     * <code>setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter params)</code>
     * <code>containsAlias(String alias)</code>
     * <code>getEntry(String alias)</code>
     * <code>isCertificateEntry(String alias)</code>
     * <code>isKeyEntry(String alias)</code>
     * methods
     * Assertions:
     * getEntry(...) throws KeyStoreException if password is incorrect;
     * setEntry(..) throws KeyStoreException if password is destroyed;
     * <p/>
     * setEntry(..) throws KeyStoreException when incorrect Entry is used;
     * <p/>
     * setEntry(..) stores Entry and getEntry(...) returns it when
     * KeyStore.PrivateKeyEntry is used.
     */
    public void testEntry02() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        TestKeyPair tkp = new TestKeyPair("DSA");
        KeyStoreTestSupport.MCertificate certs[] = {
                new KeyStoreTestSupport.MCertificate("DSA", tkp.getPrivate()
                        .getEncoded()),
                new KeyStoreTestSupport.MCertificate("DSA", tkp.getPrivate()
                        .getEncoded()) };
        PrivateKey privKey = tkp.getPrivate();
        KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(privKey,
                certs);
        char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
        KeyStore.PasswordProtection pPath = new KeyStore.PasswordProtection(pwd);
        KeyStore.PasswordProtection anotherPath = new KeyStore.PasswordProtection(
                new char[0]);
        KeyStoreTestSupport.ProtPar pPar = new KeyStoreTestSupport.ProtPar();
        KeyStore[] kss = createKS();
        assertNotNull("KeyStore objects were not created", kss);
        for (int i = 0; i < kss.length; i++) {
            kss[i].load(null, null);
            for (int j = 0; j < aliases.length; j++) {
                kss[i].setEntry(aliases[j], pKey, pPath);
            }
            KeyStore.Entry en;
            Certificate[] cc;
            for (int j = 0; j < aliases.length; j++) {
                assertTrue("Incorrect alias", kss[i].containsAlias(aliases[j]));
                assertTrue("Not KeyEntry", kss[i].isKeyEntry(aliases[j]));
                assertFalse("Incorrect CertificateEntry", kss[i]
                        .isCertificateEntry(aliases[j]));

                en = kss[i].getEntry(aliases[j], pPath);
                assertTrue("Incorrect Entry",
                        en instanceof KeyStore.PrivateKeyEntry);
                Key key = pKey.getPrivateKey();
                Key key1 = ((KeyStore.PrivateKeyEntry) en).getPrivateKey();
                if (!key.getAlgorithm().equals(key1.getAlgorithm()) ||
                        !key.getFormat().equals(key1.getFormat())) {
                    fail("Incorrect key");
                }
                byte[] enc = key.getEncoded();
                byte[] enc1 = key1.getEncoded();
                assertTrue("Diff. keys encoding", Arrays.equals(enc, enc1));

                cc = ((KeyStore.PrivateKeyEntry) en).getCertificateChain();
                assertEquals("Incorrect CertificateChain", cc.length,
                        certs.length);
                for (int t = 0; t < cc.length; t++) {
                    assertEquals("Incorrect CertificateChain", cc[t], certs[t]);
                }

                key = kss[i].getKey(aliases[j], pwd);
                key1 = privKey;
                if (!key.getAlgorithm().equals(key1.getAlgorithm()) ||
                        !key.getFormat().equals(key1.getFormat())) {
                    fail("Incorrect Entry: key");
                }
                enc = key.getEncoded();
                enc1 = key1.getEncoded();
                assertTrue("Incorrect Entry: Diff. keys encoding", Arrays.equals(enc, enc1));

                cc = kss[i].getCertificateChain(aliases[j]);
                assertEquals("Incorrect CertificateChain", cc.length,
                        certs.length);
                for (int t = 0; t < cc.length; t++) {
                    assertEquals("Incorrect CertificateChain", cc[t], certs[t]);
                }
                try {
                    kss[i].getEntry(aliases[j], anotherPath);
                    fail("KeyStoreException or UnrecoverableEntryException should be thrown "
                            + "because password is incorrect");
                } catch (KeyStoreException e) {
                } catch (UnrecoverableEntryException e) {
                }
            }
        }
        pPath.destroy();
        for (int i = 0; i < kss.length; i++) {
            try {
                kss[i].setEntry("ZZZ", pKey, pPath);
                fail("KeyStoreException should be thrown because password is destroyed");
            } catch (KeyStoreException e) {
            }

            for (int j = 0; j < aliases.length; j++) {
                try {
                    kss[i].getEntry(aliases[j], pPath);
                    fail("KeyStoreException should be thrown because password is destroyed");
                } catch (KeyStoreException e) {
                }

                try {
                    kss[i].getEntry(aliases[j], pPar);
                    fail("UnrecoverableEntryException should be thrown");
                } catch (UnrecoverableEntryException e) {
                }
            }
        }
    }

    /**
     * Test for
     * <code>setEntry(String alias, KeyStore.Entry entry, KeyStore.ProtectionParameter params)</code>
     * <code>containsAlias(String alias)</code>
     * <code>getEntry(String alias)</code>
     * <code>isCertificateEntry(String alias)</code>
     * <code>isKeyEntry(String alias)</code>
     * methods
     * Assertions:
     * setEntry(..) stores used entry and getEntry(..) returns it when
     * KeyStore.SecretKeyEntry is used;
     * <p/>
     * setEntry(..) throws KeyStoreException when incorrect Entry is used.
     * <p/>
     * FIXME: this test should be changed to verify SecretKeyEntry.
     * It is not supported.
     */
    public void testEntry03() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        TestKeyPair tkp = new TestKeyPair("DSA");
        KeyStoreTestSupport.SKey secKey = new KeyStoreTestSupport.SKey("DSA",
                tkp.getPrivate().getEncoded());
        KeyStore.SecretKeyEntry sKey = new KeyStore.SecretKeyEntry(
                secKey);
        char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
        KeyStore.PasswordProtection pPath = new KeyStore.PasswordProtection(pwd);
        KeyStoreTestSupport.AnotherEntry aEntry = new KeyStoreTestSupport.AnotherEntry();
        KeyStoreTestSupport.ProtPar pPar = new KeyStoreTestSupport.ProtPar();
        KeyStore[] kss = createKS();
        assertNotNull("KeyStore objects were not created", kss);
        for (int i = 0; i < kss.length; i++) {
            kss[i].load(null, null);
            for (int j = 0; j < aliases.length; j++) {
                try {
                    kss[i].setEntry(aliases[j], sKey, pPath);
                } catch (KeyStoreException e) {
                    //logln("testEntry03: non-PrivateKeys not supported.");
                    return;
                }
            }

            for (int j = 0; j < aliases.length; j++) {
                assertTrue("Incorrect alias", kss[i].containsAlias(aliases[j]));
                assertTrue("Not KeyEntry", kss[i].isKeyEntry(aliases[j]));
                assertFalse("Incorrect CertificateEntry", kss[i].isCertificateEntry(aliases[j]));
                Key key1;
                try {
                    key1 = kss[i].getKey(aliases[j], pwd);
                } catch (UnrecoverableKeyException e) {
                    //logln("testEntry03: non-PrivateKeys not supported.");
                    return;
                }
                if (!secKey.getAlgorithm().equals(key1.getAlgorithm()) ||
                        !secKey.getFormat().equals(key1.getFormat())) {
                    fail("Incorrect key");
                }
                byte[] enc = secKey.getEncoded();
                byte[] enc1 = key1.getEncoded();
                assertTrue("Diff. keys encoding", Arrays.equals(enc, enc1));
                assertNull("Incorrect CertificateChain", kss[i].getCertificateChain(aliases[j]));
            }
        }
        pPath.destroy();
        for (int i = 0; i < kss.length; i++) {
            try {
                kss[i].setEntry("ZZZ", aEntry, pPath);
                fail("KeyStoreException should be thrown because password is destroyed");
            } catch (KeyStoreException e) {
            }
            for (int j = 0; j < aliases.length; j++) {
                try {
                    kss[i].getEntry(aliases[j], pPath);
                    fail("KeyStoreException should be thrown because password is destroyed");
                } catch (KeyStoreException e) {
                }
                try {
                    kss[i].getEntry(aliases[j], pPar);
                    fail("UnrecoverableEntryException should be thrown");
                } catch (UnrecoverableEntryException e) {
                }
            }
        }
    }


    /**
     * Test for
     * <code>setCertificateEntry(String alias, Certificate cert)</code>
     * <code>containsAlias(String alias)</code>
     * <code>getCertificate(String alias)</code>
     * <code>isCertificateEntry(String alias)</code>
     * methods
     * Assertions:
     * setCertificateEntry(..), containsAlias(..), getCertificate(..) and isCertificateEntry(..)
     * throw NullPointerException when alias is null
     * <p/>
     * setCertificateEntry(..) stores used entry and getCertificate(..) returns it
     */
    public void testEntry04() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStoreTestSupport.MCertificate cert = new KeyStoreTestSupport.MCertificate(
                "type", new byte[0]);
        KeyStore[] kss = createKS();
        assertNotNull("KeyStore objects were not created", kss);

        for (int i = 0; i < kss.length; i++) {
            kss[i].load(null, null);
            try {
                kss[i].setCertificateEntry(null, cert);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            for (int j = 0; j < aliases.length; j++) {
                kss[i].setCertificateEntry(aliases[j], cert);
            }
        }
        for (int i = 0; i < kss.length; i++) {
            try {
                kss[i].containsAlias(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            try {
                kss[i].isCertificateEntry(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            try {
                kss[i].getCertificate(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            for (int j = 0; j < aliases.length; j++) {
                assertFalse("Incorrect alias", kss[i].containsAlias("Bad".concat(aliases[j])));
                assertTrue("Incorrect alias", kss[i].containsAlias(aliases[j]));
                assertTrue("Not CertificateEntry", kss[i].isCertificateEntry(aliases[j]));
                assertFalse("Incorrect KeyEntry", kss[i].isKeyEntry(aliases[j]));
                assertEquals("Incorrect Certificate", kss[i].getCertificate(aliases[j]),
                        cert);
            }
        }
    }

    /**
     * Test for
     * <code>setKeyEntry(String alias, Key key, char[] password, Certificate[] chain)</code>
     * <code>containsAlias(String alias)</code>
     * <code>getKey(String alias, char[] password)</code>
     * <code>isCertificateEntry(String alias)</code>
     * <code>isKeyEntry(String alias)</code>
     * <code>setCerificateEntry(String alias, Certificate cert)</code>
     * <code>getCertificateChain(String alias)</code>
     * <code>getCertificateAlias(Certificate cert)</code>
     * methods
     * <p/>
     * Assertions:
     * setKeyEntry(..), getKeyEntry(..) and isKeyEntry(..)
     * throw NullPointerException when alias is null
     * <p/>
     * setKeyEntry(...) throws KeyStoreException when key or password
     * is null
     * <p/>
     * setCertificateEntry(..) throws KeyStoreException when KeyEntry was overwritten
     * <p/>
     * setKeyEntry(..) stores used entry, getKey(..) returns it and getCertificateChain(...)
     * returns cert
     */
    public void testEntry05() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStoreTestSupport.MCertificate certs[] = {
                new KeyStoreTestSupport.MCertificate("type1", new byte[10]),
                new KeyStoreTestSupport.MCertificate("type2", new byte[10]) };
        KeyStoreTestSupport.MCertificate cert = new KeyStoreTestSupport.MCertificate(
                "type", new byte[0]);
        char[] pwd = new char[0];
        TestKeyPair tkp = new TestKeyPair("DSA");
        PrivateKey key = tkp.getPrivate();
        KeyStore[] kss = createKS();
        assertNotNull("KeyStore objects were not created", kss);
        for (int i = 0; i < kss.length; i++) {
            kss[i].load(null, null);

            // Null as alias does not necessarily lead to NullPointerException

            try {
                kss[i].setKeyEntry("ZZZ", null, pwd, certs);
                fail("KeyStoreException should be thrown when key is null");
            } catch (KeyStoreException e) {
            }
            try {
                kss[i].setKeyEntry("ZZZ", key, pwd, null);
                fail("KeyStoreException or IllegalArgumentException should be thrown "
                        + "when chain is null and key is private");
            } catch (IllegalArgumentException e) {
            }
            try {
                kss[i].setKeyEntry("ZZZ", key, pwd,
                        new KeyStoreTestSupport.MCertificate[0]);
                fail("KeyStoreException or IllegalArgumentException should be thrown "
                        + "when chain is empty and key is private");
            } catch (IllegalArgumentException e) {
            }

            for (int j = 0; j < aliases.length; j++) {
                kss[i].setKeyEntry(aliases[j], key, pwd, certs);
            }

            kss[i].setKeyEntry("KeyAlias", key, pwd, certs);
            try {
                kss[i].setCertificateEntry("KeyAlias", cert);
                fail("KeyStoreException should be thrown when we try to overwrite KeyEntry to Certificate");
            } catch (KeyStoreException e) {
            }

            try {
                kss[i].isKeyEntry(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            try {
                kss[i].getKey(null, pwd);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            try {
                kss[i].getCertificateChain(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            for (int j = 0; j < aliases.length; j++) {
                assertFalse("Incorrect alias", kss[i].containsAlias("Bad".concat(aliases[j])));
                assertTrue("Incorrect alias", kss[i].containsAlias(aliases[j]));
                assertTrue("Not KeyEntry", kss[i].isKeyEntry(aliases[j]));
                assertFalse("Incorrect CertificateEntry", kss[i].isCertificateEntry(aliases[j]));
                Key key1 = kss[i].getKey(aliases[j], pwd);
                if (!key.getAlgorithm().equals(key1.getAlgorithm()) ||
                        !key.getFormat().equals(key1.getFormat())) {
                    fail("Incorrect key");
                }
                byte[] enc = key.getEncoded();
                byte[] enc1 = key1.getEncoded();
                assertTrue("Diff. keys encoding", Arrays.equals(enc, enc1));
                Certificate[] cc = kss[i].getCertificateChain(aliases[j]);
                assertEquals("Incorrect chain", cc.length, certs.length);
                for (int t = 0; t < cc.length; t++) {
                    assertEquals("Incorrect certificate", cc[t], certs[t]);
                }
            }
            assertNull(kss[i].getCertificateAlias(cert));
            String ss = kss[i].getCertificateAlias(certs[0]);
            boolean ans = false;
            for (int j = 1; j < aliases.length; j++) {
                if (ss.equals(aliases[j])) {
                    ans = true;
                    break;
                }
            }
            assertTrue("There is no alias for certificate <type1, new byte[10]>", ans);
        }
    }

    /**
     * Test for
     * <code>deleteEntry(String alias)</code>
     * <code>size()</code>
     * methods
     * Assertions:
     * throws NullPointerException when alias is null;
     * <p/>
     * deletes entry from KeyStore.
     */
    public void testEntry06() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStore.TrustedCertificateEntry tCert = new KeyStore.TrustedCertificateEntry(
                new KeyStoreTestSupport.MCertificate("type", new byte[0]));

        TestKeyPair tkp = new TestKeyPair("DSA");
        KeyStoreTestSupport.MCertificate certs[] = {
                new KeyStoreTestSupport.MCertificate("DSA", tkp.getPrivate()
                        .getEncoded()),
                new KeyStoreTestSupport.MCertificate("DSA", tkp.getPrivate()
                        .getEncoded()) };
        KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(tkp
                .getPrivate(), certs);
        char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
        KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pwd);

        String[] aliases = { "Alias1", "Alias2", "Alias3", "Alias4", "Alias5" };

        KeyStore[] kss = createKS();
        assertNotNull("KeyStore objects were not created", kss);

        for (int i = 0; i < kss.length; i++) {
            kss[i].load(null, null);
            kss[i].setEntry(aliases[0], tCert, null);
            kss[i].setEntry(aliases[1], pKey, pp);
            kss[i].setEntry(aliases[2], pKey, pp);

            kss[i].setKeyEntry(aliases[3], tkp.getPrivate(), pwd, certs);

            kss[i].setCertificateEntry(aliases[4], certs[0]);

            assertEquals("Incorrect size", kss[i].size(), 5);
            try {
                kss[i].deleteEntry(null);
                fail("NullPointerException should be thrown when alias is null");
            } catch (NullPointerException e) {
            }
            kss[i].deleteEntry(aliases[0]);
            kss[i].deleteEntry(aliases[3]);
            assertEquals("Incorrect size", kss[i].size(), 3);
            for (int j = 1; j < 5; j++) {
                if ((j == 0) || (j == 3)) {
                    assertFalse("Incorrect deleted alias", kss[i]
                            .containsAlias(aliases[j]));
                } else {
                    assertTrue("Incorrect alias", kss[i]
                            .containsAlias(aliases[j]));
                }
            }
        }
    }

    /**
     * Test for
     * <code>entryInstanceOf(String alias, Class class)</code>
     * method
     * Assertions:
     * throws NullPointerException when alias is null
     * returns false if KeyStore does not contain entry with defined alias
     * returns false if defined alias is not correspond Entry
     * returns false
     * setEntry(..) throws KeyStoreException when incorrect Entry is used;
     * <p/>
     * setEntry(..) stores Entry and getEntry(...) returns it when
     * KeyStore.PrivateKeyEntry is used.
     */
    public void testEntry07() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        TestKeyPair tkp = new TestKeyPair("DSA");
        KeyStoreTestSupport.MCertificate certs[] = {
                new KeyStoreTestSupport.MCertificate("DSA", tkp.getPrivate()
                        .getEncoded()),
                new KeyStoreTestSupport.MCertificate("DSA", tkp.getPrivate()
                        .getEncoded()) };
        PrivateKey privKey = tkp.getPrivate();
        KeyStore.PrivateKeyEntry pKey = new KeyStore.PrivateKeyEntry(privKey, certs);
        char[] pwd = { 'p', 'a', 's', 's', 'w', 'd' };
        String aliasKE = "KeyAlias";
        KeyStore.PasswordProtection pp = new KeyStore.PasswordProtection(pwd);
        new KeyStore.PasswordProtection(new char[0]);
        KeyStore[] kss = createKS();
        assertNotNull("KeyStore objects were not created", kss);

        for (int i = 0; i < kss.length; i++) {
            kss[i].load(null, null);
            // set entries
            for (int j = 0; j < aliases.length; j++) {
                kss[i].setEntry(aliases[j], pKey, pp);
            }
            kss[i].setKeyEntry(aliasKE, privKey, pwd, certs);
            try {
                kss[i].entryInstanceOf(null, pKey.getClass());
                fail("NullPointerEXception must be thrown");
            } catch (NullPointerException e) {
            }
            assertFalse("Incorrect class entry 1",
                    kss[i].entryInstanceOf("ZZZ", pKey.getClass()));
            for (int j = 0; j < aliases.length; j++) {
                assertTrue("Incorrect class entry 2",
                        kss[i].entryInstanceOf(aliases[j], pKey.getClass()));

                //make it compilable on 1.5
                Class c = privKey.getClass();
                assertFalse("Incorrect class entry 3",
                        kss[i].entryInstanceOf(aliases[j], c));
            }

            //make it compilable on 1.5
            Class c = privKey.getClass();
            assertFalse("Incorrect class entry 4",
                    kss[i].entryInstanceOf(aliasKE, c));
            assertTrue("Incorrect class entry 5",
                    kss[i].entryInstanceOf(aliasKE, pKey.getClass()));
        }
    }


    /**
     * Test for <code>KeyStore(KeyStoreSpi spi, Provider prov, String type)</code>
     * constructor
     * Assertion: constructs KeyStore object
     */
    public void testKeyStoreConstr() throws Exception {
        assertTrue(NotSupportMsg, JKSSupported);
        KeyStoreSpi spi = new MyKeyStoreSpi();
        KeyStore keySt = new tmpKeyStore(spi, defaultProvider,
                defaultType);
        assertEquals("Incorrect name", keySt.getType(),
                defaultType);
        assertEquals("Incorrect provider", keySt.getProvider(), defaultProvider);
        char[] pwd = new char[0];
        try {
            keySt.store(null, pwd);
            fail("KeyStoreException must be thrown");
        } catch (KeyStoreException e) {
        }
        keySt = new tmpKeyStore(null, null, null);
        assertNull("Algorithm must be null", keySt.getType());
        assertNull("Provider must be null", keySt.getProvider());
        try {
            keySt.load(null, pwd);
            fail("NullPointerException must be thrown");
        } catch (NullPointerException e) {
        }
    }

}

/**
 * Additional class to verify KeyStore constructor
 */
class tmpKeyStore extends KeyStore {
    public tmpKeyStore(KeyStoreSpi spi, Provider prov, String alg) {
        super(spi, prov, alg);
    }
}