blob: 30a1a9c53b7318f8091763441a39889d7cdd5d3b [file] [log] [blame]
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001/*
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
Jorg Pleumann051f4002009-04-07 11:30:34 -070020import java.io.IOException;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080021import java.nio.ByteBuffer;
Jorg Pleumann051f4002009-04-07 11:30:34 -070022import java.nio.ReadOnlyBufferException;
23import java.nio.channels.Pipe;
24import java.nio.channels.Pipe.SinkChannel;
25import java.nio.channels.Pipe.SourceChannel;
26import java.security.KeyManagementException;
Jorg Pleumann051f4002009-04-07 11:30:34 -070027import java.security.NoSuchAlgorithmException;
Jorg Pleumann051f4002009-04-07 11:30:34 -070028import java.util.Arrays;
Urs Grobbe258112009-04-09 20:28:05 -070029import java.util.HashSet;
30import java.util.Set;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080031
Urs Grob1cd5a5c2009-03-24 20:48:11 -070032import javax.net.ssl.SSLContext;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080033import javax.net.ssl.SSLEngine;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080034import javax.net.ssl.SSLEngineResult;
Jorg Pleumann051f4002009-04-07 11:30:34 -070035import javax.net.ssl.SSLException;
Jorg Pleumann051f4002009-04-07 11:30:34 -070036import javax.net.ssl.SSLEngineResult.HandshakeStatus;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080037
38import junit.framework.TestCase;
Jorg Pleumann051f4002009-04-07 11:30:34 -070039import dalvik.annotation.AndroidOnly;
40import dalvik.annotation.KnownFailure;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080041
42
43/**
44 * Tests for SSLEngine class
Elliott Hughesf33eae72010-05-13 12:36:25 -070045 *
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080046 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080047public class SSLEngineTest extends TestCase {
48
Jorg Pleumann051f4002009-04-07 11:30:34 -070049 private HandshakeHandler clientEngine;
50 private HandshakeHandler serverEngine;
51
Jesse Wilsond37c8042009-10-23 15:31:41 -070052 @Override protected void setUp() throws Exception {
53 super.setUp();
Jesse Wilsond37c8042009-10-23 15:31:41 -070054 }
55
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080056 /**
57 * Test for <code>SSLEngine()</code> constructor Assertion: creates
58 * SSLEngine object with null host and -1 port
Elliott Hughesf33eae72010-05-13 12:36:25 -070059 * @throws NoSuchAlgorithmException
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080060 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -070061 public void test_Constructor() throws NoSuchAlgorithmException {
62 SSLEngine e = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080063 assertNull(e.getPeerHost());
64 assertEquals(-1, e.getPeerPort());
Urs Grob1cd5a5c2009-03-24 20:48:11 -070065 String[] suites = e.getSupportedCipherSuites();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080066 e.setEnabledCipherSuites(suites);
67 assertEquals(e.getEnabledCipherSuites().length, suites.length);
68 }
69
70 /**
71 * Test for <code>SSLEngine(String host, int port)</code> constructor
Elliott Hughesf33eae72010-05-13 12:36:25 -070072 * @throws NoSuchAlgorithmException
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080073 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -070074 public void test_ConstructorLjava_lang_StringI01() throws NoSuchAlgorithmException {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080075 int port = 1010;
Urs Grob1cd5a5c2009-03-24 20:48:11 -070076 SSLEngine e = getEngine(null, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080077 assertNull(e.getPeerHost());
78 assertEquals(e.getPeerPort(), port);
79 try {
80 e.beginHandshake();
Urs Grob1cd5a5c2009-03-24 20:48:11 -070081 } catch (IllegalStateException ex) {
82 // expected
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080083 } catch (SSLException ex) {
Urs Grob1cd5a5c2009-03-24 20:48:11 -070084 fail("unexpected SSLException was thrown.");
85 }
86 e = getEngine(null, port);
87 e.setUseClientMode(true);
88 try {
89 e.beginHandshake();
90 } catch (SSLException ex) {
91 // expected
92 }
93 e = getEngine(null, port);
94 e.setUseClientMode(false);
95 try {
96 e.beginHandshake();
97 } catch (SSLException ex) {
98 // expected
The Android Open Source Projectadc854b2009-03-03 19:28:47 -080099 }
100 }
101
102 /**
103 * Test for <code>SSLEngine(String host, int port)</code> constructor
Elliott Hughesf33eae72010-05-13 12:36:25 -0700104 * @throws NoSuchAlgorithmException
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800105 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700106 public void test_ConstructorLjava_lang_StringI02() throws NoSuchAlgorithmException {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800107 String host = "new host";
108 int port = 8080;
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700109 SSLEngine e = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800110 assertEquals(e.getPeerHost(), host);
111 assertEquals(e.getPeerPort(), port);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700112 String[] suites = e.getSupportedCipherSuites();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800113 e.setEnabledCipherSuites(suites);
114 assertEquals(e.getEnabledCipherSuites().length, suites.length);
115 e.setUseClientMode(true);
116 assertTrue(e.getUseClientMode());
117 }
118
119 /**
120 * Test for <code>getPeerHost()</code> method
Elliott Hughesf33eae72010-05-13 12:36:25 -0700121 * @throws NoSuchAlgorithmException
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800122 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700123 public void test_getPeerHost() throws NoSuchAlgorithmException {
124 SSLEngine e = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800125 assertNull(e.getPeerHost());
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700126 e = getEngine("www.fortify.net", 80);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800127 assertEquals("Incorrect host name", "www.fortify.net", e.getPeerHost());
128 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700129
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800130 /**
131 * Test for <code>getPeerPort()</code> method
Elliott Hughesf33eae72010-05-13 12:36:25 -0700132 * @throws NoSuchAlgorithmException
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800133 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700134 public void test_getPeerPort() throws NoSuchAlgorithmException {
135 SSLEngine e = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800136 assertEquals("Incorrect default value of peer port",
137 -1 ,e.getPeerPort());
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700138 e = getEngine("www.fortify.net", 80);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800139 assertEquals("Incorrect peer port", 80, e.getPeerPort());
140 }
141
142 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700143 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800144 * javax.net.ssl.SSLEngine#getSupportedProtocols()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800145 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700146 public void test_getSupportedProtocols() throws NoSuchAlgorithmException {
147 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800148 try {
149 String[] res = sse.getSupportedProtocols();
150 assertNotNull(res);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700151 assertTrue(res.length > 0);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800152 } catch (Exception ex) {
153 fail("Unexpected exception " + ex);
154 }
155 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700156
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800157 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700158 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800159 * javax.net.ssl.SSLEngine#setEnabledProtocols(String[] protocols)
160 * javax.net.ssl.SSLEngine#getEnabledProtocols()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800161 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700162 public void test_EnabledProtocols() throws NoSuchAlgorithmException {
163 SSLEngine sse = getEngine();
164 String[] pr = sse.getSupportedProtocols();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800165 try {
166 sse.setEnabledProtocols(pr);
167 String[] res = sse.getEnabledProtocols();
168 assertNotNull("Null array was returned", res);
169 assertEquals("Incorrect array length", res.length, pr.length);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700170 assertTrue("Incorrect array was returned", Arrays.equals(res, pr));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800171 } catch (Exception ex) {
172 fail("Unexpected exception " + ex);
173 }
174 try {
175 sse.setEnabledProtocols(null);
176 fail("IllegalArgumentException wasn't thrown");
177 } catch (IllegalArgumentException iae) {
178 //expected
179 }
180 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700181
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800182 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700183 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800184 * javax.net.ssl.SSLEngine#getSupportedCipherSuites()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800185 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700186 public void test_getSupportedCipherSuites() throws NoSuchAlgorithmException {
187 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800188 try {
189 String[] res = sse.getSupportedCipherSuites();
190 assertNotNull(res);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700191 assertTrue(res.length > 0);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800192 } catch (Exception ex) {
193 fail("Unexpected exception " + ex);
194 }
195 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700196
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800197 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700198 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800199 * javax.net.ssl.SSLEngine#setEnabledCipherSuites(String[] suites)
200 * javax.net.ssl.SSLEngine#getEnabledCipherSuites()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800201 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700202 public void test_EnabledCipherSuites() throws NoSuchAlgorithmException {
203 SSLEngine sse = getEngine();
204 String[] st = sse.getSupportedCipherSuites();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800205 try {
206 sse.setEnabledCipherSuites(st);
207 String[] res = sse.getEnabledCipherSuites();
208 assertNotNull("Null array was returned", res);
209 assertEquals("Incorrect array length", res.length, st.length);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700210 assertTrue("Incorrect array was returned", Arrays.equals(res, st));
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800211 } catch (Exception ex) {
212 fail("Unexpected exception " + ex);
213 }
214 try {
215 sse.setEnabledCipherSuites(null);
216 fail("IllegalArgumentException wasn't thrown");
217 } catch (IllegalArgumentException iae) {
218 //expected
219 }
220 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700221
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800222 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700223 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800224 * javax.net.ssl.SSLEngine#setEnableSessionCreation(boolean flag)
225 * javax.net.ssl.SSLEngine#getEnableSessionCreation()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800226 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700227 public void test_EnableSessionCreation() throws NoSuchAlgorithmException {
228 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800229 try {
230 assertTrue(sse.getEnableSessionCreation());
231 sse.setEnableSessionCreation(false);
232 assertFalse(sse.getEnableSessionCreation());
233 sse.setEnableSessionCreation(true);
234 assertTrue(sse.getEnableSessionCreation());
235 } catch (Exception ex) {
236 fail("Unexpected exception " + ex);
237 }
238 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700239
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800240 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700241 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800242 * javax.net.ssl.SSLEngine#setNeedClientAuth(boolean need)
243 * javax.net.ssl.SSLEngine#getNeedClientAuth()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800244 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700245 public void test_NeedClientAuth() throws NoSuchAlgorithmException {
246 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800247 try {
248 sse.setNeedClientAuth(false);
249 assertFalse(sse.getNeedClientAuth());
250 sse.setNeedClientAuth(true);
251 assertTrue(sse.getNeedClientAuth());
252 } catch (Exception ex) {
253 fail("Unexpected exception " + ex);
254 }
255 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700256
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800257 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700258 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800259 * javax.net.ssl.SSLEngine#setWantClientAuth(boolean want)
260 * javax.net.ssl.SSLEngine#getWantClientAuth()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800261 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700262 public void test_WantClientAuth() throws NoSuchAlgorithmException {
263 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800264 try {
265 sse.setWantClientAuth(false);
266 assertFalse(sse.getWantClientAuth());
267 sse.setWantClientAuth(true);
268 assertTrue(sse.getWantClientAuth());
269 } catch (Exception ex) {
270 fail("Unexpected exception " + ex);
271 }
272 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700273
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800274 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700275 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800276 * javax.net.ssl.SSLEngine#beginHandshake()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800277 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700278 public void test_beginHandshake() throws NoSuchAlgorithmException {
279 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800280 try {
281 sse.beginHandshake();
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700282 fail("IllegalStateException wasn't thrown");
283 } catch (IllegalStateException se) {
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800284 //expected
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700285 } catch (Exception e) {
286 fail(e + " was thrown instead of IllegalStateException");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800287 }
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700288 sse = getEngine("new host", 1080);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800289 try {
290 sse.beginHandshake();
291 fail("IllegalStateException wasn't thrown");
292 } catch (IllegalStateException ise) {
293 //expected
294 } catch (Exception e) {
295 fail(e + " was thrown instead of IllegalStateException");
296 }
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700297 sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800298 try {
299 sse.setUseClientMode(true);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800300 sse.beginHandshake();
301 } catch (Exception ex) {
302 fail("Unexpected exception " + ex);
303 }
304 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700305
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800306 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700307 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800308 * javax.net.ssl.SSLEngine#setUseClientMode(boolean mode)
309 * javax.net.ssl.SSLEngine#getUseClientMode()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800310 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700311 @AndroidOnly("The RI doesn't throw the expected IllegalStateException.")
312 public void test_UseClientMode() throws NoSuchAlgorithmException {
313 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800314 try {
315 sse.setUseClientMode(false);
316 assertFalse(sse.getUseClientMode());
317 sse.setUseClientMode(true);
318 assertTrue(sse.getUseClientMode());
319 } catch (Exception ex) {
320 fail("Unexpected exception " + ex);
321 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700322
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800323 try {
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700324 sse = getEngine(null, 1080);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800325 sse.setUseClientMode(true);
326 sse.beginHandshake();
327 try {
328 sse.setUseClientMode(false);
329 fail("IllegalArgumentException was not thrown");
330 } catch (IllegalArgumentException iae) {
331 //expected
332 }
333 } catch (Exception ex) {
334 fail("Unexpected exception " + ex);
335 }
336 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700337
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800338 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700339 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800340 * javax.net.ssl.SSLEngine#getSession()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800341 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700342 public void test_getSession() throws NoSuchAlgorithmException {
343 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800344 try {
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700345 assertNotNull(sse.getSession());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800346 } catch (Exception ex) {
347 fail("Unexpected exception " + ex);
348 }
349 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700350
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800351 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700352 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800353 * javax.net.ssl.SSLEngine#getHandshakeStatus()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800354 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700355 public void test_getHandshakeStatus() throws NoSuchAlgorithmException {
356 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800357 try {
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700358 assertEquals(sse.getHandshakeStatus().toString(), "NOT_HANDSHAKING");
359 sse.setUseClientMode(true);
360 sse.beginHandshake();
361 assertEquals(sse.getHandshakeStatus().toString(), "NEED_WRAP");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800362 } catch (Exception ex) {
363 fail("Unexpected exception " + ex);
364 }
365 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700366
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800367 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700368 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800369 * javax.net.ssl.SSLEngine#getDelegatedTask()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800370 */
Kenny Root70b79a22013-05-03 14:18:41 -0700371 @KnownFailure("com.android.org.conscrypt.SSLEngineImpl#getDelegatedTask() throws NPE instead of returning null")
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700372 public void test_getDelegatedTask() throws NoSuchAlgorithmException {
373 SSLEngine sse = getEngine();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800374 try {
375 assertNull(sse.getDelegatedTask());
376 } catch (Exception ex) {
377 fail("Unexpected exception " + ex);
378 }
379 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700380
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800381 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700382 * @throws IOException
383 * @throws InterruptedException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800384 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800385 * int offset, int length)
386 * Exception case: SSLException should be thrown.
387 */
Jorg Pleumann051f4002009-04-07 11:30:34 -0700388 public void test_unwrap_01() throws IOException, InterruptedException {
389 prepareEngines();
390 doHandshake();
Elliott Hughesf33eae72010-05-13 12:36:25 -0700391
Jorg Pleumann051f4002009-04-07 11:30:34 -0700392 ByteBuffer bbs = ByteBuffer.wrap(new byte[] {1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,3,1,2,31,2,3,1,2,3,1,2,3,1,2,3});
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700393 ByteBuffer bbd = ByteBuffer.allocate(100);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800394 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700395 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }, 0, 1);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800396 fail("SSLException wasn't thrown");
397 } catch (SSLException ex) {
398 //expected
399 }
400 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700401
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800402 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800403 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800404 * int offset, int length)
405 * Exception case: IndexOutOfBoundsException should be thrown.
406 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700407 @KnownFailure("Fixed in DonutBurger, boundary checks missing")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800408 public void test_unwrap_02() throws SSLException {
409 String host = "new host";
410 int port = 8080;
411 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
412
413 ByteBuffer bb = ByteBuffer.allocate(10);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700414 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800415 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700416
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800417 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700418 sse.unwrap(bb, bbA, -1, 3);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800419 fail("IndexOutOfBoundsException wasn't thrown");
420 } catch (IndexOutOfBoundsException iobe) {
421 //expected
422 }
423 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700424 sse.unwrap(bb, bbA, 0, -3);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800425 fail("IndexOutOfBoundsException wasn't thrown");
426 } catch (IndexOutOfBoundsException iobe) {
427 //expected
428 }
429 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700430 sse.unwrap(bb, bbA, bbA.length + 1, bbA.length);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800431 fail("IndexOutOfBoundsException wasn't thrown");
432 } catch (IndexOutOfBoundsException iobe) {
433 //expected
434 }
435 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700436 sse.unwrap(bb, bbA, 0, bbA.length + 1);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800437 fail("IndexOutOfBoundsException wasn't thrown");
438 } catch (IndexOutOfBoundsException iobe) {
439 //expected
440 }
441 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700442
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800443 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800444 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800445 * int offset, int length)
446 * Exception case: ReadOnlyBufferException should be thrown.
447 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700448 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800449 public void test_unwrap_03() {
450 String host = "new host";
451 int port = 8080;
452 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer();
453 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
454
455 ByteBuffer bb = ByteBuffer.allocate(10);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700456 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800457 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700458
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800459 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700460 sse.unwrap(bb, bbA, 0, bbA.length);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800461 fail("ReadOnlyBufferException wasn't thrown");
462 } catch (ReadOnlyBufferException iobe) {
463 //expected
464 } catch (Exception e) {
465 fail(e + " was thrown instead of ReadOnlyBufferException");
466 }
467 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700468
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800469 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800470 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800471 * int offset, int length)
472 * Exception case: IllegalArgumentException should be thrown.
473 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700474 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800475 public void test_unwrap_04() {
476 String host = "new host";
477 int port = 8080;
478 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)};
479 ByteBuffer[] bbAN = {ByteBuffer.allocate(100), null, ByteBuffer.allocate(100)};
480 ByteBuffer[] bbN = null;
481 ByteBuffer bb = ByteBuffer.allocate(10);
482 ByteBuffer bN = null;
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700483 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800484 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700485
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800486 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700487 sse.unwrap(bN, bbA, 0, 3);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800488 fail("IllegalArgumentException wasn't thrown");
489 } catch (IllegalArgumentException iobe) {
490 //expected
491 } catch (NullPointerException npe) {
492 } catch (Exception e) {
493 fail(e + " was thrown instead of IllegalArgumentException");
494 }
495 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700496 sse.unwrap(bb, bbAN, 0, 3);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800497 fail("IllegalArgumentException wasn't thrown");
498 } catch (IllegalArgumentException iobe) {
499 //expected
500 } catch (NullPointerException npe) {
501 } catch (Exception e) {
502 fail(e + " was thrown instead of IllegalArgumentException");
503 }
504 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700505 sse.unwrap(bb, bbN, 0, 0);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800506 fail("IllegalArgumentException wasn't thrown");
507 } catch (IllegalArgumentException iobe) {
508 //expected
509 } catch (NullPointerException npe) {
510 } catch (Exception e) {
511 fail(e + " was thrown instead of IllegalArgumentException");
512 }
513 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700514 sse.unwrap(bN, bbN, 0, 0);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800515 fail("IllegalArgumentException wasn't thrown");
516 } catch (IllegalArgumentException iobe) {
517 //expected
518 } catch (NullPointerException npe) {
519 } catch (Exception e) {
520 fail(e + " was thrown instead of IllegalArgumentException");
521 }
522
523 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700524
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800525 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800526 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800527 * int offset, int length)
528 * Exception case: IllegalStateException should be thrown.
529 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700530 @AndroidOnly("The RI doesn't throw the IllegalStateException.")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800531 public void test_unwrap_05() {
532 String host = "new host";
533 int port = 8080;
534 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
535
536 ByteBuffer bb = ByteBuffer.allocate(10);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700537 SSLEngine sse = getEngine(host, port);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700538
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800539 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700540 sse.unwrap(bb, bbA, 0, bbA.length);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800541 fail("IllegalStateException wasn't thrown");
542 } catch (IllegalStateException iobe) {
543 //expected
544 } catch (Exception e) {
545 fail(e + " was thrown instead of IllegalStateException");
546 }
547 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700548
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800549 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800550 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800551 * int offset, int length)
552 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800553 public void test_unwrap_06() {
554 String host = "new host";
555 int port = 8080;
556 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
557
558 ByteBuffer bb = ByteBuffer.allocate(10);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700559 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800560 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700561
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800562 try {
563 SSLEngineResult res = sse.unwrap(bb, bbA, 0, bbA.length);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700564 assertEquals(0, res.bytesConsumed());
565 assertEquals(0, res.bytesProduced());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800566 } catch (Exception ex) {
567 fail("Unexpected exception: " + ex);
568 }
569 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700570
Jorg Pleumann051f4002009-04-07 11:30:34 -0700571 public void test_wrap_01() throws IOException, InterruptedException {
572 prepareEngines();
573 doHandshake();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800574 ByteBuffer bbs = ByteBuffer.allocate(100);
Jorg Pleumann051f4002009-04-07 11:30:34 -0700575 ByteBuffer bbd = ByteBuffer.allocate(20000);
Jesse Wilson5a1dfc82011-01-13 14:01:03 -0800576 clientEngine.engine.wrap(new ByteBuffer[] { bbs }, 0, 1, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800577 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700578
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800579 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800580 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800581 * int length, ByteBuffer dst)
582 * Exception case: IndexOutOfBoundsException should be thrown.
583 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700584 @KnownFailure("Fixed in DonutBurger, boundary checks missing")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800585 public void test_wrap_02() throws SSLException {
586 String host = "new host";
587 int port = 8080;
588 ByteBuffer bb = ByteBuffer.allocate(10);
589 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700590 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800591 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700592
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800593 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700594 sse.wrap(bbA, -1, 3, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800595 fail("IndexOutOfBoundsException wasn't thrown");
596 } catch (IndexOutOfBoundsException iobe) {
597 //expected
598 }
599 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700600 sse.wrap(bbA, 0, -3, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800601 fail("IndexOutOfBoundsException wasn't thrown");
602 } catch (IndexOutOfBoundsException iobe) {
603 //expected
604 }
605 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700606 sse.wrap(bbA, bbA.length + 1, bbA.length, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800607 fail("IndexOutOfBoundsException wasn't thrown");
608 } catch (IndexOutOfBoundsException iobe) {
609 //expected
610 }
611 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700612 sse.wrap(bbA, 0, bbA.length + 1, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800613 fail("IndexOutOfBoundsException wasn't thrown");
614 } catch (IndexOutOfBoundsException iobe) {
615 //expected
616 }
617 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700618
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800619 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800620 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800621 * int length, ByteBuffer dst)
622 * Exception case: ReadOnlyBufferException should be thrown.
623 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800624 public void test_wrap_03() throws SSLException {
625 String host = "new host";
626 int port = 8080;
627 ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer();
628 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700629 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800630 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700631
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800632 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700633 sse.wrap(bbA, 0, bbA.length, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800634 fail("ReadOnlyBufferException wasn't thrown");
635 } catch (ReadOnlyBufferException iobe) {
636 //expected
637 }
638 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700639
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800640 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800641 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800642 * int length, ByteBuffer dst)
643 * Exception case: IllegalArgumentException should be thrown.
644 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700645 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800646 public void test_wrap_04() {
647 String host = "new host";
648 int port = 8080;
649 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)};
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800650 ByteBuffer[] bbN = null;
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800651 ByteBuffer bN = null;
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700652 SSLEngine e = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800653 e.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700654
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800655 try {
656 e.wrap(bbA, 0, 3, bN);
657 fail("IllegalArgumentException must be thrown for null srcs byte buffer array");
658 } catch (NullPointerException npe) {
659 } catch (IllegalArgumentException ex) {
660 } catch (Exception ex) {
661 fail(ex + " was thrown instead of IllegalArgumentException");
662 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700663
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800664 try {
665 e.wrap(bbN, 0, 0, bN);
666 fail("IllegalArgumentException wasn't thrown");
667 } catch (IllegalArgumentException ex) {
668 } catch (NullPointerException npe) {
669 } catch (Exception ex) {
670 fail(ex + " was thrown instead of IllegalArgumentException");
671 }
672 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700673
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800674 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800675 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800676 * int length, ByteBuffer dst)
677 * Exception case: IllegalStateException should be thrown.
678 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700679 @AndroidOnly("The RI doesn't throw the IllegalStateException.")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800680 public void test_wrap_05() throws SSLException {
681 String host = "new host";
682 int port = 8080;
683 ByteBuffer bb = ByteBuffer.allocate(10);
684 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700685 SSLEngine sse = getEngine(host, port);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700686
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800687 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700688 sse.wrap(bbA, 0, bbA.length, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800689 fail("IllegalStateException wasn't thrown");
690 } catch (IllegalStateException iobe) {
691 //expected
692 }
693 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700694
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800695 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800696 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset,
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800697 * int length, ByteBuffer dst)
698 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800699 public void test_wrap_06() {
700 String host = "new host";
701 int port = 8080;
702 ByteBuffer bb = ByteBuffer.allocate(10);
703 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700704 SSLEngine sse = getEngine(host, port);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700705 sse.setUseClientMode(true);
706
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800707 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700708 sse.wrap(bbA, 0, bbA.length, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800709 } catch (Exception ex) {
710 fail("Unexpected exception: " + ex);
711 }
712 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700713
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800714 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700715 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800716 * javax.net.ssl.SSLEngine#closeOutbound()
717 * javax.net.ssl.SSLEngine#isOutboundDone()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800718 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700719 public void test_closeOutbound() throws NoSuchAlgorithmException {
720 SSLEngine sse = getEngine();
Elliott Hughesf33eae72010-05-13 12:36:25 -0700721
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800722 try {
723 assertFalse(sse.isOutboundDone());
724 sse.closeOutbound();
725 assertTrue(sse.isOutboundDone());
726 } catch (Exception ex) {
727 fail("Unexpected exception: " + ex);
728 }
729 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700730
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800731 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -0700732 * @throws NoSuchAlgorithmException
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800733 * javax.net.ssl.SSLEngine#closeInbound()
734 * javax.net.ssl.SSLEngine#isInboundDone()
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800735 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700736 public void test_closeInbound() throws NoSuchAlgorithmException {
737 SSLEngine sse = getEngine();
Elliott Hughesf33eae72010-05-13 12:36:25 -0700738
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800739 try {
740 assertFalse(sse.isInboundDone());
741 sse.closeInbound();
742 assertTrue(sse.isInboundDone());
743 } catch (Exception ex) {
744 fail("Unexpected exception: " + ex);
745 }
746 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700747
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800748 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800749 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800750 * SSLException should be thrown.
751 */
Jorg Pleumann051f4002009-04-07 11:30:34 -0700752 public void test_unwrap_ByteBuffer_ByteBuffer_01() throws InterruptedException, IOException {
753 prepareEngines();
754 doHandshake();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800755 ByteBuffer bbs = ByteBuffer.allocate(100);
Jorg Pleumann051f4002009-04-07 11:30:34 -0700756 ByteBuffer bbd = ByteBuffer.allocate(100);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700757
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800758 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700759 SSLEngineResult unwrap = clientEngine.engine.unwrap(bbs, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800760 fail("SSLException wasn't thrown");
761 } catch (SSLException ex) {
762 //expected
763 }
764 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700765
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800766 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800767 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800768 * ReadOnlyBufferException should be thrown.
769 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700770 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800771 public void test_unwrap_ByteBuffer_ByteBuffer_02() {
772 String host = "new host";
773 int port = 8080;
774 ByteBuffer bbs = ByteBuffer.allocate(10);
775 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer();
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700776 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800777 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700778
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800779 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700780 sse.unwrap(bbs, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800781 fail("ReadOnlyBufferException wasn't thrown");
782 } catch (ReadOnlyBufferException iobe) {
783 //expected
784 } catch (Exception e) {
785 fail(e + " was thrown instead of ReadOnlyBufferException");
786 }
787 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700788
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800789 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800790 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800791 * IllegalArgumentException should be thrown.
792 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700793 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800794 public void test_unwrap_ByteBuffer_ByteBuffer_03() {
795 String host = "new host";
796 int port = 8080;
797 ByteBuffer bbsN = null;
798 ByteBuffer bbdN = null;
799 ByteBuffer bbs = ByteBuffer.allocate(10);
800 ByteBuffer bbd = ByteBuffer.allocate(100);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700801 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800802 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700803
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800804 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700805 sse.unwrap(bbsN, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800806 fail("IllegalArgumentException wasn't thrown");
807 } catch (IllegalArgumentException iae) {
808 //expected
809 } catch (NullPointerException npe) {
810 } catch (Exception e) {
811 fail(e + " was thrown instead of IllegalArgumentException");
812 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700813
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800814 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700815 sse.unwrap(bbs, bbdN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800816 fail("IllegalArgumentException wasn't thrown");
817 } catch (IllegalArgumentException iae) {
818 //expected
819 } catch (NullPointerException npe) {
820 } catch (Exception e) {
821 fail(e + " was thrown instead of IllegalArgumentException");
822 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700823
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800824 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700825 sse.unwrap(bbsN, bbdN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800826 fail("IllegalArgumentException wasn't thrown");
827 } catch (IllegalArgumentException iae) {
828 //expected
829 } catch (NullPointerException npe) {
830 } catch (Exception e) {
831 fail(e + " was thrown instead of IllegalArgumentException");
832 }
833 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700834
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800835 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800836 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800837 * IllegalStateException should be thrown.
838 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700839 @AndroidOnly("The RI doesn't throw the IllegalStateException.")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800840 public void test_unwrap_ByteBuffer_ByteBuffer_04() {
841 String host = "new host";
842 int port = 8080;
843 ByteBuffer bbs = ByteBuffer.allocate(10);
844 ByteBuffer bbd = ByteBuffer.allocate(100);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700845 SSLEngine sse = getEngine(host, port);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700846
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800847 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700848 sse.unwrap(bbs, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800849 fail("IllegalStateException wasn't thrown");
850 } catch (IllegalStateException iobe) {
851 //expected
852 } catch (Exception e) {
853 fail(e + " was thrown instead of IllegalStateException");
854 }
855 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700856
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800857 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800858 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800859 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800860 public void test_unwrap_ByteBuffer_ByteBuffer_05() {
861 String host = "new host";
862 int port = 8080;
863 ByteBuffer bbs = ByteBuffer.allocate(10);
864 ByteBuffer bbd = ByteBuffer.allocate(100);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700865 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800866 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700867
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800868 try {
869 SSLEngineResult res = sse.unwrap(bbs, bbd);
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700870 assertEquals(0, res.bytesConsumed());
871 assertEquals(0, res.bytesProduced());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800872 } catch (Exception e) {
873 fail("Unexpected exception: " + e);
874 }
875 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700876
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800877 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800878 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800879 * SSLException should be thrown.
880 */
Urs Grobbe258112009-04-09 20:28:05 -0700881 public void test_unwrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException {
882 prepareEngines();
883 doHandshake();
Jorg Pleumann051f4002009-04-07 11:30:34 -0700884
Elliott Hughesf33eae72010-05-13 12:36:25 -0700885 ByteBuffer bbs = ByteBuffer.allocate(100);
Urs Grobbe258112009-04-09 20:28:05 -0700886 ByteBuffer bbd = ByteBuffer.allocate(100);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700887
Jorg Pleumann051f4002009-04-07 11:30:34 -0700888 try {
Urs Grobbe258112009-04-09 20:28:05 -0700889 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd });
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800890 fail("SSLException wasn't thrown");
891 } catch (SSLException ex) {
892 //expected
893 }
894 }
Jorg Pleumann051f4002009-04-07 11:30:34 -0700895
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800896 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800897 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800898 * ReadOnlyBufferException should be thrown.
899 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700900 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800901 public void test_unwrap_ByteBuffer$ByteBuffer_02() {
902 String host = "new host";
903 int port = 8080;
904 ByteBuffer bbs = ByteBuffer.allocate(10);
905 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer();
906 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700907 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800908 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700909
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800910 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700911 sse.unwrap(bbs, bbA);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800912 fail("ReadOnlyBufferException wasn't thrown");
913 } catch (ReadOnlyBufferException iobe) {
914 //expected
915 } catch (Exception e) {
916 fail(e + " was thrown instead of ReadOnlyBufferException");
917 }
918 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700919
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800920 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800921 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800922 * IllegalArgumentException should be thrown.
923 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700924 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800925 public void test_unwrap_ByteBuffer$ByteBuffer_03() {
926 String host = "new host";
927 int port = 8080;
928 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
929 ByteBuffer[] bbN = { ByteBuffer.allocate(100), null, ByteBuffer.allocate(100) };
930 ByteBuffer[] bbAN = null;
931 ByteBuffer bb = ByteBuffer.allocate(10);
932 ByteBuffer bN = null;
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700933 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800934 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700935
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800936 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700937 sse.unwrap(bN, bbA);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800938 fail("IllegalArgumentException wasn't thrown");
939 } catch (IllegalArgumentException iobe) {
940 //expected
941 } catch (NullPointerException npe) {
942 } catch (Exception e) {
943 fail(e + " was thrown instead of IllegalArgumentException");
944 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700945
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800946 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700947 sse.unwrap(bb, bbAN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800948 fail("IllegalArgumentException wasn't thrown");
949 } catch (IllegalArgumentException iobe) {
950 //expected
951 } catch (NullPointerException npe) {
952 } catch (Exception e) {
953 fail(e + " was thrown instead of IllegalArgumentException");
954 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700955
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800956 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700957 sse.unwrap(bb, bbN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800958 fail("IllegalArgumentException wasn't thrown");
959 } catch (IllegalArgumentException iobe) {
960 //expected
961 } catch (NullPointerException npe) {
962 } catch (Exception e) {
963 fail(e + " was thrown instead of IllegalArgumentException");
964 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700965
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800966 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700967 sse.unwrap(bN, bbAN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800968 fail("IllegalArgumentException wasn't thrown");
969 } catch (IllegalArgumentException iobe) {
970 //expected
971 } catch (NullPointerException npe) {
972 } catch (Exception e) {
973 fail(e + " was thrown instead of IllegalArgumentException");
974 }
975 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700976
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800977 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -0800978 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800979 * IllegalStateException should be thrown.
980 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700981 @AndroidOnly("The RI doesn't throw the IllegalStateException.")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800982 public void test_unwrap_ByteBuffer$ByteBuffer_04() {
983 String host = "new host";
984 int port = 8080;
985 ByteBuffer bbs = ByteBuffer.allocate(10);
986 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
Urs Grob1cd5a5c2009-03-24 20:48:11 -0700987 SSLEngine sse = getEngine(host, port);
Elliott Hughesf33eae72010-05-13 12:36:25 -0700988
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800989 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -0700990 sse.unwrap(bbs, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800991 fail("IllegalStateException wasn't thrown");
992 } catch (IllegalStateException iobe) {
993 //expected
994 } catch (Exception e) {
995 fail(e + " was thrown instead of IllegalStateException");
996 }
997 }
Elliott Hughesf33eae72010-05-13 12:36:25 -0700998
The Android Open Source Projectadc854b2009-03-03 19:28:47 -0800999 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001000 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001001 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001002 public void test_unwrap_ByteBuffer$ByteBuffer_05() {
1003 String host = "new host";
1004 int port = 8080;
1005 ByteBuffer bbs = ByteBuffer.allocate(10);
1006 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) };
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001007 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001008 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001009
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001010 try {
1011 SSLEngineResult res = sse.unwrap(bbs, bbd);
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001012 assertEquals(0, res.bytesConsumed());
1013 assertEquals(0, res.bytesProduced());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001014 } catch (Exception ex) {
1015 fail("Unexpected exception: " + ex);
1016 }
1017 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001018
Jorg Pleumann051f4002009-04-07 11:30:34 -07001019 public void test_wrap_ByteBuffer_ByteBuffer_01() throws IOException, InterruptedException {
1020 prepareEngines();
1021 doHandshake();
1022 ByteBuffer bbs = ByteBuffer.allocate(20);
1023 ByteBuffer bbd = ByteBuffer.allocate(20000);
Jesse Wilson5a1dfc82011-01-13 14:01:03 -08001024 clientEngine.engine.wrap(bbs, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001025 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001026
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001027 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001028 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001029 * ReadOnlyBufferException should be thrown.
1030 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001031 public void test_wrap_ByteBuffer_ByteBuffer_02() {
1032 String host = "new host";
1033 int port = 8080;
1034 ByteBuffer bbs = ByteBuffer.allocate(10);
1035 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer();
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001036 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001037 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001038
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001039 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001040 sse.wrap(bbs, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001041 fail("ReadOnlyBufferException wasn't thrown");
1042 } catch (ReadOnlyBufferException iobe) {
1043 //expected
1044 } catch (Exception e) {
1045 fail(e + " was thrown instead of ReadOnlyBufferException");
1046 }
1047 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001048
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001049 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001050 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001051 * IllegalArgumentException should be thrown.
1052 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001053 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001054 public void test_wrap_ByteBuffer_ByteBuffer_03() {
1055 String host = "new host";
1056 int port = 8080;
1057 ByteBuffer bbsN = null;
1058 ByteBuffer bbdN = null;
1059 ByteBuffer bbs = ByteBuffer.allocate(10);
1060 ByteBuffer bbd = ByteBuffer.allocate(100);
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001061 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001062 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001063
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001064 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001065 sse.wrap(bbsN, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001066 fail("IllegalArgumentException wasn't thrown");
1067 } catch (IllegalArgumentException iae) {
1068 //expected
1069 } catch (NullPointerException npe) {
1070 } catch (Exception e) {
1071 fail(e + " was thrown instead of IllegalArgumentException");
1072 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001073
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001074 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001075 sse.wrap(bbs, bbdN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001076 fail("IllegalArgumentException wasn't thrown");
1077 } catch (IllegalArgumentException iae) {
1078 //expected
1079 } catch (NullPointerException npe) {
1080 } catch (Exception e) {
1081 fail(e + " was thrown instead of IllegalArgumentException");
1082 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001083
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001084 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001085 sse.wrap(bbsN, bbdN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001086 fail("IllegalArgumentException wasn't thrown");
1087 } catch (IllegalArgumentException iae) {
1088 //expected
1089 } catch (NullPointerException npe) {
1090 } catch (Exception e) {
1091 fail(e + " was thrown instead of IllegalArgumentException");
1092 }
1093 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001094
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001095 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001096 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001097 * IllegalStateException should be thrown.
1098 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001099 @AndroidOnly("The RI doesn't throw the IllegalStateException.")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001100 public void test_wrap_ByteBuffer_ByteBuffer_04() {
1101 String host = "new host";
1102 int port = 8080;
1103 ByteBuffer bbs = ByteBuffer.allocate(10);
1104 ByteBuffer bbd = ByteBuffer.allocate(10);
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001105 SSLEngine sse = getEngine(host, port);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001106
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001107 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001108 sse.wrap(bbs, bbd);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001109 fail("IllegalStateException wasn't thrown");
1110 } catch (IllegalStateException iobe) {
1111 //expected
1112 } catch (Exception e) {
1113 fail(e + " was thrown instead of IllegalStateException");
1114 }
1115 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001116
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001117 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001118 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001119 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001120 public void test_wrap_ByteBuffer_ByteBuffer_05() {
1121 String host = "new host";
1122 int port = 8080;
1123 ByteBuffer bb = ByteBuffer.allocate(10);
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001124 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001125 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001126
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001127 try {
1128 SSLEngineResult res = sse.wrap(bb, ByteBuffer.allocate(10));
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001129 assertEquals(0, res.bytesConsumed());
1130 assertEquals(0, res.bytesProduced());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001131 } catch (Exception e) {
1132 fail("Unexpected exception: " + e);
1133 }
1134 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001135
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001136 /**
Elliott Hughesf33eae72010-05-13 12:36:25 -07001137 * @throws IOException
1138 * @throws InterruptedException
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001139 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001140 * SSLException should be thrown.
1141 */
Jorg Pleumann051f4002009-04-07 11:30:34 -07001142 public void test_wrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException {
1143 prepareEngines();
1144 doHandshake();
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001145 ByteBuffer bbs = ByteBuffer.allocate(100);
Jorg Pleumann051f4002009-04-07 11:30:34 -07001146 ByteBuffer bbd = ByteBuffer.allocate(20000);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001147
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001148 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001149 clientEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd);
1150 serverEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd);
1151 //fail("SSLException wasn't thrown");
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001152 } catch (SSLException ex) {
1153 //expected
1154 }
1155 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001156
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001157 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001158 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001159 * ReadOnlyBufferException should be thrown.
1160 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001161 public void test_wrap_ByteBuffer$ByteBuffer_02() {
1162 String host = "new host";
1163 int port = 8080;
1164 ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer();
1165 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)};
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001166 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001167 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001168
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001169 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001170 sse.wrap(bbA, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001171 fail("ReadOnlyBufferException wasn't thrown");
1172 } catch (ReadOnlyBufferException iobe) {
1173 //expected
1174 } catch (Exception e) {
1175 fail(e + " was thrown instead of ReadOnlyBufferException");
1176 }
1177 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001178
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001179 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001180 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001181 * IllegalArgumentException should be thrown.
1182 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001183 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001184 public void test_wrap_ByteBuffer$ByteBuffer_03() {
1185 String host = "new host";
1186 int port = 8080;
1187 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)};
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001188 ByteBuffer[] bbAN = null;
1189 ByteBuffer bb = ByteBuffer.allocate(10);
1190 ByteBuffer bN = null;
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001191 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001192 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001193
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001194 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001195 sse.wrap(bbA, bN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001196 fail("IllegalArgumentException wasn't thrown");
1197 } catch (IllegalArgumentException iobe) {
1198 //expected
1199 } catch (NullPointerException npe) {
1200 } catch (Exception e) {
1201 fail(e + " was thrown instead of IllegalArgumentException");
1202 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001203
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001204 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001205 sse.wrap(bbAN, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001206 fail("IllegalArgumentException wasn't thrown");
1207 } catch (IllegalArgumentException iobe) {
1208 //expected
1209 } catch (NullPointerException npe) {
1210 } catch (Exception e) {
1211 fail(e + " was thrown instead of IllegalArgumentException");
1212 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001213
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001214 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001215 sse.wrap(bbAN, bN);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001216 fail("IllegalArgumentException wasn't thrown");
1217 } catch (IllegalArgumentException iobe) {
1218 //expected
1219 } catch (NullPointerException npe) {
1220 } catch (Exception e) {
1221 fail(e + " was thrown instead of IllegalArgumentException");
1222 }
1223 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001224
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001225 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001226 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001227 * IllegalStateException should be thrown.
1228 */
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001229 @AndroidOnly("The RI doesn't throw the IllegalStateException.")
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001230 public void test_wrap_ByteBuffer$ByteBuffer_04() {
1231 String host = "new host";
1232 int port = 8080;
1233 ByteBuffer bb = ByteBuffer.allocate(10);
1234 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) };
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001235 SSLEngine sse = getEngine(host, port);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001236
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001237 try {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001238 sse.wrap(bbA, bb);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001239 fail("IllegalStateException wasn't thrown");
1240 } catch (IllegalStateException iobe) {
1241 //expected
1242 } catch (Exception e) {
1243 fail(e + " was thrown instead of IllegalStateException");
1244 }
1245 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001246
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001247 /**
Brian Carlstrom229e34b2011-03-02 23:34:29 -08001248 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst)
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001249 */
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001250 public void test_wrap_ByteBuffer$ByteBuffer_05() {
1251 String host = "new host";
1252 int port = 8080;
1253 ByteBuffer bb = ByteBuffer.allocate(10);
1254 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) };
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001255 SSLEngine sse = getEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001256 sse.setUseClientMode(true);
Elliott Hughesf33eae72010-05-13 12:36:25 -07001257
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001258 try {
1259 SSLEngineResult res = sse.wrap(bbA, bb);
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001260 assertEquals(0, res.bytesConsumed());
1261 assertEquals(0, res.bytesProduced());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001262 } catch (Exception ex) {
1263 fail("Unexpected exception: " + ex);
1264 }
1265 }
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001266
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001267 private SSLEngine getEngine() {
1268 SSLContext context = null;
1269 try {
1270 context = SSLContext.getInstance("TLS");
1271 context.init(null, null, null);
1272 } catch (KeyManagementException e) {
1273 fail("Could not get SSLEngine: key management exception "
1274 + e.getMessage());
1275 } catch (NoSuchAlgorithmException e) {
1276 fail("Could not get SSLEngine: no such algorithm " + e.getMessage());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001277 }
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001278 return context.createSSLEngine();
1279 }
1280
1281 private SSLEngine getEngine(String host, int port) {
1282 SSLContext context = null;
1283 try {
1284 context = SSLContext.getInstance("TLS");
1285 context.init(null, null, null);
1286 } catch (KeyManagementException e) {
1287 fail("Could not get SSLEngine: key management exception "
1288 + e.getMessage());
1289 } catch (NoSuchAlgorithmException e) {
1290 fail("Could not get SSLEngine: no such algorithm " + e.getMessage());
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001291 }
Urs Grob1cd5a5c2009-03-24 20:48:11 -07001292 return context.createSSLEngine(host, port);
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001293 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001294
Jorg Pleumann051f4002009-04-07 11:30:34 -07001295 class HandshakeHandler implements Runnable {
1296
1297 private final SSLEngine engine;
1298
1299 private final SourceChannel in;
1300
1301 private final SinkChannel out;
1302
1303 private final ByteBuffer EMPTY = ByteBuffer.allocate(0);
1304
1305 @SuppressWarnings("unused")
1306 private final String LOGTAG;
1307
1308 private SSLEngineResult.HandshakeStatus status;
1309
1310 private ByteBuffer readBuffer;
1311
1312 private ByteBuffer writeBuffer;
1313
Urs Grobbe258112009-04-09 20:28:05 -07001314 HandshakeHandler(boolean clientMode, SourceChannel in, SinkChannel out)
1315 throws SSLException {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001316 this.in = in;
1317 this.out = out;
1318 engine = getEngine();
1319 engine.setUseClientMode(clientMode);
1320 String[] cipherSuites = engine.getSupportedCipherSuites();
Urs Grobbe258112009-04-09 20:28:05 -07001321 Set<String> enabledSuites = new HashSet<String>();
Jorg Pleumann051f4002009-04-07 11:30:34 -07001322 for (String cipherSuite : cipherSuites) {
1323 if (cipherSuite.contains("anon")) {
1324 enabledSuites.add(cipherSuite);
1325 }
1326 }
Urs Grobbe258112009-04-09 20:28:05 -07001327 engine.setEnabledCipherSuites((String[]) enabledSuites.toArray(
1328 new String[enabledSuites.size()]));
Jorg Pleumann051f4002009-04-07 11:30:34 -07001329
Urs Grobbe258112009-04-09 20:28:05 -07001330 engine.beginHandshake();
1331 status = engine.getHandshakeStatus();
Jorg Pleumann051f4002009-04-07 11:30:34 -07001332
1333 if (clientMode) {
1334 LOGTAG = "CLIENT: ";
1335 } else {
1336 LOGTAG = "SERVER: ";
1337 }
1338
1339 log("CipherSuites: " + Arrays.toString(engine.getEnabledCipherSuites()));
1340 log(status);
1341
1342 readBuffer = ByteBuffer.allocate(200000);
1343 writeBuffer = ByteBuffer.allocate(20000);
1344 }
1345
1346 public SSLEngineResult.HandshakeStatus getStatus() {
1347 return status;
1348 }
1349
1350 private void log(Object o) {
1351 //System.out.print(LOGTAG);
1352 //System.out.println(o);
1353 }
1354
Jorg Pleumann051f4002009-04-07 11:30:34 -07001355 private ByteBuffer read() throws IOException {
1356 if (readBuffer == null || readBuffer.remaining() == 0 || readBuffer.position() == 0) {
Urs Grobbe258112009-04-09 20:28:05 -07001357 readBuffer.clear();
Jorg Pleumann051f4002009-04-07 11:30:34 -07001358 int read = in.read(readBuffer);
1359 log("read: " + read);
1360 readBuffer.rewind();
1361 readBuffer.limit(read);
1362 }
1363 return readBuffer;
1364 }
1365
1366 public void run() {
Urs Grobbe258112009-04-09 20:28:05 -07001367 try {
1368 while (true) {
Jorg Pleumann051f4002009-04-07 11:30:34 -07001369 switch (status) {
1370 case FINISHED: {
1371 log(status);
1372 return;
1373 }
1374 case NEED_TASK: {
1375 log(status);
Urs Grobbe258112009-04-09 20:28:05 -07001376 Runnable task;
Jorg Pleumann051f4002009-04-07 11:30:34 -07001377 while ((task = engine.getDelegatedTask()) != null) {
1378 task.run();
1379 }
1380 status = engine.getHandshakeStatus();
1381 break;
1382 }
1383 case NEED_UNWRAP: {
1384 log(status);
1385 ByteBuffer source = read();
Urs Grobbe258112009-04-09 20:28:05 -07001386 writeBuffer.clear();
Jorg Pleumann051f4002009-04-07 11:30:34 -07001387
1388 while (status == HandshakeStatus.NEED_UNWRAP) {
1389 SSLEngineResult result = engine.unwrap(source, writeBuffer);
1390 status = result.getHandshakeStatus();
1391 log(result);
1392 }
1393 break;
1394 }
1395 case NEED_WRAP: {
1396 log(status);
Urs Grobbe258112009-04-09 20:28:05 -07001397 writeBuffer.clear();
Jorg Pleumann051f4002009-04-07 11:30:34 -07001398
1399 int produced = 0;
1400 SSLEngineResult result = null;
1401 while (status == HandshakeStatus.NEED_WRAP) {
1402 result = engine.wrap(EMPTY, writeBuffer);
1403 status = result.getHandshakeStatus();
1404 produced += result.bytesProduced();
1405 log(result);
1406 }
1407 writeBuffer.rewind();
1408 writeBuffer.limit(produced);
1409 log("write: " + produced);
1410 out.write(writeBuffer);
1411 break;
1412 }
1413 case NOT_HANDSHAKING: {
1414 log("Not Handshaking");
1415 return;
1416 }
1417 }
Jorg Pleumann051f4002009-04-07 11:30:34 -07001418 }
Urs Grobbe258112009-04-09 20:28:05 -07001419 } catch (IOException e) {
1420 log(e);
1421 } catch (RuntimeException e) {
1422 // ignore;
Jorg Pleumann051f4002009-04-07 11:30:34 -07001423 }
1424 }
1425 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001426
Urs Grobbe258112009-04-09 20:28:05 -07001427 @KnownFailure("Handshake Status is never finished. NPE in "
1428 + "ClientSessionContext$HostAndPort.hashCode() when host is null")
Jorg Pleumann051f4002009-04-07 11:30:34 -07001429 public void testHandshake() throws IOException, InterruptedException {
Urs Grobbe258112009-04-09 20:28:05 -07001430
Jorg Pleumann051f4002009-04-07 11:30:34 -07001431 prepareEngines();
Urs Grobbe258112009-04-09 20:28:05 -07001432
Jorg Pleumann051f4002009-04-07 11:30:34 -07001433 assertTrue("handshake failed", doHandshake());
Elliott Hughesf33eae72010-05-13 12:36:25 -07001434
Jorg Pleumann051f4002009-04-07 11:30:34 -07001435 System.out.println(clientEngine.engine.getSession().getCipherSuite());
Urs Grobbe258112009-04-09 20:28:05 -07001436
1437 assertEquals("Handshake not finished",
1438 SSLEngineResult.HandshakeStatus.FINISHED,
1439 clientEngine.getStatus());
1440 assertEquals("Handshake not finished",
1441 SSLEngineResult.HandshakeStatus.FINISHED,
1442 serverEngine.getStatus());
Jorg Pleumann051f4002009-04-07 11:30:34 -07001443 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001444
Jorg Pleumann051f4002009-04-07 11:30:34 -07001445 void prepareEngines() throws IOException {
1446 Pipe clientSendPipe = Pipe.open();
1447 Pipe serverSendPipe = Pipe.open();
Elliott Hughesf33eae72010-05-13 12:36:25 -07001448
Jorg Pleumann051f4002009-04-07 11:30:34 -07001449 SinkChannel clientSink = clientSendPipe.sink();
1450 SourceChannel serverSource = clientSendPipe.source();
1451 SinkChannel serverSink = serverSendPipe.sink();
1452 SourceChannel clientSource = serverSendPipe.source();
Elliott Hughesf33eae72010-05-13 12:36:25 -07001453
Jorg Pleumann051f4002009-04-07 11:30:34 -07001454 clientEngine = new HandshakeHandler(true, clientSource, clientSink);
1455 serverEngine = new HandshakeHandler(false, serverSource, serverSink);
1456 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001457
Jorg Pleumann051f4002009-04-07 11:30:34 -07001458 boolean doHandshake() throws InterruptedException {
1459 Thread clientThread = new Thread(clientEngine);
1460 clientThread.start();
Elliott Hughesf33eae72010-05-13 12:36:25 -07001461
Jorg Pleumann051f4002009-04-07 11:30:34 -07001462 Thread serverThread = new Thread(serverEngine);
1463 serverThread.start();
Elliott Hughesf33eae72010-05-13 12:36:25 -07001464
Jorg Pleumann051f4002009-04-07 11:30:34 -07001465 int i = 0;
1466 while (clientThread.isAlive() && serverThread.isAlive() && i < 20) {
1467 Thread.sleep(500);
1468 i++;
1469 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001470
Jorg Pleumann051f4002009-04-07 11:30:34 -07001471 if (clientThread.isAlive()) {
1472 clientThread.interrupt();
1473 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001474
Jorg Pleumann051f4002009-04-07 11:30:34 -07001475 if (serverThread.isAlive()) {
1476 serverThread.interrupt();
1477 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001478
Jorg Pleumann051f4002009-04-07 11:30:34 -07001479 return clientEngine.getStatus() == HandshakeStatus.FINISHED && serverEngine.getStatus() == HandshakeStatus.FINISHED;
1480 }
Elliott Hughesf33eae72010-05-13 12:36:25 -07001481
The Android Open Source Projectadc854b2009-03-03 19:28:47 -08001482}