OSDN Git Service

AI 144825: am: CL 144820 am: CL 144816 Bringing XNET down to zero broken tests.
[android-x86/dalvik.git] / libcore / x-net / src / test / java / tests / api / javax / net / ssl / KeyManagerFactory1Test.java
1 /*
2  *  Licensed to the Apache Software Foundation (ASF) under one or more
3  *  contributor license agreements.  See the NOTICE file distributed with
4  *  this work for additional information regarding copyright ownership.
5  *  The ASF licenses this file to You under the Apache License, Version 2.0
6  *  (the "License"); you may not use this file except in compliance with
7  *  the License.  You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an "AS IS" BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  */
17
18 package tests.api.javax.net.ssl;
19
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.KnownFailure;
24
25 import java.io.IOException;
26 import java.security.InvalidAlgorithmParameterException;
27 import java.security.KeyStore;
28 import java.security.KeyStoreException;
29 import java.security.NoSuchAlgorithmException;
30 import java.security.NoSuchProviderException;
31 import java.security.Provider;
32 import java.security.Security;
33 import java.security.UnrecoverableKeyException;
34 import java.security.cert.CertificateException;
35
36 import javax.net.ssl.KeyStoreBuilderParameters;
37 import javax.net.ssl.ManagerFactoryParameters;
38 import javax.net.ssl.KeyManager;
39 import javax.net.ssl.KeyManagerFactory;
40 import javax.net.ssl.KeyManagerFactorySpi;
41
42 import org.apache.harmony.security.tests.support.SpiEngUtils;
43 import org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi;
44
45 import junit.framework.TestCase;
46
47 /**
48  * Tests for <code>KeyManagerFactory</code> class constructors and methods.
49  * 
50  */
51 @TestTargetClass(KeyManagerFactory.class)
52 public class KeyManagerFactory1Test extends TestCase {
53
54     private static final String srvKeyManagerFactory = "KeyManagerFactory";
55
56     private static String defaultAlgorithm = null;
57
58     private static String defaultProviderName = null;
59
60     private static Provider defaultProvider = null;
61
62     private static boolean DEFSupported = false;
63
64     private static final String NotSupportedMsg = "There is no suitable provider for KeyManagerFactory";
65
66     private static final String[] invalidValues = SpiEngUtils.invalidValues;
67
68     private static String[] validValues = new String[3];
69     static {
70         defaultAlgorithm = Security
71                 .getProperty("ssl.KeyManagerFactory.algorithm");
72         if (defaultAlgorithm != null) {
73             defaultProvider = SpiEngUtils.isSupport(defaultAlgorithm,
74                     srvKeyManagerFactory);            
75             DEFSupported = (defaultProvider != null);
76             defaultProviderName = (DEFSupported ? defaultProvider.getName()
77                     : null);
78             validValues[0] = defaultAlgorithm;
79             validValues[1] = defaultAlgorithm.toUpperCase();
80             validValues[2] = defaultAlgorithm.toLowerCase();
81         }
82     }
83
84     protected KeyManagerFactory[] createKMFac() {
85         if (!DEFSupported) {
86             fail(defaultAlgorithm + " algorithm is not supported");
87             return null;
88         }
89         KeyManagerFactory[] kMF = new KeyManagerFactory[3];
90         try {
91             kMF[0] = KeyManagerFactory.getInstance(defaultAlgorithm);
92             kMF[1] = KeyManagerFactory.getInstance(defaultAlgorithm,
93                     defaultProvider);
94             kMF[2] = KeyManagerFactory.getInstance(defaultAlgorithm,
95                     defaultProviderName);
96             return kMF;
97         } catch (Exception e) {
98             e.printStackTrace();
99             return null;
100         }
101     }
102
103     /**
104      * @tests avax.net.ssl.KeyManagerFactory#getAlgorithm() 
105      */
106     @TestTargetNew(
107         level = TestLevel.COMPLETE,
108         notes = "",
109         method = "getAlgorithm",
110         args = {}
111     )
112     public void test_getAlgorithm()
113         throws NoSuchAlgorithmException, NoSuchProviderException {
114         if (!DEFSupported) fail(NotSupportedMsg);
115         assertEquals("Incorrect algorithm",
116                 defaultAlgorithm,
117                 KeyManagerFactory
118                 .getInstance(defaultAlgorithm).getAlgorithm());
119         assertEquals("Incorrect algorithm",
120                 defaultAlgorithm,
121                 KeyManagerFactory
122                 .getInstance(defaultAlgorithm, defaultProviderName)
123                 .getAlgorithm());
124         assertEquals("Incorrect algorithm",
125                 defaultAlgorithm,
126                 KeyManagerFactory.getInstance(defaultAlgorithm, defaultProvider)
127                 .getAlgorithm());
128     }
129
130        /**
131      *  Test for <code>getDefaultAlgorithm()</code> method
132      * Assertion: returns value which is specifoed in security property
133      */
134     @TestTargetNew(
135         level = TestLevel.COMPLETE,
136         notes = "",
137         method = "getDefaultAlgorithm",
138         args = {}
139     )
140     public void test_getDefaultAlgorithm() {
141         if (!DEFSupported) {
142             fail(NotSupportedMsg);
143             return;
144         }
145         String def = KeyManagerFactory.getDefaultAlgorithm();
146         if (defaultAlgorithm == null) {
147             assertNull("DefaultAlgorithm must be null", def);
148         } else {
149             assertEquals("Invalid default algorithm", def, defaultAlgorithm);
150         }
151         String defA = "Proba.keymanagerfactory.defaul.type";
152         Security.setProperty("ssl.KeyManagerFactory.algorithm", defA);
153         assertEquals("Incorrect defaultAlgorithm", 
154                 KeyManagerFactory.getDefaultAlgorithm(), defA);
155         if (def == null) {
156             def = "";
157         }
158         Security.setProperty("ssl.KeyManagerFactory.algorithm", def); 
159         assertEquals("Incorrect defaultAlgorithm", 
160                 KeyManagerFactory.getDefaultAlgorithm(), def);        
161     }
162     
163     /**
164      * Test for <code>getInstance(String algorithm)</code> method
165      * Assertions: 
166      * returns security property "ssl.KeyManagerFactory.algorithm";
167      * returns instance of KeyManagerFactory
168      */
169     @TestTargetNew(
170         level = TestLevel.PARTIAL_COMPLETE,
171         notes = "",
172         method = "getInstance",
173         args = {java.lang.String.class}
174     )
175     public void test_getInstanceLjava_lang_String01() throws NoSuchAlgorithmException {
176         if (!DEFSupported) {
177             fail(NotSupportedMsg);
178             return;
179         }
180         KeyManagerFactory keyMF;
181         for (int i = 0; i < validValues.length; i++) {
182             keyMF = KeyManagerFactory.getInstance(validValues[i]);
183             assertTrue("Not KeyManagerFactory object",
184                     keyMF instanceof KeyManagerFactory);
185             assertEquals("Invalid algorithm", keyMF.getAlgorithm(),
186                     validValues[i]);
187         }
188     }
189
190     /**
191      * Test for <code>getInstance(String algorithm)</code> method 
192      * Assertion:
193      * throws NullPointerException when algorithm is null;
194      * throws NoSuchAlgorithmException when algorithm is not correct;
195      */
196     @TestTargetNew(
197         level = TestLevel.PARTIAL_COMPLETE,
198         notes = "",
199         method = "getInstance",
200         args = {java.lang.String.class}
201     )
202     public void test_getInstanceLjava_lang_String02() {
203         try {
204             KeyManagerFactory.getInstance(null);
205             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
206         } catch (NoSuchAlgorithmException e) {
207         } catch (NullPointerException e) {
208         }
209         for (int i = 0; i < invalidValues.length; i++) {
210             try {
211                 KeyManagerFactory.getInstance(invalidValues[i]);
212                 fail("NoSuchAlgorithmException was not thrown as expected for algorithm: "
213                         .concat(invalidValues[i]));
214             } catch (NoSuchAlgorithmException e) {
215             }
216         }
217     }
218
219     /**
220      * Test for <code>getInstance(String algorithm, String provider)</code>
221      * method 
222      * Assertion: throws IllegalArgumentException when provider is null or empty
223      */
224     @TestTargetNew(
225         level = TestLevel.PARTIAL_COMPLETE,
226         notes = "",
227         method = "getInstance",
228         args = {java.lang.String.class, java.lang.String.class}
229     )
230     public void test_getInstanceLjava_lang_StringLjava_lang_String01() throws NoSuchProviderException,
231             NoSuchAlgorithmException {
232         if (!DEFSupported) {
233             fail(NotSupportedMsg);
234             return;
235         }
236         String provider = null;
237         for (int i = 0; i < validValues.length; i++) {
238             try {
239                 KeyManagerFactory.getInstance(validValues[i], provider);
240                 fail("Expected IllegalArgumentException was not thrown for null provider");
241             } catch (IllegalArgumentException e) {
242             }
243             try {
244                 KeyManagerFactory.getInstance(validValues[i], "");
245                 fail("Expected IllegalArgumentException was not thrown for empty provider");
246             } catch (IllegalArgumentException e) {
247             }
248         }
249     }
250
251     /**
252      * Test for <code>getInstance(String algorithm, String provider)</code>
253      * method 
254      * Assertion: 
255      * throws NullPointerException when algorithm is null;
256      * throws NoSuchAlgorithmException when algorithm is not correct;
257      */
258     @TestTargetNew(
259         level = TestLevel.PARTIAL_COMPLETE,
260         notes = "",
261         method = "getInstance",
262         args = {java.lang.String.class, java.lang.String.class}
263     )
264     public void test_getInstanceLjava_lang_StringLjava_lang_String02() throws NoSuchProviderException {
265         if (!DEFSupported) {
266             fail(NotSupportedMsg);
267             return;
268         }
269         try {
270             KeyManagerFactory.getInstance(null, defaultProviderName);
271             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
272         } catch (NoSuchAlgorithmException e) {
273         } catch (NullPointerException e) {
274         }
275         for (int i = 0; i < invalidValues.length; i++) {
276             try {
277                 KeyManagerFactory.getInstance(invalidValues[i],
278                         defaultProviderName);
279                 fail("NoSuchAlgorithmException must be thrown (algorithm: "
280                         .concat(invalidValues[i]).concat(")"));
281             } catch (NoSuchAlgorithmException e) {
282             }
283         }
284     }
285
286     /**
287      * Test for <code>getInstance(String algorithm, String provider)</code>
288      * method 
289      * Assertion: throws NoSuchProviderException when provider has
290      * invalid value
291      */
292     @TestTargetNew(
293         level = TestLevel.PARTIAL_COMPLETE,
294         notes = "",
295         method = "getInstance",
296         args = {java.lang.String.class, java.lang.String.class}
297     )
298     public void test_getInstanceLjava_lang_StringLjava_lang_String03()
299         throws NoSuchAlgorithmException {
300         if (!DEFSupported) {
301             fail(NotSupportedMsg);
302             return;
303         }
304         for (int i = 0; i < validValues.length; i++) {
305             for (int j = 1; j < invalidValues.length; j++) {
306                 try {
307                     KeyManagerFactory.getInstance(validValues[i],
308                             invalidValues[j]);
309                     fail("NuSuchProviderException must be thrown (algorithm: "
310                             + validValues[i] + " provider: " + invalidValues[j]
311                             + ")");
312                 } catch (NoSuchProviderException e) {
313                 }
314             }
315         }
316     }
317
318     /**
319      * Test for <code>getInstance(String algorithm, String provider)</code>
320      * method Assertion: returns instance of KeyManagerFactory
321      */
322     @TestTargetNew(
323         level = TestLevel.PARTIAL_COMPLETE,
324         notes = "",
325         method = "getInstance",
326         args = {java.lang.String.class, java.lang.String.class}
327     )
328     public void test_getInstanceLjava_lang_StringLjava_lang_String04()
329         throws NoSuchProviderException,
330             NoSuchAlgorithmException {
331         if (!DEFSupported) {
332             fail(NotSupportedMsg);
333             return;
334         }
335         KeyManagerFactory kMF;
336         for (int i = 0; i < validValues.length; i++) {
337             kMF = KeyManagerFactory.getInstance(validValues[i],
338                     defaultProviderName);
339             assertTrue("Not KeyManagerFactory object",
340                     kMF instanceof KeyManagerFactory);
341             assertEquals("Incorrect algorithm", kMF.getAlgorithm(),
342                     validValues[i]);
343             assertEquals("Incorrect provider", kMF.getProvider().getName(),
344                     defaultProviderName);
345         }
346     }
347
348     /**
349      * Test for <code>getInstance(String algorithm, Provider provider)</code>
350      * method 
351      * Assertion: throws IllegalArgumentException when provider is null
352      */
353     @TestTargetNew(
354         level = TestLevel.PARTIAL_COMPLETE,
355         notes = "",
356         method = "getInstance",
357         args = {java.lang.String.class, java.security.Provider.class}
358     )
359     public void test_getInstanceLjava_lang_StringLjava_security_Provider01()
360         throws NoSuchAlgorithmException {
361         if (!DEFSupported) {
362             fail(NotSupportedMsg);
363             return;
364         }
365         Provider provider = null;
366         for (int i = 0; i < validValues.length; i++) {
367             try {
368                 KeyManagerFactory.getInstance(validValues[i], provider);
369                 fail("Expected IllegalArgumentException was not thrown when provider is null");
370             } catch (IllegalArgumentException e) {
371             }
372         }
373     }
374
375     /**
376      * Test for <code>getInstance(String algorithm, Provider provider)</code>
377      * method 
378      * Assertion: 
379      * throws NullPointerException when algorithm is null;
380      * throws NoSuchAlgorithmException when algorithm is not correct;
381      */
382     @TestTargetNew(
383         level = TestLevel.PARTIAL_COMPLETE,
384         notes = "",
385         method = "getInstance",
386         args = {java.lang.String.class, java.security.Provider.class}
387     )
388     public void test_getInstanceLjava_lang_StringLjava_security_Provider02() {
389         if (!DEFSupported) {
390             fail(NotSupportedMsg);
391             return;
392         }        
393         try {
394             KeyManagerFactory.getInstance(null, defaultProvider);
395             fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
396         } catch (NoSuchAlgorithmException e) {
397         } catch (NullPointerException e) {
398         }
399         for (int i = 0; i < invalidValues.length; i++) {
400             try {
401                 KeyManagerFactory
402                         .getInstance(invalidValues[i], defaultProvider);
403                 fail("Expected NuSuchAlgorithmException was not thrown");
404             } catch (NoSuchAlgorithmException e) {
405             }
406         } 
407     }
408
409     /**
410      * Test for <code>getInstance(String algorithm, Provider provider)</code>
411      * method 
412      * Assertion: returns instance of KeyManagerFactory
413      */
414     @TestTargetNew(
415         level = TestLevel.PARTIAL_COMPLETE,
416         notes = "",
417         method = "getInstance",
418         args = {java.lang.String.class, java.security.Provider.class}
419     )
420     public void test_getInstanceLjava_lang_StringLjava_security_Provider03()
421         throws NoSuchAlgorithmException,
422             IllegalArgumentException {
423         if (!DEFSupported) {
424             fail(NotSupportedMsg);
425             return;
426         }
427         KeyManagerFactory kMF;
428         for (int i = 0; i < validValues.length; i++) {
429             kMF = KeyManagerFactory
430                     .getInstance(validValues[i], defaultProvider);
431             assertTrue(kMF instanceof KeyManagerFactory);
432             assertEquals(kMF.getAlgorithm(), validValues[i]);
433             assertEquals(kMF.getProvider(), defaultProvider);
434         }
435     }
436
437     /**
438      * Test for <code>KeyManagerFactory</code> constructor 
439      * Assertion: returns KeyManagerFactory object
440      */
441     @TestTargetNew(
442         level = TestLevel.COMPLETE,
443         notes = "",
444         method = "KeyManagerFactory",
445         args = {javax.net.ssl.KeyManagerFactorySpi.class, java.security.Provider.class, java.lang.String.class}
446     )
447     public void test_Constructor() throws NoSuchAlgorithmException {
448         if (!DEFSupported) {
449             fail(NotSupportedMsg);
450             return;
451         }
452         KeyManagerFactorySpi spi = new MyKeyManagerFactorySpi();
453         KeyManagerFactory keyMF = new myKeyManagerFactory(spi, defaultProvider,
454                 defaultAlgorithm);
455         assertTrue("Not CertStore object", keyMF instanceof KeyManagerFactory);
456         assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
457                 defaultAlgorithm);
458         assertEquals("Incorrect provider", keyMF.getProvider(), defaultProvider);
459         try {
460             keyMF.init(null, new char[1]);
461             fail("UnrecoverableKeyException must be thrown");
462         } catch (UnrecoverableKeyException e) {
463         } catch (Exception e) {
464             fail("Unexpected: "+e.toString()+" was thrown");
465         }
466         keyMF = new myKeyManagerFactory(null, null, null);
467         assertTrue("Not CertStore object", keyMF instanceof KeyManagerFactory);
468         assertNull("Aalgorithm must be null", keyMF.getAlgorithm());
469         assertNull("Provider must be null", keyMF.getProvider());
470         try {
471             keyMF.getKeyManagers();
472         } catch (NullPointerException e) {
473         }
474     }
475
476     /**
477      * @tests avax.net.ssl.KeyManagerFactory#getKeyManagers()
478      * @throws NoSuchAlgorithmException 
479      * @throws KeyStoreException 
480      * @throws IOException 
481      * @throws CertificateException 
482      * @throws UnrecoverableKeyException
483      */
484     @TestTargetNew(
485         level = TestLevel.COMPLETE,
486         notes = "",
487         method = "getKeyManagers",
488         args = {}
489     )
490     public void test_getKeyManagers()
491         throws Exception {
492         if (!DEFSupported) fail(NotSupportedMsg);
493         KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
494         char[] pass = "password".toCharArray();
495         kmf.init(null, pass);
496         assertNotNull("Key manager array is null", kmf.getKeyManagers());
497         assertEquals("Incorrect size of array",
498                 1, kmf.getKeyManagers().length);
499     }
500     
501     /**
502      * @tests avax.net.ssl.KeyManagerFactory#getProvider()
503      */
504     @TestTargetNew(
505         level = TestLevel.COMPLETE,
506         notes = "",
507         method = "getProvider",
508         args = {}
509     )
510     public void test_getProvider()
511         throws Exception {
512         if (!DEFSupported) fail(NotSupportedMsg);
513         assertEquals("Incorrect provider",
514                 defaultProvider,
515                 KeyManagerFactory
516                 .getInstance(defaultAlgorithm).getProvider());
517         assertEquals("Incorrect provider",
518                 defaultProvider,
519                 KeyManagerFactory
520                 .getInstance(defaultAlgorithm, defaultProviderName)
521                 .getProvider());
522         assertEquals("Incorrect provider",
523                 defaultProvider,
524                 KeyManagerFactory.getInstance(defaultAlgorithm, defaultProvider)
525                 .getProvider());
526     }
527
528     /**
529      * Test for <code>init(KeyStore keyStore, char[] password)</code> and
530      * <code>getKeyManagers()</code> 
531      * Assertion: returns not empty KeyManager array
532      */
533     @TestTargetNew(
534         level = TestLevel.SUFFICIENT,
535         notes = "KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException checking missed",
536         method = "init",
537         args = {java.security.KeyStore.class, char[].class}
538     )
539     public void test_initLjava_security_KeyStore$C()
540         throws NoSuchAlgorithmException,
541         KeyStoreException, UnrecoverableKeyException {
542         if (!DEFSupported) {
543             fail(NotSupportedMsg);
544             return;
545         }
546         KeyManagerFactory[] keyMF = createKMFac();
547         assertNotNull("KeyManagerFactory object were not created", keyMF);
548         KeyStore ksNull = null;
549         KeyManager[] km;
550         for (int i = 0; i < keyMF.length; i++) {
551             keyMF[i].init(ksNull, new char[10]);
552             km = keyMF[i].getKeyManagers();
553             assertNotNull("Result should not be null", km);
554             assertTrue("Length of result KeyManager array should not be 0",
555                     (km.length > 0));
556         }
557         KeyStore ks;
558         try {
559             ks = KeyStore.getInstance(KeyStore.getDefaultType());
560             ks.load(null, null);
561         } catch (KeyStoreException e) {
562             fail(e.toString() + "default KeyStore type is not supported");
563             return;
564         } catch (Exception e) {
565             fail("Unexpected: " + e.toString());
566             return;
567         }
568         for (int i = 0; i < keyMF.length; i++) {
569             try {
570                 keyMF[i].init(ks, new char[10]);
571             } catch (KeyStoreException e) {              
572             }
573             km = keyMF[i].getKeyManagers();
574             assertNotNull("Result has not be null", km);
575             assertTrue("Length of result KeyManager array should not be 0",
576                     (km.length > 0));
577         }
578       
579     }
580
581     /**
582      * Test for <code>init(ManagerFactoryParameters params)</code> 
583      * Assertion:
584      * throws InvalidAlgorithmParameterException when params is null
585      */
586     @TestTargetNew(
587         level = TestLevel.COMPLETE,
588         notes = "functionality is not implemented in org.apache.harmony.xnet.provider.jsse.engineInit(ManagerFactoryParameters)",
589         method = "init",
590         args = {javax.net.ssl.ManagerFactoryParameters.class}
591     )
592     public void test_initLjavax_net_ssl_ManagerFactoryParameters()
593         throws NoSuchAlgorithmException {
594
595         if (!DEFSupported) {
596             fail(NotSupportedMsg);
597             return;
598         }
599         ManagerFactoryParameters par = null;
600         KeyManagerFactory[] keyMF = createKMFac();
601         assertNotNull("KeyManagerFactory object were not created", keyMF);
602         for (int i = 0; i < keyMF.length; i++) {
603             try {
604                 keyMF[i].init(par);
605                 fail("InvalidAlgorithmParameterException must be thrown");
606             } catch (InvalidAlgorithmParameterException e) {
607             }
608         }
609         
610         KeyStore.ProtectionParameter pp = new ProtectionParameterImpl();
611         KeyStore.Builder bld = KeyStore.Builder.newInstance("testType", null, pp);
612         assertNotNull("Null object KeyStore.Builder", bld);
613         
614         try {
615             KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
616             KeyStoreBuilderParameters ksp = new KeyStoreBuilderParameters(bld);
617             assertNotNull(ksp.getParameters());
618             kmf.init(ksp);
619             fail("InvalidAlgorithmParameterException must be thrown");
620         } catch (InvalidAlgorithmParameterException e) {
621         }
622     }
623     
624 }
625
626 /**
627  * Additional class for KeyManagerFactory constructor verification
628  */
629 class myKeyManagerFactory extends KeyManagerFactory {
630     public myKeyManagerFactory(KeyManagerFactorySpi spi, Provider prov,
631             String alg) {
632         super(spi, prov, alg);
633     }
634 }
635
636 class ProtectionParameterImpl implements KeyStore.ProtectionParameter {
637     ProtectionParameterImpl(){}
638 }