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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package tests.api.javax.net.ssl;
20 import dalvik.annotation.TestTargetClass;
21 import dalvik.annotation.TestLevel;
22 import dalvik.annotation.TestTargetNew;
23 import dalvik.annotation.KnownFailure;
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;
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;
42 import org.apache.harmony.security.tests.support.SpiEngUtils;
43 import org.apache.harmony.xnet.tests.support.MyKeyManagerFactorySpi;
45 import junit.framework.TestCase;
48 * Tests for <code>KeyManagerFactory</code> class constructors and methods.
51 @TestTargetClass(KeyManagerFactory.class)
52 public class KeyManagerFactory1Test extends TestCase {
54 private static final String srvKeyManagerFactory = "KeyManagerFactory";
56 private static String defaultAlgorithm = null;
58 private static String defaultProviderName = null;
60 private static Provider defaultProvider = null;
62 private static boolean DEFSupported = false;
64 private static final String NotSupportedMsg = "There is no suitable provider for KeyManagerFactory";
66 private static final String[] invalidValues = SpiEngUtils.invalidValues;
68 private static String[] validValues = new String[3];
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()
78 validValues[0] = defaultAlgorithm;
79 validValues[1] = defaultAlgorithm.toUpperCase();
80 validValues[2] = defaultAlgorithm.toLowerCase();
84 protected KeyManagerFactory[] createKMFac() {
86 fail(defaultAlgorithm + " algorithm is not supported");
89 KeyManagerFactory[] kMF = new KeyManagerFactory[3];
91 kMF[0] = KeyManagerFactory.getInstance(defaultAlgorithm);
92 kMF[1] = KeyManagerFactory.getInstance(defaultAlgorithm,
94 kMF[2] = KeyManagerFactory.getInstance(defaultAlgorithm,
97 } catch (Exception e) {
104 * @tests avax.net.ssl.KeyManagerFactory#getAlgorithm()
107 level = TestLevel.COMPLETE,
109 method = "getAlgorithm",
112 public void test_getAlgorithm()
113 throws NoSuchAlgorithmException, NoSuchProviderException {
114 if (!DEFSupported) fail(NotSupportedMsg);
115 assertEquals("Incorrect algorithm",
118 .getInstance(defaultAlgorithm).getAlgorithm());
119 assertEquals("Incorrect algorithm",
122 .getInstance(defaultAlgorithm, defaultProviderName)
124 assertEquals("Incorrect algorithm",
126 KeyManagerFactory.getInstance(defaultAlgorithm, defaultProvider)
131 * Test for <code>getDefaultAlgorithm()</code> method
132 * Assertion: returns value which is specifoed in security property
135 level = TestLevel.COMPLETE,
137 method = "getDefaultAlgorithm",
140 public void test_getDefaultAlgorithm() {
142 fail(NotSupportedMsg);
145 String def = KeyManagerFactory.getDefaultAlgorithm();
146 if (defaultAlgorithm == null) {
147 assertNull("DefaultAlgorithm must be null", def);
149 assertEquals("Invalid default algorithm", def, defaultAlgorithm);
151 String defA = "Proba.keymanagerfactory.defaul.type";
152 Security.setProperty("ssl.KeyManagerFactory.algorithm", defA);
153 assertEquals("Incorrect defaultAlgorithm",
154 KeyManagerFactory.getDefaultAlgorithm(), defA);
158 Security.setProperty("ssl.KeyManagerFactory.algorithm", def);
159 assertEquals("Incorrect defaultAlgorithm",
160 KeyManagerFactory.getDefaultAlgorithm(), def);
164 * Test for <code>getInstance(String algorithm)</code> method
166 * returns security property "ssl.KeyManagerFactory.algorithm";
167 * returns instance of KeyManagerFactory
170 level = TestLevel.PARTIAL_COMPLETE,
172 method = "getInstance",
173 args = {java.lang.String.class}
175 public void test_getInstanceLjava_lang_String01() throws NoSuchAlgorithmException {
177 fail(NotSupportedMsg);
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(),
191 * Test for <code>getInstance(String algorithm)</code> method
193 * throws NullPointerException when algorithm is null;
194 * throws NoSuchAlgorithmException when algorithm is not correct;
197 level = TestLevel.PARTIAL_COMPLETE,
199 method = "getInstance",
200 args = {java.lang.String.class}
202 public void test_getInstanceLjava_lang_String02() {
204 KeyManagerFactory.getInstance(null);
205 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
206 } catch (NoSuchAlgorithmException e) {
207 } catch (NullPointerException e) {
209 for (int i = 0; i < invalidValues.length; i++) {
211 KeyManagerFactory.getInstance(invalidValues[i]);
212 fail("NoSuchAlgorithmException was not thrown as expected for algorithm: "
213 .concat(invalidValues[i]));
214 } catch (NoSuchAlgorithmException e) {
220 * Test for <code>getInstance(String algorithm, String provider)</code>
222 * Assertion: throws IllegalArgumentException when provider is null or empty
225 level = TestLevel.PARTIAL_COMPLETE,
227 method = "getInstance",
228 args = {java.lang.String.class, java.lang.String.class}
230 public void test_getInstanceLjava_lang_StringLjava_lang_String01() throws NoSuchProviderException,
231 NoSuchAlgorithmException {
233 fail(NotSupportedMsg);
236 String provider = null;
237 for (int i = 0; i < validValues.length; i++) {
239 KeyManagerFactory.getInstance(validValues[i], provider);
240 fail("Expected IllegalArgumentException was not thrown for null provider");
241 } catch (IllegalArgumentException e) {
244 KeyManagerFactory.getInstance(validValues[i], "");
245 fail("Expected IllegalArgumentException was not thrown for empty provider");
246 } catch (IllegalArgumentException e) {
252 * Test for <code>getInstance(String algorithm, String provider)</code>
255 * throws NullPointerException when algorithm is null;
256 * throws NoSuchAlgorithmException when algorithm is not correct;
259 level = TestLevel.PARTIAL_COMPLETE,
261 method = "getInstance",
262 args = {java.lang.String.class, java.lang.String.class}
264 public void test_getInstanceLjava_lang_StringLjava_lang_String02() throws NoSuchProviderException {
266 fail(NotSupportedMsg);
270 KeyManagerFactory.getInstance(null, defaultProviderName);
271 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
272 } catch (NoSuchAlgorithmException e) {
273 } catch (NullPointerException e) {
275 for (int i = 0; i < invalidValues.length; i++) {
277 KeyManagerFactory.getInstance(invalidValues[i],
278 defaultProviderName);
279 fail("NoSuchAlgorithmException must be thrown (algorithm: "
280 .concat(invalidValues[i]).concat(")"));
281 } catch (NoSuchAlgorithmException e) {
287 * Test for <code>getInstance(String algorithm, String provider)</code>
289 * Assertion: throws NoSuchProviderException when provider has
293 level = TestLevel.PARTIAL_COMPLETE,
295 method = "getInstance",
296 args = {java.lang.String.class, java.lang.String.class}
298 public void test_getInstanceLjava_lang_StringLjava_lang_String03()
299 throws NoSuchAlgorithmException {
301 fail(NotSupportedMsg);
304 for (int i = 0; i < validValues.length; i++) {
305 for (int j = 1; j < invalidValues.length; j++) {
307 KeyManagerFactory.getInstance(validValues[i],
309 fail("NuSuchProviderException must be thrown (algorithm: "
310 + validValues[i] + " provider: " + invalidValues[j]
312 } catch (NoSuchProviderException e) {
319 * Test for <code>getInstance(String algorithm, String provider)</code>
320 * method Assertion: returns instance of KeyManagerFactory
323 level = TestLevel.PARTIAL_COMPLETE,
325 method = "getInstance",
326 args = {java.lang.String.class, java.lang.String.class}
328 public void test_getInstanceLjava_lang_StringLjava_lang_String04()
329 throws NoSuchProviderException,
330 NoSuchAlgorithmException {
332 fail(NotSupportedMsg);
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(),
343 assertEquals("Incorrect provider", kMF.getProvider().getName(),
344 defaultProviderName);
349 * Test for <code>getInstance(String algorithm, Provider provider)</code>
351 * Assertion: throws IllegalArgumentException when provider is null
354 level = TestLevel.PARTIAL_COMPLETE,
356 method = "getInstance",
357 args = {java.lang.String.class, java.security.Provider.class}
359 public void test_getInstanceLjava_lang_StringLjava_security_Provider01()
360 throws NoSuchAlgorithmException {
362 fail(NotSupportedMsg);
365 Provider provider = null;
366 for (int i = 0; i < validValues.length; i++) {
368 KeyManagerFactory.getInstance(validValues[i], provider);
369 fail("Expected IllegalArgumentException was not thrown when provider is null");
370 } catch (IllegalArgumentException e) {
376 * Test for <code>getInstance(String algorithm, Provider provider)</code>
379 * throws NullPointerException when algorithm is null;
380 * throws NoSuchAlgorithmException when algorithm is not correct;
383 level = TestLevel.PARTIAL_COMPLETE,
385 method = "getInstance",
386 args = {java.lang.String.class, java.security.Provider.class}
388 public void test_getInstanceLjava_lang_StringLjava_security_Provider02() {
390 fail(NotSupportedMsg);
394 KeyManagerFactory.getInstance(null, defaultProvider);
395 fail("NoSuchAlgorithmException or NullPointerException should be thrown (algorithm is null");
396 } catch (NoSuchAlgorithmException e) {
397 } catch (NullPointerException e) {
399 for (int i = 0; i < invalidValues.length; i++) {
402 .getInstance(invalidValues[i], defaultProvider);
403 fail("Expected NuSuchAlgorithmException was not thrown");
404 } catch (NoSuchAlgorithmException e) {
410 * Test for <code>getInstance(String algorithm, Provider provider)</code>
412 * Assertion: returns instance of KeyManagerFactory
415 level = TestLevel.PARTIAL_COMPLETE,
417 method = "getInstance",
418 args = {java.lang.String.class, java.security.Provider.class}
420 public void test_getInstanceLjava_lang_StringLjava_security_Provider03()
421 throws NoSuchAlgorithmException,
422 IllegalArgumentException {
424 fail(NotSupportedMsg);
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);
438 * Test for <code>KeyManagerFactory</code> constructor
439 * Assertion: returns KeyManagerFactory object
442 level = TestLevel.COMPLETE,
444 method = "KeyManagerFactory",
445 args = {javax.net.ssl.KeyManagerFactorySpi.class, java.security.Provider.class, java.lang.String.class}
447 public void test_Constructor() throws NoSuchAlgorithmException {
449 fail(NotSupportedMsg);
452 KeyManagerFactorySpi spi = new MyKeyManagerFactorySpi();
453 KeyManagerFactory keyMF = new myKeyManagerFactory(spi, defaultProvider,
455 assertTrue("Not CertStore object", keyMF instanceof KeyManagerFactory);
456 assertEquals("Incorrect algorithm", keyMF.getAlgorithm(),
458 assertEquals("Incorrect provider", keyMF.getProvider(), defaultProvider);
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");
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());
471 keyMF.getKeyManagers();
472 } catch (NullPointerException e) {
477 * @tests avax.net.ssl.KeyManagerFactory#getKeyManagers()
478 * @throws NoSuchAlgorithmException
479 * @throws KeyStoreException
480 * @throws IOException
481 * @throws CertificateException
482 * @throws UnrecoverableKeyException
485 level = TestLevel.COMPLETE,
487 method = "getKeyManagers",
490 public void test_getKeyManagers()
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);
502 * @tests avax.net.ssl.KeyManagerFactory#getProvider()
505 level = TestLevel.COMPLETE,
507 method = "getProvider",
510 public void test_getProvider()
512 if (!DEFSupported) fail(NotSupportedMsg);
513 assertEquals("Incorrect provider",
516 .getInstance(defaultAlgorithm).getProvider());
517 assertEquals("Incorrect provider",
520 .getInstance(defaultAlgorithm, defaultProviderName)
522 assertEquals("Incorrect provider",
524 KeyManagerFactory.getInstance(defaultAlgorithm, defaultProvider)
529 * Test for <code>init(KeyStore keyStore, char[] password)</code> and
530 * <code>getKeyManagers()</code>
531 * Assertion: returns not empty KeyManager array
534 level = TestLevel.SUFFICIENT,
535 notes = "KeyStoreException, NoSuchAlgorithmException, UnrecoverableKeyException checking missed",
537 args = {java.security.KeyStore.class, char[].class}
539 public void test_initLjava_security_KeyStore$C()
540 throws NoSuchAlgorithmException,
541 KeyStoreException, UnrecoverableKeyException {
543 fail(NotSupportedMsg);
546 KeyManagerFactory[] keyMF = createKMFac();
547 assertNotNull("KeyManagerFactory object were not created", keyMF);
548 KeyStore ksNull = null;
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",
559 ks = KeyStore.getInstance(KeyStore.getDefaultType());
561 } catch (KeyStoreException e) {
562 fail(e.toString() + "default KeyStore type is not supported");
564 } catch (Exception e) {
565 fail("Unexpected: " + e.toString());
568 for (int i = 0; i < keyMF.length; i++) {
570 keyMF[i].init(ks, new char[10]);
571 } catch (KeyStoreException e) {
573 km = keyMF[i].getKeyManagers();
574 assertNotNull("Result has not be null", km);
575 assertTrue("Length of result KeyManager array should not be 0",
582 * Test for <code>init(ManagerFactoryParameters params)</code>
584 * throws InvalidAlgorithmParameterException when params is null
587 level = TestLevel.COMPLETE,
588 notes = "functionality is not implemented in org.apache.harmony.xnet.provider.jsse.engineInit(ManagerFactoryParameters)",
590 args = {javax.net.ssl.ManagerFactoryParameters.class}
592 public void test_initLjavax_net_ssl_ManagerFactoryParameters()
593 throws NoSuchAlgorithmException {
596 fail(NotSupportedMsg);
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++) {
605 fail("InvalidAlgorithmParameterException must be thrown");
606 } catch (InvalidAlgorithmParameterException e) {
610 KeyStore.ProtectionParameter pp = new ProtectionParameterImpl();
611 KeyStore.Builder bld = KeyStore.Builder.newInstance("testType", null, pp);
612 assertNotNull("Null object KeyStore.Builder", bld);
615 KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
616 KeyStoreBuilderParameters ksp = new KeyStoreBuilderParameters(bld);
617 assertNotNull(ksp.getParameters());
619 fail("InvalidAlgorithmParameterException must be thrown");
620 } catch (InvalidAlgorithmParameterException e) {
627 * Additional class for KeyManagerFactory constructor verification
629 class myKeyManagerFactory extends KeyManagerFactory {
630 public myKeyManagerFactory(KeyManagerFactorySpi spi, Provider prov,
632 super(spi, prov, alg);
636 class ProtectionParameterImpl implements KeyStore.ProtectionParameter {
637 ProtectionParameterImpl(){}