1561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/*
2561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  Licensed to the Apache Software Foundation (ASF) under one or more
3561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  contributor license agreements.  See the NOTICE file distributed with
4561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  this work for additional information regarding copyright ownership.
5561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  The ASF licenses this file to You under the Apache License, Version 2.0
6561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  (the "License"); you may not use this file except in compliance with
7561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  the License.  You may obtain a copy of the License at
8561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
9561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *     http://www.apache.org/licenses/LICENSE-2.0
10561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *
11561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  Unless required by applicable law or agreed to in writing, software
12561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  distributed under the License is distributed on an "AS IS" BASIS,
13561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  See the License for the specific language governing permissions and
15561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes *  limitations under the License.
16561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */
17561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
18561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespackage org.apache.harmony.xnet.tests.javax.net.ssl;
19561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
20561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.KeyManagementException;
21561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.KeyStore;
22561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.KeyStoreException;
23561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.NoSuchAlgorithmException;
24561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.NoSuchProviderException;
25561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.Provider;
26561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.SecureRandom;
27561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.Security;
28561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport java.security.UnrecoverableKeyException;
29561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
30561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.KeyManager;
31561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.KeyManagerFactory;
32561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLContext;
33561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLContextSpi;
34561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLEngine;
35561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLParameters;
36561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLPermission;
37561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLSessionContext;
38561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLServerSocketFactory;
39561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLSocketFactory;
40561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.SSLSocket;
41561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.TrustManager;
42561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport javax.net.ssl.TrustManagerFactory;
43561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
44561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport org.apache.harmony.security.fortress.Services;
45561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport org.apache.harmony.xnet.tests.support.SpiEngUtils;
46561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport org.apache.harmony.xnet.tests.support.MySSLContextSpi;
47561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesimport junit.framework.TestCase;
48561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
49561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/**
50561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Tests for <code>SSLContext</code> class constructors and methods.
51561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */
52561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
53561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughespublic class SSLContext1Test extends TestCase {
54561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
55561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static String srvSSLContext = "SSLContext";
56561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
57561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public static String defaultProtocol = "TLS";
58561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
59561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static final String NotSupportMsg = "Default protocol is not supported";
60561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
61561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static String defaultProviderName = null;
62561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
63561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static Provider defaultProvider = null;
64561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
65561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static final String[] invalidValues = SpiEngUtils.invalidValues;
66561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
67561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static boolean DEFSupported = false;
68561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
69561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static final String NotSupportedMsg = "There is no suitable provider for SSLContext";
70561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
71561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    private static String[] validValues = new String[3];
728d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
73561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    static {
74561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        defaultProvider = SpiEngUtils.isSupport(defaultProtocol, srvSSLContext);
75561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        DEFSupported = (defaultProvider != null);
76561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (DEFSupported) {
77561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            defaultProviderName = (DEFSupported ? defaultProvider.getName()
78561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    : null);
79561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            validValues[0] = defaultProtocol;
80561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            validValues[1] = defaultProtocol.toUpperCase();
81561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            validValues[2] = defaultProtocol.toLowerCase();
82561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } else {
83561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            defaultProtocol = null;
84561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
858d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
86561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLParameters staticSupportSSLParameter = new SSLParameters(new String[] {
87561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                "TLS_RSA_WITH_RC4_128_MD5", "TLS_RSA_WITH_RC4_128_SHA" },
88561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                new String[] { "TLSv1", "SSLv3" });
898d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
90561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLParameters staticDefaultSSLParameter = new SSLParameters(new String[] {
91561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                "TLS_RSA_WITH_RC4_128_MD5", "TLS_RSA_WITH_RC4_128_SHA" },
92561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                new String[] { "TLSv1", "SSLv3" });
93561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
94561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
95561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    protected SSLContext[] createSSLCon() {
96561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
97561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(defaultProtocol + " protocol is not supported");
98561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return null;
99561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
100561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext[] sslC = new SSLContext[3];
101561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
102561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslC[0] = SSLContext.getInstance(defaultProtocol);
103561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslC[1] = SSLContext.getInstance(defaultProtocol, defaultProvider);
104561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslC[2] = SSLContext.getInstance(defaultProtocol,
105561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    defaultProviderName);
106561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return sslC;
107561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (Exception e) {
108561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            e.printStackTrace();
109561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return null;
110561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
111561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
112561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
113561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
114561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol)</code> method Assertion:
115561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * returns SSLContext object
116561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
117561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext01() throws NoSuchAlgorithmException {
118561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
119561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
120561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
121561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
122561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext sslContext;
123561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
124561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslContext = SSLContext.getInstance(validValues[i]);
125561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue("Not SSLContext object",
126561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    sslContext instanceof SSLContext);
127561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Invalid protocol", sslContext.getProtocol(),
128561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    validValues[i]);
129561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
130561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
131561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
132561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
133561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol)</code> method Assertion:
134561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * throws NullPointerException when protocol is null; throws
135561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * NoSuchAlgorithmException when protocol is not correct;
136561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
137561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext02() {
138561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
139561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            SSLContext.getInstance(null);
140561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
141561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NoSuchAlgorithmException e) {
142561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
143561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
144561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < invalidValues.length; i++) {
145561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
146561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                SSLContext.getInstance(invalidValues[i]);
147561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("NoSuchAlgorithmException was not thrown as expected for provider: "
148561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        .concat(invalidValues[i]));
149561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (NoSuchAlgorithmException e) {
150561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
151561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
152561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
153561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
154561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
155561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol, String provider)</code>
156561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * method Assertion: throws IllegalArgumentException when provider is null
157561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * or empty
158561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
159561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext03() throws NoSuchProviderException,
160561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            NoSuchAlgorithmException {
161561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
162561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
163561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
164561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
165561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        String provider = null;
166561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
167561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
168561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                SSLContext.getInstance(defaultProtocol, provider);
169561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("IllegalArgumentException must be thrown when provider is null");
170561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (IllegalArgumentException e) {
171561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
172561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
173561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                SSLContext.getInstance(defaultProtocol, "");
174561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("IllegalArgumentException must be thrown when provider is empty");
175561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (IllegalArgumentException e) {
176561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
177561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
178561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
179561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
180561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
181561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol, String provider)</code>
182561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * method Assertion: throws NullPointerException when protocol is null;
183561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * throws NoSuchAlgorithmException when protocol is not correct;
184561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
185561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext04() throws NoSuchProviderException {
186561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
187561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
188561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
189561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
190561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
191561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            SSLContext.getInstance(null, defaultProviderName);
192561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
193561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NoSuchAlgorithmException e) {
194561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
195561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
196561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < invalidValues.length; i++) {
197561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
198561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                SSLContext.getInstance(invalidValues[i], defaultProviderName);
199561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("NoSuchAlgorithmException was not thrown as expected (protocol: "
200561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        .concat(invalidValues[i]).concat(")"));
201561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (NoSuchAlgorithmException e) {
202561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
203561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
204561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
205561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
206561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
207561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol, String provider)</code>
208561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * method Assertion: throws NoSuchProviderException when provider has
209561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * invalid value
210561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
211561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext05() throws NoSuchAlgorithmException {
212561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
213561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
214561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
215561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
216561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 1; i < invalidValues.length; i++) {
217561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            for (int j = 0; j < validValues.length; j++) {
218561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                try {
219561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    SSLContext.getInstance(validValues[j], invalidValues[i]);
220561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    fail("NuSuchProviderException must be thrown (protocol: "
221561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                            .concat(validValues[j]).concat(" provider: ")
222561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                            .concat(invalidValues[i]).concat(")"));
223561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                } catch (NoSuchProviderException e) {
224561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                }
225561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
226561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
227561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
228561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
229561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
230561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol, String provider)</code>
231561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * method Assertion: returns instance of SSLContext
232561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
233561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext06() throws NoSuchAlgorithmException,
234561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            NoSuchProviderException {
235561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
236561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
237561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
238561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
239561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext sslContext;
240561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
241561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslContext = SSLContext.getInstance(validValues[i],
242561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    defaultProviderName);
243561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue("Not SSLContext object",
244561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    sslContext instanceof SSLContext);
245561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Invalid protocol", sslContext.getProtocol(),
246561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    validValues[i]);
247561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Invalid provider", sslContext.getProvider(),
248561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    defaultProvider);
249561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
250561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
251561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
252561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
253561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol, Provider provider)</code>
254561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * method Assertion: throws IllegalArgumentException when provider is null
255561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
256561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext07() throws NoSuchAlgorithmException {
257561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
258561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
259561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
260561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
261561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        Provider provider = null;
262561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
263561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
264561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                SSLContext.getInstance(validValues[i], provider);
265561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("IllegalArgumentException must be thrown when provider is null");
266561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (IllegalArgumentException e) {
267561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
268561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
269561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
270561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
271561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
272561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol, Provider provider)</code>
273561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * method Assertion: throws NullPointerException when protocol is null;
274561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * throws NoSuchAlgorithmException when protocol is not correct;
275561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
276561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext08() {
277561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
278561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
279561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
280561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
281561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
282561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            SSLContext.getInstance(null, defaultProvider);
283561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("NoSuchAlgorithmException or NullPointerException should be thrown (protocol is null");
284561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NoSuchAlgorithmException e) {
285561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
286561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
287561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < invalidValues.length; i++) {
288561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            try {
289561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                SSLContext.getInstance(invalidValues[i], defaultProvider);
290561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                fail("Expected NoSuchAlgorithmException was not thrown as expected");
291561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            } catch (NoSuchAlgorithmException e) {
292561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            }
293561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
294561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
295561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
296561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
297561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getInstance(String protocol, Provider provider)</code>
298561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * method Assertion: returns instance of SSLContext
299561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
300561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext09() throws NoSuchAlgorithmException {
301561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
302561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
303561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
304561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
305561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext sslContext;
306561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < validValues.length; i++) {
307561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslContext = SSLContext
308561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .getInstance(validValues[i], defaultProvider);
309561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue("Not SSLContext object",
310561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    sslContext instanceof SSLContext);
311561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Invalid protocol", sslContext.getProtocol(),
312561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    validValues[i]);
313561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertEquals("Invalid provider", sslContext.getProvider(),
314561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    defaultProvider);
315561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
316561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
317561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
318561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
319561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getClientSessionContext()</code>
320561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * <code>getServiceSessionContext()</code>
321561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * methods Assertion: returns correspondent object
322561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
323561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext10() throws NoSuchAlgorithmException {
324561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
325561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
326561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
327561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
328561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext[] sslC = createSSLCon();
329561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNotNull("SSLContext objects were not created", sslC);
330561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < sslC.length; i++) {
331561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue(sslC[i].getClientSessionContext() instanceof SSLSessionContext);
332561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue(sslC[i].getServerSessionContext() instanceof SSLSessionContext);
333561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
334561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
335561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
336561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
337561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>getServerSocketFactory()</code>
338561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * <code>getSocketFactory()</code>
339561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * <code>init(KeyManager[] km, TrustManager[] tm, SecureRandom random)</code>
340561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * methods Assertion: returns correspondent object
341561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
3428d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
3438d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath    public void testSSLContext11() throws NoSuchAlgorithmException,
344561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            KeyManagementException, KeyStoreException,
345561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            UnrecoverableKeyException {
346561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
347561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
348561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
349561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
350561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext[] sslC = createSSLCon();
351561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNotNull("SSLContext objects were not created", sslC);
352561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        String tAlg = TrustManagerFactory.getDefaultAlgorithm();
353561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        String kAlg = KeyManagerFactory.getDefaultAlgorithm();
354561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (tAlg == null) {
355561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("TrustManagerFactory default algorithm is not defined");
356561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
357561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
358561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (kAlg == null) {
359561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("KeyManagerFactory default algorithm is not defined");
360561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
361561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
362561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        KeyManagerFactory kmf = KeyManagerFactory.getInstance(kAlg);
363561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        KeyStore ks = null;
364561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        kmf.init(ks, new char[10]);
365561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        KeyManager[] kms = kmf.getKeyManagers();
366561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        TrustManagerFactory tmf = TrustManagerFactory.getInstance(tAlg);
367561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        tmf.init(ks);
368561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        TrustManager[] tms = tmf.getTrustManagers();
369561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < sslC.length; i++) {
370561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslC[i].init(kms, tms, new SecureRandom());
371561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue(sslC[i].getServerSocketFactory() instanceof SSLServerSocketFactory);
372561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue(sslC[i].getSocketFactory() instanceof SSLSocketFactory);
373561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
374561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
375561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
376561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    /**
377561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * Test for <code>SSLContext</code> constructor Assertion: returns
378561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     * SSLContext object
379561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes     */
380561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testSSLContext12() throws NoSuchAlgorithmException,
381561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            KeyManagementException {
382561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        if (!DEFSupported) {
383561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail(NotSupportMsg);
384561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            return;
385561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
386561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContextSpi spi = new MySSLContextSpi();
387561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext sslContext = new MySSLContext(spi, defaultProvider,
388561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                defaultProtocol);
389561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertTrue("Not CertStore object", sslContext instanceof SSLContext);
390561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertEquals("Incorrect protocol", sslContext.getProtocol(),
391561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                defaultProtocol);
392561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertEquals("Incorrect provider", sslContext.getProvider(),
393561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                defaultProvider);
394561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        TrustManager[] tm = null;
395561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        KeyManager[] km = null;
396561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        sslContext.init(km, tm, new SecureRandom());
397561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertTrue(sslContext.createSSLEngine() instanceof SSLEngine);
398561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertTrue(sslContext.createSSLEngine("host host", 8888) instanceof SSLEngine);
399561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
400561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslContext.init(km, tm, null);
401561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("KeyManagementException should be thrown for null SEcureRandom");
402561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (KeyManagementException e) {
403561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
404561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
405561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        sslContext = new MySSLContext(null, null, null);
406561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertTrue("Not CertStore object", sslContext instanceof SSLContext);
407561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNull("Incorrect protocol", sslContext.getProtocol());
408561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNull("Incorrect provider", sslContext.getProvider());
409561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
410561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslContext.createSSLEngine();
411561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("NullPointerException should be thrown");
412561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
413561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
414561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
415561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslContext.getSocketFactory();
416561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            fail("NullPointerException should be thrown");
417561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NullPointerException e) {
418561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
419561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
4208d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
421561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetDefault() throws Exception {
422561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        //TODO: Need evaluation
4238d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath        class PrivateClassLoader extends ClassLoader {
4248d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath        }
425561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        try {
426561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            // register my provider and its service.
427561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            Security.addProvider(new MyProvider());
428561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            // FIXME
429561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            ClassLoader privateClassLoader = new PrivateClassLoader();
430561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            Class class1 = privateClassLoader
431561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .loadClass("org.apache.harmony.xnet.tests.javax.net.ssl.MySSLContext");
4328d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath            SSLContext sslContext = (SSLContext) class1.newInstance();
433561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            System.out.println(SSLContext.getInstance("Default"));
434561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            assertTrue((sslContext.getDefault()) instanceof SSLContext);
435561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        } catch (NoSuchAlgorithmException e) {
436561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            // expected
437561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
4388d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath    }
439561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
440561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
441561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetDefaultSSLParameters() throws Exception {
442561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext[] sslContexts = createSSLCon();
443561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNotNull("SSLContext objects were not created", sslContexts);
444561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
445561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < sslContexts.length; i++) {
446561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslContexts[i].init(null, null, null);
447561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            SSLParameters defaultSSLParameters = sslContexts[i]
448561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .getDefaultSSLParameters();
449561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            SSLSocket sslSocket = (SSLSocket) (sslContexts[i]
450561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .getSocketFactory().createSocket());
451561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
452561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            String[] enabledCipherSuites = sslSocket.getEnabledCipherSuites();
453561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            String[] enabledProtocols = sslSocket.getEnabledProtocols();
454561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
455561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            for (int j = 0; j < enabledCipherSuites.length; j++)
456561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                assertEquals((defaultSSLParameters.getCipherSuites())[j],
457561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        enabledCipherSuites[j]);
458561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            for (int k = 0; k < enabledProtocols.length; k++)
459561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                assertEquals((defaultSSLParameters.getProtocols())[k],
460561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        enabledProtocols[k]);
461561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
462561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
463561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
464561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public void testGetSupportedSSLParameters() throws Exception {
465561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        SSLContext[] sslContexts = createSSLCon();
466561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        assertNotNull("SSLContext objects were not created", sslContexts);
467561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
468561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        for (int i = 0; i < sslContexts.length; i++) {
469561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            sslContexts[i].init(null, null, null);
470561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            SSLParameters defaultSSLParameters = sslContexts[i]
471561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .getSupportedSSLParameters();
472561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            SSLSocket sslSocket = (SSLSocket) (sslContexts[i]
473561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                    .getSocketFactory().createSocket());
474561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            String[] supportedCipherSuites = sslSocket.getSupportedCipherSuites();
475561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            String[] supportedProtocols = sslSocket.getSupportedProtocols();
476561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
477561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            for (int j = 0; j < supportedCipherSuites.length; j++)
478561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                assertEquals((defaultSSLParameters.getCipherSuites())[j],
479561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        supportedCipherSuites[j]);
480561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes            for (int k = 0; k < supportedProtocols.length; k++)
481561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                assertEquals((defaultSSLParameters.getProtocols())[k],
482561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes                        supportedProtocols[k]);
483561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        }
484561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
485561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes}
486561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes
487561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes/**
488561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes * Addifional class to verify SSLContext constructor
489561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes */
4908d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamathclass MyProvider extends Provider {
491561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    MyProvider() {
492561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        super("MyProviderForSSLContextTest", 1.0, "Provider for testing");
493561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        put("SSLContext.Default", "org.apache.harmony.xnet.tests.javax.net.ssl.MySSLContext");
494561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
495561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes}
4968d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
497561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughesclass MySSLContext extends SSLContext {
498561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    public MySSLContext(SSLContextSpi spi, Provider prov, String alg) {
499561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        super(spi, prov, alg);
500561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
5018d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath
5028d8858e39800de641b50f6e8e864af9cf68bedeaNarayan Kamath    public MySSLContext() {
503561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes        super(null, null, null);
504561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes    }
505561ee011997c6c2f1befbfaa9d5f0a99771c1d63Elliott Hughes}
506