The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | * contributor license agreements. See the NOTICE file distributed with |
| 4 | * this work for additional information regarding copyright ownership. |
| 5 | * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | * (the "License"); you may not use this file except in compliance with |
| 7 | * the License. You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | package tests.api.javax.net.ssl; |
| 19 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 20 | import java.io.IOException; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 21 | import java.nio.ByteBuffer; |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 22 | import java.nio.ReadOnlyBufferException; |
| 23 | import java.nio.channels.Pipe; |
| 24 | import java.nio.channels.Pipe.SinkChannel; |
| 25 | import java.nio.channels.Pipe.SourceChannel; |
| 26 | import java.security.KeyManagementException; |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 27 | import java.security.NoSuchAlgorithmException; |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 28 | import java.util.Arrays; |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 29 | import java.util.HashSet; |
| 30 | import java.util.Set; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 31 | |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 32 | import javax.net.ssl.SSLContext; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 33 | import javax.net.ssl.SSLEngine; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 34 | import javax.net.ssl.SSLEngineResult; |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 35 | import javax.net.ssl.SSLException; |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 36 | import javax.net.ssl.SSLEngineResult.HandshakeStatus; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 37 | |
| 38 | import junit.framework.TestCase; |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 39 | import dalvik.annotation.AndroidOnly; |
| 40 | import dalvik.annotation.KnownFailure; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 41 | |
| 42 | |
| 43 | /** |
| 44 | * Tests for SSLEngine class |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 45 | * |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 46 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 47 | public class SSLEngineTest extends TestCase { |
| 48 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 49 | private HandshakeHandler clientEngine; |
| 50 | private HandshakeHandler serverEngine; |
| 51 | |
Jesse Wilson | d37c804 | 2009-10-23 15:31:41 -0700 | [diff] [blame] | 52 | @Override protected void setUp() throws Exception { |
| 53 | super.setUp(); |
Jesse Wilson | d37c804 | 2009-10-23 15:31:41 -0700 | [diff] [blame] | 54 | } |
| 55 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Test for <code>SSLEngine()</code> constructor Assertion: creates |
| 58 | * SSLEngine object with null host and -1 port |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 59 | * @throws NoSuchAlgorithmException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 60 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 61 | public void test_Constructor() throws NoSuchAlgorithmException { |
| 62 | SSLEngine e = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 63 | assertNull(e.getPeerHost()); |
| 64 | assertEquals(-1, e.getPeerPort()); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 65 | String[] suites = e.getSupportedCipherSuites(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 66 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 72 | * @throws NoSuchAlgorithmException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 73 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 74 | public void test_ConstructorLjava_lang_StringI01() throws NoSuchAlgorithmException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 75 | int port = 1010; |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 76 | SSLEngine e = getEngine(null, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 77 | assertNull(e.getPeerHost()); |
| 78 | assertEquals(e.getPeerPort(), port); |
| 79 | try { |
| 80 | e.beginHandshake(); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 81 | } catch (IllegalStateException ex) { |
| 82 | // expected |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 83 | } catch (SSLException ex) { |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 84 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Test for <code>SSLEngine(String host, int port)</code> constructor |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 104 | * @throws NoSuchAlgorithmException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 105 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 106 | public void test_ConstructorLjava_lang_StringI02() throws NoSuchAlgorithmException { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 107 | String host = "new host"; |
| 108 | int port = 8080; |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 109 | SSLEngine e = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 110 | assertEquals(e.getPeerHost(), host); |
| 111 | assertEquals(e.getPeerPort(), port); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 112 | String[] suites = e.getSupportedCipherSuites(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 113 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 121 | * @throws NoSuchAlgorithmException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 122 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 123 | public void test_getPeerHost() throws NoSuchAlgorithmException { |
| 124 | SSLEngine e = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 125 | assertNull(e.getPeerHost()); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 126 | e = getEngine("www.fortify.net", 80); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 127 | assertEquals("Incorrect host name", "www.fortify.net", e.getPeerHost()); |
| 128 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 129 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 130 | /** |
| 131 | * Test for <code>getPeerPort()</code> method |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 132 | * @throws NoSuchAlgorithmException |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 133 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 134 | public void test_getPeerPort() throws NoSuchAlgorithmException { |
| 135 | SSLEngine e = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 136 | assertEquals("Incorrect default value of peer port", |
| 137 | -1 ,e.getPeerPort()); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 138 | e = getEngine("www.fortify.net", 80); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 139 | assertEquals("Incorrect peer port", 80, e.getPeerPort()); |
| 140 | } |
| 141 | |
| 142 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 143 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 144 | * javax.net.ssl.SSLEngine#getSupportedProtocols() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 145 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 146 | public void test_getSupportedProtocols() throws NoSuchAlgorithmException { |
| 147 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 148 | try { |
| 149 | String[] res = sse.getSupportedProtocols(); |
| 150 | assertNotNull(res); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 151 | assertTrue(res.length > 0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 152 | } catch (Exception ex) { |
| 153 | fail("Unexpected exception " + ex); |
| 154 | } |
| 155 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 156 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 157 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 158 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 159 | * javax.net.ssl.SSLEngine#setEnabledProtocols(String[] protocols) |
| 160 | * javax.net.ssl.SSLEngine#getEnabledProtocols() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 161 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 162 | public void test_EnabledProtocols() throws NoSuchAlgorithmException { |
| 163 | SSLEngine sse = getEngine(); |
| 164 | String[] pr = sse.getSupportedProtocols(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 165 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 170 | assertTrue("Incorrect array was returned", Arrays.equals(res, pr)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 171 | } 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 181 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 182 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 183 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 184 | * javax.net.ssl.SSLEngine#getSupportedCipherSuites() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 185 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 186 | public void test_getSupportedCipherSuites() throws NoSuchAlgorithmException { |
| 187 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 188 | try { |
| 189 | String[] res = sse.getSupportedCipherSuites(); |
| 190 | assertNotNull(res); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 191 | assertTrue(res.length > 0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 192 | } catch (Exception ex) { |
| 193 | fail("Unexpected exception " + ex); |
| 194 | } |
| 195 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 196 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 197 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 198 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 199 | * javax.net.ssl.SSLEngine#setEnabledCipherSuites(String[] suites) |
| 200 | * javax.net.ssl.SSLEngine#getEnabledCipherSuites() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 201 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 202 | public void test_EnabledCipherSuites() throws NoSuchAlgorithmException { |
| 203 | SSLEngine sse = getEngine(); |
| 204 | String[] st = sse.getSupportedCipherSuites(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 205 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 210 | assertTrue("Incorrect array was returned", Arrays.equals(res, st)); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 211 | } 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 221 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 222 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 223 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 224 | * javax.net.ssl.SSLEngine#setEnableSessionCreation(boolean flag) |
| 225 | * javax.net.ssl.SSLEngine#getEnableSessionCreation() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 226 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 227 | public void test_EnableSessionCreation() throws NoSuchAlgorithmException { |
| 228 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 229 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 239 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 240 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 241 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 242 | * javax.net.ssl.SSLEngine#setNeedClientAuth(boolean need) |
| 243 | * javax.net.ssl.SSLEngine#getNeedClientAuth() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 244 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 245 | public void test_NeedClientAuth() throws NoSuchAlgorithmException { |
| 246 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 247 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 256 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 257 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 258 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 259 | * javax.net.ssl.SSLEngine#setWantClientAuth(boolean want) |
| 260 | * javax.net.ssl.SSLEngine#getWantClientAuth() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 261 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 262 | public void test_WantClientAuth() throws NoSuchAlgorithmException { |
| 263 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 264 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 273 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 274 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 275 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 276 | * javax.net.ssl.SSLEngine#beginHandshake() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 277 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 278 | public void test_beginHandshake() throws NoSuchAlgorithmException { |
| 279 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 280 | try { |
| 281 | sse.beginHandshake(); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 282 | fail("IllegalStateException wasn't thrown"); |
| 283 | } catch (IllegalStateException se) { |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 284 | //expected |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 285 | } catch (Exception e) { |
| 286 | fail(e + " was thrown instead of IllegalStateException"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 287 | } |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 288 | sse = getEngine("new host", 1080); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 289 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 297 | sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 298 | try { |
| 299 | sse.setUseClientMode(true); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 300 | sse.beginHandshake(); |
| 301 | } catch (Exception ex) { |
| 302 | fail("Unexpected exception " + ex); |
| 303 | } |
| 304 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 305 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 306 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 307 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 308 | * javax.net.ssl.SSLEngine#setUseClientMode(boolean mode) |
| 309 | * javax.net.ssl.SSLEngine#getUseClientMode() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 310 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 311 | @AndroidOnly("The RI doesn't throw the expected IllegalStateException.") |
| 312 | public void test_UseClientMode() throws NoSuchAlgorithmException { |
| 313 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 314 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 322 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 323 | try { |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 324 | sse = getEngine(null, 1080); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 325 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 337 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 338 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 339 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 340 | * javax.net.ssl.SSLEngine#getSession() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 341 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 342 | public void test_getSession() throws NoSuchAlgorithmException { |
| 343 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 344 | try { |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 345 | assertNotNull(sse.getSession()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 346 | } catch (Exception ex) { |
| 347 | fail("Unexpected exception " + ex); |
| 348 | } |
| 349 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 350 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 351 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 352 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 353 | * javax.net.ssl.SSLEngine#getHandshakeStatus() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 354 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 355 | public void test_getHandshakeStatus() throws NoSuchAlgorithmException { |
| 356 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 357 | try { |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 358 | assertEquals(sse.getHandshakeStatus().toString(), "NOT_HANDSHAKING"); |
| 359 | sse.setUseClientMode(true); |
| 360 | sse.beginHandshake(); |
| 361 | assertEquals(sse.getHandshakeStatus().toString(), "NEED_WRAP"); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 362 | } catch (Exception ex) { |
| 363 | fail("Unexpected exception " + ex); |
| 364 | } |
| 365 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 366 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 367 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 368 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 369 | * javax.net.ssl.SSLEngine#getDelegatedTask() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 370 | */ |
Kenny Root | 70b79a2 | 2013-05-03 14:18:41 -0700 | [diff] [blame] | 371 | @KnownFailure("com.android.org.conscrypt.SSLEngineImpl#getDelegatedTask() throws NPE instead of returning null") |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 372 | public void test_getDelegatedTask() throws NoSuchAlgorithmException { |
| 373 | SSLEngine sse = getEngine(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 374 | try { |
| 375 | assertNull(sse.getDelegatedTask()); |
| 376 | } catch (Exception ex) { |
| 377 | fail("Unexpected exception " + ex); |
| 378 | } |
| 379 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 380 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 381 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 382 | * @throws IOException |
| 383 | * @throws InterruptedException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 384 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 385 | * int offset, int length) |
| 386 | * Exception case: SSLException should be thrown. |
| 387 | */ |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 388 | public void test_unwrap_01() throws IOException, InterruptedException { |
| 389 | prepareEngines(); |
| 390 | doHandshake(); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 391 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 392 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 393 | ByteBuffer bbd = ByteBuffer.allocate(100); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 394 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 395 | clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }, 0, 1); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 396 | fail("SSLException wasn't thrown"); |
| 397 | } catch (SSLException ex) { |
| 398 | //expected |
| 399 | } |
| 400 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 401 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 402 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 403 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 404 | * int offset, int length) |
| 405 | * Exception case: IndexOutOfBoundsException should be thrown. |
| 406 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 407 | @KnownFailure("Fixed in DonutBurger, boundary checks missing") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 408 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 414 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 415 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 416 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 417 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 418 | sse.unwrap(bb, bbA, -1, 3); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 419 | fail("IndexOutOfBoundsException wasn't thrown"); |
| 420 | } catch (IndexOutOfBoundsException iobe) { |
| 421 | //expected |
| 422 | } |
| 423 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 424 | sse.unwrap(bb, bbA, 0, -3); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 425 | fail("IndexOutOfBoundsException wasn't thrown"); |
| 426 | } catch (IndexOutOfBoundsException iobe) { |
| 427 | //expected |
| 428 | } |
| 429 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 430 | sse.unwrap(bb, bbA, bbA.length + 1, bbA.length); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 431 | fail("IndexOutOfBoundsException wasn't thrown"); |
| 432 | } catch (IndexOutOfBoundsException iobe) { |
| 433 | //expected |
| 434 | } |
| 435 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 436 | sse.unwrap(bb, bbA, 0, bbA.length + 1); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 437 | fail("IndexOutOfBoundsException wasn't thrown"); |
| 438 | } catch (IndexOutOfBoundsException iobe) { |
| 439 | //expected |
| 440 | } |
| 441 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 442 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 443 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 444 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 445 | * int offset, int length) |
| 446 | * Exception case: ReadOnlyBufferException should be thrown. |
| 447 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 448 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 449 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 456 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 457 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 458 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 459 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 460 | sse.unwrap(bb, bbA, 0, bbA.length); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 461 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 468 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 469 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 470 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 471 | * int offset, int length) |
| 472 | * Exception case: IllegalArgumentException should be thrown. |
| 473 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 474 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 475 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 483 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 484 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 485 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 486 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 487 | sse.unwrap(bN, bbA, 0, 3); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 488 | 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 Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 496 | sse.unwrap(bb, bbAN, 0, 3); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 497 | 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 Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 505 | sse.unwrap(bb, bbN, 0, 0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 506 | 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 Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 514 | sse.unwrap(bN, bbN, 0, 0); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 515 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 524 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 525 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 526 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 527 | * int offset, int length) |
| 528 | * Exception case: IllegalStateException should be thrown. |
| 529 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 530 | @AndroidOnly("The RI doesn't throw the IllegalStateException.") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 531 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 537 | SSLEngine sse = getEngine(host, port); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 538 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 539 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 540 | sse.unwrap(bb, bbA, 0, bbA.length); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 541 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 548 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 549 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 550 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 551 | * int offset, int length) |
| 552 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 553 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 559 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 560 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 561 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 562 | try { |
| 563 | SSLEngineResult res = sse.unwrap(bb, bbA, 0, bbA.length); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 564 | assertEquals(0, res.bytesConsumed()); |
| 565 | assertEquals(0, res.bytesProduced()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 566 | } catch (Exception ex) { |
| 567 | fail("Unexpected exception: " + ex); |
| 568 | } |
| 569 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 570 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 571 | public void test_wrap_01() throws IOException, InterruptedException { |
| 572 | prepareEngines(); |
| 573 | doHandshake(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 574 | ByteBuffer bbs = ByteBuffer.allocate(100); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 575 | ByteBuffer bbd = ByteBuffer.allocate(20000); |
Jesse Wilson | 5a1dfc8 | 2011-01-13 14:01:03 -0800 | [diff] [blame] | 576 | clientEngine.engine.wrap(new ByteBuffer[] { bbs }, 0, 1, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 577 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 578 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 579 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 580 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 581 | * int length, ByteBuffer dst) |
| 582 | * Exception case: IndexOutOfBoundsException should be thrown. |
| 583 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 584 | @KnownFailure("Fixed in DonutBurger, boundary checks missing") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 585 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 590 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 591 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 592 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 593 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 594 | sse.wrap(bbA, -1, 3, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 595 | fail("IndexOutOfBoundsException wasn't thrown"); |
| 596 | } catch (IndexOutOfBoundsException iobe) { |
| 597 | //expected |
| 598 | } |
| 599 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 600 | sse.wrap(bbA, 0, -3, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 601 | fail("IndexOutOfBoundsException wasn't thrown"); |
| 602 | } catch (IndexOutOfBoundsException iobe) { |
| 603 | //expected |
| 604 | } |
| 605 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 606 | sse.wrap(bbA, bbA.length + 1, bbA.length, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 607 | fail("IndexOutOfBoundsException wasn't thrown"); |
| 608 | } catch (IndexOutOfBoundsException iobe) { |
| 609 | //expected |
| 610 | } |
| 611 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 612 | sse.wrap(bbA, 0, bbA.length + 1, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 613 | fail("IndexOutOfBoundsException wasn't thrown"); |
| 614 | } catch (IndexOutOfBoundsException iobe) { |
| 615 | //expected |
| 616 | } |
| 617 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 618 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 619 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 620 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 621 | * int length, ByteBuffer dst) |
| 622 | * Exception case: ReadOnlyBufferException should be thrown. |
| 623 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 624 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 629 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 630 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 631 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 632 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 633 | sse.wrap(bbA, 0, bbA.length, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 634 | fail("ReadOnlyBufferException wasn't thrown"); |
| 635 | } catch (ReadOnlyBufferException iobe) { |
| 636 | //expected |
| 637 | } |
| 638 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 639 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 640 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 641 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 642 | * int length, ByteBuffer dst) |
| 643 | * Exception case: IllegalArgumentException should be thrown. |
| 644 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 645 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 646 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 650 | ByteBuffer[] bbN = null; |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 651 | ByteBuffer bN = null; |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 652 | SSLEngine e = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 653 | e.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 654 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 655 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 663 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 664 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 673 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 674 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 675 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 676 | * int length, ByteBuffer dst) |
| 677 | * Exception case: IllegalStateException should be thrown. |
| 678 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 679 | @AndroidOnly("The RI doesn't throw the IllegalStateException.") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 680 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 685 | SSLEngine sse = getEngine(host, port); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 686 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 687 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 688 | sse.wrap(bbA, 0, bbA.length, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 689 | fail("IllegalStateException wasn't thrown"); |
| 690 | } catch (IllegalStateException iobe) { |
| 691 | //expected |
| 692 | } |
| 693 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 694 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 695 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 696 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 697 | * int length, ByteBuffer dst) |
| 698 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 699 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 704 | SSLEngine sse = getEngine(host, port); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 705 | sse.setUseClientMode(true); |
| 706 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 707 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 708 | sse.wrap(bbA, 0, bbA.length, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 709 | } catch (Exception ex) { |
| 710 | fail("Unexpected exception: " + ex); |
| 711 | } |
| 712 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 713 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 714 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 715 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 716 | * javax.net.ssl.SSLEngine#closeOutbound() |
| 717 | * javax.net.ssl.SSLEngine#isOutboundDone() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 718 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 719 | public void test_closeOutbound() throws NoSuchAlgorithmException { |
| 720 | SSLEngine sse = getEngine(); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 721 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 722 | try { |
| 723 | assertFalse(sse.isOutboundDone()); |
| 724 | sse.closeOutbound(); |
| 725 | assertTrue(sse.isOutboundDone()); |
| 726 | } catch (Exception ex) { |
| 727 | fail("Unexpected exception: " + ex); |
| 728 | } |
| 729 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 730 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 731 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 732 | * @throws NoSuchAlgorithmException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 733 | * javax.net.ssl.SSLEngine#closeInbound() |
| 734 | * javax.net.ssl.SSLEngine#isInboundDone() |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 735 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 736 | public void test_closeInbound() throws NoSuchAlgorithmException { |
| 737 | SSLEngine sse = getEngine(); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 738 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 739 | try { |
| 740 | assertFalse(sse.isInboundDone()); |
| 741 | sse.closeInbound(); |
| 742 | assertTrue(sse.isInboundDone()); |
| 743 | } catch (Exception ex) { |
| 744 | fail("Unexpected exception: " + ex); |
| 745 | } |
| 746 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 747 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 748 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 749 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 750 | * SSLException should be thrown. |
| 751 | */ |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 752 | public void test_unwrap_ByteBuffer_ByteBuffer_01() throws InterruptedException, IOException { |
| 753 | prepareEngines(); |
| 754 | doHandshake(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 755 | ByteBuffer bbs = ByteBuffer.allocate(100); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 756 | ByteBuffer bbd = ByteBuffer.allocate(100); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 757 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 758 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 759 | SSLEngineResult unwrap = clientEngine.engine.unwrap(bbs, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 760 | fail("SSLException wasn't thrown"); |
| 761 | } catch (SSLException ex) { |
| 762 | //expected |
| 763 | } |
| 764 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 765 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 766 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 767 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 768 | * ReadOnlyBufferException should be thrown. |
| 769 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 770 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 771 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 776 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 777 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 778 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 779 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 780 | sse.unwrap(bbs, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 781 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 788 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 789 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 790 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 791 | * IllegalArgumentException should be thrown. |
| 792 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 793 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 794 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 801 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 802 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 803 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 804 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 805 | sse.unwrap(bbsN, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 806 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 813 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 814 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 815 | sse.unwrap(bbs, bbdN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 816 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 823 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 824 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 825 | sse.unwrap(bbsN, bbdN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 826 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 834 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 835 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 836 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 837 | * IllegalStateException should be thrown. |
| 838 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 839 | @AndroidOnly("The RI doesn't throw the IllegalStateException.") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 840 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 845 | SSLEngine sse = getEngine(host, port); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 846 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 847 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 848 | sse.unwrap(bbs, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 849 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 856 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 857 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 858 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 859 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 860 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 865 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 866 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 867 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 868 | try { |
| 869 | SSLEngineResult res = sse.unwrap(bbs, bbd); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 870 | assertEquals(0, res.bytesConsumed()); |
| 871 | assertEquals(0, res.bytesProduced()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 872 | } catch (Exception e) { |
| 873 | fail("Unexpected exception: " + e); |
| 874 | } |
| 875 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 876 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 877 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 878 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 879 | * SSLException should be thrown. |
| 880 | */ |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 881 | public void test_unwrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException { |
| 882 | prepareEngines(); |
| 883 | doHandshake(); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 884 | |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 885 | ByteBuffer bbs = ByteBuffer.allocate(100); |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 886 | ByteBuffer bbd = ByteBuffer.allocate(100); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 887 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 888 | try { |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 889 | clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 890 | fail("SSLException wasn't thrown"); |
| 891 | } catch (SSLException ex) { |
| 892 | //expected |
| 893 | } |
| 894 | } |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 895 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 896 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 897 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 898 | * ReadOnlyBufferException should be thrown. |
| 899 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 900 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 901 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 907 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 908 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 909 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 910 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 911 | sse.unwrap(bbs, bbA); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 912 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 919 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 920 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 921 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 922 | * IllegalArgumentException should be thrown. |
| 923 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 924 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 925 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 933 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 934 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 935 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 936 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 937 | sse.unwrap(bN, bbA); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 938 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 945 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 946 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 947 | sse.unwrap(bb, bbAN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 948 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 955 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 956 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 957 | sse.unwrap(bb, bbN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 958 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 965 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 966 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 967 | sse.unwrap(bN, bbAN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 968 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 976 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 977 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 978 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 979 | * IllegalStateException should be thrown. |
| 980 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 981 | @AndroidOnly("The RI doesn't throw the IllegalStateException.") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 982 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 987 | SSLEngine sse = getEngine(host, port); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 988 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 989 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 990 | sse.unwrap(bbs, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 991 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 998 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 999 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1000 | * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1001 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1002 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1007 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1008 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1009 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1010 | try { |
| 1011 | SSLEngineResult res = sse.unwrap(bbs, bbd); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1012 | assertEquals(0, res.bytesConsumed()); |
| 1013 | assertEquals(0, res.bytesProduced()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1014 | } catch (Exception ex) { |
| 1015 | fail("Unexpected exception: " + ex); |
| 1016 | } |
| 1017 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1018 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1019 | 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 Wilson | 5a1dfc8 | 2011-01-13 14:01:03 -0800 | [diff] [blame] | 1024 | clientEngine.engine.wrap(bbs, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1025 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1026 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1027 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1028 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1029 | * ReadOnlyBufferException should be thrown. |
| 1030 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1031 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1036 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1037 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1038 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1039 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1040 | sse.wrap(bbs, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1041 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1048 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1049 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1050 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1051 | * IllegalArgumentException should be thrown. |
| 1052 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1053 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1054 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1061 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1062 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1063 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1064 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1065 | sse.wrap(bbsN, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1066 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1073 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1074 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1075 | sse.wrap(bbs, bbdN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1076 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1083 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1084 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1085 | sse.wrap(bbsN, bbdN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1086 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1094 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1095 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1096 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1097 | * IllegalStateException should be thrown. |
| 1098 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1099 | @AndroidOnly("The RI doesn't throw the IllegalStateException.") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1100 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1105 | SSLEngine sse = getEngine(host, port); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1106 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1107 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1108 | sse.wrap(bbs, bbd); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1109 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1116 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1117 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1118 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1119 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1120 | public void test_wrap_ByteBuffer_ByteBuffer_05() { |
| 1121 | String host = "new host"; |
| 1122 | int port = 8080; |
| 1123 | ByteBuffer bb = ByteBuffer.allocate(10); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1124 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1125 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1126 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1127 | try { |
| 1128 | SSLEngineResult res = sse.wrap(bb, ByteBuffer.allocate(10)); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1129 | assertEquals(0, res.bytesConsumed()); |
| 1130 | assertEquals(0, res.bytesProduced()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1131 | } catch (Exception e) { |
| 1132 | fail("Unexpected exception: " + e); |
| 1133 | } |
| 1134 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1135 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1136 | /** |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1137 | * @throws IOException |
| 1138 | * @throws InterruptedException |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1139 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1140 | * SSLException should be thrown. |
| 1141 | */ |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1142 | public void test_wrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException { |
| 1143 | prepareEngines(); |
| 1144 | doHandshake(); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1145 | ByteBuffer bbs = ByteBuffer.allocate(100); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1146 | ByteBuffer bbd = ByteBuffer.allocate(20000); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1147 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1148 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1149 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1152 | } catch (SSLException ex) { |
| 1153 | //expected |
| 1154 | } |
| 1155 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1156 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1157 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1158 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1159 | * ReadOnlyBufferException should be thrown. |
| 1160 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1161 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1166 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1167 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1168 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1169 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1170 | sse.wrap(bbA, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1171 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1178 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1179 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1180 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1181 | * IllegalArgumentException should be thrown. |
| 1182 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1183 | @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1184 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1188 | ByteBuffer[] bbAN = null; |
| 1189 | ByteBuffer bb = ByteBuffer.allocate(10); |
| 1190 | ByteBuffer bN = null; |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1191 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1192 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1193 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1194 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1195 | sse.wrap(bbA, bN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1196 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1203 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1204 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1205 | sse.wrap(bbAN, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1206 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1213 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1214 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1215 | sse.wrap(bbAN, bN); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1216 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1224 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1225 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1226 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1227 | * IllegalStateException should be thrown. |
| 1228 | */ |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1229 | @AndroidOnly("The RI doesn't throw the IllegalStateException.") |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1230 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1235 | SSLEngine sse = getEngine(host, port); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1236 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1237 | try { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1238 | sse.wrap(bbA, bb); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1239 | 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 Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1246 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1247 | /** |
Brian Carlstrom | 229e34b | 2011-03-02 23:34:29 -0800 | [diff] [blame] | 1248 | * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1249 | */ |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1250 | 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 Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1255 | SSLEngine sse = getEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1256 | sse.setUseClientMode(true); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1257 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1258 | try { |
| 1259 | SSLEngineResult res = sse.wrap(bbA, bb); |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1260 | assertEquals(0, res.bytesConsumed()); |
| 1261 | assertEquals(0, res.bytesProduced()); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1262 | } catch (Exception ex) { |
| 1263 | fail("Unexpected exception: " + ex); |
| 1264 | } |
| 1265 | } |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1266 | |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1267 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1277 | } |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1278 | 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 Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1291 | } |
Urs Grob | 1cd5a5c | 2009-03-24 20:48:11 -0700 | [diff] [blame] | 1292 | return context.createSSLEngine(host, port); |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1293 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1294 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1295 | 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 Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1314 | HandshakeHandler(boolean clientMode, SourceChannel in, SinkChannel out) |
| 1315 | throws SSLException { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1316 | this.in = in; |
| 1317 | this.out = out; |
| 1318 | engine = getEngine(); |
| 1319 | engine.setUseClientMode(clientMode); |
| 1320 | String[] cipherSuites = engine.getSupportedCipherSuites(); |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1321 | Set<String> enabledSuites = new HashSet<String>(); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1322 | for (String cipherSuite : cipherSuites) { |
| 1323 | if (cipherSuite.contains("anon")) { |
| 1324 | enabledSuites.add(cipherSuite); |
| 1325 | } |
| 1326 | } |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1327 | engine.setEnabledCipherSuites((String[]) enabledSuites.toArray( |
| 1328 | new String[enabledSuites.size()])); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1329 | |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1330 | engine.beginHandshake(); |
| 1331 | status = engine.getHandshakeStatus(); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1332 | |
| 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 Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1355 | private ByteBuffer read() throws IOException { |
| 1356 | if (readBuffer == null || readBuffer.remaining() == 0 || readBuffer.position() == 0) { |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1357 | readBuffer.clear(); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1358 | 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 Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1367 | try { |
| 1368 | while (true) { |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1369 | switch (status) { |
| 1370 | case FINISHED: { |
| 1371 | log(status); |
| 1372 | return; |
| 1373 | } |
| 1374 | case NEED_TASK: { |
| 1375 | log(status); |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1376 | Runnable task; |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1377 | 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 Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1386 | writeBuffer.clear(); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1387 | |
| 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 Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1397 | writeBuffer.clear(); |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1398 | |
| 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 Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1418 | } |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1419 | } catch (IOException e) { |
| 1420 | log(e); |
| 1421 | } catch (RuntimeException e) { |
| 1422 | // ignore; |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1426 | |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1427 | @KnownFailure("Handshake Status is never finished. NPE in " |
| 1428 | + "ClientSessionContext$HostAndPort.hashCode() when host is null") |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1429 | public void testHandshake() throws IOException, InterruptedException { |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1430 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1431 | prepareEngines(); |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1432 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1433 | assertTrue("handshake failed", doHandshake()); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1434 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1435 | System.out.println(clientEngine.engine.getSession().getCipherSuite()); |
Urs Grob | be25811 | 2009-04-09 20:28:05 -0700 | [diff] [blame] | 1436 | |
| 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 Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1443 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1444 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1445 | void prepareEngines() throws IOException { |
| 1446 | Pipe clientSendPipe = Pipe.open(); |
| 1447 | Pipe serverSendPipe = Pipe.open(); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1448 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1449 | SinkChannel clientSink = clientSendPipe.sink(); |
| 1450 | SourceChannel serverSource = clientSendPipe.source(); |
| 1451 | SinkChannel serverSink = serverSendPipe.sink(); |
| 1452 | SourceChannel clientSource = serverSendPipe.source(); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1453 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1454 | clientEngine = new HandshakeHandler(true, clientSource, clientSink); |
| 1455 | serverEngine = new HandshakeHandler(false, serverSource, serverSink); |
| 1456 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1457 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1458 | boolean doHandshake() throws InterruptedException { |
| 1459 | Thread clientThread = new Thread(clientEngine); |
| 1460 | clientThread.start(); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1461 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1462 | Thread serverThread = new Thread(serverEngine); |
| 1463 | serverThread.start(); |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1464 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1465 | int i = 0; |
| 1466 | while (clientThread.isAlive() && serverThread.isAlive() && i < 20) { |
| 1467 | Thread.sleep(500); |
| 1468 | i++; |
| 1469 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1470 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1471 | if (clientThread.isAlive()) { |
| 1472 | clientThread.interrupt(); |
| 1473 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1474 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1475 | if (serverThread.isAlive()) { |
| 1476 | serverThread.interrupt(); |
| 1477 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1478 | |
Jorg Pleumann | 051f400 | 2009-04-07 11:30:34 -0700 | [diff] [blame] | 1479 | return clientEngine.getStatus() == HandshakeStatus.FINISHED && serverEngine.getStatus() == HandshakeStatus.FINISHED; |
| 1480 | } |
Elliott Hughes | f33eae7 | 2010-05-13 12:36:25 -0700 | [diff] [blame] | 1481 | |
The Android Open Source Project | adc854b | 2009-03-03 19:28:47 -0800 | [diff] [blame] | 1482 | } |