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