1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.xnet.provider.jsse; 19 20 import java.io.IOException; 21 import java.net.InetAddress; 22 import java.net.InetSocketAddress; 23 import java.net.ServerSocket; 24 import java.net.Socket; 25 import javax.net.ssl.HandshakeCompletedEvent; 26 import javax.net.ssl.HandshakeCompletedListener; 27 import javax.net.ssl.SSLSession; 28 import javax.net.ssl.SSLSocket; 29 30 import junit.framework.Test; 31 import junit.framework.TestCase; 32 import junit.framework.TestSuite; 33 34 /** 35 * SSLSocketImplTest test 36 */ 37 public class SSLSocketImplTest extends TestCase { 38 39 // turn on/off the debug logging 40 private static boolean doLog = false; 41 42 /** 43 * Sets up the test case. 44 */ 45 @Override 46 public void setUp() throws Exception { 47 if (doLog) { 48 System.out.println(""); 49 System.out.println("========================"); 50 System.out.println("====== Running the test: " + getName()); 51 } 52 } 53 54 private SSLSocket createSSLSocket() throws Exception { 55 return new SSLSocketImpl(JSSETestData.getSSLParameters()); 56 } 57 58 private SSLSocket createSSLSocket(int port) throws Exception { 59 return new SSLSocketImpl("localhost", port, 60 JSSETestData.getSSLParameters()); 61 } 62 63 /** 64 * SSLSocketImpl(SSLParameters sslParameters) method testing. 65 */ 66 public void testSSLSocketImpl1() throws Exception { 67 Server server = null; 68 SSLSocket socket = null; 69 try { 70 server = new Server(); 71 72 socket = new SSLSocketImpl(JSSETestData.getSSLParameters()); 73 socket.connect( 74 new InetSocketAddress("localhost", server.getPort())); 75 ((SSLSocketImpl) socket).init(); 76 socket.setUseClientMode(true); 77 78 server.start(); 79 final SSLSocket s = socket; 80 Thread thread = new Thread() { 81 @Override 82 public void run() { 83 try { 84 s.startHandshake(); 85 } catch (Exception e) { 86 } 87 } 88 }; 89 90 thread.start(); 91 92 int timeout = 10; // wait no more than 10*500 ms for handshake 93 while (!server.handshakeStarted()) { 94 // wait for handshake start 95 try { 96 Thread.sleep(500); 97 } catch (Exception e) { 98 } 99 timeout--; 100 if (timeout < 0) { 101 try { 102 server.close(); 103 } catch (IOException ex) { 104 } 105 try { 106 socket.close(); 107 } catch (IOException ex) { 108 } 109 fail("Handshake was not started"); 110 } 111 } 112 } finally { 113 if (server != null) { 114 try { 115 server.close(); 116 } catch (IOException ex) { 117 } 118 } 119 if (socket != null) { 120 try { 121 socket.close(); 122 } catch (IOException ex) { 123 } 124 } 125 } 126 } 127 128 /** 129 * SSLSocketImpl(String host, int port, SSLParameters sslParameters) method 130 * testing. 131 */ 132 public void testSSLSocketImpl2() throws Exception { 133 Server server = null; 134 SSLSocket socket = null; 135 try { 136 server = new Server(); 137 138 socket = new SSLSocketImpl("localhost", 139 server.getPort(), JSSETestData.getSSLParameters()); 140 socket.setUseClientMode(true); 141 142 server.start(); 143 final SSLSocket s = socket; 144 Thread thread = new Thread() { 145 @Override 146 public void run() { 147 try { 148 s.startHandshake(); 149 } catch (Exception e) { 150 } 151 } 152 }; 153 154 thread.start(); 155 156 int timeout = 10; // wait no more than 5 seconds for handshake 157 while (!server.handshakeStarted()) { 158 // wait for handshake start 159 try { 160 Thread.sleep(500); 161 } catch (Exception e) { 162 } 163 timeout--; 164 if (timeout < 0) { 165 try { 166 server.close(); 167 } catch (IOException ex) { 168 } 169 try { 170 socket.close(); 171 } catch (IOException ex) { 172 } 173 fail("Handshake was not started"); 174 } 175 } 176 } finally { 177 if (server != null) { 178 try { 179 server.close(); 180 } catch (IOException ex) { 181 } 182 } 183 if (socket != null) { 184 try { 185 socket.close(); 186 } catch (IOException ex) { 187 } 188 } 189 } 190 } 191 192 /** 193 * SSLSocketImpl(String host, int port, InetAddress localHost, int 194 * localPort, SSLParameters sslParameters) method testing. 195 */ 196 public void testSSLSocketImpl3() throws Exception { 197 Server server = null; 198 SSLSocket socket = null; 199 try { 200 server = new Server(); 201 202 socket = new SSLSocketImpl( 203 "localhost", 204 server.getPort(), 205 InetAddress.getByName("localhost"), 206 0, JSSETestData.getSSLParameters()); 207 socket.setUseClientMode(true); 208 209 server.start(); 210 final SSLSocket s = socket; 211 Thread thread = new Thread() { 212 @Override 213 public void run() { 214 try { 215 s.startHandshake(); 216 } catch (Exception e) { 217 } 218 } 219 }; 220 221 thread.start(); 222 223 int timeout = 10; // wait no more than 5 seconds for handshake 224 while (!server.handshakeStarted()) { 225 // wait for handshake start 226 try { 227 Thread.sleep(500); 228 } catch (Exception e) { 229 } 230 timeout--; 231 if (timeout < 0) { 232 try { 233 server.close(); 234 } catch (IOException ex) { 235 } 236 try { 237 socket.close(); 238 } catch (IOException ex) { 239 } 240 fail("Handshake was not started"); 241 } 242 } 243 } finally { 244 if (server != null) { 245 try { 246 server.close(); 247 } catch (IOException ex) { 248 } 249 } 250 if (socket != null) { 251 try { 252 socket.close(); 253 } catch (IOException ex) { 254 } 255 } 256 } 257 } 258 259 /** 260 * SSLSocketImpl(InetAddress host, int port, SSLParameters sslParameters) 261 * method testing. 262 */ 263 public void testSSLSocketImpl4() throws Exception { 264 Server server = null; 265 SSLSocket socket = null; 266 try { 267 server = new Server(); 268 269 socket = new SSLSocketImpl( 270 InetAddress.getByName("localhost"), 271 server.getPort(), 272 JSSETestData.getSSLParameters()); 273 socket.setUseClientMode(true); 274 275 server.start(); 276 final SSLSocket s = socket; 277 Thread thread = new Thread() { 278 @Override 279 public void run() { 280 try { 281 s.startHandshake(); 282 } catch (Exception e) { 283 } 284 } 285 }; 286 287 thread.start(); 288 289 int timeout = 10; // wait no more than 5 seconds for handshake 290 while (!server.handshakeStarted()) { 291 // wait for handshake start 292 try { 293 Thread.sleep(500); 294 } catch (Exception e) { 295 } 296 timeout--; 297 if (timeout < 0) { 298 try { 299 server.close(); 300 } catch (IOException ex) { 301 } 302 try { 303 socket.close(); 304 } catch (IOException ex) { 305 } 306 fail("Handshake was not started"); 307 } 308 } 309 } finally { 310 if (server != null) { 311 try { 312 server.close(); 313 } catch (IOException ex) { 314 } 315 } 316 if (socket != null) { 317 try { 318 socket.close(); 319 } catch (IOException ex) { 320 } 321 } 322 } 323 } 324 325 /** 326 * SSLSocketImpl(InetAddress address, int port, InetAddress localAddress, 327 * int localPort, SSLParameters sslParameters) method testing. 328 */ 329 public void testSSLSocketImpl5() throws Exception { 330 Server server = null; 331 SSLSocket socket = null; 332 try { 333 server = new Server(); 334 335 socket = new SSLSocketImpl( 336 InetAddress.getByName("localhost"), 337 server.getPort(), 338 InetAddress.getByName("localhost"), 339 0, JSSETestData.getSSLParameters()); 340 socket.setUseClientMode(true); 341 342 server.start(); 343 final SSLSocket s = socket; 344 Thread thread = new Thread() { 345 @Override 346 public void run() { 347 try { 348 s.startHandshake(); 349 } catch (Exception e) { 350 } 351 } 352 }; 353 354 thread.start(); 355 356 int timeout = 10; // wait no more than 5 seconds for handshake 357 while (!server.handshakeStarted()) { 358 // wait for handshake start 359 try { 360 Thread.sleep(500); 361 } catch (Exception e) { 362 } 363 timeout--; 364 if (timeout < 0) { 365 try { 366 server.close(); 367 } catch (IOException ex) { 368 } 369 try { 370 socket.close(); 371 } catch (IOException ex) { 372 } 373 fail("Handshake was not started"); 374 } 375 } 376 } finally { 377 if (server != null) { 378 try { 379 server.close(); 380 } catch (IOException ex) { 381 } 382 } 383 if (socket != null) { 384 try { 385 socket.close(); 386 } catch (IOException ex) { 387 } 388 } 389 } 390 } 391 392 /** 393 * getSupportedCipherSuites() method testing. 394 */ 395 public void testGetSupportedCipherSuites() throws Exception { 396 SSLSocket socket = createSSLSocket(); 397 String[] supported = socket.getSupportedCipherSuites(); 398 assertNotNull(supported); 399 supported[0] = "NOT_SUPPORTED_CIPHER_SUITE"; 400 supported = socket.getEnabledCipherSuites(); 401 for (int i = 0; i < supported.length; i++) { 402 if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) { 403 fail("Modification of the returned result " 404 + "causes the modification of the internal state"); 405 } 406 } 407 } 408 409 /** 410 * getEnabledCipherSuites() method testing. 411 */ 412 public void testGetEnabledCipherSuites() throws Exception { 413 SSLSocket socket = createSSLSocket(); 414 String[] enabled = socket.getEnabledCipherSuites(); 415 assertNotNull(enabled); 416 String[] supported = socket.getSupportedCipherSuites(); 417 for (int i = 0; i < enabled.length; i++) { 418 found: 419 { 420 for (int j = 0; j < supported.length; j++) { 421 if (enabled[i].equals(supported[j])) { 422 break found; 423 } 424 } 425 fail("Enabled suite does not belong to the set " 426 + "of supported cipher suites: " + enabled[i]); 427 } 428 } 429 socket.setEnabledCipherSuites(supported); 430 for (int i = 0; i < supported.length; i++) { 431 enabled = new String[supported.length - i]; 432 System.arraycopy(supported, 0, 433 enabled, 0, supported.length - i); 434 socket.setEnabledCipherSuites(enabled); 435 String[] result = socket.getEnabledCipherSuites(); 436 if (result.length != enabled.length) { 437 fail("Returned result does not correspond to expected."); 438 } 439 for (int k = 0; k < result.length; k++) { 440 found: 441 { 442 for (int n = 0; n < enabled.length; n++) { 443 if (result[k].equals(enabled[n])) { 444 break found; 445 } 446 } 447 if (result.length != enabled.length) { 448 fail("Returned result does not correspond " 449 + "to expected."); 450 } 451 } 452 } 453 } 454 } 455 456 /** 457 * setEnabledCipherSuites(String[] suites) method testing. 458 */ 459 public void testSetEnabledCipherSuites() throws Exception { 460 SSLSocket socket = createSSLSocket(); 461 String[] enabled = socket.getEnabledCipherSuites(); 462 assertNotNull(enabled); 463 String[] supported = socket.getSupportedCipherSuites(); 464 for (int i = 0; i < enabled.length; i++) { 465 found: 466 { 467 for (int j = 0; j < supported.length; j++) { 468 if (enabled[i].equals(supported[j])) { 469 break found; 470 } 471 } 472 fail("Enabled suite does not belong to the set " 473 + "of supported cipher suites: " + enabled[i]); 474 } 475 } 476 socket.setEnabledCipherSuites(supported); 477 socket.setEnabledCipherSuites(enabled); 478 socket.setEnabledCipherSuites(supported); 479 String[] more_than_supported = new String[supported.length + 1]; 480 for (int i = 0; i < supported.length + 1; i++) { 481 more_than_supported[i] 482 = "NOT_SUPPORTED_CIPHER_SUITE"; 483 System.arraycopy(supported, 0, 484 more_than_supported, 0, i); 485 System.arraycopy(supported, i, 486 more_than_supported, i + 1, supported.length - i); 487 try { 488 socket.setEnabledCipherSuites(more_than_supported); 489 fail("Expected IllegalArgumentException was not thrown"); 490 } catch (IllegalArgumentException e) { 491 } 492 } 493 enabled = socket.getEnabledCipherSuites(); 494 enabled[0] = "NOT_SUPPORTED_CIPHER_SUITE"; 495 enabled = socket.getEnabledCipherSuites(); 496 for (int i = 0; i < enabled.length; i++) { 497 if ("NOT_SUPPORTED_CIPHER_SUITE".equals(enabled[i])) { 498 fail("Modification of the returned result " 499 + "causes the modification of the internal state"); 500 } 501 } 502 } 503 504 /** 505 * getSupportedProtocols() method testing. 506 */ 507 public void testGetSupportedProtocols() throws Exception { 508 SSLSocket socket = createSSLSocket(); 509 String[] supported = socket.getSupportedProtocols(); 510 assertNotNull(supported); 511 assertFalse(supported.length == 0); 512 supported[0] = "NOT_SUPPORTED_PROTOCOL"; 513 supported = socket.getSupportedProtocols(); 514 for (int i = 0; i < supported.length; i++) { 515 if ("NOT_SUPPORTED_PROTOCOL".equals(supported[i])) { 516 fail("Modification of the returned result " 517 + "causes the modification of the internal state"); 518 } 519 } 520 } 521 522 /** 523 * getEnabledProtocols() method testing. 524 */ 525 public void testGetEnabledProtocols() throws Exception { 526 SSLSocket socket = createSSLSocket(); 527 String[] enabled = socket.getEnabledProtocols(); 528 assertNotNull(enabled); 529 String[] supported = socket.getSupportedProtocols(); 530 for (int i = 0; i < enabled.length; i++) { 531 found: 532 { 533 for (int j = 0; j < supported.length; j++) { 534 if (enabled[i].equals(supported[j])) { 535 break found; 536 } 537 } 538 fail("Enabled protocol does not belong to the set " 539 + "of supported protocols: " + enabled[i]); 540 } 541 } 542 socket.setEnabledProtocols(supported); 543 for (int i = 0; i < supported.length; i++) { 544 enabled = new String[supported.length - i]; 545 System.arraycopy(supported, i, 546 enabled, 0, supported.length - i); 547 socket.setEnabledProtocols(enabled); 548 String[] result = socket.getEnabledProtocols(); 549 if (result.length != enabled.length) { 550 fail("Returned result does not correspond to expected."); 551 } 552 for (int k = 0; k < result.length; k++) { 553 found: 554 { 555 for (int n = 0; n < enabled.length; n++) { 556 if (result[k].equals(enabled[n])) { 557 break found; 558 } 559 } 560 if (result.length != enabled.length) { 561 fail("Returned result does not correspond " 562 + "to expected."); 563 } 564 } 565 } 566 } 567 } 568 569 /** 570 * setEnabledProtocols(String[] protocols) method testing. 571 */ 572 public void testSetEnabledProtocols() throws Exception { 573 SSLSocket socket = createSSLSocket(); 574 String[] enabled = socket.getEnabledProtocols(); 575 assertNotNull(enabled); 576 String[] supported = socket.getSupportedProtocols(); 577 for (int i = 0; i < enabled.length; i++) { 578 //System.out.println("Checking of "+enabled[i]); 579 found: 580 { 581 for (int j = 0; j < supported.length; j++) { 582 if (enabled[i].equals(supported[j])) { 583 break found; 584 } 585 } 586 fail("Enabled suite does not belong to the set " 587 + "of supported cipher suites: " + enabled[i]); 588 } 589 } 590 socket.setEnabledProtocols(supported); 591 socket.setEnabledProtocols(enabled); 592 socket.setEnabledProtocols(supported); 593 String[] more_than_supported = new String[supported.length + 1]; 594 for (int i = 0; i < supported.length + 1; i++) { 595 more_than_supported[i] 596 = "NOT_SUPPORTED_PROTOCOL"; 597 System.arraycopy(supported, 0, 598 more_than_supported, 0, i); 599 System.arraycopy(supported, i, 600 more_than_supported, i + 1, supported.length - i); 601 try { 602 socket.setEnabledProtocols(more_than_supported); 603 fail("Expected IllegalArgumentException was not thrown"); 604 } catch (IllegalArgumentException e) { 605 } 606 } 607 enabled = socket.getEnabledProtocols(); 608 enabled[0] = "NOT_SUPPORTED_PROTOCOL"; 609 enabled = socket.getEnabledProtocols(); 610 for (int i = 0; i < enabled.length; i++) { 611 if ("NOT_SUPPORTED_PROTOCOL".equals(enabled[i])) { 612 fail("Modification of the returned result " 613 + "causes the modification of the internal state"); 614 } 615 } 616 } 617 618 private static class Server extends Thread { 619 620 private final ServerSocket server; 621 private boolean closed; 622 private boolean handshake_started = false; 623 private Socket endpoint = null; 624 625 public Server() throws IOException { 626 super(); 627 server = new ServerSocket(0); 628 server.setSoTimeout(1000); 629 } 630 631 public int getPort() { 632 return server.getLocalPort(); 633 } 634 635 @Override 636 public void run() { 637 while (!closed) { 638 try { 639 if (doLog) { 640 System.out.print("."); 641 } 642 if (!handshake_started) { 643 endpoint = server.accept(); 644 endpoint.getInputStream().read(); 645 handshake_started = true; 646 } 647 Thread.sleep(1000); 648 } catch (Exception e) { 649 e.printStackTrace(); 650 } 651 } 652 if (endpoint != null) { 653 try { 654 endpoint.close(); 655 } catch (IOException e) { 656 } 657 } 658 } 659 660 public boolean handshakeStarted() { 661 return handshake_started; 662 } 663 664 public void close() throws IOException { 665 closed = true; 666 server.close(); 667 } 668 669 } 670 671 ; 672 673 /** 674 * setUseClientMode(boolean mode) method testing. 675 * getUseClientMode() method testing. 676 */ 677 public void testSetGetUseClientMode() throws Exception { 678 Server server = null; 679 SSLSocket socket = null; 680 try { 681 server = new Server(); 682 683 socket = createSSLSocket(server.getPort()); 684 685 socket.setUseClientMode(false); 686 assertFalse("Result does not correspond to expected", 687 socket.getUseClientMode()); 688 socket.setUseClientMode(true); 689 assertTrue("Result does not correspond to expected", 690 socket.getUseClientMode()); 691 692 server.start(); 693 final SSLSocket s = socket; 694 new Thread() { 695 @Override 696 public void run() { 697 try { 698 s.startHandshake(); 699 } catch (IOException e) { 700 //e.printStackTrace(); 701 } 702 } 703 }.start(); 704 705 while (!server.handshakeStarted()) { 706 // wait for handshake start 707 try { 708 Thread.sleep(500); 709 } catch (Exception e) { 710 } 711 } 712 713 try { 714 socket.setUseClientMode(false); 715 server.close(); 716 socket.close(); 717 fail("Expected IllegalArgumentException was not thrown"); 718 } catch (IllegalArgumentException e) { 719 } 720 } finally { 721 if (server != null) { 722 try { 723 server.close(); 724 } catch (IOException ex) { 725 } 726 } 727 if (socket != null) { 728 try { 729 socket.close(); 730 } catch (IOException ex) { 731 } 732 } 733 } 734 } 735 736 /** 737 * setNeedClientAuth(boolean need) method testing. 738 * getNeedClientAuth() method testing. 739 */ 740 public void testSetGetNeedClientAuth() throws Exception { 741 SSLSocket socket = createSSLSocket(); 742 743 socket.setWantClientAuth(true); 744 socket.setNeedClientAuth(false); 745 assertFalse("Result does not correspond to expected", 746 socket.getNeedClientAuth()); 747 assertFalse("Socket did not reset its want client auth state", 748 socket.getWantClientAuth()); 749 socket.setWantClientAuth(true); 750 socket.setNeedClientAuth(true); 751 assertTrue("Result does not correspond to expected", 752 socket.getNeedClientAuth()); 753 assertFalse("Socket did not reset its want client auth state", 754 socket.getWantClientAuth()); 755 } 756 757 /** 758 * setWantClientAuth(boolean want) method testing. 759 * getWantClientAuth() method testing. 760 */ 761 public void testSetGetWantClientAuth() throws Exception { 762 SSLSocket socket = createSSLSocket(); 763 764 socket.setNeedClientAuth(true); 765 socket.setWantClientAuth(false); 766 assertFalse("Result does not correspond to expected", 767 socket.getWantClientAuth()); 768 assertFalse("Socket did not reset its want client auth state", 769 socket.getNeedClientAuth()); 770 socket.setNeedClientAuth(true); 771 socket.setWantClientAuth(true); 772 assertTrue("Result does not correspond to expected", 773 socket.getWantClientAuth()); 774 assertFalse("Socket did not reset its want client auth state", 775 socket.getNeedClientAuth()); 776 } 777 778 /** 779 * setEnableSessionCreation(boolean flag) method testing. 780 * getEnableSessionCreation() method testing. 781 */ 782 public void testSetGetEnableSessionCreation() throws Exception { 783 SSLSocket socket = createSSLSocket(); 784 785 socket.setEnableSessionCreation(false); 786 assertFalse("Result does not correspond to expected", 787 socket.getEnableSessionCreation()); 788 socket.setEnableSessionCreation(true); 789 assertTrue("Result does not correspond to expected", 790 socket.getEnableSessionCreation()); 791 } 792 793 /** 794 * getSession() method testing. 795 */ 796 public void testGetSession() throws Exception { 797 Server server = null; 798 SSLSocket socket = null; 799 try { 800 server = new Server(); 801 802 socket = createSSLSocket(server.getPort()); 803 socket.setUseClientMode(true); 804 805 server.start(); 806 final SSLSocket s = socket; 807 final SSLSession[] session = new SSLSession[1]; 808 Thread thread = new Thread() { 809 @Override 810 public void run() { 811 try { 812 session[0] = s.getSession(); 813 } catch (Exception e) { 814 e.printStackTrace(); 815 } 816 } 817 }; 818 819 thread.start(); 820 821 int timeout = 10; // wait no more than 5 seconds for handshake 822 while (!server.handshakeStarted()) { 823 // wait for handshake start 824 try { 825 Thread.sleep(500); 826 } catch (Exception e) { 827 } 828 timeout--; 829 if (timeout < 0) { 830 try { 831 server.close(); 832 } catch (IOException ex) { 833 } 834 try { 835 socket.close(); 836 } catch (IOException ex) { 837 } 838 fail("getSession method did not start a handshake"); 839 } 840 } 841 842 server.close(); // makes error during the handshake 843 thread.join(); 844 if ((session[0] == null) || 845 (!session[0].getCipherSuite() 846 .endsWith("_NULL_WITH_NULL_NULL"))) { 847 fail("Returned session is null " 848 + "or not TLS_NULL_WITH_NULL_NULL"); 849 } 850 } finally { 851 if (server != null) { 852 try { 853 server.close(); 854 } catch (IOException ex) { 855 } 856 } 857 if (socket != null) { 858 try { 859 socket.close(); 860 } catch (IOException ex) { 861 } 862 } 863 } 864 } 865 866 /** 867 * addHandshakeCompletedListener( HandshakeCompletedListener listener) 868 * method testing. 869 * removeHandshakeCompletedListener( HandshakeCompletedListener listener) 870 * method testing. 871 */ 872 public void testAddRemoveHandshakeCompletedListener() throws Exception { 873 HandshakeCompletedListener listener = 874 new HandshakeCompletedListener() { 875 public void handshakeCompleted( 876 HandshakeCompletedEvent event) { 877 } 878 }; 879 SSLSocket socket = createSSLSocket(); 880 socket.addHandshakeCompletedListener(listener); 881 try { 882 socket.addHandshakeCompletedListener(null); 883 fail("Expected IllegalArgumentException was not thrown."); 884 } catch (IllegalArgumentException e) { 885 } 886 try { 887 socket.removeHandshakeCompletedListener(null); 888 fail("Expected IllegalArgumentException was not thrown."); 889 } catch (IllegalArgumentException e) { 890 } 891 try { 892 socket.removeHandshakeCompletedListener( 893 new HandshakeCompletedListener() { 894 public void handshakeCompleted( 895 HandshakeCompletedEvent event) { 896 } 897 }); 898 fail("Expected IllegalArgumentException was not thrown."); 899 } catch (IllegalArgumentException e) { 900 } 901 try { 902 socket.removeHandshakeCompletedListener(listener); 903 } catch (IllegalArgumentException e) { 904 fail("Unxpected IllegalArgumentException was thrown."); 905 } 906 } 907 908 /** 909 * startHandshake() method testing. 910 */ 911 public void testStartHandshake() throws Exception { 912 Server server = null; 913 SSLSocket socket = null; 914 try { 915 server = new Server(); 916 917 socket = createSSLSocket(server.getPort()); 918 socket.setUseClientMode(true); 919 920 server.start(); 921 final SSLSocket s = socket; 922 final Exception[] exception = new Exception[1]; 923 Thread thread = new Thread() { 924 @Override 925 public void run() { 926 try { 927 s.startHandshake(); 928 } catch (Exception e) { 929 exception[0] = e; 930 } 931 } 932 }; 933 934 thread.start(); 935 936 int timeout = 10; // wait no more than 5 seconds for handshake 937 while (!server.handshakeStarted()) { 938 // wait for handshake start 939 try { 940 Thread.sleep(500); 941 } catch (Exception e) { 942 } 943 timeout--; 944 if (timeout < 0) { 945 fail("Handshake was not started"); 946 } 947 } 948 949 server.close(); // makes error during the handshake 950 thread.join(); 951 if (exception[0] == null) { 952 fail("Expected IOException was not thrown"); 953 } 954 } finally { 955 if (server != null) { 956 try { 957 server.close(); 958 } catch (IOException ex) { 959 } 960 } 961 if (socket != null) { 962 try { 963 socket.close(); 964 } catch (IOException ex) { 965 } 966 } 967 } 968 } 969 970 /** 971 * getInputStream() method testing. 972 */ 973 public void testGetInputStream() throws Exception { 974 Server server = null; 975 SSLSocket socket = null; 976 try { 977 server = new Server(); 978 979 socket = createSSLSocket(server.getPort()); 980 socket.setUseClientMode(true); 981 982 server.start(); 983 final SSLSocket s = socket; 984 Thread thread = new Thread() { 985 @Override 986 public void run() { 987 try { 988 s.getInputStream().read(); // should start handshake 989 } catch (Exception e) { 990 } 991 } 992 }; 993 994 thread.start(); 995 996 int timeout = 10; // wait no more than 5 seconds for handshake 997 while (!server.handshakeStarted()) { 998 // wait for handshake start 999 try { 1000 Thread.sleep(500); 1001 } catch (Exception e) { 1002 } 1003 timeout--; 1004 if (timeout < 0) { 1005 try { 1006 server.close(); 1007 } catch (IOException ex) { 1008 } 1009 try { 1010 socket.close(); 1011 } catch (IOException ex) { 1012 } 1013 fail("Handshake was not started"); 1014 } 1015 } 1016 } finally { 1017 if (server != null) { 1018 try { 1019 server.close(); 1020 } catch (IOException ex) { 1021 } 1022 } 1023 if (socket != null) { 1024 try { 1025 socket.close(); 1026 } catch (IOException ex) { 1027 } 1028 } 1029 } 1030 } 1031 1032 /** 1033 * getOutputStream() method testing. 1034 */ 1035 public void testGetOutputStream() throws Exception { 1036 Server server = null; 1037 SSLSocket socket = null; 1038 try { 1039 server = new Server(); 1040 1041 socket = createSSLSocket(server.getPort()); 1042 socket.setUseClientMode(true); 1043 1044 server.start(); 1045 final SSLSocket s = socket; 1046 Thread thread = new Thread() { 1047 @Override 1048 public void run() { 1049 try { 1050 s.getOutputStream().write(0); // should start handshake 1051 } catch (Exception e) { 1052 } 1053 } 1054 }; 1055 1056 thread.start(); 1057 1058 int timeout = 10; // wait no more than 5 seconds for handshake 1059 while (!server.handshakeStarted()) { 1060 // wait for handshake start 1061 try { 1062 Thread.sleep(500); 1063 } catch (Exception e) { 1064 } 1065 timeout--; 1066 if (timeout < 0) { 1067 try { 1068 server.close(); 1069 } catch (IOException ex) { 1070 } 1071 try { 1072 socket.close(); 1073 } catch (IOException ex) { 1074 } 1075 fail("Handshake was not started"); 1076 } 1077 } 1078 } finally { 1079 if (server != null) { 1080 try { 1081 server.close(); 1082 } catch (IOException ex) { 1083 } 1084 } 1085 if (socket != null) { 1086 try { 1087 socket.close(); 1088 } catch (IOException ex) { 1089 } 1090 } 1091 } 1092 } 1093 1094 /** 1095 * sendUrgentData(int data) method testing. 1096 */ 1097 public void testSendUrgentData() { 1098 Server server = null; 1099 SSLSocket socket = null; 1100 try { 1101 server = new Server(); 1102 socket = createSSLSocket(server.getPort()); 1103 1104 socket.sendUrgentData(0); 1105 fail("Expected exception was not thrown"); 1106 } catch (Exception e) { 1107 if (doLog) { 1108 System.out.println("Trowed exception: " + e.getMessage()); 1109 } 1110 } finally { 1111 if (server != null) { 1112 try { 1113 server.close(); 1114 } catch (IOException ex) { 1115 } 1116 } 1117 if (socket != null) { 1118 try { 1119 socket.close(); 1120 } catch (IOException ex) { 1121 } 1122 } 1123 } 1124 } 1125 1126 /** 1127 * setOOBInline(boolean on) method testing. 1128 */ 1129 public void testSetOOBInline() { 1130 Server server = null; 1131 SSLSocket socket = null; 1132 try { 1133 server = new Server(); 1134 socket = createSSLSocket(server.getPort()); 1135 1136 socket.setOOBInline(true); 1137 fail("Expected exception was not thrown"); 1138 } catch (Exception e) { 1139 if (doLog) { 1140 System.out.println("Trowed exception: " + e.getMessage()); 1141 } 1142 } finally { 1143 if (server != null) { 1144 try { 1145 server.close(); 1146 } catch (IOException ex) { 1147 } 1148 } 1149 if (socket != null) { 1150 try { 1151 socket.close(); 1152 } catch (IOException ex) { 1153 } 1154 } 1155 } 1156 } 1157 1158 /** 1159 * shutdownOutput() method testing. 1160 */ 1161 public void testShutdownOutput() { 1162 Server server = null; 1163 SSLSocket socket = null; 1164 try { 1165 server = new Server(); 1166 socket = createSSLSocket(server.getPort()); 1167 1168 socket.shutdownOutput(); 1169 fail("Expected exception was not thrown"); 1170 } catch (Exception e) { 1171 if (doLog) { 1172 System.out.println("Trowed exception: " + e.getMessage()); 1173 } 1174 } finally { 1175 if (server != null) { 1176 try { 1177 server.close(); 1178 } catch (IOException ex) { 1179 } 1180 } 1181 if (socket != null) { 1182 try { 1183 socket.close(); 1184 } catch (IOException ex) { 1185 } 1186 } 1187 } 1188 } 1189 1190 /** 1191 * shutdownInput() method testing. 1192 */ 1193 public void testShutdownInput() { 1194 Server server = null; 1195 SSLSocket socket = null; 1196 try { 1197 server = new Server(); 1198 socket = createSSLSocket(server.getPort()); 1199 1200 socket.shutdownInput(); 1201 fail("Expected exception was not thrown"); 1202 } catch (Exception e) { 1203 if (doLog) { 1204 System.out.println("Trowed exception: " + e.getMessage()); 1205 } 1206 } finally { 1207 if (server != null) { 1208 try { 1209 server.close(); 1210 } catch (IOException ex) { 1211 } 1212 } 1213 if (socket != null) { 1214 try { 1215 socket.close(); 1216 } catch (IOException ex) { 1217 } 1218 } 1219 } 1220 } 1221 1222 /** 1223 * toString() method testing. 1224 */ 1225 public void testToString() throws Exception { 1226 SSLSocket socket = createSSLSocket(); 1227 assertNotNull("String representation is null", socket.toString()); 1228 } 1229 1230 public static Test suite() { 1231 return new TestSuite(SSLSocketImplTest.class); 1232 } 1233 1234 } 1235