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