SSLEngineTest.java revision ceacc61d16fe3aec9c805de525a79f547e607a27
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 public void test_wrap_01() throws IOException, InterruptedException { 768 prepareEngines(); 769 doHandshake(); 770 771 ByteBuffer bbs = ByteBuffer.allocate(100); 772 ByteBuffer bbd = ByteBuffer.allocate(20000); 773 774 try { 775 @SuppressWarnings("unused") 776 SSLEngineResult result = clientEngine.engine.wrap(new ByteBuffer[] { bbs }, 0, 1, bbd); 777 fail("SSLException wasn't thrown"); 778 } catch (SSLException ex) { 779 //expected 780 } 781 } 782 783 /** 784 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 785 * int length, ByteBuffer dst) 786 * Exception case: IndexOutOfBoundsException should be thrown. 787 */ 788 @TestTargetNew( 789 level = TestLevel.PARTIAL_COMPLETE, 790 notes = "", 791 method = "wrap", 792 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 793 ) 794 @KnownFailure("Fixed in DonutBurger, boundary checks missing") 795 public void test_wrap_02() throws SSLException { 796 String host = "new host"; 797 int port = 8080; 798 ByteBuffer bb = ByteBuffer.allocate(10); 799 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 800 SSLEngine sse = getEngine(host, port); 801 sse.setUseClientMode(true); 802 803 try { 804 sse.wrap(bbA, -1, 3, bb); 805 fail("IndexOutOfBoundsException wasn't thrown"); 806 } catch (IndexOutOfBoundsException iobe) { 807 //expected 808 } 809 try { 810 sse.wrap(bbA, 0, -3, bb); 811 fail("IndexOutOfBoundsException wasn't thrown"); 812 } catch (IndexOutOfBoundsException iobe) { 813 //expected 814 } 815 try { 816 sse.wrap(bbA, bbA.length + 1, bbA.length, bb); 817 fail("IndexOutOfBoundsException wasn't thrown"); 818 } catch (IndexOutOfBoundsException iobe) { 819 //expected 820 } 821 try { 822 sse.wrap(bbA, 0, bbA.length + 1, bb); 823 fail("IndexOutOfBoundsException wasn't thrown"); 824 } catch (IndexOutOfBoundsException iobe) { 825 //expected 826 } 827 } 828 829 /** 830 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 831 * int length, ByteBuffer dst) 832 * Exception case: ReadOnlyBufferException should be thrown. 833 */ 834 @TestTargetNew( 835 level = TestLevel.PARTIAL_COMPLETE, 836 notes = "", 837 method = "wrap", 838 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 839 ) 840 public void test_wrap_03() throws SSLException { 841 String host = "new host"; 842 int port = 8080; 843 ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer(); 844 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 845 SSLEngine sse = getEngine(host, port); 846 sse.setUseClientMode(true); 847 848 try { 849 sse.wrap(bbA, 0, bbA.length, bb); 850 fail("ReadOnlyBufferException wasn't thrown"); 851 } catch (ReadOnlyBufferException iobe) { 852 //expected 853 } 854 } 855 856 /** 857 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 858 * int length, ByteBuffer dst) 859 * Exception case: IllegalArgumentException should be thrown. 860 */ 861 @TestTargetNew( 862 level = TestLevel.PARTIAL_COMPLETE, 863 notes = "IllegalArgumentException must be thrown", 864 method = "wrap", 865 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 866 ) 867 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 868 public void test_wrap_04() { 869 String host = "new host"; 870 int port = 8080; 871 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 872 ByteBuffer[] bbN = null; 873 ByteBuffer bN = null; 874 SSLEngine e = getEngine(host, port); 875 e.setUseClientMode(true); 876 877 try { 878 e.wrap(bbA, 0, 3, bN); 879 fail("IllegalArgumentException must be thrown for null srcs byte buffer array"); 880 } catch (NullPointerException npe) { 881 } catch (IllegalArgumentException ex) { 882 } catch (Exception ex) { 883 fail(ex + " was thrown instead of IllegalArgumentException"); 884 } 885 886 try { 887 e.wrap(bbN, 0, 0, bN); 888 fail("IllegalArgumentException wasn't thrown"); 889 } catch (IllegalArgumentException ex) { 890 } catch (NullPointerException npe) { 891 } catch (Exception ex) { 892 fail(ex + " was thrown instead of IllegalArgumentException"); 893 } 894 } 895 896 /** 897 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 898 * int length, ByteBuffer dst) 899 * Exception case: IllegalStateException should be thrown. 900 */ 901 @TestTargetNew( 902 level = TestLevel.PARTIAL_COMPLETE, 903 notes = "", 904 method = "wrap", 905 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 906 ) 907 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 908 public void test_wrap_05() throws SSLException { 909 String host = "new host"; 910 int port = 8080; 911 ByteBuffer bb = ByteBuffer.allocate(10); 912 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 913 SSLEngine sse = getEngine(host, port); 914 915 try { 916 sse.wrap(bbA, 0, bbA.length, bb); 917 fail("IllegalStateException wasn't thrown"); 918 } catch (IllegalStateException iobe) { 919 //expected 920 } 921 } 922 923 /** 924 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, int offset, 925 * int length, ByteBuffer dst) 926 */ 927 @TestTargetNew( 928 level = TestLevel.PARTIAL_COMPLETE, 929 notes = "", 930 method = "wrap", 931 args = {ByteBuffer[].class, int.class, int.class, ByteBuffer.class} 932 ) 933 public void test_wrap_06() { 934 String host = "new host"; 935 int port = 8080; 936 ByteBuffer bb = ByteBuffer.allocate(10); 937 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 938 SSLEngine sse = getEngine(host, port); 939 sse.setUseClientMode(true); 940 941 try { 942 sse.wrap(bbA, 0, bbA.length, bb); 943 } catch (Exception ex) { 944 fail("Unexpected exception: " + ex); 945 } 946 } 947 948 /** 949 * @throws NoSuchAlgorithmException 950 * @tests javax.net.ssl.SSLEngine#closeOutbound() 951 * @tests javax.net.ssl.SSLEngine#isOutboundDone() 952 */ 953 @TestTargets({ 954 @TestTargetNew( 955 level = TestLevel.COMPLETE, 956 notes = "", 957 method = "closeOutbound", 958 args = {} 959 ), 960 @TestTargetNew( 961 level = TestLevel.COMPLETE, 962 notes = "", 963 method = "isOutboundDone", 964 args = {} 965 ) 966 }) 967 public void test_closeOutbound() throws NoSuchAlgorithmException { 968 SSLEngine sse = getEngine(); 969 970 try { 971 assertFalse(sse.isOutboundDone()); 972 sse.closeOutbound(); 973 assertTrue(sse.isOutboundDone()); 974 } catch (Exception ex) { 975 fail("Unexpected exception: " + ex); 976 } 977 } 978 979 /** 980 * @throws NoSuchAlgorithmException 981 * @tests javax.net.ssl.SSLEngine#closeInbound() 982 * @tests javax.net.ssl.SSLEngine#isInboundDone() 983 */ 984 @TestTargets({ 985 @TestTargetNew( 986 level = TestLevel.SUFFICIENT, 987 notes = "", 988 method = "closeInbound", 989 args = {} 990 ), 991 @TestTargetNew( 992 level = TestLevel.COMPLETE, 993 notes = "", 994 method = "isInboundDone", 995 args = {} 996 ) 997 }) 998 public void test_closeInbound() throws NoSuchAlgorithmException { 999 SSLEngine sse = getEngine(); 1000 1001 try { 1002 assertFalse(sse.isInboundDone()); 1003 sse.closeInbound(); 1004 assertTrue(sse.isInboundDone()); 1005 } catch (Exception ex) { 1006 fail("Unexpected exception: " + ex); 1007 } 1008 } 1009 1010 /** 1011 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1012 * SSLException should be thrown. 1013 */ 1014 @TestTargetNew( 1015 level = TestLevel.PARTIAL_COMPLETE, 1016 notes = "", 1017 method = "unwrap", 1018 args = {ByteBuffer.class, ByteBuffer.class} 1019 ) 1020 public void test_unwrap_ByteBuffer_ByteBuffer_01() throws InterruptedException, IOException { 1021 prepareEngines(); 1022 doHandshake(); 1023 ByteBuffer bbs = ByteBuffer.allocate(100); 1024 ByteBuffer bbd = ByteBuffer.allocate(100); 1025 1026 try { 1027 SSLEngineResult unwrap = clientEngine.engine.unwrap(bbs, bbd); 1028 fail("SSLException wasn't thrown"); 1029 } catch (SSLException ex) { 1030 //expected 1031 } 1032 } 1033 1034 /** 1035 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1036 * ReadOnlyBufferException should be thrown. 1037 */ 1038 @TestTargetNew( 1039 level = TestLevel.PARTIAL_COMPLETE, 1040 notes = "", 1041 method = "unwrap", 1042 args = {ByteBuffer.class, ByteBuffer.class} 1043 ) 1044 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1045 public void test_unwrap_ByteBuffer_ByteBuffer_02() { 1046 String host = "new host"; 1047 int port = 8080; 1048 ByteBuffer bbs = ByteBuffer.allocate(10); 1049 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer(); 1050 SSLEngine sse = getEngine(host, port); 1051 sse.setUseClientMode(true); 1052 1053 try { 1054 sse.unwrap(bbs, bbd); 1055 fail("ReadOnlyBufferException wasn't thrown"); 1056 } catch (ReadOnlyBufferException iobe) { 1057 //expected 1058 } catch (Exception e) { 1059 fail(e + " was thrown instead of ReadOnlyBufferException"); 1060 } 1061 } 1062 1063 /** 1064 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1065 * IllegalArgumentException should be thrown. 1066 */ 1067 @TestTargetNew( 1068 level = TestLevel.PARTIAL_COMPLETE, 1069 notes = "", 1070 method = "unwrap", 1071 args = {ByteBuffer.class, ByteBuffer.class} 1072 ) 1073 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1074 public void test_unwrap_ByteBuffer_ByteBuffer_03() { 1075 String host = "new host"; 1076 int port = 8080; 1077 ByteBuffer bbsN = null; 1078 ByteBuffer bbdN = null; 1079 ByteBuffer bbs = ByteBuffer.allocate(10); 1080 ByteBuffer bbd = ByteBuffer.allocate(100); 1081 SSLEngine sse = getEngine(host, port); 1082 sse.setUseClientMode(true); 1083 1084 try { 1085 sse.unwrap(bbsN, bbd); 1086 fail("IllegalArgumentException wasn't thrown"); 1087 } catch (IllegalArgumentException iae) { 1088 //expected 1089 } catch (NullPointerException npe) { 1090 } catch (Exception e) { 1091 fail(e + " was thrown instead of IllegalArgumentException"); 1092 } 1093 1094 try { 1095 sse.unwrap(bbs, bbdN); 1096 fail("IllegalArgumentException wasn't thrown"); 1097 } catch (IllegalArgumentException iae) { 1098 //expected 1099 } catch (NullPointerException npe) { 1100 } catch (Exception e) { 1101 fail(e + " was thrown instead of IllegalArgumentException"); 1102 } 1103 1104 try { 1105 sse.unwrap(bbsN, bbdN); 1106 fail("IllegalArgumentException wasn't thrown"); 1107 } catch (IllegalArgumentException iae) { 1108 //expected 1109 } catch (NullPointerException npe) { 1110 } catch (Exception e) { 1111 fail(e + " was thrown instead of IllegalArgumentException"); 1112 } 1113 } 1114 1115 /** 1116 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1117 * IllegalStateException should be thrown. 1118 */ 1119 @TestTargetNew( 1120 level = TestLevel.PARTIAL_COMPLETE, 1121 notes = "", 1122 method = "unwrap", 1123 args = {ByteBuffer.class, ByteBuffer.class} 1124 ) 1125 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 1126 public void test_unwrap_ByteBuffer_ByteBuffer_04() { 1127 String host = "new host"; 1128 int port = 8080; 1129 ByteBuffer bbs = ByteBuffer.allocate(10); 1130 ByteBuffer bbd = ByteBuffer.allocate(100); 1131 SSLEngine sse = getEngine(host, port); 1132 1133 try { 1134 sse.unwrap(bbs, bbd); 1135 fail("IllegalStateException wasn't thrown"); 1136 } catch (IllegalStateException iobe) { 1137 //expected 1138 } catch (Exception e) { 1139 fail(e + " was thrown instead of IllegalStateException"); 1140 } 1141 } 1142 1143 /** 1144 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer dst) 1145 */ 1146 @TestTargetNew( 1147 level = TestLevel.PARTIAL_COMPLETE, 1148 notes = "", 1149 method = "unwrap", 1150 args = {ByteBuffer.class, ByteBuffer.class} 1151 ) 1152 public void test_unwrap_ByteBuffer_ByteBuffer_05() { 1153 String host = "new host"; 1154 int port = 8080; 1155 ByteBuffer bbs = ByteBuffer.allocate(10); 1156 ByteBuffer bbd = ByteBuffer.allocate(100); 1157 SSLEngine sse = getEngine(host, port); 1158 sse.setUseClientMode(true); 1159 1160 try { 1161 SSLEngineResult res = sse.unwrap(bbs, bbd); 1162 assertEquals(0, res.bytesConsumed()); 1163 assertEquals(0, res.bytesProduced()); 1164 } catch (Exception e) { 1165 fail("Unexpected exception: " + e); 1166 } 1167 } 1168 1169 /** 1170 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1171 * SSLException should be thrown. 1172 */ 1173 @TestTargetNew( 1174 level = TestLevel.PARTIAL_COMPLETE, 1175 notes = "", 1176 method = "unwrap", 1177 args = {ByteBuffer.class, ByteBuffer[].class} 1178 ) 1179 public void test_unwrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException { 1180 prepareEngines(); 1181 doHandshake(); 1182 1183 ByteBuffer bbs = ByteBuffer.allocate(100); 1184 ByteBuffer bbd = ByteBuffer.allocate(100); 1185 1186 try { 1187 clientEngine.engine.unwrap(bbs, new ByteBuffer[] { bbd }); 1188 fail("SSLException wasn't thrown"); 1189 } catch (SSLException ex) { 1190 //expected 1191 } 1192 } 1193 1194 /** 1195 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1196 * ReadOnlyBufferException should be thrown. 1197 */ 1198 @TestTargetNew( 1199 level = TestLevel.PARTIAL_COMPLETE, 1200 notes = "", 1201 method = "unwrap", 1202 args = {ByteBuffer.class, ByteBuffer[].class} 1203 ) 1204 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1205 public void test_unwrap_ByteBuffer$ByteBuffer_02() { 1206 String host = "new host"; 1207 int port = 8080; 1208 ByteBuffer bbs = ByteBuffer.allocate(10); 1209 ByteBuffer bbR = ByteBuffer.allocate(100).asReadOnlyBuffer(); 1210 ByteBuffer[] bbA = { bbR, ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 1211 SSLEngine sse = getEngine(host, port); 1212 sse.setUseClientMode(true); 1213 1214 try { 1215 sse.unwrap(bbs, bbA); 1216 fail("ReadOnlyBufferException wasn't thrown"); 1217 } catch (ReadOnlyBufferException iobe) { 1218 //expected 1219 } catch (Exception e) { 1220 fail(e + " was thrown instead of ReadOnlyBufferException"); 1221 } 1222 } 1223 1224 /** 1225 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1226 * IllegalArgumentException should be thrown. 1227 */ 1228 @TestTargetNew( 1229 level = TestLevel.PARTIAL_COMPLETE, 1230 notes = "", 1231 method = "unwrap", 1232 args = {ByteBuffer.class, ByteBuffer[].class} 1233 ) 1234 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1235 public void test_unwrap_ByteBuffer$ByteBuffer_03() { 1236 String host = "new host"; 1237 int port = 8080; 1238 ByteBuffer[] bbA = { ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 1239 ByteBuffer[] bbN = { ByteBuffer.allocate(100), null, ByteBuffer.allocate(100) }; 1240 ByteBuffer[] bbAN = null; 1241 ByteBuffer bb = ByteBuffer.allocate(10); 1242 ByteBuffer bN = null; 1243 SSLEngine sse = getEngine(host, port); 1244 sse.setUseClientMode(true); 1245 1246 try { 1247 sse.unwrap(bN, bbA); 1248 fail("IllegalArgumentException wasn't thrown"); 1249 } catch (IllegalArgumentException iobe) { 1250 //expected 1251 } catch (NullPointerException npe) { 1252 } catch (Exception e) { 1253 fail(e + " was thrown instead of IllegalArgumentException"); 1254 } 1255 1256 try { 1257 sse.unwrap(bb, bbAN); 1258 fail("IllegalArgumentException wasn't thrown"); 1259 } catch (IllegalArgumentException iobe) { 1260 //expected 1261 } catch (NullPointerException npe) { 1262 } catch (Exception e) { 1263 fail(e + " was thrown instead of IllegalArgumentException"); 1264 } 1265 1266 try { 1267 sse.unwrap(bb, bbN); 1268 fail("IllegalArgumentException wasn't thrown"); 1269 } catch (IllegalArgumentException iobe) { 1270 //expected 1271 } catch (NullPointerException npe) { 1272 } catch (Exception e) { 1273 fail(e + " was thrown instead of IllegalArgumentException"); 1274 } 1275 1276 try { 1277 sse.unwrap(bN, bbAN); 1278 fail("IllegalArgumentException wasn't thrown"); 1279 } catch (IllegalArgumentException iobe) { 1280 //expected 1281 } catch (NullPointerException npe) { 1282 } catch (Exception e) { 1283 fail(e + " was thrown instead of IllegalArgumentException"); 1284 } 1285 } 1286 1287 /** 1288 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1289 * IllegalStateException should be thrown. 1290 */ 1291 @TestTargetNew( 1292 level = TestLevel.PARTIAL_COMPLETE, 1293 notes = "", 1294 method = "unwrap", 1295 args = {ByteBuffer.class, ByteBuffer[].class} 1296 ) 1297 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 1298 public void test_unwrap_ByteBuffer$ByteBuffer_04() { 1299 String host = "new host"; 1300 int port = 8080; 1301 ByteBuffer bbs = ByteBuffer.allocate(10); 1302 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 1303 SSLEngine sse = getEngine(host, port); 1304 1305 try { 1306 sse.unwrap(bbs, bbd); 1307 fail("IllegalStateException wasn't thrown"); 1308 } catch (IllegalStateException iobe) { 1309 //expected 1310 } catch (Exception e) { 1311 fail(e + " was thrown instead of IllegalStateException"); 1312 } 1313 } 1314 1315 /** 1316 * @tests javax.net.ssl.SSLEngine#unwrap(ByteBuffer src, ByteBuffer[] dsts) 1317 */ 1318 @TestTargetNew( 1319 level = TestLevel.PARTIAL_COMPLETE, 1320 notes = "", 1321 method = "unwrap", 1322 args = {ByteBuffer.class, ByteBuffer[].class} 1323 ) 1324 public void test_unwrap_ByteBuffer$ByteBuffer_05() { 1325 String host = "new host"; 1326 int port = 8080; 1327 ByteBuffer bbs = ByteBuffer.allocate(10); 1328 ByteBuffer[] bbd = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100) }; 1329 SSLEngine sse = getEngine(host, port); 1330 sse.setUseClientMode(true); 1331 1332 try { 1333 SSLEngineResult res = sse.unwrap(bbs, bbd); 1334 assertEquals(0, res.bytesConsumed()); 1335 assertEquals(0, res.bytesProduced()); 1336 } catch (Exception ex) { 1337 fail("Unexpected exception: " + ex); 1338 } 1339 } 1340 1341 /** 1342 * @throws IOException 1343 * @throws InterruptedException 1344 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1345 * SSLException should be thrown. 1346 */ 1347 public void test_wrap_ByteBuffer_ByteBuffer_01() throws IOException, InterruptedException { 1348 prepareEngines(); 1349 doHandshake(); 1350 ByteBuffer bbs = ByteBuffer.allocate(20); 1351 ByteBuffer bbd = ByteBuffer.allocate(20000); 1352 1353 try { 1354 clientEngine.engine.wrap(bbs, bbd); 1355 fail("SSLException wasn't thrown"); 1356 } catch (SSLException ex) { 1357 //expected 1358 } 1359 } 1360 1361 /** 1362 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1363 * ReadOnlyBufferException should be thrown. 1364 */ 1365 @TestTargetNew( 1366 level = TestLevel.PARTIAL_COMPLETE, 1367 notes = "", 1368 method = "wrap", 1369 args = {ByteBuffer.class, ByteBuffer.class} 1370 ) 1371 public void test_wrap_ByteBuffer_ByteBuffer_02() { 1372 String host = "new host"; 1373 int port = 8080; 1374 ByteBuffer bbs = ByteBuffer.allocate(10); 1375 ByteBuffer bbd = ByteBuffer.allocate(100).asReadOnlyBuffer(); 1376 SSLEngine sse = getEngine(host, port); 1377 sse.setUseClientMode(true); 1378 1379 try { 1380 sse.wrap(bbs, bbd); 1381 fail("ReadOnlyBufferException wasn't thrown"); 1382 } catch (ReadOnlyBufferException iobe) { 1383 //expected 1384 } catch (Exception e) { 1385 fail(e + " was thrown instead of ReadOnlyBufferException"); 1386 } 1387 } 1388 1389 /** 1390 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1391 * IllegalArgumentException should be thrown. 1392 */ 1393 @TestTargetNew( 1394 level = TestLevel.PARTIAL_COMPLETE, 1395 notes = "", 1396 method = "wrap", 1397 args = {ByteBuffer.class, ByteBuffer.class} 1398 ) 1399 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1400 public void test_wrap_ByteBuffer_ByteBuffer_03() { 1401 String host = "new host"; 1402 int port = 8080; 1403 ByteBuffer bbsN = null; 1404 ByteBuffer bbdN = null; 1405 ByteBuffer bbs = ByteBuffer.allocate(10); 1406 ByteBuffer bbd = ByteBuffer.allocate(100); 1407 SSLEngine sse = getEngine(host, port); 1408 sse.setUseClientMode(true); 1409 1410 try { 1411 sse.wrap(bbsN, bbd); 1412 fail("IllegalArgumentException wasn't thrown"); 1413 } catch (IllegalArgumentException iae) { 1414 //expected 1415 } catch (NullPointerException npe) { 1416 } catch (Exception e) { 1417 fail(e + " was thrown instead of IllegalArgumentException"); 1418 } 1419 1420 try { 1421 sse.wrap(bbs, bbdN); 1422 fail("IllegalArgumentException wasn't thrown"); 1423 } catch (IllegalArgumentException iae) { 1424 //expected 1425 } catch (NullPointerException npe) { 1426 } catch (Exception e) { 1427 fail(e + " was thrown instead of IllegalArgumentException"); 1428 } 1429 1430 try { 1431 sse.wrap(bbsN, bbdN); 1432 fail("IllegalArgumentException wasn't thrown"); 1433 } catch (IllegalArgumentException iae) { 1434 //expected 1435 } catch (NullPointerException npe) { 1436 } catch (Exception e) { 1437 fail(e + " was thrown instead of IllegalArgumentException"); 1438 } 1439 } 1440 1441 /** 1442 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1443 * IllegalStateException should be thrown. 1444 */ 1445 @TestTargetNew( 1446 level = TestLevel.PARTIAL_COMPLETE, 1447 notes = "", 1448 method = "wrap", 1449 args = {ByteBuffer.class, ByteBuffer.class} 1450 ) 1451 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 1452 public void test_wrap_ByteBuffer_ByteBuffer_04() { 1453 String host = "new host"; 1454 int port = 8080; 1455 ByteBuffer bbs = ByteBuffer.allocate(10); 1456 ByteBuffer bbd = ByteBuffer.allocate(10); 1457 SSLEngine sse = getEngine(host, port); 1458 1459 try { 1460 sse.wrap(bbs, bbd); 1461 fail("IllegalStateException wasn't thrown"); 1462 } catch (IllegalStateException iobe) { 1463 //expected 1464 } catch (Exception e) { 1465 fail(e + " was thrown instead of IllegalStateException"); 1466 } 1467 } 1468 1469 /** 1470 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer src, ByteBuffer dst) 1471 */ 1472 @TestTargetNew( 1473 level = TestLevel.PARTIAL_COMPLETE, 1474 notes = "", 1475 method = "wrap", 1476 args = {ByteBuffer.class, ByteBuffer.class} 1477 ) 1478 public void test_wrap_ByteBuffer_ByteBuffer_05() { 1479 String host = "new host"; 1480 int port = 8080; 1481 ByteBuffer bb = ByteBuffer.allocate(10); 1482 SSLEngine sse = getEngine(host, port); 1483 sse.setUseClientMode(true); 1484 1485 try { 1486 SSLEngineResult res = sse.wrap(bb, ByteBuffer.allocate(10)); 1487 assertEquals(0, res.bytesConsumed()); 1488 assertEquals(0, res.bytesProduced()); 1489 } catch (Exception e) { 1490 fail("Unexpected exception: " + e); 1491 } 1492 } 1493 1494 /** 1495 * @throws IOException 1496 * @throws InterruptedException 1497 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1498 * SSLException should be thrown. 1499 */ 1500 @TestTargetNew( 1501 level = TestLevel.PARTIAL_COMPLETE, 1502 notes = "wrap cannot be forced to throw SSLException", 1503 method = "wrap", 1504 args = {ByteBuffer[].class, ByteBuffer.class} 1505 ) 1506 public void test_wrap_ByteBuffer$ByteBuffer_01() throws IOException, InterruptedException { 1507 prepareEngines(); 1508 doHandshake(); 1509 ByteBuffer bbs = ByteBuffer.allocate(100); 1510 ByteBuffer bbd = ByteBuffer.allocate(20000); 1511 1512 try { 1513 clientEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd); 1514 serverEngine.engine.wrap(new ByteBuffer[] { bbs }, bbd); 1515 //fail("SSLException wasn't thrown"); 1516 } catch (SSLException ex) { 1517 //expected 1518 } 1519 } 1520 1521 /** 1522 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1523 * ReadOnlyBufferException should be thrown. 1524 */ 1525 @TestTargetNew( 1526 level = TestLevel.PARTIAL_COMPLETE, 1527 notes = "", 1528 method = "wrap", 1529 args = {ByteBuffer[].class, ByteBuffer.class} 1530 ) 1531 public void test_wrap_ByteBuffer$ByteBuffer_02() { 1532 String host = "new host"; 1533 int port = 8080; 1534 ByteBuffer bb = ByteBuffer.allocate(10).asReadOnlyBuffer(); 1535 ByteBuffer[] bbA = {ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5)}; 1536 SSLEngine sse = getEngine(host, port); 1537 sse.setUseClientMode(true); 1538 1539 try { 1540 sse.wrap(bbA, bb); 1541 fail("ReadOnlyBufferException wasn't thrown"); 1542 } catch (ReadOnlyBufferException iobe) { 1543 //expected 1544 } catch (Exception e) { 1545 fail(e + " was thrown instead of ReadOnlyBufferException"); 1546 } 1547 } 1548 1549 /** 1550 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1551 * IllegalArgumentException should be thrown. 1552 */ 1553 @TestTargetNew( 1554 level = TestLevel.PARTIAL_COMPLETE, 1555 notes = "", 1556 method = "wrap", 1557 args = {ByteBuffer[].class, ByteBuffer.class} 1558 ) 1559 @KnownFailure("Fixed on DonutBurger, Wrong Exception thrown") 1560 public void test_wrap_ByteBuffer$ByteBuffer_03() { 1561 String host = "new host"; 1562 int port = 8080; 1563 ByteBuffer[] bbA = {ByteBuffer.allocate(100), ByteBuffer.allocate(10), ByteBuffer.allocate(100)}; 1564 ByteBuffer[] bbAN = null; 1565 ByteBuffer bb = ByteBuffer.allocate(10); 1566 ByteBuffer bN = null; 1567 SSLEngine sse = getEngine(host, port); 1568 sse.setUseClientMode(true); 1569 1570 try { 1571 sse.wrap(bbA, bN); 1572 fail("IllegalArgumentException wasn't thrown"); 1573 } catch (IllegalArgumentException iobe) { 1574 //expected 1575 } catch (NullPointerException npe) { 1576 } catch (Exception e) { 1577 fail(e + " was thrown instead of IllegalArgumentException"); 1578 } 1579 1580 try { 1581 sse.wrap(bbAN, bb); 1582 fail("IllegalArgumentException wasn't thrown"); 1583 } catch (IllegalArgumentException iobe) { 1584 //expected 1585 } catch (NullPointerException npe) { 1586 } catch (Exception e) { 1587 fail(e + " was thrown instead of IllegalArgumentException"); 1588 } 1589 1590 try { 1591 sse.wrap(bbAN, bN); 1592 fail("IllegalArgumentException wasn't thrown"); 1593 } catch (IllegalArgumentException iobe) { 1594 //expected 1595 } catch (NullPointerException npe) { 1596 } catch (Exception e) { 1597 fail(e + " was thrown instead of IllegalArgumentException"); 1598 } 1599 } 1600 1601 /** 1602 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1603 * IllegalStateException should be thrown. 1604 */ 1605 @TestTargetNew( 1606 level = TestLevel.PARTIAL_COMPLETE, 1607 notes = "", 1608 method = "wrap", 1609 args = {ByteBuffer[].class, ByteBuffer.class} 1610 ) 1611 @AndroidOnly("The RI doesn't throw the IllegalStateException.") 1612 public void test_wrap_ByteBuffer$ByteBuffer_04() { 1613 String host = "new host"; 1614 int port = 8080; 1615 ByteBuffer bb = ByteBuffer.allocate(10); 1616 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) }; 1617 SSLEngine sse = getEngine(host, port); 1618 1619 try { 1620 sse.wrap(bbA, bb); 1621 fail("IllegalStateException wasn't thrown"); 1622 } catch (IllegalStateException iobe) { 1623 //expected 1624 } catch (Exception e) { 1625 fail(e + " was thrown instead of IllegalStateException"); 1626 } 1627 } 1628 1629 /** 1630 * @tests javax.net.ssl.SSLEngine#wrap(ByteBuffer[] srcs, ByteBuffer dst) 1631 */ 1632 @TestTargetNew( 1633 level = TestLevel.PARTIAL_COMPLETE, 1634 notes = "", 1635 method = "wrap", 1636 args = {ByteBuffer[].class, ByteBuffer.class} 1637 ) 1638 public void test_wrap_ByteBuffer$ByteBuffer_05() { 1639 String host = "new host"; 1640 int port = 8080; 1641 ByteBuffer bb = ByteBuffer.allocate(10); 1642 ByteBuffer[] bbA = { ByteBuffer.allocate(5), ByteBuffer.allocate(10), ByteBuffer.allocate(5) }; 1643 SSLEngine sse = getEngine(host, port); 1644 sse.setUseClientMode(true); 1645 1646 try { 1647 SSLEngineResult res = sse.wrap(bbA, bb); 1648 assertEquals(0, res.bytesConsumed()); 1649 assertEquals(0, res.bytesProduced()); 1650 } catch (Exception ex) { 1651 fail("Unexpected exception: " + ex); 1652 } 1653 } 1654 1655 private SSLEngine getEngine() { 1656 SSLContext context = null; 1657 try { 1658 context = SSLContext.getInstance("TLS"); 1659 context.init(null, null, null); 1660 } catch (KeyManagementException e) { 1661 fail("Could not get SSLEngine: key management exception " 1662 + e.getMessage()); 1663 } catch (NoSuchAlgorithmException e) { 1664 fail("Could not get SSLEngine: no such algorithm " + e.getMessage()); 1665 } 1666 return context.createSSLEngine(); 1667 } 1668 1669 private SSLEngine getEngine(String host, int port) { 1670 SSLContext context = null; 1671 try { 1672 context = SSLContext.getInstance("TLS"); 1673 context.init(null, null, null); 1674 } catch (KeyManagementException e) { 1675 fail("Could not get SSLEngine: key management exception " 1676 + e.getMessage()); 1677 } catch (NoSuchAlgorithmException e) { 1678 fail("Could not get SSLEngine: no such algorithm " + e.getMessage()); 1679 } 1680 return context.createSSLEngine(host, port); 1681 } 1682 1683 class HandshakeHandler implements Runnable { 1684 1685 private final SSLEngine engine; 1686 1687 private final SourceChannel in; 1688 1689 private final SinkChannel out; 1690 1691 private final ByteBuffer EMPTY = ByteBuffer.allocate(0); 1692 1693 @SuppressWarnings("unused") 1694 private final String LOGTAG; 1695 1696 private SSLEngineResult.HandshakeStatus status; 1697 1698 private ByteBuffer readBuffer; 1699 1700 private ByteBuffer writeBuffer; 1701 1702 HandshakeHandler(boolean clientMode, SourceChannel in, SinkChannel out) 1703 throws SSLException { 1704 this.in = in; 1705 this.out = out; 1706 engine = getEngine(); 1707 engine.setUseClientMode(clientMode); 1708 String[] cipherSuites = engine.getSupportedCipherSuites(); 1709 Set<String> enabledSuites = new HashSet<String>(); 1710 for (String cipherSuite : cipherSuites) { 1711 if (cipherSuite.contains("anon")) { 1712 enabledSuites.add(cipherSuite); 1713 } 1714 } 1715 engine.setEnabledCipherSuites((String[]) enabledSuites.toArray( 1716 new String[enabledSuites.size()])); 1717 1718 engine.beginHandshake(); 1719 status = engine.getHandshakeStatus(); 1720 1721 if (clientMode) { 1722 LOGTAG = "CLIENT: "; 1723 } else { 1724 LOGTAG = "SERVER: "; 1725 } 1726 1727 log("CipherSuites: " + Arrays.toString(engine.getEnabledCipherSuites())); 1728 log(status); 1729 1730 readBuffer = ByteBuffer.allocate(200000); 1731 writeBuffer = ByteBuffer.allocate(20000); 1732 } 1733 1734 public SSLEngineResult.HandshakeStatus getStatus() { 1735 return status; 1736 } 1737 1738 private void log(Object o) { 1739 //System.out.print(LOGTAG); 1740 //System.out.println(o); 1741 } 1742 1743 private ByteBuffer read() throws IOException { 1744 if (readBuffer == null || readBuffer.remaining() == 0 || readBuffer.position() == 0) { 1745 readBuffer.clear(); 1746 int read = in.read(readBuffer); 1747 log("read: " + read); 1748 readBuffer.rewind(); 1749 readBuffer.limit(read); 1750 } 1751 return readBuffer; 1752 } 1753 1754 public void run() { 1755 try { 1756 while (true) { 1757 switch (status) { 1758 case FINISHED: { 1759 log(status); 1760 return; 1761 } 1762 case NEED_TASK: { 1763 log(status); 1764 Runnable task; 1765 while ((task = engine.getDelegatedTask()) != null) { 1766 task.run(); 1767 } 1768 status = engine.getHandshakeStatus(); 1769 break; 1770 } 1771 case NEED_UNWRAP: { 1772 log(status); 1773 ByteBuffer source = read(); 1774 writeBuffer.clear(); 1775 1776 while (status == HandshakeStatus.NEED_UNWRAP) { 1777 SSLEngineResult result = engine.unwrap(source, writeBuffer); 1778 status = result.getHandshakeStatus(); 1779 log(result); 1780 } 1781 break; 1782 } 1783 case NEED_WRAP: { 1784 log(status); 1785 writeBuffer.clear(); 1786 1787 int produced = 0; 1788 SSLEngineResult result = null; 1789 while (status == HandshakeStatus.NEED_WRAP) { 1790 result = engine.wrap(EMPTY, writeBuffer); 1791 status = result.getHandshakeStatus(); 1792 produced += result.bytesProduced(); 1793 log(result); 1794 } 1795 writeBuffer.rewind(); 1796 writeBuffer.limit(produced); 1797 log("write: " + produced); 1798 out.write(writeBuffer); 1799 break; 1800 } 1801 case NOT_HANDSHAKING: { 1802 log("Not Handshaking"); 1803 return; 1804 } 1805 } 1806 } 1807 } catch (IOException e) { 1808 log(e); 1809 } catch (RuntimeException e) { 1810 // ignore; 1811 } 1812 } 1813 } 1814 1815 @TestTargets({ 1816 @TestTargetNew( 1817 level = TestLevel.PARTIAL_COMPLETE, 1818 notes = "", 1819 method = "wrap", 1820 args = {ByteBuffer.class, ByteBuffer.class} 1821 ), 1822 @TestTargetNew( 1823 level = TestLevel.PARTIAL_COMPLETE, 1824 notes = "", 1825 method = "unwrap", 1826 args = {ByteBuffer.class, ByteBuffer.class} 1827 ), 1828 @TestTargetNew( 1829 level = TestLevel.PARTIAL_COMPLETE, 1830 notes = "", 1831 method = "beginHandshake", 1832 args = {} 1833 ), 1834 @TestTargetNew( 1835 level = TestLevel.PARTIAL_COMPLETE, 1836 notes = "", 1837 method = "getHandshakeStatus", 1838 args = {} 1839 ), 1840 @TestTargetNew( 1841 level = TestLevel.PARTIAL_COMPLETE, 1842 notes = "", 1843 method = "wrap", 1844 args = {ByteBuffer[].class, ByteBuffer.class} 1845 ), 1846 @TestTargetNew( 1847 level = TestLevel.PARTIAL_COMPLETE, 1848 notes = "", 1849 method = "getDelegatedTask", 1850 args = {} 1851 ) 1852 }) 1853 @KnownFailure("Handshake Status is never finished. NPE in " 1854 + "ClientSessionContext$HostAndPort.hashCode() when host is null") 1855 public void testHandshake() throws IOException, InterruptedException { 1856 1857 prepareEngines(); 1858 1859 assertTrue("handshake failed", doHandshake()); 1860 1861 System.out.println(clientEngine.engine.getSession().getCipherSuite()); 1862 1863 assertEquals("Handshake not finished", 1864 SSLEngineResult.HandshakeStatus.FINISHED, 1865 clientEngine.getStatus()); 1866 assertEquals("Handshake not finished", 1867 SSLEngineResult.HandshakeStatus.FINISHED, 1868 serverEngine.getStatus()); 1869 } 1870 1871 void prepareEngines() throws IOException { 1872 Pipe clientSendPipe = Pipe.open(); 1873 Pipe serverSendPipe = Pipe.open(); 1874 1875 SinkChannel clientSink = clientSendPipe.sink(); 1876 SourceChannel serverSource = clientSendPipe.source(); 1877 SinkChannel serverSink = serverSendPipe.sink(); 1878 SourceChannel clientSource = serverSendPipe.source(); 1879 1880 clientEngine = new HandshakeHandler(true, clientSource, clientSink); 1881 serverEngine = new HandshakeHandler(false, serverSource, serverSink); 1882 } 1883 1884 boolean doHandshake() throws InterruptedException { 1885 Thread clientThread = new Thread(clientEngine); 1886 clientThread.start(); 1887 1888 Thread serverThread = new Thread(serverEngine); 1889 serverThread.start(); 1890 1891 int i = 0; 1892 while (clientThread.isAlive() && serverThread.isAlive() && i < 20) { 1893 Thread.sleep(500); 1894 i++; 1895 } 1896 1897 if (clientThread.isAlive()) { 1898 clientThread.interrupt(); 1899 } 1900 1901 if (serverThread.isAlive()) { 1902 serverThread.interrupt(); 1903 } 1904 1905 return clientEngine.getStatus() == HandshakeStatus.FINISHED && serverEngine.getStatus() == HandshakeStatus.FINISHED; 1906 } 1907 1908} 1909