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 org.apache.harmony.tests.javax.net.ssl; 19 20 import java.io.IOException; 21 import java.nio.ByteBuffer; 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; 27 import java.security.NoSuchAlgorithmException; 28 import java.util.Arrays; 29 import java.util.HashSet; 30 import java.util.Set; 31 import javax.net.ssl.SSLContext; 32 import javax.net.ssl.SSLEngine; 33 import javax.net.ssl.SSLEngineResult; 34 import javax.net.ssl.SSLEngineResult.Status; 35 import javax.net.ssl.SSLException; 36 import javax.net.ssl.SSLEngineResult.HandshakeStatus; 37 import junit.framework.TestCase; 38 import libcore.java.security.StandardNames; 39 import libcore.javax.net.ssl.TestSSLContext; 40 41 /** 42 * Tests for SSLEngine class 43 * 44 */ 45 public class SSLEngineTest extends TestCase { 46 private static final int MAX_TLS_RECORD_SIZE = 32768; 47 48 private HandshakeHandler clientEngine; 49 private HandshakeHandler serverEngine; 50 51 @Override protected void setUp() throws Exception { 52 super.setUp(); 53 } 54 55 /** 56 * Test for <code>SSLEngine()</code> constructor Assertion: creates 57 * SSLEngine object with null host and -1 port 58 */ 59 public void test_Constructor() throws Exception { 60 SSLEngine e = getEngine(); 61 assertNull(e.getPeerHost()); 62 assertEquals(-1, e.getPeerPort()); 63 String[] suites = e.getSupportedCipherSuites(); 64 e.setEnabledCipherSuites(suites); 65 assertEquals(e.getEnabledCipherSuites().length, suites.length); 66 } 67 68 /** 69 * Test for <code>SSLEngine(String host, int port)</code> constructor 70 */ 71 public void test_ConstructorLjava_lang_StringI01() throws Exception { 72 int port = 1010; 73 SSLEngine e = getEngine(null, port); 74 assertNull(e.getPeerHost()); 75 assertEquals(e.getPeerPort(), port); 76 try { 77 e.beginHandshake(); 78 fail("should throw IllegalStateException"); 79 } catch (IllegalStateException expected) { 80 } 81 82 e = getEngine(null, port); 83 e.setUseClientMode(true); 84 e.beginHandshake(); 85 86 e = getEngine(null, port); 87 e.setUseClientMode(false); 88 e.beginHandshake(); 89 } 90 91 /** 92 * Test for <code>SSLEngine(String host, int port)</code> constructor 93 */ 94 public void test_ConstructorLjava_lang_StringI02() throws Exception { 95 String host = "new host"; 96 int port = 8080; 97 SSLEngine e = getEngine(host, port); 98 assertEquals(e.getPeerHost(), host); 99 assertEquals(e.getPeerPort(), port); 100 String[] suites = e.getSupportedCipherSuites(); 101 e.setEnabledCipherSuites(suites); 102 assertEquals(e.getEnabledCipherSuites().length, suites.length); 103 e.setUseClientMode(true); 104 assertTrue(e.getUseClientMode()); 105 } 106 107 /** 108 * Test for <code>getPeerHost()</code> method 109 */ 110 public void test_getPeerHost() throws Exception { 111 SSLEngine e = getEngine(); 112 assertNull(e.getPeerHost()); 113 e = getEngine("www.fortify.net", 80); 114 assertEquals("Incorrect host name", "www.fortify.net", e.getPeerHost()); 115 } 116 117 /** 118 * Test for <code>getPeerPort()</code> method 119 */ 120 public void test_getPeerPort() throws Exception { 121 SSLEngine e = getEngine(); 122 assertEquals("Incorrect default value of peer port", 123 -1 ,e.getPeerPort()); 124 e = getEngine("www.fortify.net", 80); 125 assertEquals("Incorrect peer port", 80, e.getPeerPort()); 126 } 127 128 public void test_getSupportedProtocols() throws Exception { 129 SSLEngine sse = getEngine(); 130 131 String[] res = sse.getSupportedProtocols(); 132 assertNotNull(res); 133 assertTrue(res.length > 0); 134 } 135 136 /** 137 * javax.net.ssl.SSLEngine#setEnabledProtocols(String[] protocols) 138 * javax.net.ssl.SSLEngine#getEnabledProtocols() 139 */ 140 public void test_EnabledProtocols() throws Exception { 141 SSLEngine sse = getEngine(); 142 String[] pr = sse.getSupportedProtocols(); 143 144 sse.setEnabledProtocols(pr); 145 String[] res = sse.getEnabledProtocols(); 146 assertNotNull("Null array was returned", res); 147 assertEquals("Incorrect array length", res.length, pr.length); 148 assertTrue("Incorrect array was returned", Arrays.equals(res, pr)); 149 150 try { 151 sse.setEnabledProtocols(null); 152 fail("IllegalArgumentException wasn't thrown"); 153 } catch (IllegalArgumentException expected) { 154 } 155 } 156 157 /** 158 * javax.net.ssl.SSLEngine#getSupportedCipherSuites() 159 */ 160 public void test_getSupportedCipherSuites() throws Exception { 161 SSLEngine sse = getEngine(); 162 163 String[] res = sse.getSupportedCipherSuites(); 164 assertNotNull(res); 165 assertTrue(res.length > 0); 166 } 167 168 /** 169 * javax.net.ssl.SSLEngine#setEnabledCipherSuites(String[] suites) 170 * javax.net.ssl.SSLEngine#getEnabledCipherSuites() 171 */ 172 public void test_EnabledCipherSuites() throws Exception { 173 SSLEngine sse = getEngine(); 174 String[] st = sse.getSupportedCipherSuites(); 175 176 sse.setEnabledCipherSuites(st); 177 String[] res = sse.getEnabledCipherSuites(); 178 assertNotNull("Null array was returned", res); 179 assertEquals("Incorrect array length", res.length, st.length); 180 assertTrue("Incorrect array was returned", Arrays.equals(res, st)); 181 182 try { 183 sse.setEnabledCipherSuites(null); 184 fail("IllegalArgumentException wasn't thrown"); 185 } catch (IllegalArgumentException expected) { 186 } 187 } 188 189 /** 190 * javax.net.ssl.SSLEngine#setEnableSessionCreation(boolean flag) 191 * javax.net.ssl.SSLEngine#getEnableSessionCreation() 192 */ 193 public void test_EnableSessionCreation() throws Exception { 194 SSLEngine sse = getEngine(); 195 try { 196 assertTrue(sse.getEnableSessionCreation()); 197 sse.setEnableSessionCreation(false); 198 assertFalse(sse.getEnableSessionCreation()); 199 sse.setEnableSessionCreation(true); 200 assertTrue(sse.getEnableSessionCreation()); 201 } catch (Exception ex) { 202 fail("Unexpected exception " + ex); 203 } 204 } 205 206 /** 207 * javax.net.ssl.SSLEngine#setNeedClientAuth(boolean need) 208 * javax.net.ssl.SSLEngine#getNeedClientAuth() 209 */ 210 public void test_NeedClientAuth() throws Exception { 211 SSLEngine sse = getEngine(); 212 try { 213 sse.setNeedClientAuth(false); 214 assertFalse(sse.getNeedClientAuth()); 215 sse.setNeedClientAuth(true); 216 assertTrue(sse.getNeedClientAuth()); 217 } catch (Exception ex) { 218 fail("Unexpected exception " + ex); 219 } 220 } 221 222 /** 223 * javax.net.ssl.SSLEngine#setWantClientAuth(boolean want) 224 * javax.net.ssl.SSLEngine#getWantClientAuth() 225 */ 226 public void test_WantClientAuth() throws Exception { 227 SSLEngine sse = getEngine(); 228 sse.setWantClientAuth(false); 229 assertFalse(sse.getWantClientAuth()); 230 sse.setWantClientAuth(true); 231 assertTrue(sse.getWantClientAuth()); 232 } 233 234 /** 235 * javax.net.ssl.SSLEngine#beginHandshake() 236 */ 237 public void test_beginHandshake() throws Exception { 238 SSLEngine sse = getEngine(); 239 try { 240 sse.beginHandshake(); 241 fail("IllegalStateException wasn't thrown"); 242 } catch (IllegalStateException expected) { 243 } 244 sse = getEngine("new host", 1080); 245 try { 246 sse.beginHandshake(); 247 fail("IllegalStateException wasn't thrown"); 248 } catch (IllegalStateException expected) { 249 } 250 sse = getEngine(); 251 sse.setUseClientMode(true); 252 sse.beginHandshake(); 253 } 254 255 /** 256 * javax.net.ssl.SSLEngine#setUseClientMode(boolean mode) 257 * javax.net.ssl.SSLEngine#getUseClientMode() 258 */ 259 public void test_UseClientMode() throws Exception { 260 SSLEngine sse = getEngine(); 261 sse.setUseClientMode(false); 262 assertFalse(sse.getUseClientMode()); 263 sse.setUseClientMode(true); 264 assertTrue(sse.getUseClientMode()); 265 266 sse = getEngine(null, 1080); 267 sse.setUseClientMode(true); 268 sse.beginHandshake(); 269 try { 270 sse.setUseClientMode(false); 271 fail("IllegalArgumentException was not thrown"); 272 } catch (IllegalArgumentException expected) { 273 } 274 } 275 276 /** 277 * javax.net.ssl.SSLEngine#getSession() 278 */ 279 public void test_getSession() throws Exception { 280 SSLEngine sse = getEngine(); 281 assertNotNull(sse.getSession()); 282 } 283 284 /** 285 * javax.net.ssl.SSLEngine#getHandshakeStatus() 286 */ 287 public void test_getHandshakeStatus() throws Exception { 288 SSLEngine sse = getEngine(); 289 assertEquals(sse.getHandshakeStatus().toString(), "NOT_HANDSHAKING"); 290 sse.setUseClientMode(true); 291 sse.beginHandshake(); 292 assertEquals(sse.getHandshakeStatus().toString(), "NEED_WRAP"); 293 } 294 295 /** 296 * javax.net.ssl.SSLEngine#getDelegatedTask() 297 */ 298 public void test_getDelegatedTask() throws Exception { 299 SSLEngine sse = getEngine(); 300 assertNull(sse.getDelegatedTask()); 301 } 302 303 /** 304 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 305 * int offset, int length) 306 * Exception case: SSLException should be thrown. 307 */ 308 public void test_unwrap_01() throws Exception { 309 prepareEngines(); 310 doHandshake(); 311 312 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}); 313 ByteBuffer bbd = ByteBuffer.allocate(100); 314 try { 315 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }, 0, 1); 316 fail("SSLException wasn't thrown"); 317 } catch (SSLException expected) { 318 } 319 } 320 321 /** 322 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 323 * int offset, int length) 324 * Exception case: IndexOutOfBoundsException should be thrown. 325 */ 326 public void test_unwrap_02() throws Exception { 327 String host = "new host"; 328 int port = 8080; 329 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 330 331 ByteBuffer bb = ByteBuffer.allocate(10); 332 SSLEngine sse = getEngine(host, port); 333 sse.setUseClientMode(true); 334 335 try { 336 sse.unwrap(bb, bbA, -1, 3); 337 fail("IndexOutOfBoundsException wasn't thrown"); 338 } catch (IndexOutOfBoundsException expected) { 339 } 340 try { 341 sse.unwrap(bb, bbA, 0, -3); 342 fail("IndexOutOfBoundsException wasn't thrown"); 343 } catch (IndexOutOfBoundsException expected) { 344 } 345 try { 346 sse.unwrap(bb, bbA, bbA.length + 1, bbA.length); 347 fail("IndexOutOfBoundsException wasn't thrown"); 348 } catch (IndexOutOfBoundsException expected) { 349 } 350 try { 351 sse.unwrap(bb, bbA, 0, bbA.length + 1); 352 fail("IndexOutOfBoundsException wasn't thrown"); 353 } catch (IndexOutOfBoundsException expected) { 354 } 355 } 356 357 /** 358 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 359 * int offset, int length) 360 * Exception case: ReadOnlyBufferException should be thrown. 361 */ 362 public void test_unwrap_03() throws Exception { 363 String host = "new host"; 364 int port = 8080; 365 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer(); 366 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 367 368 ByteBuffer bb = ByteBuffer.allocate(10); 369 SSLEngine sse = getEngine(host, port); 370 sse.setUseClientMode(true); 371 372 try { 373 sse.unwrap(bb, bbA, 0, bbA.length); 374 fail("ReadOnlyBufferException wasn't thrown"); 375 } catch (ReadOnlyBufferException expected) { 376 } 377 } 378 379 /** 380 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 381 * int offset, int length) 382 * Exception case: IllegalArgumentException should be thrown. 383 */ 384 public void test_unwrap_04() throws Exception { 385 String host = "new host"; 386 int port = 8080; 387 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 388 ByteBuffer[] bbAN = {ByteBuffer.allocate(100), null, ByteBuffer.allocate(100)}; 389 ByteBuffer[] bbN = null; 390 ByteBuffer bb = ByteBuffer.allocate(10); 391 ByteBuffer bN = null; 392 SSLEngine sse = getEngine(host, port); 393 sse.setUseClientMode(true); 394 395 try { 396 sse.unwrap(bN, bbA, 0, 3); 397 fail("IllegalArgumentException wasn't thrown"); 398 } catch (IllegalArgumentException expected) { 399 } 400 try { 401 sse.unwrap(bb, bbAN, 0, 3); 402 fail("IllegalArgumentException wasn't thrown"); 403 } catch (IllegalArgumentException expected) { 404 } 405 try { 406 sse.unwrap(bb, bbN, 0, 0); 407 fail("IllegalArgumentException wasn't thrown"); 408 } catch (IllegalArgumentException expected) { 409 } 410 try { 411 sse.unwrap(bN, bbN, 0, 0); 412 fail("IllegalArgumentException wasn't thrown"); 413 } catch (IllegalArgumentException expected) { 414 } 415 } 416 417 /** 418 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 419 * int offset, int length) 420 * Exception case: IllegalStateException should be thrown. 421 */ 422 public void test_unwrap_05() throws Exception { 423 String host = "new host"; 424 int port = 8080; 425 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 426 427 ByteBuffer bb = ByteBuffer.allocate(10); 428 SSLEngine sse = getEngine(host, port); 429 430 try { 431 SSLEngineResult result = sse.unwrap(bb, bbA, 0, bbA.length); 432 fail("IllegalStateException wasn't thrown"); 433 } catch (IllegalStateException expected) { 434 } 435 } 436 437 /** 438 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 439 * int offset, int length) 440 */ 441 public void test_unwrap_06() throws Exception { 442 String host = "new host"; 443 int port = 8080; 444 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 445 446 ByteBuffer bb = ByteBuffer.allocate(10); 447 SSLEngine sse = getEngine(host, port); 448 sse.setUseClientMode(true); 449 450 SSLEngineResult res = sse.unwrap(bb, bbA, 0, bbA.length); 451 assertEquals(0, res.bytesConsumed()); 452 assertEquals(0, res.bytesProduced()); 453 } 454 455 public void test_wrap_01() throws Exception { 456 prepareEngines(); 457 doHandshake(); 458 ByteBuffer bbs = ByteBuffer.allocate(100); 459 ByteBuffer bbd = ByteBuffer.allocate(MAX_TLS_RECORD_SIZE); 460 clientEngine.engine.wrap(new ByteBuffer[] { bbs }, 0, 1, bbd); 461 } 462 463 /** 464 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 465 * int length, ByteBuffer dst) 466 * Exception case: IndexOutOfBoundsException should be thrown. 467 */ 468 public void test_wrap_02() throws Exception { 469 String host = "new host"; 470 int port = 8080; 471 ByteBuffer bb = ByteBuffer.allocate(10); 472 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 473 SSLEngine sse = getEngine(host, port); 474 sse.setUseClientMode(true); 475 476 try { 477 sse.wrap(bbA, -1, 3, bb); 478 fail("IndexOutOfBoundsException wasn't thrown"); 479 } catch (IndexOutOfBoundsException expected) { 480 } 481 try { 482 sse.wrap(bbA, 0, -3, bb); 483 fail("IndexOutOfBoundsException wasn't thrown"); 484 } catch (IndexOutOfBoundsException expected) { 485 } 486 try { 487 sse.wrap(bbA, bbA.length + 1, bbA.length, bb); 488 fail("IndexOutOfBoundsException wasn't thrown"); 489 } catch (IndexOutOfBoundsException expected) { 490 } 491 try { 492 sse.wrap(bbA, 0, bbA.length + 1, bb); 493 fail("IndexOutOfBoundsException wasn't thrown"); 494 } catch (IndexOutOfBoundsException expected) { 495 } 496 } 497 498 /** 499 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 500 * int length, ByteBuffer dst) 501 * Exception case: ReadOnlyBufferException should be thrown. 502 */ 503 public void test_wrap_03() throws Exception { 504 String host = "new host"; 505 int port = 8080; 506 ByteBuffer bb = ByteBuffer.allocate(MAX_TLS_RECORD_SIZE).asReadOnlyBuffer(); 507 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 508 SSLEngine sse = getEngine(host, port); 509 sse.setUseClientMode(true); 510 511 try { 512 sse.wrap(bbA, 0, bbA.length, bb); 513 fail("ReadOnlyBufferException wasn't thrown"); 514 } catch (ReadOnlyBufferException expected) { 515 } 516 } 517 518 /** 519 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 520 * int length, ByteBuffer dst) 521 * Exception case: IllegalArgumentException should be thrown. 522 */ 523 public void test_wrap_04() throws Exception { 524 String host = "new host"; 525 int port = 8080; 526 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 527 ByteBuffer[] bbN = null; 528 ByteBuffer bN = null; 529 SSLEngine e = getEngine(host, port); 530 e.setUseClientMode(true); 531 532 try { 533 e.wrap(bbA, 0, 3, bN); 534 fail("IllegalArgumentException must be thrown for null srcs byte buffer array"); 535 } catch (IllegalArgumentException ex) { 536 } 537 538 try { 539 e.wrap(bbN, 0, 0, bN); 540 fail("IllegalArgumentException wasn't thrown"); 541 } catch (IllegalArgumentException ex) { 542 } 543 } 544 545 /** 546 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 547 * int length, ByteBuffer dst) 548 * Exception case: IllegalStateException should be thrown. 549 */ 550 public void test_wrap_05() throws Exception { 551 String host = "new host"; 552 int port = 8080; 553 ByteBuffer bb = ByteBuffer.allocate(MAX_TLS_RECORD_SIZE); 554 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 555 SSLEngine sse = getEngine(host, port); 556 557 try { 558 SSLEngineResult result = sse.wrap(bbA, 0, bbA.length, bb); 559 fail("Should fail since mode not set yet"); 560 } catch (IllegalStateException expected) { 561 } 562 } 563 564 /** 565 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 566 * int length, ByteBuffer dst) 567 */ 568 public void test_wrap_06() throws Exception { 569 String host = "new host"; 570 int port = 8080; 571 ByteBuffer bb = ByteBuffer.allocate(MAX_TLS_RECORD_SIZE); 572 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 573 SSLEngine sse = getEngine(host, port); 574 sse.setUseClientMode(true); 575 576 SSLEngineResult result = sse.wrap(bbA, 0, bbA.length, bb); 577 assertEquals(SSLEngineResult.Status.OK, result.getStatus()); 578 assertEquals(0, result.bytesConsumed()); 579 assertTrue(result.bytesProduced() > 0); 580 } 581 582 /** 583 * javax.net.ssl.SSLEngine#closeOutbound() 584 * javax.net.ssl.SSLEngine#isOutboundDone() 585 */ 586 public void test_closeOutbound() throws Exception { 587 SSLEngine sse = getEngine(); 588 589 assertFalse(sse.isOutboundDone()); 590 sse.closeOutbound(); 591 assertTrue(sse.isOutboundDone()); 592 } 593 594 /** 595 * javax.net.ssl.SSLEngine#closeInbound() 596 * javax.net.ssl.SSLEngine#isInboundDone() 597 */ 598 public void test_closeInbound() throws Exception { 599 SSLEngine sse = getEngine(); 600 601 assertFalse(sse.isInboundDone()); 602 sse.closeInbound(); 603 assertTrue(sse.isInboundDone()); 604 } 605 606 /** 607 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 608 * SSLException should be thrown. 609 */ 610 public void test_unwrap_ByteBuffer_ByteBuffer_01() throws Exception { 611 prepareEngines(); 612 doHandshake(); 613 ByteBuffer bbs = ByteBuffer.allocate(100); 614 ByteBuffer bbd = ByteBuffer.allocate(100); 615 616 try { 617 SSLEngineResult unwrap = clientEngine.engine.unwrap(bbs, bbd); 618 fail("SSLException wasn't thrown"); 619 } catch (SSLException ex) { 620 //expected 621 } 622 } 623 624 /** 625 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 626 * ReadOnlyBufferException should be thrown. 627 */ 628 public void test_unwrap_ByteBuffer_ByteBuffer_02() throws Exception { 629 String host = "new host"; 630 int port = 8080; 631 ByteBuffer bbs = ByteBuffer.allocate(10); 632 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer(); 633 SSLEngine sse = getEngine(host, port); 634 sse.setUseClientMode(true); 635 636 try { 637 sse.unwrap(bbs, bbd); 638 fail("ReadOnlyBufferException wasn't thrown"); 639 } catch (ReadOnlyBufferException iobe) { 640 //expected 641 } catch (Exception e) { 642 fail(e + " was thrown instead of ReadOnlyBufferException"); 643 } 644 } 645 646 /** 647 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 648 * IllegalArgumentException should be thrown. 649 */ 650 public void test_unwrap_ByteBuffer_ByteBuffer_03() throws Exception { 651 String host = "new host"; 652 int port = 8080; 653 ByteBuffer bbsN = null; 654 ByteBuffer bbdN = null; 655 ByteBuffer bbs = ByteBuffer.allocate(10); 656 ByteBuffer bbd = ByteBuffer.allocate(100); 657 SSLEngine sse = getEngine(host, port); 658 sse.setUseClientMode(true); 659 660 try { 661 sse.unwrap(bbsN, bbd); 662 fail("IllegalArgumentException wasn't thrown"); 663 } catch (IllegalArgumentException expected) { 664 } 665 666 try { 667 sse.unwrap(bbs, bbdN); 668 fail("IllegalArgumentException wasn't thrown"); 669 } catch (IllegalArgumentException expected) { 670 } 671 672 try { 673 sse.unwrap(bbsN, bbdN); 674 fail("IllegalArgumentException wasn't thrown"); 675 } catch (IllegalArgumentException expected) { 676 } 677 } 678 679 /** 680 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 681 * IllegalStateException should be thrown. 682 */ 683 public void test_unwrap_ByteBuffer_ByteBuffer_04() throws Exception { 684 String host = "new host"; 685 int port = 8080; 686 ByteBuffer bbs = ByteBuffer.allocate(10); 687 ByteBuffer bbd = ByteBuffer.allocate(100); 688 SSLEngine sse = getEngine(host, port); 689 690 try { 691 sse.unwrap(bbs, bbd); 692 fail("IllegalStateException wasn't thrown"); 693 } catch (IllegalStateException expected) { 694 } 695 } 696 697 /** 698 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 699 */ 700 public void test_unwrap_ByteBuffer_ByteBuffer_05() throws Exception { 701 String host = "new host"; 702 int port = 8080; 703 ByteBuffer bbs = ByteBuffer.allocate(10); 704 ByteBuffer bbd = ByteBuffer.allocate(100); 705 SSLEngine sse = getEngine(host, port); 706 sse.setUseClientMode(true); 707 708 SSLEngineResult res = sse.unwrap(bbs, bbd); 709 assertEquals(0, res.bytesConsumed()); 710 assertEquals(0, res.bytesProduced()); 711 } 712 713 /** 714 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 715 * SSLException should be thrown. 716 */ 717 public void test_unwrap_ByteBuffer$ByteBuffer_01() throws Exception { 718 prepareEngines(); 719 doHandshake(); 720 721 ByteBuffer bbs = ByteBuffer.allocate(100); 722 ByteBuffer bbd = ByteBuffer.allocate(100); 723 724 try { 725 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }); 726 fail("SSLException wasn't thrown"); 727 } catch (SSLException expected) { 728 } 729 } 730 731 /** 732 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 733 * ReadOnlyBufferException should be thrown. 734 */ 735 public void test_unwrap_ByteBuffer$ByteBuffer_02() throws Exception { 736 String host = "new host"; 737 int port = 8080; 738 ByteBuffer bbs = ByteBuffer.allocate(10); 739 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer(); 740 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 741 SSLEngine sse = getEngine(host, port); 742 sse.setUseClientMode(true); 743 744 try { 745 sse.unwrap(bbs, bbA); 746 fail("ReadOnlyBufferException wasn't thrown"); 747 } catch (ReadOnlyBufferException expected) { 748 } 749 } 750 751 /** 752 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 753 * IllegalArgumentException should be thrown. 754 */ 755 public void test_unwrap_ByteBuffer$ByteBuffer_03() throws Exception { 756 String host = "new host"; 757 int port = 8080; 758 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 759 ByteBuffer[] bbN = { ByteBuffer.allocate(100), null, ByteBuffer.allocate(100) }; 760 ByteBuffer[] bbAN = null; 761 ByteBuffer bb = ByteBuffer.allocate(10); 762 ByteBuffer bN = null; 763 SSLEngine sse = getEngine(host, port); 764 sse.setUseClientMode(true); 765 766 try { 767 sse.unwrap(bN, bbA); 768 fail("IllegalArgumentException wasn't thrown"); 769 } catch (IllegalArgumentException expected) { 770 } 771 772 try { 773 sse.unwrap(bb, bbAN); 774 fail("IllegalArgumentException wasn't thrown"); 775 } catch (IllegalArgumentException expected) { 776 } 777 778 try { 779 sse.unwrap(bb, bbN); 780 fail("IllegalArgumentException wasn't thrown"); 781 } catch (IllegalArgumentException expected) { 782 } 783 784 try { 785 sse.unwrap(bN, bbAN); 786 fail("IllegalArgumentException wasn't thrown"); 787 } catch (IllegalArgumentException expected) { 788 } 789 } 790 791 /** 792 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 793 * IllegalStateException should be thrown. 794 */ 795 public void test_unwrap_ByteBuffer$ByteBuffer_04() throws Exception { 796 String host = "new host"; 797 int port = 8080; 798 ByteBuffer bbs = ByteBuffer.allocate(10); 799 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 800 SSLEngine sse = getEngine(host, port); 801 802 try { 803 sse.unwrap(bbs, bbd); 804 fail("IllegalStateException wasn't thrown"); 805 } catch (IllegalStateException expected) { 806 } 807 } 808 809 /** 810 * javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 811 */ 812 public void test_unwrap_ByteBuffer$ByteBuffer_05() throws Exception { 813 String host = "new host"; 814 int port = 8080; 815 ByteBuffer bbs = ByteBuffer.allocate(10); 816 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 817 SSLEngine sse = getEngine(host, port); 818 sse.setUseClientMode(true); 819 820 SSLEngineResult res = sse.unwrap(bbs, bbd); 821 assertEquals(0, res.bytesConsumed()); 822 assertEquals(0, res.bytesProduced()); 823 } 824 825 public void test_wrap_ByteBuffer_ByteBuffer_01() throws Exception { 826 prepareEngines(); 827 doHandshake(); 828 ByteBuffer bbs = ByteBuffer.allocate(20); 829 ByteBuffer bbd = ByteBuffer.allocate(20000); 830 clientEngine.engine.wrap(bbs, bbd); 831 } 832 833 /** 834 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 835 * ReadOnlyBufferException should be thrown. 836 */ 837 public void test_wrap_ByteBuffer_ByteBuffer_02() throws Exception { 838 String host = "new host"; 839 int port = 8080; 840 ByteBuffer bbs = ByteBuffer.allocate(10); 841 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer(); 842 SSLEngine sse = getEngine(host, port); 843 sse.setUseClientMode(true); 844 845 try { 846 sse.wrap(bbs, bbd); 847 fail("ReadOnlyBufferException wasn't thrown"); 848 } catch (ReadOnlyBufferException expected) { 849 } 850 } 851 852 /** 853 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 854 * IllegalArgumentException should be thrown. 855 */ 856 public void test_wrap_ByteBuffer_ByteBuffer_03() throws Exception { 857 String host = "new host"; 858 int port = 8080; 859 ByteBuffer bbsN = null; 860 ByteBuffer bbdN = null; 861 ByteBuffer bbs = ByteBuffer.allocate(10); 862 ByteBuffer bbd = ByteBuffer.allocate(100); 863 SSLEngine sse = getEngine(host, port); 864 sse.setUseClientMode(true); 865 866 try { 867 sse.wrap(bbsN, bbd); 868 fail("IllegalArgumentException wasn't thrown"); 869 } catch (IllegalArgumentException expected) { 870 } 871 872 try { 873 sse.wrap(bbs, bbdN); 874 fail("IllegalArgumentException wasn't thrown"); 875 } catch (IllegalArgumentException expected) { 876 } 877 878 try { 879 sse.wrap(bbsN, bbdN); 880 fail("IllegalArgumentException wasn't thrown"); 881 } catch (IllegalArgumentException expected) { 882 } 883 } 884 885 /** 886 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 887 * IllegalStateException should be thrown. 888 */ 889 public void test_wrap_ByteBuffer_ByteBuffer_04() throws Exception { 890 String host = "new host"; 891 int port = 8080; 892 ByteBuffer bbs = ByteBuffer.allocate(10); 893 ByteBuffer bbd = ByteBuffer.allocate(10); 894 SSLEngine sse = getEngine(host, port); 895 896 try { 897 SSLEngineResult result = sse.wrap(bbs, bbd); 898 } catch (IllegalStateException expected) { 899 } 900 } 901 902 /** 903 * javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 904 */ 905 public void test_wrap_ByteBuffer_ByteBuffer_05() throws Exception { 906 String host = "new host"; 907 int port = 8080; 908 ByteBuffer bb = ByteBuffer.allocate(10); 909 SSLEngine sse = getEngine(host, port); 910 sse.setUseClientMode(true); 911 912 SSLEngineResult res = sse.wrap(bb, ByteBuffer.allocate(10)); 913 assertEquals(Status.BUFFER_OVERFLOW, res.getStatus()); 914 assertEquals(0, res.bytesConsumed()); 915 assertEquals(0, res.bytesProduced()); 916 } 917 918 /** 919 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 920 * SSLException should be thrown. 921 */ 922 public void test_wrap_ByteBuffer$ByteBuffer_01() throws Exception { 923 prepareEngines(); 924 doHandshake(); 925 ByteBuffer bbs = ByteBuffer.allocate(100); 926 ByteBuffer bbd = ByteBuffer.allocate(20000); 927 928 clientEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd); 929 serverEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd); 930 } 931 932 /** 933 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 934 * ReadOnlyBufferException should be thrown. 935 */ 936 public void test_wrap_ByteBuffer$ByteBuffer_02() throws Exception { 937 String host = "new host"; 938 int port = 8080; 939 ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer(); 940 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 941 SSLEngine sse = getEngine(host, port); 942 sse.setUseClientMode(true); 943 944 try { 945 sse.wrap(bbA, bb); 946 fail("ReadOnlyBufferException wasn't thrown"); 947 } catch (ReadOnlyBufferException expected) { 948 } 949 } 950 951 /** 952 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 953 * IllegalArgumentException should be thrown. 954 */ 955 public void test_wrap_ByteBuffer$ByteBuffer_03() throws Exception { 956 String host = "new host"; 957 int port = 8080; 958 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 959 ByteBuffer[] bbAN = null; 960 ByteBuffer bb = ByteBuffer.allocate(10); 961 ByteBuffer bN = null; 962 SSLEngine sse = getEngine(host, port); 963 sse.setUseClientMode(true); 964 965 try { 966 sse.wrap(bbA, bN); 967 fail("IllegalArgumentException wasn't thrown"); 968 } catch (IllegalArgumentException expected) { 969 } 970 971 try { 972 sse.wrap(bbAN, bb); 973 fail("IllegalArgumentException wasn't thrown"); 974 } catch (IllegalArgumentException expected) { 975 } 976 977 try { 978 sse.wrap(bbAN, bN); 979 fail("IllegalArgumentException wasn't thrown"); 980 } catch (IllegalArgumentException expected) { 981 } 982 } 983 984 /** 985 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 986 * IllegalStateException should be thrown. 987 */ 988 public void test_wrap_ByteBuffer$ByteBuffer_04() throws Exception { 989 String host = "new host"; 990 int port = 8080; 991 ByteBuffer bb = ByteBuffer.allocate(10); 992 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) }; 993 SSLEngine sse = getEngine(host, port); 994 995 SSLEngineResult result = sse.wrap(bbA, bb); 996 assertEquals(Status.BUFFER_OVERFLOW, result.getStatus()); 997 } 998 999 /** 1000 * javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1001 */ 1002 public void test_wrap_ByteBuffer$ByteBuffer_05() throws Exception { 1003 String host = "new host"; 1004 int port = 8080; 1005 ByteBuffer bb = ByteBuffer.allocate(2000); 1006 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) }; 1007 SSLEngine sse = getEngine(host, port); 1008 sse.setUseClientMode(true); 1009 1010 SSLEngineResult res = sse.wrap(bbA, bb); 1011 assertEquals(0, res.bytesConsumed()); 1012 assertEquals(0, res.bytesProduced()); 1013 } 1014 1015 private SSLEngine getEngine() throws Exception { 1016 SSLContext context = SSLContext.getInstance("TLS"); 1017 context.init(null, null, null); 1018 return context.createSSLEngine(); 1019 } 1020 1021 private SSLEngine getEngine(String host, int port) throws Exception { 1022 SSLContext context = SSLContext.getInstance("TLS"); 1023 context.init(null, null, null); 1024 return context.createSSLEngine(host, port); 1025 } 1026 1027 class HandshakeHandler implements Runnable { 1028 1029 private final SSLEngine engine; 1030 1031 private final SourceChannel in; 1032 1033 private final SinkChannel out; 1034 1035 private final ByteBuffer EMPTY = ByteBuffer.allocate(0); 1036 1037 @SuppressWarnings("unused") 1038 private final String LOGTAG; 1039 1040 private SSLEngineResult.HandshakeStatus status; 1041 1042 private ByteBuffer readBuffer; 1043 1044 private ByteBuffer writeBuffer; 1045 1046 HandshakeHandler(SSLContext context, boolean clientMode, SourceChannel in, SinkChannel out) 1047 throws Exception { 1048 this.in = in; 1049 this.out = out; 1050 engine = context.createSSLEngine(); 1051 engine.setUseClientMode(clientMode); 1052 engine.beginHandshake(); 1053 status = engine.getHandshakeStatus(); 1054 1055 if (clientMode) { 1056 LOGTAG = "CLIENT: "; 1057 } else { 1058 LOGTAG = "SERVER: "; 1059 } 1060 1061 log("CipherSuites: " + Arrays.toString(engine.getEnabledCipherSuites())); 1062 log(status); 1063 1064 readBuffer = ByteBuffer.allocate(200000); 1065 writeBuffer = ByteBuffer.allocate(20000); 1066 } 1067 1068 public SSLEngineResult.HandshakeStatus getStatus() { 1069 return status; 1070 } 1071 1072 private void log(Object o) { 1073 //System.out.print(LOGTAG); 1074 //System.out.println(o); 1075 } 1076 1077 private ByteBuffer read() throws IOException { 1078 if (readBuffer == null || readBuffer.remaining() == 0 || readBuffer.position() == 0) { 1079 readBuffer.clear(); 1080 int read = in.read(readBuffer); 1081 log("read: " + read); 1082 readBuffer.rewind(); 1083 readBuffer.limit(read); 1084 } 1085 return readBuffer; 1086 } 1087 1088 public void run() { 1089 try { 1090 while (true) { 1091 switch (status) { 1092 case FINISHED: { 1093 log(status); 1094 return; 1095 } 1096 case NEED_TASK: { 1097 log(status); 1098 Runnable task; 1099 while ((task = engine.getDelegatedTask()) != null) { 1100 task.run(); 1101 } 1102 status = engine.getHandshakeStatus(); 1103 break; 1104 } 1105 case NEED_UNWRAP: { 1106 log(status); 1107 ByteBuffer source = read(); 1108 writeBuffer.clear(); 1109 1110 while (status == HandshakeStatus.NEED_UNWRAP) { 1111 SSLEngineResult result = engine.unwrap(source, writeBuffer); 1112 status = result.getHandshakeStatus(); 1113 log(result); 1114 } 1115 break; 1116 } 1117 case NEED_WRAP: { 1118 log(status); 1119 writeBuffer.clear(); 1120 1121 int produced = 0; 1122 SSLEngineResult result = null; 1123 while (status == HandshakeStatus.NEED_WRAP) { 1124 result = engine.wrap(EMPTY, writeBuffer); 1125 status = result.getHandshakeStatus(); 1126 produced += result.bytesProduced(); 1127 log(result); 1128 } 1129 writeBuffer.rewind(); 1130 writeBuffer.limit(produced); 1131 log("write: " + produced); 1132 out.write(writeBuffer); 1133 break; 1134 } 1135 case NOT_HANDSHAKING: { 1136 log("Not Handshaking"); 1137 return; 1138 } 1139 } 1140 } 1141 } catch (IOException e) { 1142 log(e); 1143 } catch (RuntimeException e) { 1144 // ignore; 1145 } 1146 } 1147 } 1148 1149 public void testHandshake() throws Exception { 1150 1151 prepareEngines(); 1152 1153 assertTrue("handshake failed", doHandshake()); 1154 1155 System.out.println(clientEngine.engine.getSession().getCipherSuite()); 1156 1157 assertEquals("Handshake not finished", 1158 SSLEngineResult.HandshakeStatus.FINISHED, 1159 clientEngine.getStatus()); 1160 assertEquals("Handshake not finished", 1161 SSLEngineResult.HandshakeStatus.FINISHED, 1162 serverEngine.getStatus()); 1163 } 1164 1165 void prepareEngines() throws Exception { 1166 Pipe clientSendPipe = Pipe.open(); 1167 Pipe serverSendPipe = Pipe.open(); 1168 1169 SinkChannel clientSink = clientSendPipe.sink(); 1170 SourceChannel serverSource = clientSendPipe.source(); 1171 SinkChannel serverSink = serverSendPipe.sink(); 1172 SourceChannel clientSource = serverSendPipe.source(); 1173 1174 TestSSLContext context = TestSSLContext.create(); 1175 clientEngine = new HandshakeHandler(context.clientContext, true, clientSource, clientSink); 1176 serverEngine = new HandshakeHandler(context.serverContext, false, serverSource, serverSink); 1177 } 1178 1179 boolean doHandshake() throws InterruptedException { 1180 Thread clientThread = new Thread(clientEngine); 1181 clientThread.start(); 1182 1183 Thread serverThread = new Thread(serverEngine); 1184 serverThread.start(); 1185 1186 int i = 0; 1187 while (clientThread.isAlive() && serverThread.isAlive() && i < 20) { 1188 Thread.sleep(500); 1189 i++; 1190 } 1191 1192 if (clientThread.isAlive()) { 1193 clientThread.interrupt(); 1194 } 1195 1196 if (serverThread.isAlive()) { 1197 serverThread.interrupt(); 1198 } 1199 1200 return clientEngine.getStatus() == HandshakeStatus.FINISHED && serverEngine.getStatus() == HandshakeStatus.FINISHED; 1201 } 1202 1203 } 1204