SSLEngineTest.java revision 6e65088f21ac5bda5d25fade13ee360c5cba3457
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 18package tests.api.javax.net.ssl; 19 20import java.io.IOException; 21import java.nio.ByteBuffer; 22import java.nio.ReadOnlyBufferException; 23import java.nio.channels.Pipe; 24import java.nio.channels.Pipe.SinkChannel; 25import java.nio.channels.Pipe.SourceChannel; 26import java.security.KeyManagementException; 27import java.security.NoSuchAlgorithmException; 28import java.util.Arrays; 29import java.util.HashSet; 30import java.util.Set; 31 32import javax.net.ssl.SSLContext; 33import javax.net.ssl.SSLEngine; 34import javax.net.ssl.SSLEngineResult; 35import javax.net.ssl.SSLException; 36import javax.net.ssl.SSLEngineResult.HandshakeStatus; 37 38import junit.framework.TestCase; 39import dalvik.annotation.AndroidOnly; 40import dalvik.annotation.KnownFailure; 41import dalvik.annotation.TestLevel; 42import dalvik.annotation.TestTargetClass; 43import dalvik.annotation.TestTargetNew; 44import dalvik.annotation.TestTargets; 45 46 47/** 48 * Tests for SSLEngine class 49 * 50 */ 51@TestTargetClass(SSLEngine.class) 52public class SSLEngineTest extends TestCase { 53 54 private HandshakeHandler clientEngine; 55 private HandshakeHandler serverEngine; 56 57 @Override protected void setUp() throws Exception { 58 super.setUp(); 59 } 60 61 /** 62 * Test for <code>SSLEngine()</code> constructor Assertion: creates 63 * SSLEngine object with null host and -1 port 64 * @throws NoSuchAlgorithmException 65 */ 66 @TestTargetNew( 67 level = TestLevel.COMPLETE, 68 notes = "", 69 method = "SSLEngine", 70 args = {} 71 ) 72 public void test_Constructor() throws NoSuchAlgorithmException { 73 SSLEngine e = getEngine(); 74 assertNull(e.getPeerHost()); 75 assertEquals(-1, e.getPeerPort()); 76 String[] suites = e.getSupportedCipherSuites(); 77 e.setEnabledCipherSuites(suites); 78 assertEquals(e.getEnabledCipherSuites().length, suites.length); 79 } 80 81 /** 82 * Test for <code>SSLEngine(String host, int port)</code> constructor 83 * @throws NoSuchAlgorithmException 84 */ 85 @TestTargetNew( 86 level = TestLevel.PARTIAL_COMPLETE, 87 notes = "Verification with incorrect parameters missed", 88 method = "SSLEngine", 89 args = {java.lang.String.class, int.class} 90 ) 91 public void test_ConstructorLjava_lang_StringI01() throws NoSuchAlgorithmException { 92 int port = 1010; 93 SSLEngine e = getEngine(null, port); 94 assertNull(e.getPeerHost()); 95 assertEquals(e.getPeerPort(), port); 96 try { 97 e.beginHandshake(); 98 } catch (IllegalStateException ex) { 99 // expected 100 } catch (SSLException ex) { 101 fail("unexpected SSLException was thrown."); 102 } 103 e = getEngine(null, port); 104 e.setUseClientMode(true); 105 try { 106 e.beginHandshake(); 107 } catch (SSLException ex) { 108 // expected 109 } 110 e = getEngine(null, port); 111 e.setUseClientMode(false); 112 try { 113 e.beginHandshake(); 114 } catch (SSLException ex) { 115 // expected 116 } 117 } 118 119 /** 120 * Test for <code>SSLEngine(String host, int port)</code> constructor 121 * @throws NoSuchAlgorithmException 122 */ 123 @TestTargetNew( 124 level = TestLevel.PARTIAL_COMPLETE, 125 notes = "Verification with incorrect parameters missed", 126 method = "SSLEngine", 127 args = {java.lang.String.class, int.class} 128 ) 129 public void test_ConstructorLjava_lang_StringI02() throws NoSuchAlgorithmException { 130 String host = "new host"; 131 int port = 8080; 132 SSLEngine e = getEngine(host, port); 133 assertEquals(e.getPeerHost(), host); 134 assertEquals(e.getPeerPort(), port); 135 String[] suites = e.getSupportedCipherSuites(); 136 e.setEnabledCipherSuites(suites); 137 assertEquals(e.getEnabledCipherSuites().length, suites.length); 138 e.setUseClientMode(true); 139 assertTrue(e.getUseClientMode()); 140 } 141 142 /** 143 * Test for <code>getPeerHost()</code> method 144 * @throws NoSuchAlgorithmException 145 */ 146 @TestTargetNew( 147 level = TestLevel.COMPLETE, 148 notes = "", 149 method = "getPeerHost", 150 args = {} 151 ) 152 public void test_getPeerHost() throws NoSuchAlgorithmException { 153 SSLEngine e = getEngine(); 154 assertNull(e.getPeerHost()); 155 e = getEngine("www.fortify.net", 80); 156 assertEquals("Incorrect host name", "www.fortify.net", e.getPeerHost()); 157 } 158 159 /** 160 * Test for <code>getPeerPort()</code> method 161 * @throws NoSuchAlgorithmException 162 */ 163 @TestTargetNew( 164 level = TestLevel.COMPLETE, 165 notes = "", 166 method = "getPeerPort", 167 args = {} 168 ) 169 public void test_getPeerPort() throws NoSuchAlgorithmException { 170 SSLEngine e = getEngine(); 171 assertEquals("Incorrect default value of peer port", 172 -1 ,e.getPeerPort()); 173 e = getEngine("www.fortify.net", 80); 174 assertEquals("Incorrect peer port", 80, e.getPeerPort()); 175 } 176 177 /** 178 * @throws NoSuchAlgorithmException 179 * @tests javax.net.ssl.SSLEngine#getSupportedProtocols() 180 */ 181 @TestTargetNew( 182 level = TestLevel.COMPLETE, 183 notes = "", 184 method = "getSupportedProtocols", 185 args = {} 186 ) 187 public void test_getSupportedProtocols() throws NoSuchAlgorithmException { 188 SSLEngine sse = getEngine(); 189 try { 190 String[] res = sse.getSupportedProtocols(); 191 assertNotNull(res); 192 assertTrue(res.length > 0); 193 } catch (Exception ex) { 194 fail("Unexpected exception " + ex); 195 } 196 } 197 198 /** 199 * @throws NoSuchAlgorithmException 200 * @tests javax.net.ssl.SSLEngine#setEnabledProtocols(String[] protocols) 201 * @tests javax.net.ssl.SSLEngine#getEnabledProtocols() 202 */ 203 @TestTargets({ 204 @TestTargetNew( 205 level = TestLevel.COMPLETE, 206 notes = "", 207 method = "getEnabledProtocols", 208 args = {} 209 ), 210 @TestTargetNew( 211 level = TestLevel.COMPLETE, 212 notes = "", 213 method = "setEnabledProtocols", 214 args = {String[].class} 215 ) 216 }) 217 public void test_EnabledProtocols() throws NoSuchAlgorithmException { 218 SSLEngine sse = getEngine(); 219 String[] pr = sse.getSupportedProtocols(); 220 try { 221 sse.setEnabledProtocols(pr); 222 String[] res = sse.getEnabledProtocols(); 223 assertNotNull("Null array was returned", res); 224 assertEquals("Incorrect array length", res.length, pr.length); 225 assertTrue("Incorrect array was returned", Arrays.equals(res, pr)); 226 } catch (Exception ex) { 227 fail("Unexpected exception " + ex); 228 } 229 try { 230 sse.setEnabledProtocols(null); 231 fail("IllegalArgumentException wasn't thrown"); 232 } catch (IllegalArgumentException iae) { 233 //expected 234 } 235 } 236 237 /** 238 * @throws NoSuchAlgorithmException 239 * @tests javax.net.ssl.SSLEngine#getSupportedCipherSuites() 240 */ 241 @TestTargetNew( 242 level = TestLevel.COMPLETE, 243 notes = "", 244 method = "getSupportedCipherSuites", 245 args = {} 246 ) 247 public void test_getSupportedCipherSuites() throws NoSuchAlgorithmException { 248 SSLEngine sse = getEngine(); 249 try { 250 String[] res = sse.getSupportedCipherSuites(); 251 assertNotNull(res); 252 assertTrue(res.length > 0); 253 } catch (Exception ex) { 254 fail("Unexpected exception " + ex); 255 } 256 } 257 258 /** 259 * @throws NoSuchAlgorithmException 260 * @tests javax.net.ssl.SSLEngine#setEnabledCipherSuites(String[] suites) 261 * @tests javax.net.ssl.SSLEngine#getEnabledCipherSuites() 262 */ 263 @TestTargets({ 264 @TestTargetNew( 265 level = TestLevel.COMPLETE, 266 notes = "", 267 method = "setEnabledCipherSuites", 268 args = {String[].class} 269 ), 270 @TestTargetNew( 271 level = TestLevel.COMPLETE, 272 notes = "", 273 method = "getEnabledCipherSuites", 274 args = {} 275 ) 276 }) 277 public void test_EnabledCipherSuites() throws NoSuchAlgorithmException { 278 SSLEngine sse = getEngine(); 279 String[] st = sse.getSupportedCipherSuites(); 280 try { 281 sse.setEnabledCipherSuites(st); 282 String[] res = sse.getEnabledCipherSuites(); 283 assertNotNull("Null array was returned", res); 284 assertEquals("Incorrect array length", res.length, st.length); 285 assertTrue("Incorrect array was returned", Arrays.equals(res, st)); 286 } catch (Exception ex) { 287 fail("Unexpected exception " + ex); 288 } 289 try { 290 sse.setEnabledCipherSuites(null); 291 fail("IllegalArgumentException wasn't thrown"); 292 } catch (IllegalArgumentException iae) { 293 //expected 294 } 295 } 296 297 /** 298 * @throws NoSuchAlgorithmException 299 * @tests javax.net.ssl.SSLEngine#setEnableSessionCreation(boolean flag) 300 * @tests javax.net.ssl.SSLEngine#getEnableSessionCreation() 301 */ 302 @TestTargets({ 303 @TestTargetNew( 304 level = TestLevel.COMPLETE, 305 notes = "", 306 method = "setEnableSessionCreation", 307 args = {boolean.class} 308 ), 309 @TestTargetNew( 310 level = TestLevel.COMPLETE, 311 notes = "", 312 method = "getEnableSessionCreation", 313 args = {} 314 ) 315 }) 316 public void test_EnableSessionCreation() throws NoSuchAlgorithmException { 317 SSLEngine sse = getEngine(); 318 try { 319 assertTrue(sse.getEnableSessionCreation()); 320 sse.setEnableSessionCreation(false); 321 assertFalse(sse.getEnableSessionCreation()); 322 sse.setEnableSessionCreation(true); 323 assertTrue(sse.getEnableSessionCreation()); 324 } catch (Exception ex) { 325 fail("Unexpected exception " + ex); 326 } 327 } 328 329 /** 330 * @throws NoSuchAlgorithmException 331 * @tests javax.net.ssl.SSLEngine#setNeedClientAuth(boolean need) 332 * @tests javax.net.ssl.SSLEngine#getNeedClientAuth() 333 */ 334 @TestTargets({ 335 @TestTargetNew( 336 level = TestLevel.COMPLETE, 337 notes = "", 338 method = "setNeedClientAuth", 339 args = {boolean.class} 340 ), 341 @TestTargetNew( 342 level = TestLevel.COMPLETE, 343 notes = "", 344 method = "getNeedClientAuth", 345 args = {} 346 ) 347 }) 348 public void test_NeedClientAuth() throws NoSuchAlgorithmException { 349 SSLEngine sse = getEngine(); 350 try { 351 sse.setNeedClientAuth(false); 352 assertFalse(sse.getNeedClientAuth()); 353 sse.setNeedClientAuth(true); 354 assertTrue(sse.getNeedClientAuth()); 355 } catch (Exception ex) { 356 fail("Unexpected exception " + ex); 357 } 358 } 359 360 /** 361 * @throws NoSuchAlgorithmException 362 * @tests javax.net.ssl.SSLEngine#setWantClientAuth(boolean want) 363 * @tests javax.net.ssl.SSLEngine#getWantClientAuth() 364 */ 365 @TestTargets({ 366 @TestTargetNew( 367 level = TestLevel.COMPLETE, 368 notes = "", 369 method = "setWantClientAuth", 370 args = {boolean.class} 371 ), 372 @TestTargetNew( 373 level = TestLevel.COMPLETE, 374 notes = "", 375 method = "getWantClientAuth", 376 args = {} 377 ) 378 }) 379 public void test_WantClientAuth() throws NoSuchAlgorithmException { 380 SSLEngine sse = getEngine(); 381 try { 382 sse.setWantClientAuth(false); 383 assertFalse(sse.getWantClientAuth()); 384 sse.setWantClientAuth(true); 385 assertTrue(sse.getWantClientAuth()); 386 } catch (Exception ex) { 387 fail("Unexpected exception " + ex); 388 } 389 } 390 391 /** 392 * @throws NoSuchAlgorithmException 393 * @tests javax.net.ssl.SSLEngine#beginHandshake() 394 */ 395 @TestTargetNew( 396 level = TestLevel.COMPLETE, 397 notes = "", 398 method = "beginHandshake", 399 args = {} 400 ) 401 public void test_beginHandshake() throws NoSuchAlgorithmException { 402 SSLEngine sse = getEngine(); 403 try { 404 sse.beginHandshake(); 405 fail("IllegalStateException wasn't thrown"); 406 } catch (IllegalStateException se) { 407 //expected 408 } catch (Exception e) { 409 fail(e + " was thrown instead of IllegalStateException"); 410 } 411 sse = getEngine("new host", 1080); 412 try { 413 sse.beginHandshake(); 414 fail("IllegalStateException wasn't thrown"); 415 } catch (IllegalStateException ise) { 416 //expected 417 } catch (Exception e) { 418 fail(e + " was thrown instead of IllegalStateException"); 419 } 420 sse = getEngine(); 421 try { 422 sse.setUseClientMode(true); 423 sse.beginHandshake(); 424 } catch (Exception ex) { 425 fail("Unexpected exception " + ex); 426 } 427 } 428 429 /** 430 * @throws NoSuchAlgorithmException 431 * @tests javax.net.ssl.SSLEngine#setUseClientMode(boolean mode) 432 * @tests javax.net.ssl.SSLEngine#getUseClientMode() 433 */ 434 @TestTargets({ 435 @TestTargetNew( 436 level = TestLevel.COMPLETE, 437 notes = "", 438 method = "setUseClientMode", 439 args = {boolean.class} 440 ), 441 @TestTargetNew( 442 level = TestLevel.COMPLETE, 443 notes = "", 444 method = "getUseClientMode", 445 args = {} 446 ) 447 }) 448 @AndroidOnly("The RI doesn't throw the expected IllegalStateException.") 449 public void test_UseClientMode() throws NoSuchAlgorithmException { 450 SSLEngine sse = getEngine(); 451 try { 452 sse.setUseClientMode(false); 453 assertFalse(sse.getUseClientMode()); 454 sse.setUseClientMode(true); 455 assertTrue(sse.getUseClientMode()); 456 } catch (Exception ex) { 457 fail("Unexpected exception " + ex); 458 } 459 460 try { 461 sse = getEngine(null, 1080); 462 sse.setUseClientMode(true); 463 sse.beginHandshake(); 464 try { 465 sse.setUseClientMode(false); 466 fail("IllegalArgumentException was not thrown"); 467 } catch (IllegalArgumentException iae) { 468 //expected 469 } 470 } catch (Exception ex) { 471 fail("Unexpected exception " + ex); 472 } 473 } 474 475 /** 476 * @throws NoSuchAlgorithmException 477 * @tests javax.net.ssl.SSLEngine#getSession() 478 */ 479 @TestTargetNew( 480 level = TestLevel.COMPLETE, 481 notes = "", 482 method = "getSession", 483 args = {} 484 ) 485 public void test_getSession() throws NoSuchAlgorithmException { 486 SSLEngine sse = getEngine(); 487 try { 488 assertNotNull(sse.getSession()); 489 } catch (Exception ex) { 490 fail("Unexpected exception " + ex); 491 } 492 } 493 494 /** 495 * @throws NoSuchAlgorithmException 496 * @tests javax.net.ssl.SSLEngine#getHandshakeStatus() 497 */ 498 @TestTargetNew( 499 level = TestLevel.COMPLETE, 500 notes = "", 501 method = "getHandshakeStatus", 502 args = {} 503 ) 504 public void test_getHandshakeStatus() throws NoSuchAlgorithmException { 505 SSLEngine sse = getEngine(); 506 try { 507 assertEquals(sse.getHandshakeStatus().toString(), "NOT_HANDSHAKING"); 508 sse.setUseClientMode(true); 509 sse.beginHandshake(); 510 assertEquals(sse.getHandshakeStatus().toString(), "NEED_WRAP"); 511 } catch (Exception ex) { 512 fail("Unexpected exception " + ex); 513 } 514 } 515 516 /** 517 * @throws NoSuchAlgorithmException 518 * @tests javax.net.ssl.SSLEngine#getDelegatedTask() 519 */ 520 @TestTargetNew( 521 level = TestLevel.COMPLETE, 522 notes = "", 523 method = "getDelegatedTask", 524 args = {} 525 ) 526 @KnownFailure("org.apache.harmony.xnet.provider.jsse.SSLEngineImpl#getDelegatedTask() throws NPE instead of returning null") 527 public void test_getDelegatedTask() throws NoSuchAlgorithmException { 528 SSLEngine sse = getEngine(); 529 try { 530 assertNull(sse.getDelegatedTask()); 531 } catch (Exception ex) { 532 fail("Unexpected exception " + ex); 533 } 534 } 535 536 /** 537 * @throws IOException 538 * @throws InterruptedException 539 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 540 * int offset, int length) 541 * Exception case: SSLException should be thrown. 542 */ 543 @TestTargetNew( 544 level = TestLevel.PARTIAL_COMPLETE, 545 notes = "", 546 method = "unwrap", 547 args = {ByteBuffer.class, ByteBuffer[].class, int.class, int.class} 548 ) 549 public void test_unwrap_01() throws IOException, InterruptedException { 550 prepareEngines(); 551 doHandshake(); 552 553 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}); 554 ByteBuffer bbd = ByteBuffer.allocate(100); 555 try { 556 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }, 0, 1); 557 fail("SSLException wasn't thrown"); 558 } catch (SSLException ex) { 559 //expected 560 } 561 } 562 563 /** 564 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 565 * int offset, int length) 566 * Exception case: IndexOutOfBoundsException should be thrown. 567 */ 568 @TestTargetNew( 569 level = TestLevel.PARTIAL_COMPLETE, 570 notes = "", 571 method = "unwrap", 572 args = {ByteBuffer.class, ByteBuffer[].class, int.class, int.class} 573 ) 574 @KnownFailure("Fixed in DonutBurger, boundary checks missing") 575 public void test_unwrap_02() throws SSLException { 576 String host = "new host"; 577 int port = 8080; 578 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 579 580 ByteBuffer bb = ByteBuffer.allocate(10); 581 SSLEngine sse = getEngine(host, port); 582 sse.setUseClientMode(true); 583 584 try { 585 sse.unwrap(bb, bbA, -1, 3); 586 fail("IndexOutOfBoundsException wasn't thrown"); 587 } catch (IndexOutOfBoundsException iobe) { 588 //expected 589 } 590 try { 591 sse.unwrap(bb, bbA, 0, -3); 592 fail("IndexOutOfBoundsException wasn't thrown"); 593 } catch (IndexOutOfBoundsException iobe) { 594 //expected 595 } 596 try { 597 sse.unwrap(bb, bbA, bbA.length + 1, bbA.length); 598 fail("IndexOutOfBoundsException wasn't thrown"); 599 } catch (IndexOutOfBoundsException iobe) { 600 //expected 601 } 602 try { 603 sse.unwrap(bb, bbA, 0, bbA.length + 1); 604 fail("IndexOutOfBoundsException wasn't thrown"); 605 } catch (IndexOutOfBoundsException iobe) { 606 //expected 607 } 608 } 609 610 /** 611 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 612 * int offset, int length) 613 * Exception case: ReadOnlyBufferException should be thrown. 614 */ 615 @TestTargetNew( 616 level = TestLevel.PARTIAL_COMPLETE, 617 notes = "", 618 method = "unwrap", 619 args = {ByteBuffer.class, ByteBuffer[].class, int.class, int.class} 620 ) 621 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 622 public void test_unwrap_03() { 623 String host = "new host"; 624 int port = 8080; 625 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer(); 626 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 627 628 ByteBuffer bb = ByteBuffer.allocate(10); 629 SSLEngine sse = getEngine(host, port); 630 sse.setUseClientMode(true); 631 632 try { 633 sse.unwrap(bb, bbA, 0, bbA.length); 634 fail("ReadOnlyBufferException wasn't thrown"); 635 } catch (ReadOnlyBufferException iobe) { 636 //expected 637 } catch (Exception e) { 638 fail(e + " was thrown instead of ReadOnlyBufferException"); 639 } 640 } 641 642 /** 643 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 644 * int offset, int length) 645 * Exception case: IllegalArgumentException should be thrown. 646 */ 647 @TestTargetNew( 648 level = TestLevel.PARTIAL_COMPLETE, 649 notes = "IllegalArgumentException should be thrown", 650 method = "unwrap", 651 args = {ByteBuffer.class, ByteBuffer[].class, int.class, int.class} 652 ) 653 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 654 public void test_unwrap_04() { 655 String host = "new host"; 656 int port = 8080; 657 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 658 ByteBuffer[] bbAN = {ByteBuffer.allocate(100), null, ByteBuffer.allocate(100)}; 659 ByteBuffer[] bbN = null; 660 ByteBuffer bb = ByteBuffer.allocate(10); 661 ByteBuffer bN = null; 662 SSLEngine sse = getEngine(host, port); 663 sse.setUseClientMode(true); 664 665 try { 666 sse.unwrap(bN, bbA, 0, 3); 667 fail("IllegalArgumentException wasn't thrown"); 668 } catch (IllegalArgumentException iobe) { 669 //expected 670 } catch (NullPointerException npe) { 671 } catch (Exception e) { 672 fail(e + " was thrown instead of IllegalArgumentException"); 673 } 674 try { 675 sse.unwrap(bb, bbAN, 0, 3); 676 fail("IllegalArgumentException wasn't thrown"); 677 } catch (IllegalArgumentException iobe) { 678 //expected 679 } catch (NullPointerException npe) { 680 } catch (Exception e) { 681 fail(e + " was thrown instead of IllegalArgumentException"); 682 } 683 try { 684 sse.unwrap(bb, bbN, 0, 0); 685 fail("IllegalArgumentException wasn't thrown"); 686 } catch (IllegalArgumentException iobe) { 687 //expected 688 } catch (NullPointerException npe) { 689 } catch (Exception e) { 690 fail(e + " was thrown instead of IllegalArgumentException"); 691 } 692 try { 693 sse.unwrap(bN, bbN, 0, 0); 694 fail("IllegalArgumentException wasn't thrown"); 695 } catch (IllegalArgumentException iobe) { 696 //expected 697 } catch (NullPointerException npe) { 698 } catch (Exception e) { 699 fail(e + " was thrown instead of IllegalArgumentException"); 700 } 701 702 } 703 704 /** 705 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 706 * int offset, int length) 707 * Exception case: IllegalStateException should be thrown. 708 */ 709 @TestTargetNew( 710 level = TestLevel.PARTIAL_COMPLETE, 711 notes = "", 712 method = "unwrap", 713 args = {ByteBuffer.class, ByteBuffer[].class, int.class, int.class} 714 ) 715 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 716 public void test_unwrap_05() { 717 String host = "new host"; 718 int port = 8080; 719 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 720 721 ByteBuffer bb = ByteBuffer.allocate(10); 722 SSLEngine sse = getEngine(host, port); 723 724 try { 725 sse.unwrap(bb, bbA, 0, bbA.length); 726 fail("IllegalStateException wasn't thrown"); 727 } catch (IllegalStateException iobe) { 728 //expected 729 } catch (Exception e) { 730 fail(e + " was thrown instead of IllegalStateException"); 731 } 732 } 733 734 /** 735 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts, 736 * int offset, int length) 737 */ 738 @TestTargetNew( 739 level = TestLevel.PARTIAL_COMPLETE, 740 notes = "", 741 method = "unwrap", 742 args = {ByteBuffer.class, ByteBuffer[].class, int.class, int.class} 743 ) 744 public void test_unwrap_06() { 745 String host = "new host"; 746 int port = 8080; 747 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 748 749 ByteBuffer bb = ByteBuffer.allocate(10); 750 SSLEngine sse = getEngine(host, port); 751 sse.setUseClientMode(true); 752 753 try { 754 SSLEngineResult res = sse.unwrap(bb, bbA, 0, bbA.length); 755 assertEquals(0, res.bytesConsumed()); 756 assertEquals(0, res.bytesProduced()); 757 } catch (Exception ex) { 758 fail("Unexpected exception: " + ex); 759 } 760 } 761 762 /** 763 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 764 * int length, ByteBuffer dst) 765 * Exception case: SSLException should be thrown. 766 */ 767 @TestTargetNew( 768 level = TestLevel.NOT_FEASIBLE, 769 notes = "wrap cannot be forced to fail", 770 method = "wrap", 771 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 772 ) 773 public void test_wrap_01() throws IOException, InterruptedException { 774 prepareEngines(); 775 doHandshake(); 776 777 ByteBuffer bbs = ByteBuffer.allocate(100); 778 ByteBuffer bbd = ByteBuffer.allocate(20000); 779 780 try { 781 @SuppressWarnings("unused") 782 SSLEngineResult result = clientEngine.engine.wrap(new ByteBuffer[] { bbs }, 0, 1, bbd); 783 //fail("SSLException wasn't thrown"); 784 } catch (SSLException ex) { 785 //expected 786 } 787 } 788 789 /** 790 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 791 * int length, ByteBuffer dst) 792 * Exception case: IndexOutOfBoundsException should be thrown. 793 */ 794 @TestTargetNew( 795 level = TestLevel.PARTIAL_COMPLETE, 796 notes = "", 797 method = "wrap", 798 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 799 ) 800 @KnownFailure("Fixed in DonutBurger, boundary checks missing") 801 public void test_wrap_02() throws SSLException { 802 String host = "new host"; 803 int port = 8080; 804 ByteBuffer bb = ByteBuffer.allocate(10); 805 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 806 SSLEngine sse = getEngine(host, port); 807 sse.setUseClientMode(true); 808 809 try { 810 sse.wrap(bbA, -1, 3, bb); 811 fail("IndexOutOfBoundsException wasn't thrown"); 812 } catch (IndexOutOfBoundsException iobe) { 813 //expected 814 } 815 try { 816 sse.wrap(bbA, 0, -3, bb); 817 fail("IndexOutOfBoundsException wasn't thrown"); 818 } catch (IndexOutOfBoundsException iobe) { 819 //expected 820 } 821 try { 822 sse.wrap(bbA, bbA.length + 1, bbA.length, bb); 823 fail("IndexOutOfBoundsException wasn't thrown"); 824 } catch (IndexOutOfBoundsException iobe) { 825 //expected 826 } 827 try { 828 sse.wrap(bbA, 0, bbA.length + 1, bb); 829 fail("IndexOutOfBoundsException wasn't thrown"); 830 } catch (IndexOutOfBoundsException iobe) { 831 //expected 832 } 833 } 834 835 /** 836 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 837 * int length, ByteBuffer dst) 838 * Exception case: ReadOnlyBufferException should be thrown. 839 */ 840 @TestTargetNew( 841 level = TestLevel.PARTIAL_COMPLETE, 842 notes = "", 843 method = "wrap", 844 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 845 ) 846 public void test_wrap_03() throws SSLException { 847 String host = "new host"; 848 int port = 8080; 849 ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer(); 850 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 851 SSLEngine sse = getEngine(host, port); 852 sse.setUseClientMode(true); 853 854 try { 855 sse.wrap(bbA, 0, bbA.length, bb); 856 fail("ReadOnlyBufferException wasn't thrown"); 857 } catch (ReadOnlyBufferException iobe) { 858 //expected 859 } 860 } 861 862 /** 863 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 864 * int length, ByteBuffer dst) 865 * Exception case: IllegalArgumentException should be thrown. 866 */ 867 @TestTargetNew( 868 level = TestLevel.PARTIAL_COMPLETE, 869 notes = "IllegalArgumentException must be thrown", 870 method = "wrap", 871 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 872 ) 873 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 874 public void test_wrap_04() { 875 String host = "new host"; 876 int port = 8080; 877 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 878 ByteBuffer[] bbN = null; 879 ByteBuffer bN = null; 880 SSLEngine e = getEngine(host, port); 881 e.setUseClientMode(true); 882 883 try { 884 e.wrap(bbA, 0, 3, bN); 885 fail("IllegalArgumentException must be thrown for null srcs byte buffer array"); 886 } catch (NullPointerException npe) { 887 } catch (IllegalArgumentException ex) { 888 } catch (Exception ex) { 889 fail(ex + " was thrown instead of IllegalArgumentException"); 890 } 891 892 try { 893 e.wrap(bbN, 0, 0, bN); 894 fail("IllegalArgumentException wasn't thrown"); 895 } catch (IllegalArgumentException ex) { 896 } catch (NullPointerException npe) { 897 } catch (Exception ex) { 898 fail(ex + " was thrown instead of IllegalArgumentException"); 899 } 900 } 901 902 /** 903 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 904 * int length, ByteBuffer dst) 905 * Exception case: IllegalStateException should be thrown. 906 */ 907 @TestTargetNew( 908 level = TestLevel.PARTIAL_COMPLETE, 909 notes = "", 910 method = "wrap", 911 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 912 ) 913 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 914 public void test_wrap_05() throws SSLException { 915 String host = "new host"; 916 int port = 8080; 917 ByteBuffer bb = ByteBuffer.allocate(10); 918 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 919 SSLEngine sse = getEngine(host, port); 920 921 try { 922 sse.wrap(bbA, 0, bbA.length, bb); 923 fail("IllegalStateException wasn't thrown"); 924 } catch (IllegalStateException iobe) { 925 //expected 926 } 927 } 928 929 /** 930 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 931 * int length, ByteBuffer dst) 932 */ 933 @TestTargetNew( 934 level = TestLevel.PARTIAL_COMPLETE, 935 notes = "", 936 method = "wrap", 937 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 938 ) 939 public void test_wrap_06() { 940 String host = "new host"; 941 int port = 8080; 942 ByteBuffer bb = ByteBuffer.allocate(10); 943 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 944 SSLEngine sse = getEngine(host, port); 945 sse.setUseClientMode(true); 946 947 try { 948 sse.wrap(bbA, 0, bbA.length, bb); 949 } catch (Exception ex) { 950 fail("Unexpected exception: " + ex); 951 } 952 } 953 954 /** 955 * @throws NoSuchAlgorithmException 956 * @tests javax.net.ssl.SSLEngine#closeOutbound() 957 * @tests javax.net.ssl.SSLEngine#isOutboundDone() 958 */ 959 @TestTargets({ 960 @TestTargetNew( 961 level = TestLevel.COMPLETE, 962 notes = "", 963 method = "closeOutbound", 964 args = {} 965 ), 966 @TestTargetNew( 967 level = TestLevel.COMPLETE, 968 notes = "", 969 method = "isOutboundDone", 970 args = {} 971 ) 972 }) 973 public void test_closeOutbound() throws NoSuchAlgorithmException { 974 SSLEngine sse = getEngine(); 975 976 try { 977 assertFalse(sse.isOutboundDone()); 978 sse.closeOutbound(); 979 assertTrue(sse.isOutboundDone()); 980 } catch (Exception ex) { 981 fail("Unexpected exception: " + ex); 982 } 983 } 984 985 /** 986 * @throws NoSuchAlgorithmException 987 * @tests javax.net.ssl.SSLEngine#closeInbound() 988 * @tests javax.net.ssl.SSLEngine#isInboundDone() 989 */ 990 @TestTargets({ 991 @TestTargetNew( 992 level = TestLevel.SUFFICIENT, 993 notes = "", 994 method = "closeInbound", 995 args = {} 996 ), 997 @TestTargetNew( 998 level = TestLevel.COMPLETE, 999 notes = "", 1000 method = "isInboundDone", 1001 args = {} 1002 ) 1003 }) 1004 public void test_closeInbound() throws NoSuchAlgorithmException { 1005 SSLEngine sse = getEngine(); 1006 1007 try { 1008 assertFalse(sse.isInboundDone()); 1009 sse.closeInbound(); 1010 assertTrue(sse.isInboundDone()); 1011 } catch (Exception ex) { 1012 fail("Unexpected exception: " + ex); 1013 } 1014 } 1015 1016 /** 1017 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1018 * SSLException should be thrown. 1019 */ 1020 @TestTargetNew( 1021 level = TestLevel.PARTIAL_COMPLETE, 1022 notes = "", 1023 method = "unwrap", 1024 args = {ByteBuffer.class, ByteBuffer.class} 1025 ) 1026 public void test_unwrap_ByteBuffer_ByteBuffer_01() throws InterruptedException, IOException { 1027 prepareEngines(); 1028 doHandshake(); 1029 ByteBuffer bbs = ByteBuffer.allocate(100); 1030 ByteBuffer bbd = ByteBuffer.allocate(100); 1031 1032 try { 1033 SSLEngineResult unwrap = clientEngine.engine.unwrap(bbs, bbd); 1034 fail("SSLException wasn't thrown"); 1035 } catch (SSLException ex) { 1036 //expected 1037 } 1038 } 1039 1040 /** 1041 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1042 * ReadOnlyBufferException should be thrown. 1043 */ 1044 @TestTargetNew( 1045 level = TestLevel.PARTIAL_COMPLETE, 1046 notes = "", 1047 method = "unwrap", 1048 args = {ByteBuffer.class, ByteBuffer.class} 1049 ) 1050 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1051 public void test_unwrap_ByteBuffer_ByteBuffer_02() { 1052 String host = "new host"; 1053 int port = 8080; 1054 ByteBuffer bbs = ByteBuffer.allocate(10); 1055 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer(); 1056 SSLEngine sse = getEngine(host, port); 1057 sse.setUseClientMode(true); 1058 1059 try { 1060 sse.unwrap(bbs, bbd); 1061 fail("ReadOnlyBufferException wasn't thrown"); 1062 } catch (ReadOnlyBufferException iobe) { 1063 //expected 1064 } catch (Exception e) { 1065 fail(e + " was thrown instead of ReadOnlyBufferException"); 1066 } 1067 } 1068 1069 /** 1070 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1071 * IllegalArgumentException should be thrown. 1072 */ 1073 @TestTargetNew( 1074 level = TestLevel.PARTIAL_COMPLETE, 1075 notes = "", 1076 method = "unwrap", 1077 args = {ByteBuffer.class, ByteBuffer.class} 1078 ) 1079 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1080 public void test_unwrap_ByteBuffer_ByteBuffer_03() { 1081 String host = "new host"; 1082 int port = 8080; 1083 ByteBuffer bbsN = null; 1084 ByteBuffer bbdN = null; 1085 ByteBuffer bbs = ByteBuffer.allocate(10); 1086 ByteBuffer bbd = ByteBuffer.allocate(100); 1087 SSLEngine sse = getEngine(host, port); 1088 sse.setUseClientMode(true); 1089 1090 try { 1091 sse.unwrap(bbsN, bbd); 1092 fail("IllegalArgumentException wasn't thrown"); 1093 } catch (IllegalArgumentException iae) { 1094 //expected 1095 } catch (NullPointerException npe) { 1096 } catch (Exception e) { 1097 fail(e + " was thrown instead of IllegalArgumentException"); 1098 } 1099 1100 try { 1101 sse.unwrap(bbs, bbdN); 1102 fail("IllegalArgumentException wasn't thrown"); 1103 } catch (IllegalArgumentException iae) { 1104 //expected 1105 } catch (NullPointerException npe) { 1106 } catch (Exception e) { 1107 fail(e + " was thrown instead of IllegalArgumentException"); 1108 } 1109 1110 try { 1111 sse.unwrap(bbsN, bbdN); 1112 fail("IllegalArgumentException wasn't thrown"); 1113 } catch (IllegalArgumentException iae) { 1114 //expected 1115 } catch (NullPointerException npe) { 1116 } catch (Exception e) { 1117 fail(e + " was thrown instead of IllegalArgumentException"); 1118 } 1119 } 1120 1121 /** 1122 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1123 * IllegalStateException should be thrown. 1124 */ 1125 @TestTargetNew( 1126 level = TestLevel.PARTIAL_COMPLETE, 1127 notes = "", 1128 method = "unwrap", 1129 args = {ByteBuffer.class, ByteBuffer.class} 1130 ) 1131 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 1132 public void test_unwrap_ByteBuffer_ByteBuffer_04() { 1133 String host = "new host"; 1134 int port = 8080; 1135 ByteBuffer bbs = ByteBuffer.allocate(10); 1136 ByteBuffer bbd = ByteBuffer.allocate(100); 1137 SSLEngine sse = getEngine(host, port); 1138 1139 try { 1140 sse.unwrap(bbs, bbd); 1141 fail("IllegalStateException wasn't thrown"); 1142 } catch (IllegalStateException iobe) { 1143 //expected 1144 } catch (Exception e) { 1145 fail(e + " was thrown instead of IllegalStateException"); 1146 } 1147 } 1148 1149 /** 1150 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1151 */ 1152 @TestTargetNew( 1153 level = TestLevel.PARTIAL_COMPLETE, 1154 notes = "", 1155 method = "unwrap", 1156 args = {ByteBuffer.class, ByteBuffer.class} 1157 ) 1158 public void test_unwrap_ByteBuffer_ByteBuffer_05() { 1159 String host = "new host"; 1160 int port = 8080; 1161 ByteBuffer bbs = ByteBuffer.allocate(10); 1162 ByteBuffer bbd = ByteBuffer.allocate(100); 1163 SSLEngine sse = getEngine(host, port); 1164 sse.setUseClientMode(true); 1165 1166 try { 1167 SSLEngineResult res = sse.unwrap(bbs, bbd); 1168 assertEquals(0, res.bytesConsumed()); 1169 assertEquals(0, res.bytesProduced()); 1170 } catch (Exception e) { 1171 fail("Unexpected exception: " + e); 1172 } 1173 } 1174 1175 /** 1176 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1177 * SSLException should be thrown. 1178 */ 1179 @TestTargetNew( 1180 level = TestLevel.PARTIAL_COMPLETE, 1181 notes = "", 1182 method = "unwrap", 1183 args = {ByteBuffer.class, ByteBuffer[].class} 1184 ) 1185 public void test_unwrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException { 1186 prepareEngines(); 1187 doHandshake(); 1188 1189 ByteBuffer bbs = ByteBuffer.allocate(100); 1190 ByteBuffer bbd = ByteBuffer.allocate(100); 1191 1192 try { 1193 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }); 1194 fail("SSLException wasn't thrown"); 1195 } catch (SSLException ex) { 1196 //expected 1197 } 1198 } 1199 1200 /** 1201 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1202 * ReadOnlyBufferException should be thrown. 1203 */ 1204 @TestTargetNew( 1205 level = TestLevel.PARTIAL_COMPLETE, 1206 notes = "", 1207 method = "unwrap", 1208 args = {ByteBuffer.class, ByteBuffer[].class} 1209 ) 1210 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1211 public void test_unwrap_ByteBuffer$ByteBuffer_02() { 1212 String host = "new host"; 1213 int port = 8080; 1214 ByteBuffer bbs = ByteBuffer.allocate(10); 1215 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer(); 1216 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 1217 SSLEngine sse = getEngine(host, port); 1218 sse.setUseClientMode(true); 1219 1220 try { 1221 sse.unwrap(bbs, bbA); 1222 fail("ReadOnlyBufferException wasn't thrown"); 1223 } catch (ReadOnlyBufferException iobe) { 1224 //expected 1225 } catch (Exception e) { 1226 fail(e + " was thrown instead of ReadOnlyBufferException"); 1227 } 1228 } 1229 1230 /** 1231 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1232 * IllegalArgumentException should be thrown. 1233 */ 1234 @TestTargetNew( 1235 level = TestLevel.PARTIAL_COMPLETE, 1236 notes = "", 1237 method = "unwrap", 1238 args = {ByteBuffer.class, ByteBuffer[].class} 1239 ) 1240 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1241 public void test_unwrap_ByteBuffer$ByteBuffer_03() { 1242 String host = "new host"; 1243 int port = 8080; 1244 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 1245 ByteBuffer[] bbN = { ByteBuffer.allocate(100), null, ByteBuffer.allocate(100) }; 1246 ByteBuffer[] bbAN = null; 1247 ByteBuffer bb = ByteBuffer.allocate(10); 1248 ByteBuffer bN = null; 1249 SSLEngine sse = getEngine(host, port); 1250 sse.setUseClientMode(true); 1251 1252 try { 1253 sse.unwrap(bN, bbA); 1254 fail("IllegalArgumentException wasn't thrown"); 1255 } catch (IllegalArgumentException iobe) { 1256 //expected 1257 } catch (NullPointerException npe) { 1258 } catch (Exception e) { 1259 fail(e + " was thrown instead of IllegalArgumentException"); 1260 } 1261 1262 try { 1263 sse.unwrap(bb, bbAN); 1264 fail("IllegalArgumentException wasn't thrown"); 1265 } catch (IllegalArgumentException iobe) { 1266 //expected 1267 } catch (NullPointerException npe) { 1268 } catch (Exception e) { 1269 fail(e + " was thrown instead of IllegalArgumentException"); 1270 } 1271 1272 try { 1273 sse.unwrap(bb, bbN); 1274 fail("IllegalArgumentException wasn't thrown"); 1275 } catch (IllegalArgumentException iobe) { 1276 //expected 1277 } catch (NullPointerException npe) { 1278 } catch (Exception e) { 1279 fail(e + " was thrown instead of IllegalArgumentException"); 1280 } 1281 1282 try { 1283 sse.unwrap(bN, bbAN); 1284 fail("IllegalArgumentException wasn't thrown"); 1285 } catch (IllegalArgumentException iobe) { 1286 //expected 1287 } catch (NullPointerException npe) { 1288 } catch (Exception e) { 1289 fail(e + " was thrown instead of IllegalArgumentException"); 1290 } 1291 } 1292 1293 /** 1294 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1295 * IllegalStateException should be thrown. 1296 */ 1297 @TestTargetNew( 1298 level = TestLevel.PARTIAL_COMPLETE, 1299 notes = "", 1300 method = "unwrap", 1301 args = {ByteBuffer.class, ByteBuffer[].class} 1302 ) 1303 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 1304 public void test_unwrap_ByteBuffer$ByteBuffer_04() { 1305 String host = "new host"; 1306 int port = 8080; 1307 ByteBuffer bbs = ByteBuffer.allocate(10); 1308 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 1309 SSLEngine sse = getEngine(host, port); 1310 1311 try { 1312 sse.unwrap(bbs, bbd); 1313 fail("IllegalStateException wasn't thrown"); 1314 } catch (IllegalStateException iobe) { 1315 //expected 1316 } catch (Exception e) { 1317 fail(e + " was thrown instead of IllegalStateException"); 1318 } 1319 } 1320 1321 /** 1322 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1323 */ 1324 @TestTargetNew( 1325 level = TestLevel.PARTIAL_COMPLETE, 1326 notes = "", 1327 method = "unwrap", 1328 args = {ByteBuffer.class, ByteBuffer[].class} 1329 ) 1330 public void test_unwrap_ByteBuffer$ByteBuffer_05() { 1331 String host = "new host"; 1332 int port = 8080; 1333 ByteBuffer bbs = ByteBuffer.allocate(10); 1334 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 1335 SSLEngine sse = getEngine(host, port); 1336 sse.setUseClientMode(true); 1337 1338 try { 1339 SSLEngineResult res = sse.unwrap(bbs, bbd); 1340 assertEquals(0, res.bytesConsumed()); 1341 assertEquals(0, res.bytesProduced()); 1342 } catch (Exception ex) { 1343 fail("Unexpected exception: " + ex); 1344 } 1345 } 1346 1347 /** 1348 * @throws IOException 1349 * @throws InterruptedException 1350 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1351 * SSLException should be thrown. 1352 */ 1353 @TestTargetNew( 1354 level = TestLevel.NOT_FEASIBLE, 1355 notes = "wrap cannot be forced to produce SSLException", 1356 method = "wrap", 1357 args = {ByteBuffer.class, ByteBuffer.class} 1358 ) 1359 public void test_wrap_ByteBuffer_ByteBuffer_01() throws IOException, InterruptedException { 1360 prepareEngines(); 1361 doHandshake(); 1362 ByteBuffer bbs = ByteBuffer.allocate(20); 1363 ByteBuffer bbd = ByteBuffer.allocate(20000); 1364 1365 try { 1366 clientEngine.engine.wrap(bbs, bbd); 1367 //fail("SSLException wasn't thrown"); 1368 } catch (SSLException ex) { 1369 //expected 1370 } 1371 } 1372 1373 /** 1374 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1375 * ReadOnlyBufferException should be thrown. 1376 */ 1377 @TestTargetNew( 1378 level = TestLevel.PARTIAL_COMPLETE, 1379 notes = "", 1380 method = "wrap", 1381 args = {ByteBuffer.class, ByteBuffer.class} 1382 ) 1383 public void test_wrap_ByteBuffer_ByteBuffer_02() { 1384 String host = "new host"; 1385 int port = 8080; 1386 ByteBuffer bbs = ByteBuffer.allocate(10); 1387 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer(); 1388 SSLEngine sse = getEngine(host, port); 1389 sse.setUseClientMode(true); 1390 1391 try { 1392 sse.wrap(bbs, bbd); 1393 fail("ReadOnlyBufferException wasn't thrown"); 1394 } catch (ReadOnlyBufferException iobe) { 1395 //expected 1396 } catch (Exception e) { 1397 fail(e + " was thrown instead of ReadOnlyBufferException"); 1398 } 1399 } 1400 1401 /** 1402 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1403 * IllegalArgumentException should be thrown. 1404 */ 1405 @TestTargetNew( 1406 level = TestLevel.PARTIAL_COMPLETE, 1407 notes = "", 1408 method = "wrap", 1409 args = {ByteBuffer.class, ByteBuffer.class} 1410 ) 1411 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1412 public void test_wrap_ByteBuffer_ByteBuffer_03() { 1413 String host = "new host"; 1414 int port = 8080; 1415 ByteBuffer bbsN = null; 1416 ByteBuffer bbdN = null; 1417 ByteBuffer bbs = ByteBuffer.allocate(10); 1418 ByteBuffer bbd = ByteBuffer.allocate(100); 1419 SSLEngine sse = getEngine(host, port); 1420 sse.setUseClientMode(true); 1421 1422 try { 1423 sse.wrap(bbsN, bbd); 1424 fail("IllegalArgumentException wasn't thrown"); 1425 } catch (IllegalArgumentException iae) { 1426 //expected 1427 } catch (NullPointerException npe) { 1428 } catch (Exception e) { 1429 fail(e + " was thrown instead of IllegalArgumentException"); 1430 } 1431 1432 try { 1433 sse.wrap(bbs, bbdN); 1434 fail("IllegalArgumentException wasn't thrown"); 1435 } catch (IllegalArgumentException iae) { 1436 //expected 1437 } catch (NullPointerException npe) { 1438 } catch (Exception e) { 1439 fail(e + " was thrown instead of IllegalArgumentException"); 1440 } 1441 1442 try { 1443 sse.wrap(bbsN, bbdN); 1444 fail("IllegalArgumentException wasn't thrown"); 1445 } catch (IllegalArgumentException iae) { 1446 //expected 1447 } catch (NullPointerException npe) { 1448 } catch (Exception e) { 1449 fail(e + " was thrown instead of IllegalArgumentException"); 1450 } 1451 } 1452 1453 /** 1454 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1455 * IllegalStateException should be thrown. 1456 */ 1457 @TestTargetNew( 1458 level = TestLevel.PARTIAL_COMPLETE, 1459 notes = "", 1460 method = "wrap", 1461 args = {ByteBuffer.class, ByteBuffer.class} 1462 ) 1463 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 1464 public void test_wrap_ByteBuffer_ByteBuffer_04() { 1465 String host = "new host"; 1466 int port = 8080; 1467 ByteBuffer bbs = ByteBuffer.allocate(10); 1468 ByteBuffer bbd = ByteBuffer.allocate(10); 1469 SSLEngine sse = getEngine(host, port); 1470 1471 try { 1472 sse.wrap(bbs, bbd); 1473 fail("IllegalStateException wasn't thrown"); 1474 } catch (IllegalStateException iobe) { 1475 //expected 1476 } catch (Exception e) { 1477 fail(e + " was thrown instead of IllegalStateException"); 1478 } 1479 } 1480 1481 /** 1482 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1483 */ 1484 @TestTargetNew( 1485 level = TestLevel.PARTIAL_COMPLETE, 1486 notes = "", 1487 method = "wrap", 1488 args = {ByteBuffer.class, ByteBuffer.class} 1489 ) 1490 public void test_wrap_ByteBuffer_ByteBuffer_05() { 1491 String host = "new host"; 1492 int port = 8080; 1493 ByteBuffer bb = ByteBuffer.allocate(10); 1494 SSLEngine sse = getEngine(host, port); 1495 sse.setUseClientMode(true); 1496 1497 try { 1498 SSLEngineResult res = sse.wrap(bb, ByteBuffer.allocate(10)); 1499 assertEquals(0, res.bytesConsumed()); 1500 assertEquals(0, res.bytesProduced()); 1501 } catch (Exception e) { 1502 fail("Unexpected exception: " + e); 1503 } 1504 } 1505 1506 /** 1507 * @throws IOException 1508 * @throws InterruptedException 1509 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1510 * SSLException should be thrown. 1511 */ 1512 @TestTargetNew( 1513 level = TestLevel.PARTIAL_COMPLETE, 1514 notes = "wrap cannot be forced to throw SSLException", 1515 method = "wrap", 1516 args = {ByteBuffer[].class, ByteBuffer.class} 1517 ) 1518 public void test_wrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException { 1519 prepareEngines(); 1520 doHandshake(); 1521 ByteBuffer bbs = ByteBuffer.allocate(100); 1522 ByteBuffer bbd = ByteBuffer.allocate(20000); 1523 1524 try { 1525 clientEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd); 1526 serverEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd); 1527 //fail("SSLException wasn't thrown"); 1528 } catch (SSLException ex) { 1529 //expected 1530 } 1531 } 1532 1533 /** 1534 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1535 * ReadOnlyBufferException should be thrown. 1536 */ 1537 @TestTargetNew( 1538 level = TestLevel.PARTIAL_COMPLETE, 1539 notes = "", 1540 method = "wrap", 1541 args = {ByteBuffer[].class, ByteBuffer.class} 1542 ) 1543 public void test_wrap_ByteBuffer$ByteBuffer_02() { 1544 String host = "new host"; 1545 int port = 8080; 1546 ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer(); 1547 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 1548 SSLEngine sse = getEngine(host, port); 1549 sse.setUseClientMode(true); 1550 1551 try { 1552 sse.wrap(bbA, bb); 1553 fail("ReadOnlyBufferException wasn't thrown"); 1554 } catch (ReadOnlyBufferException iobe) { 1555 //expected 1556 } catch (Exception e) { 1557 fail(e + " was thrown instead of ReadOnlyBufferException"); 1558 } 1559 } 1560 1561 /** 1562 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1563 * IllegalArgumentException should be thrown. 1564 */ 1565 @TestTargetNew( 1566 level = TestLevel.PARTIAL_COMPLETE, 1567 notes = "", 1568 method = "wrap", 1569 args = {ByteBuffer[].class, ByteBuffer.class} 1570 ) 1571 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1572 public void test_wrap_ByteBuffer$ByteBuffer_03() { 1573 String host = "new host"; 1574 int port = 8080; 1575 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 1576 ByteBuffer[] bbAN = null; 1577 ByteBuffer bb = ByteBuffer.allocate(10); 1578 ByteBuffer bN = null; 1579 SSLEngine sse = getEngine(host, port); 1580 sse.setUseClientMode(true); 1581 1582 try { 1583 sse.wrap(bbA, bN); 1584 fail("IllegalArgumentException wasn't thrown"); 1585 } catch (IllegalArgumentException iobe) { 1586 //expected 1587 } catch (NullPointerException npe) { 1588 } catch (Exception e) { 1589 fail(e + " was thrown instead of IllegalArgumentException"); 1590 } 1591 1592 try { 1593 sse.wrap(bbAN, bb); 1594 fail("IllegalArgumentException wasn't thrown"); 1595 } catch (IllegalArgumentException iobe) { 1596 //expected 1597 } catch (NullPointerException npe) { 1598 } catch (Exception e) { 1599 fail(e + " was thrown instead of IllegalArgumentException"); 1600 } 1601 1602 try { 1603 sse.wrap(bbAN, bN); 1604 fail("IllegalArgumentException wasn't thrown"); 1605 } catch (IllegalArgumentException iobe) { 1606 //expected 1607 } catch (NullPointerException npe) { 1608 } catch (Exception e) { 1609 fail(e + " was thrown instead of IllegalArgumentException"); 1610 } 1611 } 1612 1613 /** 1614 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1615 * IllegalStateException should be thrown. 1616 */ 1617 @TestTargetNew( 1618 level = TestLevel.PARTIAL_COMPLETE, 1619 notes = "", 1620 method = "wrap", 1621 args = {ByteBuffer[].class, ByteBuffer.class} 1622 ) 1623 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 1624 public void test_wrap_ByteBuffer$ByteBuffer_04() { 1625 String host = "new host"; 1626 int port = 8080; 1627 ByteBuffer bb = ByteBuffer.allocate(10); 1628 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) }; 1629 SSLEngine sse = getEngine(host, port); 1630 1631 try { 1632 sse.wrap(bbA, bb); 1633 fail("IllegalStateException wasn't thrown"); 1634 } catch (IllegalStateException iobe) { 1635 //expected 1636 } catch (Exception e) { 1637 fail(e + " was thrown instead of IllegalStateException"); 1638 } 1639 } 1640 1641 /** 1642 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1643 */ 1644 @TestTargetNew( 1645 level = TestLevel.PARTIAL_COMPLETE, 1646 notes = "", 1647 method = "wrap", 1648 args = {ByteBuffer[].class, ByteBuffer.class} 1649 ) 1650 public void test_wrap_ByteBuffer$ByteBuffer_05() { 1651 String host = "new host"; 1652 int port = 8080; 1653 ByteBuffer bb = ByteBuffer.allocate(10); 1654 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) }; 1655 SSLEngine sse = getEngine(host, port); 1656 sse.setUseClientMode(true); 1657 1658 try { 1659 SSLEngineResult res = sse.wrap(bbA, bb); 1660 assertEquals(0, res.bytesConsumed()); 1661 assertEquals(0, res.bytesProduced()); 1662 } catch (Exception ex) { 1663 fail("Unexpected exception: " + ex); 1664 } 1665 } 1666 1667 private SSLEngine getEngine() { 1668 SSLContext context = null; 1669 try { 1670 context = SSLContext.getInstance("TLS"); 1671 context.init(null, null, null); 1672 } catch (KeyManagementException e) { 1673 fail("Could not get SSLEngine: key management exception " 1674 + e.getMessage()); 1675 } catch (NoSuchAlgorithmException e) { 1676 fail("Could not get SSLEngine: no such algorithm " + e.getMessage()); 1677 } 1678 return context.createSSLEngine(); 1679 } 1680 1681 private SSLEngine getEngine(String host, int port) { 1682 SSLContext context = null; 1683 try { 1684 context = SSLContext.getInstance("TLS"); 1685 context.init(null, null, null); 1686 } catch (KeyManagementException e) { 1687 fail("Could not get SSLEngine: key management exception " 1688 + e.getMessage()); 1689 } catch (NoSuchAlgorithmException e) { 1690 fail("Could not get SSLEngine: no such algorithm " + e.getMessage()); 1691 } 1692 return context.createSSLEngine(host, port); 1693 } 1694 1695 class HandshakeHandler implements Runnable { 1696 1697 private final SSLEngine engine; 1698 1699 private final SourceChannel in; 1700 1701 private final SinkChannel out; 1702 1703 private final ByteBuffer EMPTY = ByteBuffer.allocate(0); 1704 1705 @SuppressWarnings("unused") 1706 private final String LOGTAG; 1707 1708 private SSLEngineResult.HandshakeStatus status; 1709 1710 private ByteBuffer readBuffer; 1711 1712 private ByteBuffer writeBuffer; 1713 1714 HandshakeHandler(boolean clientMode, SourceChannel in, SinkChannel out) 1715 throws SSLException { 1716 this.in = in; 1717 this.out = out; 1718 engine = getEngine(); 1719 engine.setUseClientMode(clientMode); 1720 String[] cipherSuites = engine.getSupportedCipherSuites(); 1721 Set<String> enabledSuites = new HashSet<String>(); 1722 for (String cipherSuite : cipherSuites) { 1723 if (cipherSuite.contains("anon")) { 1724 enabledSuites.add(cipherSuite); 1725 } 1726 } 1727 engine.setEnabledCipherSuites((String[]) enabledSuites.toArray( 1728 new String[enabledSuites.size()])); 1729 1730 engine.beginHandshake(); 1731 status = engine.getHandshakeStatus(); 1732 1733 if (clientMode) { 1734 LOGTAG = "CLIENT: "; 1735 } else { 1736 LOGTAG = "SERVER: "; 1737 } 1738 1739 log("CipherSuites: " + Arrays.toString(engine.getEnabledCipherSuites())); 1740 log(status); 1741 1742 readBuffer = ByteBuffer.allocate(200000); 1743 writeBuffer = ByteBuffer.allocate(20000); 1744 } 1745 1746 public SSLEngineResult.HandshakeStatus getStatus() { 1747 return status; 1748 } 1749 1750 private void log(Object o) { 1751 //System.out.print(LOGTAG); 1752 //System.out.println(o); 1753 } 1754 1755 private ByteBuffer read() throws IOException { 1756 if (readBuffer == null || readBuffer.remaining() == 0 || readBuffer.position() == 0) { 1757 readBuffer.clear(); 1758 int read = in.read(readBuffer); 1759 log("read: " + read); 1760 readBuffer.rewind(); 1761 readBuffer.limit(read); 1762 } 1763 return readBuffer; 1764 } 1765 1766 public void run() { 1767 try { 1768 while (true) { 1769 switch (status) { 1770 case FINISHED: { 1771 log(status); 1772 return; 1773 } 1774 case NEED_TASK: { 1775 log(status); 1776 Runnable task; 1777 while ((task = engine.getDelegatedTask()) != null) { 1778 task.run(); 1779 } 1780 status = engine.getHandshakeStatus(); 1781 break; 1782 } 1783 case NEED_UNWRAP: { 1784 log(status); 1785 ByteBuffer source = read(); 1786 writeBuffer.clear(); 1787 1788 while (status == HandshakeStatus.NEED_UNWRAP) { 1789 SSLEngineResult result = engine.unwrap(source, writeBuffer); 1790 status = result.getHandshakeStatus(); 1791 log(result); 1792 } 1793 break; 1794 } 1795 case NEED_WRAP: { 1796 log(status); 1797 writeBuffer.clear(); 1798 1799 int produced = 0; 1800 SSLEngineResult result = null; 1801 while (status == HandshakeStatus.NEED_WRAP) { 1802 result = engine.wrap(EMPTY, writeBuffer); 1803 status = result.getHandshakeStatus(); 1804 produced += result.bytesProduced(); 1805 log(result); 1806 } 1807 writeBuffer.rewind(); 1808 writeBuffer.limit(produced); 1809 log("write: " + produced); 1810 out.write(writeBuffer); 1811 break; 1812 } 1813 case NOT_HANDSHAKING: { 1814 log("Not Handshaking"); 1815 return; 1816 } 1817 } 1818 } 1819 } catch (IOException e) { 1820 log(e); 1821 } catch (RuntimeException e) { 1822 // ignore; 1823 } 1824 } 1825 } 1826 1827 @TestTargets({ 1828 @TestTargetNew( 1829 level = TestLevel.PARTIAL_COMPLETE, 1830 notes = "", 1831 method = "wrap", 1832 args = {ByteBuffer.class, ByteBuffer.class} 1833 ), 1834 @TestTargetNew( 1835 level = TestLevel.PARTIAL_COMPLETE, 1836 notes = "", 1837 method = "unwrap", 1838 args = {ByteBuffer.class, ByteBuffer.class} 1839 ), 1840 @TestTargetNew( 1841 level = TestLevel.PARTIAL_COMPLETE, 1842 notes = "", 1843 method = "beginHandshake", 1844 args = {} 1845 ), 1846 @TestTargetNew( 1847 level = TestLevel.PARTIAL_COMPLETE, 1848 notes = "", 1849 method = "getHandshakeStatus", 1850 args = {} 1851 ), 1852 @TestTargetNew( 1853 level = TestLevel.PARTIAL_COMPLETE, 1854 notes = "", 1855 method = "wrap", 1856 args = {ByteBuffer[].class, ByteBuffer.class} 1857 ), 1858 @TestTargetNew( 1859 level = TestLevel.PARTIAL_COMPLETE, 1860 notes = "", 1861 method = "getDelegatedTask", 1862 args = {} 1863 ) 1864 }) 1865 @KnownFailure("Handshake Status is never finished. NPE in " 1866 + "ClientSessionContext$HostAndPort.hashCode() when host is null") 1867 public void testHandshake() throws IOException, InterruptedException { 1868 1869 prepareEngines(); 1870 1871 assertTrue("handshake failed", doHandshake()); 1872 1873 System.out.println(clientEngine.engine.getSession().getCipherSuite()); 1874 1875 assertEquals("Handshake not finished", 1876 SSLEngineResult.HandshakeStatus.FINISHED, 1877 clientEngine.getStatus()); 1878 assertEquals("Handshake not finished", 1879 SSLEngineResult.HandshakeStatus.FINISHED, 1880 serverEngine.getStatus()); 1881 } 1882 1883 void prepareEngines() throws IOException { 1884 Pipe clientSendPipe = Pipe.open(); 1885 Pipe serverSendPipe = Pipe.open(); 1886 1887 SinkChannel clientSink = clientSendPipe.sink(); 1888 SourceChannel serverSource = clientSendPipe.source(); 1889 SinkChannel serverSink = serverSendPipe.sink(); 1890 SourceChannel clientSource = serverSendPipe.source(); 1891 1892 clientEngine = new HandshakeHandler(true, clientSource, clientSink); 1893 serverEngine = new HandshakeHandler(false, serverSource, serverSink); 1894 } 1895 1896 boolean doHandshake() throws InterruptedException { 1897 Thread clientThread = new Thread(clientEngine); 1898 clientThread.start(); 1899 1900 Thread serverThread = new Thread(serverEngine); 1901 serverThread.start(); 1902 1903 int i = 0; 1904 while (clientThread.isAlive() && serverThread.isAlive() && i < 20) { 1905 Thread.sleep(500); 1906 i++; 1907 } 1908 1909 if (clientThread.isAlive()) { 1910 clientThread.interrupt(); 1911 } 1912 1913 if (serverThread.isAlive()) { 1914 serverThread.interrupt(); 1915 } 1916 1917 return clientEngine.getStatus() == HandshakeStatus.FINISHED && serverEngine.getStatus() == HandshakeStatus.FINISHED; 1918 } 1919 1920} 1921