1/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package tests.api.javax.net.ssl;
17
18import javax.net.ssl.KeyManagerFactory;
19import javax.net.ssl.SSLContextSpi;
20import javax.net.ssl.SSLEngine;
21import javax.net.ssl.SSLServerSocketFactory;
22import javax.net.ssl.SSLSessionContext;
23import javax.net.ssl.SSLSocketFactory;
24import javax.net.ssl.KeyManager;
25import javax.net.ssl.TrustManager;
26import javax.net.ssl.TrustManagerFactory;
27import java.security.KeyManagementException;
28
29import java.security.KeyStore;
30import java.security.SecureRandom;
31import java.security.Security;
32
33import junit.framework.TestCase;
34
35import org.apache.harmony.xnet.tests.support.SSLContextSpiImpl;
36
37public class SSLContextSpiTest extends TestCase {
38
39    /**
40     * javax.net.ssl.SSLContextSpi#SSLContextSpi()
41     */
42    public void test_Constructor() {
43        try {
44            SSLContextSpiImpl ssl = new SSLContextSpiImpl();
45            assertTrue(ssl instanceof SSLContextSpi);
46        } catch (Exception e) {
47            fail("Unexpected exception " + e.toString());
48        }
49    }
50
51    /**
52     * javax.net.ssl.SSLContextSpi#engineCreateSSLEngine()
53     * Verify exception when SSLContextSpi object wasn't initialiazed.
54     */
55    public void test_engineCreateSSLEngine_01() {
56        SSLContextSpiImpl ssl = new SSLContextSpiImpl();
57        try {
58            SSLEngine sleng = ssl.engineCreateSSLEngine();
59            fail("RuntimeException wasn't thrown");
60        } catch (RuntimeException re) {
61            String str = re.getMessage();
62            if (!str.equals("Not initialiazed"))
63                fail("Incorrect exception message: " + str);
64        } catch (Exception e) {
65            fail("Incorrect exception " + e + " was thrown");
66        }
67    }
68
69    /**
70     * javax.net.ssl.SSLContextSpi#engineCreateSSLEngine(String host, int port)
71     * Verify exception when SSLContextSpi object wasn't initialiazed.
72     */
73    public void test_engineCreateSSLEngine_02() {
74        int[] invalid_port = {Integer.MIN_VALUE, -65535, -1, 65536, Integer.MAX_VALUE};
75        SSLContextSpiImpl ssl = new SSLContextSpiImpl();
76        try {
77            SSLEngine sleng = ssl.engineCreateSSLEngine("localhost", 1080);
78            fail("RuntimeException wasn't thrown");
79        } catch (RuntimeException re) {
80            String str = re.getMessage();
81            if (!str.equals("Not initialiazed"))
82                fail("Incorrect exception message: " + str);
83        } catch (Exception e) {
84            fail("Incorrect exception " + e + " was thrown");
85        }
86
87        for (int i = 0; i < invalid_port.length; i++) {
88            try {
89                SSLEngine sleng = ssl.engineCreateSSLEngine("localhost", invalid_port[i]);
90                fail("IllegalArgumentException wasn't thrown");
91            } catch (IllegalArgumentException iae) {
92                //expected
93            }
94        }
95    }
96
97    /**
98     * SSLContextSpi#engineGetClientSessionContext()
99     * SSLContextSpi#engineGetServerSessionContext()
100     * SSLContextSpi#engineGetServerSocketFactory()
101     * SSLContextSpi#engineGetSocketFactory()
102     * Verify exception when SSLContextSpi object wasn't initialiazed.
103     */
104    public void test_commonTest_01() {
105        SSLContextSpiImpl ssl = new SSLContextSpiImpl();
106
107        try {
108            SSLSessionContext slsc = ssl.engineGetClientSessionContext();
109            fail("RuntimeException wasn't thrown");
110        } catch (RuntimeException re) {
111            String str = re.getMessage();
112            if (!str.equals("Not initialiazed"))
113                fail("Incorrect exception message: " + str);
114        } catch (Exception e) {
115            fail("Incorrect exception " + e + " was thrown");
116        }
117
118        try {
119            SSLSessionContext slsc = ssl.engineGetServerSessionContext();
120            fail("RuntimeException wasn't thrown");
121        } catch (RuntimeException re) {
122            String str = re.getMessage();
123            if (!str.equals("Not initialiazed"))
124                fail("Incorrect exception message: " + str);
125        } catch (Exception e) {
126            fail("Incorrect exception " + e + " was thrown");
127        }
128
129        try {
130            SSLServerSocketFactory sssf = ssl.engineGetServerSocketFactory();
131            fail("RuntimeException wasn't thrown");
132        } catch (RuntimeException re) {
133            String str = re.getMessage();
134            if (!str.equals("Not initialiazed"))
135                fail("Incorrect exception message: " + str);
136        } catch (Exception e) {
137            fail("Incorrect exception " + e + " was thrown");
138        }
139
140        try {
141            SSLSocketFactory ssf = ssl.engineGetSocketFactory();
142            fail("RuntimeException wasn't thrown");
143        } catch (RuntimeException re) {
144            String str = re.getMessage();
145            if (!str.equals("Not initialiazed"))
146                fail("Incorrect exception message: " + str);
147        } catch (Exception e) {
148            fail("Incorrect exception " + e + " was thrown");
149        }
150    }
151
152    /**
153     * SSLContextSpi#engineInit(KeyManager[] km, TrustManager[] tm, SecureRandom sr)
154     */
155    public void test_engineInit() {
156        SSLContextSpiImpl ssl = new SSLContextSpiImpl();
157        String defaultAlgorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
158        try {
159            KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
160            char[] pass = "password".toCharArray();
161            kmf.init(null, pass);
162            KeyManager[] km = kmf.getKeyManagers();
163            defaultAlgorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm");
164            TrustManagerFactory trustMF = TrustManagerFactory.getInstance(defaultAlgorithm);
165            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
166            ks.load(null, null);
167            trustMF.init(ks);
168            TrustManager[] tm = trustMF.getTrustManagers();
169            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
170            try {
171                ssl.engineInit(km, tm, sr);
172            } catch (KeyManagementException kme) {
173                fail(kme + " was throw for engineInit method");
174            }
175            try {
176                ssl.engineInit(km, tm, null);
177                fail("KeyManagementException wasn't thrown");
178            } catch (KeyManagementException kme) {
179                //expected
180            }
181        } catch (Exception ex) {
182            fail(ex + " unexpected exception");
183        }
184    }
185
186    /**
187     * SSLContextSpi#engineCreateSSLEngine()
188     * SSLContextSpi#engineCreateSSLEngine(String host, int port)
189     * SSLContextSpi#engineGetClientSessionContext()
190     * SSLContextSpi#engineGetServerSessionContext()
191     * SSLContextSpi#engineGetServerSocketFactory()
192     * SSLContextSpi#engineGetSocketFactory()
193     */
194    public void test_commonTest_02() {
195        SSLContextSpiImpl ssl = new SSLContextSpiImpl();
196        String defaultAlgorithm = Security.getProperty("ssl.KeyManagerFactory.algorithm");
197        try {
198            KeyManagerFactory kmf = KeyManagerFactory.getInstance(defaultAlgorithm);
199            char[] pass = "password".toCharArray();
200            kmf.init(null, pass);
201            KeyManager[] km = kmf.getKeyManagers();
202            defaultAlgorithm = Security.getProperty("ssl.TrustManagerFactory.algorithm");
203            TrustManagerFactory trustMF = TrustManagerFactory.getInstance(defaultAlgorithm);
204            KeyStore ks = KeyStore.getInstance(KeyStore.getDefaultType());
205            ks.load(null, null);
206            trustMF.init(ks);
207            TrustManager[] tm = trustMF.getTrustManagers();
208            SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
209            ssl.engineInit(km, tm, sr);
210        } catch (Exception ex) {
211            fail(ex + " unexpected exception");
212        }
213
214        try {
215            assertNotNull("Subtest_01: Object is NULL", ssl.engineCreateSSLEngine());
216            SSLEngine sleng = ssl.engineCreateSSLEngine("localhost", 1080);
217            assertNotNull("Subtest_02: Object is NULL", sleng);
218            assertEquals(sleng.getPeerPort(), 1080);
219            assertEquals(sleng.getPeerHost(), "localhost");
220            assertNull("Subtest_03: Object not NULL", ssl.engineGetClientSessionContext());
221            assertNull("Subtest_04: Object not NULL", ssl.engineGetServerSessionContext());
222            assertNull("Subtest_05: Object not NULL", ssl.engineGetServerSocketFactory());
223            assertNull("Subtest_06: Object not NULL", ssl.engineGetSocketFactory());
224        } catch (Exception e) {
225            fail("Unexpected exception " + e);
226        }
227    }
228
229}
230