SSLContext1Test.java revision 6b811c5daec1b28e6f63b57f98a032236f2c3cf7
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
18package tests.api.javax.net.ssl;
19
20import java.io.FileNotFoundException;
21import java.security.KeyManagementException;
22import java.security.KeyStore;
23import java.security.KeyStoreException;
24import java.security.NoSuchAlgorithmException;
25import java.security.NoSuchProviderException;
26import java.security.Provider;
27import java.security.SecureRandom;
28import java.security.UnrecoverableKeyException;
29
30import javax.net.ssl.KeyManager;
31import javax.net.ssl.KeyManagerFactory;
32import javax.net.ssl.SSLContext;
33import javax.net.ssl.SSLContextSpi;
34import javax.net.ssl.SSLEngine;
35import javax.net.ssl.SSLServerSocketFactory;
36import javax.net.ssl.SSLSessionContext;
37import javax.net.ssl.SSLSocketFactory;
38import javax.net.ssl.TrustManager;
39import javax.net.ssl.TrustManagerFactory;
40
41import junit.framework.TestCase;
42
43import org.apache.harmony.security.tests.support.SpiEngUtils;
44import org.apache.harmony.xnet.tests.support.MySSLContextSpi;
45
46import dalvik.annotation.TestLevel;
47import dalvik.annotation.TestTargetClass;
48import dalvik.annotation.TestTargetNew;
49
50/**
51 * Tests for <code>SSLContext</code> class constructors and methods.
52 *
53 */
54@TestTargetClass(SSLContext.class)
55public class SSLContext1Test extends TestCase {
56
57    private static String srvSSLContext = "SSLContext";
58    public static String defaultProtocol = "TLS";
59    private static final String NotSupportMsg = "Default protocol is not supported";
60    private static String defaultProviderName = null;
61    private static Provider defaultProvider = null;
62    private static final String[] invalidValues = SpiEngUtils.invalidValues;
63    private static boolean DEFSupported = false;
64    private static String[] validValues = new String[3];
65    static {
66        defaultProvider = SpiEngUtils.isSupport(defaultProtocol, srvSSLContext);
67        DEFSupported = (defaultProvider != null);
68        if (DEFSupported) {
69            defaultProviderName = (DEFSupported ? defaultProvider.getName()
70                    : null);
71            validValues[0] = defaultProtocol;
72            validValues[1] = defaultProtocol.toUpperCase();
73            validValues[2] = defaultProtocol.toLowerCase();
74        } else {
75            defaultProtocol = null;
76        }
77    }
78
79    protected SSLContext[] createSSLCon() {
80        if (!DEFSupported) {
81            fail(defaultProtocol + " protocol is not supported");
82            return null;
83        }
84        SSLContext[] sslC = new SSLContext[3];
85        try {
86            sslC[0] = SSLContext.getInstance(defaultProtocol);
87            sslC[1] = SSLContext.getInstance(defaultProtocol, defaultProvider);
88            sslC[2] = SSLContext.getInstance(defaultProtocol,
89                    defaultProviderName);
90            return sslC;
91        } catch (Exception e) {
92            e.printStackTrace();
93            return null;
94        }
95    }
96
97    /**
98     * Test for <code>SSLContext</code> constructor Assertion: returns
99     * SSLContext object
100     */
101    @TestTargetNew(
102        level = TestLevel.COMPLETE,
103        notes = "",
104        method = "SSLContext",
105        args = {javax.net.ssl.SSLContextSpi.class, java.security.Provider.class, java.lang.String.class}
106    )
107    public void test_ConstructorLjavax_net_ssl_SSLContextSpiLjava_security_ProviderLjava_lang_String()
108        throws NoSuchAlgorithmException,
109            KeyManagementException {
110        if (!DEFSupported) {
111            fail(NotSupportMsg);
112            return;
113        }
114        SSLContextSpi spi = new MySSLContextSpi();
115        SSLContext sslContext = new MySslContext(spi, defaultProvider,
116                defaultProtocol);
117        assertEquals("Incorrect protocol", defaultProtocol,
118                sslContext.getProtocol());
119        assertEquals("Incorrect provider", defaultProvider,
120                sslContext.getProvider());
121        TrustManager[] tm = null;
122        KeyManager[] km = null;
123        sslContext.init(km, tm, new SecureRandom());
124        assertNotNull("No SSLEngine created",
125                sslContext.createSSLEngine());
126        assertNotNull("No SSLEngine created",
127                sslContext.createSSLEngine("host", 8888));
128        try {
129            sslContext.init(km, tm, null);
130            fail("KeyManagementException should be thrown for null "
131                    + "SecureRandom");
132        } catch (KeyManagementException e) {
133        }
134
135        sslContext = new MySslContext(null, null, null);
136        assertNull("Incorrect protocol", sslContext.getProtocol());
137        assertNull("Incorrect provider", sslContext.getProvider());
138        try {
139            sslContext.createSSLEngine();
140            fail("NullPointerException should be thrown");
141        } catch (NullPointerException e) {
142        }
143        try {
144            sslContext.getSocketFactory();
145            fail("NullPointerException should be thrown");
146        } catch (NullPointerException e) {
147        }
148    }
149
150    /**
151     * @throws KeyManagementException
152     * @tests javax.net.ssl.SSLContext#createSSLEngine()
153     */
154    @TestTargetNew(
155        level = TestLevel.SUFFICIENT,
156        notes = "UnsupportedOperationException checking missed",
157        method = "createSSLEngine",
158        args = {}
159    )
160    public void test_createSSLEngine() throws KeyManagementException {
161        if (!DEFSupported) fail(NotSupportMsg);
162        SSLContextSpi spi = new MySSLContextSpi();
163        SSLContext sslContext = new MySslContext(spi, defaultProvider,
164                defaultProtocol);
165        sslContext.init(null, null, new SecureRandom());
166        SSLEngine sslEngine = sslContext.createSSLEngine();
167        assertNotNull("SSL engine is null", sslEngine);
168    }
169
170    /**
171     * @throws KeyManagementException
172     * @tests javax.net.ssl.SSLContext#createSSLEngine(java.lang.String, int)
173     */
174    @TestTargetNew(
175        level = TestLevel.SUFFICIENT,
176        notes = "UnsupportedOperationException checking missed",
177        method = "createSSLEngine",
178        args = {java.lang.String.class, int.class}
179    )
180    public void test_createSSLEngineLjava_lang_StringI()
181        throws KeyManagementException {
182        if (!DEFSupported) fail(NotSupportMsg);
183        SSLContextSpi spi = new MySSLContextSpi();
184        SSLContext sslContext = new MySslContext(spi, defaultProvider,
185                defaultProtocol);
186        sslContext.init(null, null, new SecureRandom());
187        SSLEngine sslEngine = sslContext.createSSLEngine("www.fortify.net", 80);
188        assertNotNull("SSL engine is null", sslEngine);
189    }
190
191    /**
192     * Test for <code>getClientSessionContext()</code>
193     * <code>getServiceSessionContext()</code>
194     * methods Assertion: returns correspondent object
195     * @throws KeyManagementException
196     */
197    @TestTargetNew(
198        level = TestLevel.COMPLETE,
199        notes = "",
200        method = "getClientSessionContext",
201        args = {}
202    )
203    public void test_getClientSessionContext() throws NoSuchAlgorithmException, KeyManagementException {
204        if (!DEFSupported) {
205            fail(NotSupportMsg);
206            return;
207        }
208        SSLContext[] sslC = createSSLCon();
209        assertNotNull("SSLContext objects were not created", sslC);
210        for (int i = 0; i < sslC.length; i++) {
211            sslC[i].init(null, null, null);
212            assertNotNull("Client session is incorrectly instantiated: " + i,
213                    sslC[i].getClientSessionContext());
214            assertNotNull("Server session is incorrectly instantiated: " + i,
215                    sslC[i].getServerSessionContext());
216        }
217    }
218
219    /**
220     * Test for <code>getInstance(String protocol)</code> method Assertion:
221     * returns SSLContext object
222     */
223    @TestTargetNew(
224        level = TestLevel.PARTIAL_COMPLETE,
225        notes = "",
226        method = "getInstance",
227        args = {java.lang.String.class}
228    )
229    public void test_getInstanceLjava_lang_String01()
230            throws NoSuchAlgorithmException {
231        if (!DEFSupported) {
232            fail(NotSupportMsg);
233            return;
234        }
235        SSLContext sslContext;
236        for (int i = 0; i < validValues.length; i++) {
237            sslContext = SSLContext.getInstance(validValues[i]);
238            assertNotNull("No SSLContext created", sslContext);
239            assertEquals("Invalid protocol", validValues[i],
240                    sslContext.getProtocol());
241        }
242    }
243
244    /**
245     * Test for <code>getInstance(String protocol)</code> method Assertion:
246     * throws NullPointerException when protocol is null; throws
247     * NoSuchAlgorithmException when protocol is not correct;
248     */
249    @TestTargetNew(
250        level = TestLevel.PARTIAL_COMPLETE,
251        notes = "",
252        method = "getInstance",
253        args = {java.lang.String.class}
254    )
255    public void test_getInstanceLjava_lang_String02() {
256        try {
257            SSLContext.getInstance(null);
258            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
259        } catch (NoSuchAlgorithmException e) {
260        } catch (NullPointerException e) {
261        }
262        for (int i = 0; i < invalidValues.length; i++) {
263            try {
264                SSLContext.getInstance(invalidValues[i]);
265                fail("NoSuchAlgorithmException was not thrown as expected for provider: "
266                        .concat(invalidValues[i]));
267            } catch (NoSuchAlgorithmException e) {
268            }
269        }
270    }
271
272    /**
273     * Test for <code>getInstance(String protocol, String provider)</code>
274     * method Assertion: throws IllegalArgumentException when provider is null
275     * or empty
276     */
277    @TestTargetNew(
278        level = TestLevel.PARTIAL_COMPLETE,
279        notes = "",
280        method = "getInstance",
281        args = {java.lang.String.class, java.lang.String.class}
282    )
283    public void test_getInstanceLjava_lang_StringLjava_lang_String01() throws NoSuchProviderException,
284            NoSuchAlgorithmException {
285        if (!DEFSupported) {
286            fail(NotSupportMsg);
287            return;
288        }
289        String provider = null;
290        for (int i = 0; i < validValues.length; i++) {
291            try {
292                SSLContext.getInstance(defaultProtocol, provider);
293                fail("IllegalArgumentException must be thrown when provider is null");
294            } catch (IllegalArgumentException e) {
295            }
296            try {
297                SSLContext.getInstance(defaultProtocol, "");
298                fail("IllegalArgumentException must be thrown when provider is empty");
299            } catch (IllegalArgumentException e) {
300            }
301        }
302    }
303
304    /**
305     * Test for <code>getInstance(String protocol, String provider)</code>
306     * method Assertion: throws NullPointerException when protocol is null;
307     * throws NoSuchAlgorithmException when protocol is not correct;
308     */
309    @TestTargetNew(
310        level = TestLevel.PARTIAL_COMPLETE,
311        notes = "",
312        method = "getInstance",
313        args = {java.lang.String.class, java.lang.String.class}
314    )
315    public void test_getInstanceLjava_lang_StringLjava_lang_String02() throws NoSuchProviderException {
316        if (!DEFSupported) {
317            fail(NotSupportMsg);
318            return;
319        }
320        try {
321            SSLContext.getInstance(null, defaultProviderName);
322            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
323        } catch (NoSuchAlgorithmException e) {
324        } catch (NullPointerException e) {
325        }
326        for (int i = 0; i < invalidValues.length; i++) {
327            try {
328                SSLContext.getInstance(invalidValues[i], defaultProviderName);
329                fail("NoSuchAlgorithmException was not thrown as expected (protocol: "
330                        .concat(invalidValues[i]).concat(")"));
331            } catch (NoSuchAlgorithmException e) {
332            }
333        }
334    }
335
336    /**
337     * Test for <code>getInstance(String protocol, String provider)</code>
338     * method Assertion: throws NoSuchProviderException when provider has
339     * invalid value
340     */
341    @TestTargetNew(
342        level = TestLevel.PARTIAL_COMPLETE,
343        notes = "",
344        method = "getInstance",
345        args = {java.lang.String.class, java.lang.String.class}
346    )
347    public void test_getInstanceLjava_lang_StringLjava_lang_String03() throws NoSuchAlgorithmException {
348        if (!DEFSupported) {
349            fail(NotSupportMsg);
350            return;
351        }
352        for (int i = 1; i < invalidValues.length; i++) {
353            for (int j = 0; j < validValues.length; j++) {
354                try {
355                    SSLContext.getInstance(validValues[j], invalidValues[i]);
356                    fail("NuSuchProviderException must be thrown (protocol: "
357                            .concat(validValues[j]).concat(" provider: ")
358                            .concat(invalidValues[i]).concat(")"));
359                } catch (NoSuchProviderException e) {
360                }
361            }
362        }
363    }
364
365    /**
366     * Test for <code>getInstance(String protocol, String provider)</code>
367     * method Assertion: returns instance of SSLContext
368     */
369    @TestTargetNew(
370        level = TestLevel.PARTIAL_COMPLETE,
371        notes = "",
372        method = "getInstance",
373        args = {java.lang.String.class, java.lang.String.class}
374    )
375    public void test_getInstanceLjava_lang_StringLjava_lang_String04() throws NoSuchAlgorithmException,
376            NoSuchProviderException {
377        if (!DEFSupported) {
378            fail(NotSupportMsg);
379            return;
380        }
381        SSLContext sslContext;
382        for (int i = 0; i < validValues.length; i++) {
383            sslContext = SSLContext.getInstance(validValues[i],
384                    defaultProviderName);
385            assertNotNull("Not SSLContext created", sslContext);
386            assertEquals("Invalid protocol",
387                    validValues[i], sslContext.getProtocol());
388            assertEquals("Invalid provider",
389                    defaultProvider, sslContext.getProvider());
390        }
391    }
392
393    /**
394     * Test for <code>getInstance(String protocol, Provider provider)</code>
395     * method Assertion: throws IllegalArgumentException when provider is null
396     */
397    @TestTargetNew(
398        level = TestLevel.PARTIAL_COMPLETE,
399        notes = "",
400        method = "getInstance",
401        args = {java.lang.String.class, java.security.Provider.class}
402    )
403    public void test_getInstanceLjava_lang_StringLjava_security_Provider01() throws NoSuchAlgorithmException {
404        if (!DEFSupported) {
405            fail(NotSupportMsg);
406            return;
407        }
408        Provider provider = null;
409        for (int i = 0; i < validValues.length; i++) {
410            try {
411                SSLContext.getInstance(validValues[i], provider);
412                fail("IllegalArgumentException must be thrown when provider is null");
413            } catch (IllegalArgumentException e) {
414            }
415        }
416    }
417
418    /**
419     * Test for <code>getInstance(String protocol, Provider provider)</code>
420     * method Assertion: throws NullPointerException when protocol is null;
421     * throws NoSuchAlgorithmException when protocol is not correct;
422     */
423    @TestTargetNew(
424        level = TestLevel.PARTIAL_COMPLETE,
425        notes = "",
426        method = "getInstance",
427        args = {java.lang.String.class, java.security.Provider.class}
428    )
429    public void test_getInstanceLjava_lang_StringLjava_security_Provider02() {
430        if (!DEFSupported) {
431            fail(NotSupportMsg);
432            return;
433        }
434        try {
435            SSLContext.getInstance(null, defaultProvider);
436            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
437        } catch (NoSuchAlgorithmException e) {
438        } catch (NullPointerException e) {
439        }
440        for (int i = 0; i < invalidValues.length; i++) {
441            try {
442                SSLContext.getInstance(invalidValues[i], defaultProvider);
443                fail("Expected NoSuchAlgorithmException was not thrown as expected");
444            } catch (NoSuchAlgorithmException e) {
445            }
446        }
447    }
448
449    /**
450     * Test for <code>getInstance(String protocol, Provider provider)</code>
451     * method Assertion: returns instance of SSLContext
452     */
453    @TestTargetNew(
454        level = TestLevel.PARTIAL_COMPLETE,
455        notes = "",
456        method = "getInstance",
457        args = {java.lang.String.class, java.security.Provider.class}
458    )
459    public void test_getInstanceLjava_lang_StringLjava_security_Provider03() throws NoSuchAlgorithmException {
460        if (!DEFSupported) {
461            fail(NotSupportMsg);
462            return;
463        }
464        SSLContext sslContext;
465        for (int i = 0; i < validValues.length; i++) {
466            sslContext = SSLContext
467                    .getInstance(validValues[i], defaultProvider);
468            assertNotNull("Not SSLContext created", sslContext);
469            assertEquals("Invalid protocol", validValues[i], sslContext.getProtocol());
470            assertEquals("Invalid provider", defaultProvider, sslContext.getProvider());
471        }
472    }
473
474    /**
475     * @throws NoSuchAlgorithmException
476     * @throws NoSuchProviderException
477     * @tests javax.net.ssl.SSLContext#getProtocol()
478     */
479    @TestTargetNew(
480        level = TestLevel.COMPLETE,
481        notes = "",
482        method = "getProtocol",
483        args = {}
484    )
485    public void test_getProtocol()
486        throws NoSuchAlgorithmException, NoSuchProviderException {
487        if (!DEFSupported) fail(NotSupportMsg);
488        SSLContextSpi spi = new MySSLContextSpi();
489        SSLContext sslContext = new MySslContext(spi, defaultProvider,
490                defaultProtocol);
491        assertEquals("Incorrect protocol",
492                defaultProtocol, sslContext.getProtocol());
493        sslContext = new MySslContext(spi, defaultProvider,
494                null);
495        assertNull("Incorrect protocol", sslContext.getProtocol());
496        sslContext = SSLContext.getInstance(defaultProtocol);
497        assertEquals("Incorrect protocol",
498                defaultProtocol, sslContext.getProtocol());
499        sslContext = SSLContext.getInstance(defaultProtocol, defaultProvider);
500        assertEquals("Incorrect protocol",
501                defaultProtocol, sslContext.getProtocol());
502        sslContext = SSLContext.getInstance(defaultProtocol, defaultProviderName);
503        assertEquals("Incorrect protocol",
504                defaultProtocol, sslContext.getProtocol());
505    }
506
507    /**
508     * @throws NoSuchAlgorithmException
509     * @throws NoSuchProviderException
510     * @tests javax.net.ssl.SSLContext#getProvider()
511     */
512    @TestTargetNew(
513        level = TestLevel.COMPLETE,
514        notes = "",
515        method = "getProvider",
516        args = {}
517    )
518    public void test_getProvider()
519        throws NoSuchAlgorithmException, NoSuchProviderException {
520        if (!DEFSupported) fail(NotSupportMsg);
521        SSLContextSpi spi = new MySSLContextSpi();
522        SSLContext sslContext = new MySslContext(spi, defaultProvider,
523                defaultProtocol);
524        assertEquals("Incorrect provider",
525                defaultProvider, sslContext.getProvider());
526        sslContext = SSLContext.getInstance(defaultProtocol, defaultProvider);
527        assertEquals("Incorrect provider",
528                defaultProvider, sslContext.getProvider());
529        sslContext = SSLContext.getInstance(defaultProtocol, defaultProviderName);
530        assertEquals("Incorrect provider",
531                defaultProvider, sslContext.getProvider());
532    }
533
534    /**
535     * @tests javax.net.ssl.SSLContext#getServletSessionContext()
536     */
537    @TestTargetNew(
538        level = TestLevel.COMPLETE,
539        notes = "",
540        method = "getServerSessionContext",
541        args = {}
542    )
543    public void test_getServerSessionContext() throws NoSuchAlgorithmException,
544        KeyManagementException, KeyStoreException,
545        UnrecoverableKeyException {
546        if (!DEFSupported) fail(NotSupportMsg);
547        SSLContext[] sslC = createSSLCon();
548        assertNotNull("SSLContext objects were not created", sslC);
549        String tAlg = TrustManagerFactory.getDefaultAlgorithm();
550        String kAlg = KeyManagerFactory.getDefaultAlgorithm();
551        if (tAlg == null)
552            fail("TrustManagerFactory default algorithm is not defined");
553        if (kAlg == null)
554            fail("KeyManagerFactory default algorithm is not defined");
555        KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
556        kmf.init(null, new char[11]);
557        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
558        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
559        tmf.init(ks);
560        TrustManager[] tms = tmf.getTrustManagers();
561        for (SSLContext sslCi : sslC) {
562            sslCi.init(kmf.getKeyManagers(), tms, new SecureRandom());
563            assertNotNull("Server context is incorrectly instantiated", sslCi
564                    .getServerSessionContext());
565        }
566    }
567
568    /**
569     * Test for <code>getServerSocketFactory()</code>
570     * <code>getSocketFactory()</code>
571     * <code>init(KeyManager[] km, TrustManager[] tm, SecureRandom random)</code>
572     * methods Assertion: returns correspondent object
573     *
574     */
575    @TestTargetNew(
576        level = TestLevel.COMPLETE,
577        notes = "",
578        method = "getServerSocketFactory",
579        args = {}
580    )
581     public void test_getServerSocketFactory() throws NoSuchAlgorithmException,
582            KeyManagementException, KeyStoreException,
583            UnrecoverableKeyException {
584        if (!DEFSupported) {
585            fail(NotSupportMsg);
586            return;
587        }
588        SSLContext[] sslC = createSSLCon();
589        assertNotNull("SSLContext objects were not created", sslC);
590        String tAlg = TrustManagerFactory.getDefaultAlgorithm();
591        String kAlg = KeyManagerFactory.getDefaultAlgorithm();
592        if (tAlg == null) {
593            fail("TrustManagerFactory default algorithm is not defined");
594            return;
595        }
596        if (kAlg == null) {
597            fail("KeyManagerFactory default algorithm is not defined");
598            return;
599        }
600        KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
601        KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
602        try {
603            ks.load(null, null);
604        } catch (Exception e) {
605            fail(e + " was thrown for method load(null, null)");
606        }
607        kmf.init(ks, new char[10]);
608        KeyManager[] kms = kmf.getKeyManagers();
609        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
610        tmf.init(ks);
611        TrustManager[] tms = tmf.getTrustManagers();
612        for (int i = 0; i < sslC.length; i++) {
613            sslC[i].init(kms, tms, new SecureRandom());
614            assertNotNull("No SSLServerSocketFactory available",
615                    sslC[i].getServerSocketFactory());
616            assertNotNull("No SSLSocketFactory available",
617                    sslC[i].getSocketFactory());
618        }
619    }
620
621     /**
622      * @tests javax.net.ssl.SSLContext#getSocketFactory()
623      */
624    @TestTargetNew(
625        level = TestLevel.COMPLETE,
626        notes = "",
627        method = "getSocketFactory",
628        args = {}
629    )
630     public void test_getSocketFactory() throws NoSuchAlgorithmException,
631         KeyManagementException, KeyStoreException,
632         UnrecoverableKeyException {
633         if (!DEFSupported) fail(NotSupportMsg);
634         SSLContext[] sslC = createSSLCon();
635         assertNotNull("SSLContext objects were not created", sslC);
636         String tAlg = TrustManagerFactory.getDefaultAlgorithm();
637         String kAlg = KeyManagerFactory.getDefaultAlgorithm();
638         if (tAlg == null)
639             fail("TrustManagerFactory default algorithm is not defined");
640         if (kAlg == null)
641             fail("KeyManagerFactory default algorithm is not defined");
642         KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
643         kmf.init(null, new char[11]);
644         TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
645         KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
646         tmf.init(ks);
647         TrustManager[] tms = tmf.getTrustManagers();
648         for (SSLContext sslCi : sslC) {
649             sslCi.init(kmf.getKeyManagers(), tms, new SecureRandom());
650             assertNotNull("Socket factory is incorrectly instantiated",
651                     sslCi.getSocketFactory());
652         }
653     }
654
655     /**
656      * @throws NoSuchAlgorithmException
657     * @throws KeyStoreException
658     * @throws FileNotFoundException
659     * @throws KeyManagementException
660     * @tests javax.net.ssl.SSLContext#
661      *     init(javax.net.ssl.KeyManager[], javax.net.ssl.TrustManager[],
662      *     java.security.SecureRandom)
663      */
664    @TestTargetNew(
665        level = TestLevel.COMPLETE,
666        notes = "",
667        method = "init",
668        args = {javax.net.ssl.KeyManager[].class, javax.net.ssl.TrustManager[].class, java.security.SecureRandom.class}
669    )
670     public void test_init$Ljavax_net_ssl_KeyManager$Ljavax_net_ssl_TrustManagerLjava_security_SecureRandom()
671         throws Exception {
672         if (!DEFSupported) fail(NotSupportMsg);
673         SSLContextSpi spi = new MySSLContextSpi();
674         SSLContext sslContext = new MySslContext(spi, defaultProvider,
675                 defaultProtocol);
676         try {
677             sslContext.createSSLEngine();
678             fail("Expected RuntimeException was not thrown");
679         } catch (RuntimeException rte) {
680             // expected
681         }
682
683         try {
684             sslContext.init(null, null, null);
685             fail("KeyManagementException wasn't thrown");
686         } catch (KeyManagementException kme) {
687             //expected
688         }
689
690         try {
691             String tAlg = TrustManagerFactory.getDefaultAlgorithm();
692             String kAlg = KeyManagerFactory.getDefaultAlgorithm();
693             if (tAlg == null)
694                 fail("TrustManagerFactory default algorithm is not defined");
695             if (kAlg == null)
696                 fail("KeyManagerFactory default algorithm is not defined");
697             KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
698             kmf.init(null, new char[11]);
699             TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
700             KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
701             tmf.init(ks);
702             TrustManager[] tms = tmf.getTrustManagers();
703             sslContext.init(kmf.getKeyManagers(), tms, new SecureRandom());
704         } catch (Exception e) {
705             System.out.println("EE = " + e);
706         }
707     }
708}
709
710/**
711 * Addifional class to verify SSLContext constructor
712 */
713
714class MySslContext extends SSLContext {
715    public MySslContext(SSLContextSpi spi, Provider prov, String alg) {
716        super(spi, prov, alg);
717    }
718}
719