1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package tests.api.javax.net.ssl; 17 18 import dalvik.annotation.AndroidOnly; 19 import dalvik.annotation.TestTargetClass; 20 import dalvik.annotation.TestTargets; 21 import dalvik.annotation.TestLevel; 22 import dalvik.annotation.TestTargetNew; 23 24 import javax.net.ssl.*; 25 import javax.security.cert.X509Certificate; 26 27 import java.net.*; 28 import java.security.KeyStore; 29 import java.security.SecureRandom; 30 import java.util.Arrays; 31 import java.io.ByteArrayInputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 35 import junit.framework.TestCase; 36 37 import org.apache.harmony.luni.util.Base64; 38 39 import tests.api.javax.net.ssl.HandshakeCompletedEventTest.TestTrustManager; 40 import tests.support.Support_PortManager; 41 42 @TestTargetClass(SSLSocket.class) 43 public class SSLSocketTest extends TestCase { 44 45 public class HandshakeCL implements HandshakeCompletedListener { 46 HandshakeCL() { 47 super(); 48 } 49 public void handshakeCompleted(HandshakeCompletedEvent event) { 50 } 51 } 52 53 /** 54 * @tests javax.net.ssl.SSLSocket#SSLSocket() 55 */ 56 @TestTargetNew( 57 level = TestLevel.COMPLETE, 58 notes = "", 59 method = "SSLSocket", 60 args = {} 61 ) 62 public void testConstructor_01() { 63 try { 64 SSLSocket ssl = getSSLSocket(); 65 } catch (Exception e) { 66 fail("Unexpected exception " + e); 67 } 68 } 69 70 /** 71 * @throws IOException 72 * @throws UnknownHostException 73 * @tests javax.net.ssl.SSLSocket#SSLSocket(InetAddress address, int port) 74 */ 75 @TestTargetNew( 76 level = TestLevel.COMPLETE, 77 notes = "", 78 method = "SSLSocket", 79 args = {java.net.InetAddress.class, int.class} 80 ) 81 public void testConstructor_02() throws UnknownHostException, IOException { 82 SSLSocket ssl; 83 int sport = startServer("Cons InetAddress,I"); 84 int[] invalidPort = {-1, Integer.MIN_VALUE, 65536, Integer.MAX_VALUE}; 85 86 ssl = getSSLSocket(InetAddress.getLocalHost(), sport); 87 assertNotNull(ssl); 88 assertEquals(sport, ssl.getPort()); 89 90 try { 91 ssl = getSSLSocket(InetAddress.getLocalHost(), sport + 1); 92 fail("IOException wasn't thrown ..."); 93 } catch (IOException e) { 94 //expected 95 } 96 97 for (int i = 0; i < invalidPort.length; i++) { 98 try { 99 ssl = getSSLSocket(InetAddress.getLocalHost(), invalidPort[i]); 100 fail("IllegalArgumentException wasn't thrown for " + invalidPort[i]); 101 } catch (IllegalArgumentException iae) { 102 // expected 103 } catch (Exception e) { 104 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPort[i]); 105 } 106 } 107 } 108 109 /** 110 * @throws IOException 111 * @throws UnknownHostException 112 * @tests javax.net.ssl.SSLSocket#SSLSocket(InetAddress address, int port, 113 * InetAddress clientAddress, int clientPort) 114 */ 115 @TestTargetNew( 116 level = TestLevel.COMPLETE, 117 notes = "", 118 method = "SSLSocket", 119 args = {java.net.InetAddress.class, int.class, java.net.InetAddress.class, int.class} 120 ) 121 public void testConstructor_03() throws UnknownHostException, IOException { 122 SSLSocket ssl; 123 int sport = startServer("Cons InetAddress,I,InetAddress,I"); 124 int portNumber = Support_PortManager.getNextPort(); 125 126 ssl = getSSLSocket(InetAddress.getLocalHost(), sport, 127 InetAddress.getLocalHost(), portNumber); 128 assertNotNull(ssl); 129 assertEquals(sport, ssl.getPort()); 130 assertEquals(portNumber, ssl.getLocalPort()); 131 132 try { 133 ssl = getSSLSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082); 134 fail("IOException wasn't thrown ..."); 135 } catch (IOException e) { 136 //expected 137 } 138 139 try { 140 ssl = getSSLSocket(InetAddress.getLocalHost(), -1, 141 InetAddress.getLocalHost(), sport + 1); 142 fail("IllegalArgumentException wasn't thrown for -1"); 143 } catch (IllegalArgumentException iae) { 144 // expected 145 } catch (Exception e) { 146 fail(e + " was thrown instead of IllegalArgumentException for -1"); 147 } 148 try { 149 ssl = getSSLSocket(InetAddress.getLocalHost(), sport, 150 InetAddress.getLocalHost(), -1); 151 fail("IllegalArgumentException wasn't thrown for -1"); 152 } catch (IllegalArgumentException iae) { 153 // expected 154 } catch (Exception e) { 155 fail(e + " was thrown instead of IllegalArgumentException for -1"); 156 } 157 158 try { 159 ssl = getSSLSocket(InetAddress.getLocalHost(), Integer.MIN_VALUE, 160 InetAddress.getLocalHost(), sport + 1); 161 fail("IOException wasn't thrown for " + Integer.MIN_VALUE); 162 } catch (IOException ioe) { 163 // expected on RI 164 } catch (IllegalArgumentException iae) { 165 // expected on Android 166 } catch (Exception e) { 167 fail(e + " was thrown instead of IOException for " 168 + Integer.MIN_VALUE); 169 } 170 try { 171 ssl = getSSLSocket(InetAddress.getLocalHost(), sport, 172 InetAddress.getLocalHost(), Integer.MIN_VALUE); 173 fail("IllegalArgumentException wasn't thrown for " 174 + Integer.MIN_VALUE); 175 } catch (IllegalArgumentException iae) { 176 // expected 177 } catch (Exception e) { 178 fail(e + " was thrown instead of IllegalArgumentException for " 179 + Integer.MIN_VALUE); 180 } 181 182 try { 183 ssl = getSSLSocket(InetAddress.getLocalHost(), 65536, 184 InetAddress.getLocalHost(), sport + 1); 185 fail("IOException wasn't thrown for 65536"); 186 } catch (IOException ioe) { 187 // expected on RI 188 } catch (IllegalArgumentException iae) { 189 // expected on Android 190 } catch (Exception e) { 191 fail(e + " was thrown instead of IOException for 65536"); 192 } 193 try { 194 ssl = getSSLSocket(InetAddress.getLocalHost(), sport, 195 InetAddress.getLocalHost(), 65536); 196 fail("IllegalArgumentException wasn't thrown for 65536"); 197 } catch (IllegalArgumentException iae) { 198 // expected 199 } catch (Exception e) { 200 fail(e + " was thrown instead of IllegalArgumentException for 65536"); 201 } 202 203 try { 204 ssl = getSSLSocket(InetAddress.getLocalHost(), Integer.MAX_VALUE, 205 InetAddress.getLocalHost(), sport + 1); 206 fail("IOException wasn't thrown for " + Integer.MAX_VALUE); 207 } catch (IOException ioe) { 208 // expected on RI 209 } catch (IllegalArgumentException iae) { 210 // expected on Android 211 } catch (Exception e) { 212 fail(e + " was thrown instead of IOException for " 213 + Integer.MAX_VALUE); 214 } 215 try { 216 ssl = getSSLSocket(InetAddress.getLocalHost(), sport, 217 InetAddress.getLocalHost(), Integer.MAX_VALUE); 218 fail("IllegalArgumentException wasn't thrown for " 219 + Integer.MAX_VALUE); 220 } catch (IllegalArgumentException iae) { 221 // expected 222 } catch (Exception e) { 223 fail(e + " was thrown instead of IllegalArgumentException for " 224 + Integer.MAX_VALUE); 225 } 226 } 227 228 /** 229 * @throws IOException 230 * @throws UnknownHostException 231 * @tests javax.net.ssl.SSLSocket#SSLSocket(String host, int port) 232 */ 233 @TestTargetNew( 234 level = TestLevel.COMPLETE, 235 notes = "", 236 method = "SSLSocket", 237 args = {java.lang.String.class, int.class} 238 ) 239 public void testConstructor_04() throws UnknownHostException, IOException { 240 SSLSocket ssl; 241 int sport = startServer("Cons String,I"); 242 int[] invalidPort = {-1, Integer.MIN_VALUE, 65536, Integer.MAX_VALUE}; 243 244 ssl = getSSLSocket(InetAddress.getLocalHost().getHostName(), sport); 245 assertNotNull(ssl); 246 assertEquals(sport, ssl.getPort()); 247 248 try { 249 ssl = getSSLSocket("localhost", 8082); 250 fail("IOException wasn't thrown ..."); 251 } catch (IOException e) { 252 //expected 253 } 254 255 for (int i = 0; i < invalidPort.length; i++) { 256 try { 257 ssl = getSSLSocket(InetAddress.getLocalHost().getHostName(), invalidPort[i]); 258 fail("IllegalArgumentException wasn't thrown for " + invalidPort[i]); 259 } catch (IllegalArgumentException iae) { 260 // expected 261 } catch (Exception e) { 262 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPort[i]); 263 } 264 } 265 266 try { 267 ssl = getSSLSocket("bla-bla", sport); 268 fail("UnknownHostException wasn't thrown"); 269 } catch (UnknownHostException uhp) { 270 // expected 271 } catch (Exception e) { 272 fail(e + " was thrown instead of UnknownHostException"); 273 } 274 } 275 276 /** 277 * @throws IOException 278 * @throws UnknownHostException 279 * @tests javax.net.ssl.SSLSocket#SSLSocket(String host, int port, InetAddress clientAddress, 280 * int clientPort) 281 */ 282 @TestTargetNew( 283 level = TestLevel.COMPLETE, 284 notes = "", 285 method = "SSLSocket", 286 args = {java.lang.String.class, int.class, java.net.InetAddress.class, int.class} 287 ) 288 public void testConstructor_05() throws UnknownHostException, IOException { 289 SSLSocket ssl; 290 int sport = startServer("Cons String,I,InetAddress,I"); 291 int portNumber = Support_PortManager.getNextPort(); 292 int[] invalidPort = {-1, Integer.MIN_VALUE, 65536, Integer.MAX_VALUE}; 293 294 ssl = getSSLSocket(InetAddress.getLocalHost().getHostName(), sport, 295 InetAddress.getLocalHost(), portNumber); 296 assertNotNull(ssl); 297 assertEquals(sport, ssl.getPort()); 298 assertEquals(portNumber, ssl.getLocalPort()); 299 300 try { 301 ssl = getSSLSocket("localhost", 8081, InetAddress.getLocalHost(), 8082); 302 fail("IOException wasn't thrown ..."); 303 } catch (IOException e) { 304 //expected 305 } 306 307 for (int i = 0; i < invalidPort.length; i++) { 308 portNumber = Support_PortManager.getNextPort(); 309 try { 310 ssl = getSSLSocket(InetAddress.getLocalHost().getHostName(), invalidPort[i], 311 InetAddress.getLocalHost(), portNumber); 312 fail("IllegalArgumentException wasn't thrown for " + invalidPort[i]); 313 } catch (IllegalArgumentException iae) { 314 // expected 315 } catch (Exception e) { 316 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPort[i]); 317 } 318 try { 319 ssl = getSSLSocket(InetAddress.getLocalHost().getHostName(), sport, 320 InetAddress.getLocalHost(), invalidPort[i]); 321 fail("IllegalArgumentException wasn't thrown for " + invalidPort[i]); 322 } catch (IllegalArgumentException iae) { 323 // expected 324 } catch (Exception e) { 325 fail(e + " was thrown instead of IllegalArgumentException for " + invalidPort[i]); 326 } 327 } 328 329 portNumber = Support_PortManager.getNextPort(); 330 try { 331 ssl = getSSLSocket("bla-bla", sport, InetAddress.getLocalHost(), portNumber); 332 fail("UnknownHostException wasn't thrown"); 333 } catch (UnknownHostException uhp) { 334 // expected 335 } catch (Exception e) { 336 fail(e + " was thrown instead of UnknownHostException"); 337 } 338 } 339 340 @TestTargetNew( 341 level = TestLevel.COMPLETE, 342 notes = "Guard against native resource leakage.", 343 method = "SSLSocket", 344 args = {} 345 ) 346 public void test_creationStressTest() throws Exception { 347 // Test the default codepath, which uses /dev/urandom. 348 SSLContext sslContext = SSLContext.getInstance("TLS"); 349 sslContext.init(null, null, null); 350 for (int i = 0; i < 2048; ++i) { 351 sslContext.getSocketFactory().createSocket(); 352 } 353 354 // Test the other codepath, which copies a seed from a byte[]. 355 sslContext.init(null, null, new SecureRandom()); 356 for (int i = 0; i < 2048; ++i) { 357 sslContext.getSocketFactory().createSocket(); 358 } 359 } 360 361 /** 362 * @throws IOException 363 * @tests javax.net.ssl.SSLSocket#addHandshakeCompletedListener(HandshakeCompletedListener listener) 364 */ 365 @TestTargetNew( 366 level = TestLevel.COMPLETE, 367 notes = "", 368 method = "addHandshakeCompletedListener", 369 args = {javax.net.ssl.HandshakeCompletedListener.class} 370 ) 371 @AndroidOnly("RI doesn't throw the specified IAE") 372 public void test_addHandshakeCompletedListener() throws IOException { 373 SSLSocket ssl = getSSLSocket(); 374 HandshakeCompletedListener ls = new HandshakeCL(); 375 try { 376 ssl.addHandshakeCompletedListener(null); 377 fail("IllegalArgumentException wasn't thrown"); 378 } catch (IllegalArgumentException iae) { 379 //expected 380 } 381 try { 382 ssl.addHandshakeCompletedListener(ls); 383 } catch (Exception e) { 384 fail("Unexpected exception " + e); 385 } 386 } 387 388 /** 389 * @throws IOException 390 * @tests javax.net.ssl.SSLSocket#removeHandshakeCompletedListener(HandshakeCompletedListener listener) 391 */ 392 @TestTargetNew( 393 level = TestLevel.COMPLETE, 394 notes = "", 395 method = "removeHandshakeCompletedListener", 396 args = {javax.net.ssl.HandshakeCompletedListener.class} 397 ) 398 public void test_removeHandshakeCompletedListener() throws IOException { 399 SSLSocket ssl = getSSLSocket(); 400 HandshakeCompletedListener ls = new HandshakeCL(); 401 try { 402 ssl.removeHandshakeCompletedListener(null); 403 fail("IllegalArgumentException wasn't thrown"); 404 } catch (IllegalArgumentException iae) { 405 //expected 406 } 407 408 try { 409 ssl.removeHandshakeCompletedListener(ls); 410 } catch (IllegalArgumentException iae) { 411 //expected 412 } catch (Exception e) { 413 fail("Unexpected exception " + e); 414 } 415 416 ssl.addHandshakeCompletedListener(ls); 417 try { 418 ssl.removeHandshakeCompletedListener(ls); 419 } catch (Exception e) { 420 fail("Unexpected exception " + e); 421 } 422 } 423 424 /** 425 * @throws IOException 426 * @tests javax.net.ssl.SSLSocket#setEnableSessionCreation(boolean flag) 427 * @tests javax.net.ssl.SSLSocket#getEnableSessionCreation() 428 */ 429 @TestTargets({ 430 @TestTargetNew( 431 level = TestLevel.COMPLETE, 432 notes = "", 433 method = "getEnableSessionCreation", 434 args = {} 435 ), 436 @TestTargetNew( 437 level = TestLevel.COMPLETE, 438 notes = "", 439 method = "setEnableSessionCreation", 440 args = {boolean.class} 441 ) 442 }) 443 public void test_EnableSessionCreation() throws IOException { 444 SSLSocket ssl = getSSLSocket(); 445 assertTrue(ssl.getEnableSessionCreation()); 446 ssl.setEnableSessionCreation(false); 447 assertFalse(ssl.getEnableSessionCreation()); 448 ssl.setEnableSessionCreation(true); 449 assertTrue(ssl.getEnableSessionCreation()); 450 } 451 452 /** 453 * @throws IOException 454 * @throws UnknownHostException 455 * @tests javax.net.ssl.SSLSocket#setNeedClientAuth(boolean need) 456 * @tests javax.net.ssl.SSLSocket#getNeedClientAuthCreation() 457 */ 458 @TestTargets({ 459 @TestTargetNew( 460 level = TestLevel.COMPLETE, 461 notes = "", 462 method = "setNeedClientAuth", 463 args = {boolean.class} 464 ), 465 @TestTargetNew( 466 level = TestLevel.COMPLETE, 467 notes = "", 468 method = "getNeedClientAuth", 469 args = {} 470 ) 471 }) 472 public void test_NeedClientAuth() throws UnknownHostException, IOException { 473 SSLSocket ssl = getSSLSocket(); 474 ssl.setNeedClientAuth(true); 475 assertTrue(ssl.getNeedClientAuth()); 476 ssl.setNeedClientAuth(false); 477 assertFalse(ssl.getNeedClientAuth()); 478 } 479 480 /** 481 * @throws IOException 482 * @throws UnknownHostException 483 * @tests javax.net.ssl.SSLSocket#setWantClientAuth(boolean want) 484 * @tests javax.net.ssl.SSLSocket#getWantClientAuthCreation() 485 */ 486 @TestTargets({ 487 @TestTargetNew( 488 level = TestLevel.COMPLETE, 489 notes = "", 490 method = "setWantClientAuth", 491 args = {boolean.class} 492 ), 493 @TestTargetNew( 494 level = TestLevel.COMPLETE, 495 notes = "", 496 method = "getWantClientAuth", 497 args = {} 498 ) 499 }) 500 public void test_WantClientAuth() throws UnknownHostException, IOException { 501 SSLSocket ssl = getSSLSocket(); 502 ssl.setWantClientAuth(true); 503 assertTrue(ssl.getWantClientAuth()); 504 ssl.setWantClientAuth(false); 505 assertFalse(ssl.getWantClientAuth()); 506 } 507 508 /** 509 * @throws IOException 510 * @tests javax.net.ssl.SSLSocket#getSupportedProtocols() 511 */ 512 @TestTargetNew( 513 level = TestLevel.COMPLETE, 514 notes = "", 515 method = "getSupportedProtocols", 516 args = {} 517 ) 518 public void test_getSupportedProtocols() throws IOException { 519 SSLSocket ssl = getSSLSocket(); 520 String[] res = ssl.getSupportedProtocols(); 521 assertTrue("No supported protocols found", res.length > 0); 522 } 523 524 /** 525 * @throws IOException 526 * @tests javax.net.ssl.SSLSocket#getEnabledProtocols() 527 * @tests javax.net.ssl.SSLSocket#setEnabledProtocols(String[] protocols) 528 */ 529 @TestTargets({ 530 @TestTargetNew( 531 level = TestLevel.COMPLETE, 532 notes = "", 533 method = "setEnabledProtocols", 534 args = {java.lang.String[].class} 535 ), 536 @TestTargetNew( 537 level = TestLevel.COMPLETE, 538 notes = "", 539 method = "getEnabledProtocols", 540 args = {} 541 ) 542 }) 543 public void test_EnabledProtocols() throws IOException { 544 SSLSocket ssl = getSSLSocket(); 545 try { 546 ssl.setEnabledProtocols(null); 547 } catch (IllegalArgumentException iae) { 548 //expected 549 } 550 try { 551 ssl.setEnabledProtocols(new String[] {}); 552 } catch (IllegalArgumentException iae) { 553 //expected 554 } 555 try { 556 ssl.setEnabledProtocols(new String[] {"blubb"}); 557 } catch (IllegalArgumentException iae) { 558 //expected 559 } 560 ssl.setEnabledProtocols(ssl.getEnabledProtocols()); 561 String[] res = ssl.getEnabledProtocols(); 562 assertEquals("no enabled protocols set", 563 ssl.getEnabledProtocols().length, res.length); 564 } 565 566 /** 567 * @throws IOException 568 * @tests javax.net.ssl.SSLSocket#getSession() 569 */ 570 @TestTargetNew( 571 level = TestLevel.COMPLETE, 572 notes = "", 573 method = "getSession", 574 args = {} 575 ) 576 public void test_getSession() throws IOException { 577 SSLSocket ssl = getSSLSocket(); 578 try { 579 assertNotNull(ssl.getSession()); 580 } catch (Exception e) { 581 fail("Unexpected exception " + e); 582 } 583 } 584 585 /** 586 * @throws IOException 587 * @tests javax.net.ssl.SSLSocket#getSupportedCipherSuites() 588 */ 589 @TestTargetNew( 590 level = TestLevel.COMPLETE, 591 notes = "", 592 method = "getSupportedCipherSuites", 593 args = {} 594 ) 595 public void test_getSupportedCipherSuites() throws IOException { 596 SSLSocket ssl = getSSLSocket(); 597 String[] res = ssl.getSupportedCipherSuites(); 598 assertTrue("no supported cipher suites", res.length > 0); 599 } 600 601 /** 602 * @throws IOException 603 * @tests javax.net.ssl.SSLSocket#getEnabledCipherSuites() 604 * @tests javax.net.ssl.SSLSocket#setEnabledCipherSuites(String[] suites) 605 */ 606 @TestTargets({ 607 @TestTargetNew( 608 level = TestLevel.COMPLETE, 609 notes = "", 610 method = "getEnabledCipherSuites", 611 args = {} 612 ), 613 @TestTargetNew( 614 level = TestLevel.COMPLETE, 615 notes = "", 616 method = "setEnabledCipherSuites", 617 args = {java.lang.String[].class} 618 ) 619 }) 620 public void test_EnabledCipherSuites() throws IOException { 621 SSLSocket ssl = getSSLSocket(); 622 try { 623 ssl.setEnabledCipherSuites(null); 624 } catch (IllegalArgumentException iae) { 625 //expected 626 } 627 try { 628 ssl.setEnabledCipherSuites(new String[] {}); 629 } catch (IllegalArgumentException iae) { 630 //expected 631 } 632 try { 633 ssl.setEnabledCipherSuites(new String[] {"blubb"}); 634 } catch (IllegalArgumentException iae) { 635 //expected 636 } 637 ssl.setEnabledCipherSuites(ssl.getSupportedCipherSuites()); 638 String[] res = ssl.getEnabledCipherSuites(); 639 assertNotNull("NULL result", res); 640 assertEquals("not all supported cipher suites were enabled", 641 Arrays.asList(ssl.getSupportedCipherSuites()), 642 Arrays.asList(res)); 643 } 644 645 /** 646 * @throws IOException 647 * @tests javax.net.ssl.SSLSocket#getUseClientMode() 648 * @tests javax.net.ssl.SSLSocket#setUseClientMode(boolean mode) 649 */ 650 @TestTargets({ 651 @TestTargetNew( 652 level = TestLevel.COMPLETE, 653 notes = "", 654 method = "getUseClientMode", 655 args = {} 656 ), 657 @TestTargetNew( 658 level = TestLevel.COMPLETE, 659 notes = "", 660 method = "setUseClientMode", 661 args = {boolean.class} 662 ) 663 }) 664 public void test_UseClientMode() throws IOException { 665 SSLSocket ssl = getSSLSocket(); 666 assertTrue(ssl.getUseClientMode()); 667 ssl.setUseClientMode(false); 668 assertFalse(ssl.getUseClientMode()); 669 670 ssl = getSSLSocket("localhost", startServer("UseClientMode")); 671 try { 672 ssl.startHandshake(); 673 } catch (IOException ioe) { 674 //fail(ioe + " was thrown for method startHandshake()"); 675 } 676 try { 677 ssl.setUseClientMode(false); 678 fail("IllegalArgumentException wasn't thrown"); 679 } catch (IllegalArgumentException iae) { 680 //expected 681 } catch (Exception e) { 682 fail(e + " was thrown instead of IllegalArgumentException"); 683 } 684 } 685 686 /** 687 * @throws IOException 688 * @tests javax.net.ssl.SSLSocket#startHandshake() 689 */ 690 @TestTargetNew( 691 level = TestLevel.PARTIAL_COMPLETE, 692 notes = "", 693 method = "startHandshake", 694 args = {} 695 ) 696 public void test_startHandshake() throws IOException { 697 SSLSocket ssl = getSSLSocket(); 698 try { 699 ssl.startHandshake(); 700 fail("IOException wasn't thrown"); 701 } catch (IOException ioe) { 702 //expected 703 } catch (Exception e) { 704 fail(e + " was thrown instead of IOException"); 705 } 706 } 707 708 // Change this to false if on RI 709 boolean useBKS = true; 710 711 private String PASSWORD = "android"; 712 713 private int port = Support_PortManager.getNextPort(); 714 715 private boolean serverReady = false; 716 717 /** 718 * Defines the keystore contents for the server, BKS version. Holds just a 719 * single self-generated key. The subject name is "Test Server". 720 */ 721 private static final String SERVER_KEYS_BKS = 722 "AAAAAQAAABQDkebzoP1XwqyWKRCJEpn/t8dqIQAABDkEAAVteWtleQAAARpYl20nAAAAAQAFWC41" + 723 "MDkAAAJNMIICSTCCAbKgAwIBAgIESEfU1jANBgkqhkiG9w0BAQUFADBpMQswCQYDVQQGEwJVUzET" + 724 "MBEGA1UECBMKQ2FsaWZvcm5pYTEMMAoGA1UEBxMDTVRWMQ8wDQYDVQQKEwZHb29nbGUxEDAOBgNV" + 725 "BAsTB0FuZHJvaWQxFDASBgNVBAMTC1Rlc3QgU2VydmVyMB4XDTA4MDYwNTExNTgxNFoXDTA4MDkw" + 726 "MzExNTgxNFowaTELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExDDAKBgNVBAcTA01U" + 727 "VjEPMA0GA1UEChMGR29vZ2xlMRAwDgYDVQQLEwdBbmRyb2lkMRQwEgYDVQQDEwtUZXN0IFNlcnZl" + 728 "cjCBnzANBgkqhkiG9w0BAQEFAAOBjQAwgYkCgYEA0LIdKaIr9/vsTq8BZlA3R+NFWRaH4lGsTAQy" + 729 "DPMF9ZqEDOaL6DJuu0colSBBBQ85hQTPa9m9nyJoN3pEi1hgamqOvQIWcXBk+SOpUGRZZFXwniJV" + 730 "zDKU5nE9MYgn2B9AoiH3CSuMz6HRqgVaqtppIe1jhukMc/kHVJvlKRNy9XMCAwEAATANBgkqhkiG" + 731 "9w0BAQUFAAOBgQC7yBmJ9O/eWDGtSH9BH0R3dh2NdST3W9hNZ8hIa8U8klhNHbUCSSktZmZkvbPU" + 732 "hse5LI3dh6RyNDuqDrbYwcqzKbFJaq/jX9kCoeb3vgbQElMRX8D2ID1vRjxwlALFISrtaN4VpWzV" + 733 "yeoHPW4xldeZmoVtjn8zXNzQhLuBqX2MmAAAAqwAAAAUvkUScfw9yCSmALruURNmtBai7kQAAAZx" + 734 "4Jmijxs/l8EBaleaUru6EOPioWkUAEVWCxjM/TxbGHOi2VMsQWqRr/DZ3wsDmtQgw3QTrUK666sR" + 735 "MBnbqdnyCyvM1J2V1xxLXPUeRBmR2CXorYGF9Dye7NkgVdfA+9g9L/0Au6Ugn+2Cj5leoIgkgApN" + 736 "vuEcZegFlNOUPVEs3SlBgUF1BY6OBM0UBHTPwGGxFBBcetcuMRbUnu65vyDG0pslT59qpaR0TMVs" + 737 "P+tcheEzhyjbfM32/vwhnL9dBEgM8qMt0sqF6itNOQU/F4WGkK2Cm2v4CYEyKYw325fEhzTXosck" + 738 "MhbqmcyLab8EPceWF3dweoUT76+jEZx8lV2dapR+CmczQI43tV9btsd1xiBbBHAKvymm9Ep9bPzM" + 739 "J0MQi+OtURL9Lxke/70/MRueqbPeUlOaGvANTmXQD2OnW7PISwJ9lpeLfTG0LcqkoqkbtLKQLYHI" + 740 "rQfV5j0j+wmvmpMxzjN3uvNajLa4zQ8l0Eok9SFaRr2RL0gN8Q2JegfOL4pUiHPsh64WWya2NB7f" + 741 "V+1s65eA5ospXYsShRjo046QhGTmymwXXzdzuxu8IlnTEont6P4+J+GsWk6cldGbl20hctuUKzyx" + 742 "OptjEPOKejV60iDCYGmHbCWAzQ8h5MILV82IclzNViZmzAapeeCnexhpXhWTs+xDEYSKEiG/camt" + 743 "bhmZc3BcyVJrW23PktSfpBQ6D8ZxoMfF0L7V2GQMaUg+3r7ucrx82kpqotjv0xHghNIm95aBr1Qw" + 744 "1gaEjsC/0wGmmBDg1dTDH+F1p9TInzr3EFuYD0YiQ7YlAHq3cPuyGoLXJ5dXYuSBfhDXJSeddUkl" + 745 "k1ufZyOOcskeInQge7jzaRfmKg3U94r+spMEvb0AzDQVOKvjjo1ivxMSgFRZaDb/4qw="; 746 747 /** 748 * Defines the keystore contents for the server, JKS version. Holds just a 749 * single self-generated key. The subject name is "Test Server". 750 */ 751 private static final String SERVER_KEYS_JKS = 752 "/u3+7QAAAAIAAAABAAAAAQAFbXlrZXkAAAEaWFfBeAAAArowggK2MA4GCisGAQQBKgIRAQEFAASC" + 753 "AqI2kp5XjnF8YZkhcF92YsJNQkvsmH7zqMM87j23zSoV4DwyE3XeC/gZWq1ToScIhoqZkzlbWcu4" + 754 "T/Zfc/DrfGk/rKbBL1uWKGZ8fMtlZk8KoAhxZk1JSyJvdkyKxqmzUbxk1OFMlN2VJNu97FPVH+du" + 755 "dvjTvmpdoM81INWBW/1fZJeQeDvn4mMbbe0IxgpiLnI9WSevlaDP/sm1X3iO9yEyzHLL+M5Erspo" + 756 "Cwa558fOu5DdsICMXhvDQxjWFKFhPHnKtGe+VvwkG9/bAaDgx3kfhk0w5zvdnkKb+8Ed9ylNRzdk" + 757 "ocAa/mxlMTOsTvDKXjjsBupNPIIj7OP4GNnZaxkJjSs98pEO67op1GX2qhy6FSOPNuq8k/65HzUc" + 758 "PYn6voEeh6vm02U/sjEnzRevQ2+2wXoAdp0EwtQ/DlMe+NvcwPGWKuMgX4A4L93DZGb04N2VmAU3" + 759 "YLOtZwTO0LbuWrcCM/q99G/7LcczkxIVrO2I/rh8RXVczlf9QzcrFObFv4ATuspWJ8xG7DhsMbnk" + 760 "rT94Pq6TogYeoz8o8ZMykesAqN6mt/9+ToIemmXv+e+KU1hI5oLwWMnUG6dXM6hIvrULY6o+QCPH" + 761 "172YQJMa+68HAeS+itBTAF4Clm/bLn6reHCGGU6vNdwU0lYldpiOj9cB3t+u2UuLo6tiFWjLf5Zs" + 762 "EQJETd4g/EK9nHxJn0GAKrWnTw7pEHQJ08elzUuy04C/jEEG+4QXU1InzS4o/kR0Sqz2WTGDoSoq" + 763 "ewuPRU5bzQs/b9daq3mXrnPtRBL6HfSDAdpTK76iHqLCGdqx3avHjVSBm4zFvEuYBCev+3iKOBmg" + 764 "yh7eQRTjz4UOWfy85omMBr7lK8PtfVBDzOXpasxS0uBgdUyBDX4tO6k9jZ8a1kmQRQAAAAEABVgu" + 765 "NTA5AAACSDCCAkQwggGtAgRIR8SKMA0GCSqGSIb3DQEBBAUAMGkxCzAJBgNVBAYTAlVTMRMwEQYD" + 766 "VQQIEwpDYWxpZm9ybmlhMQwwCgYDVQQHEwNNVFYxDzANBgNVBAoTBkdvb2dsZTEQMA4GA1UECxMH" + 767 "QW5kcm9pZDEUMBIGA1UEAxMLVGVzdCBTZXJ2ZXIwHhcNMDgwNjA1MTA0ODQyWhcNMDgwOTAzMTA0" + 768 "ODQyWjBpMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEMMAoGA1UEBxMDTVRWMQ8w" + 769 "DQYDVQQKEwZHb29nbGUxEDAOBgNVBAsTB0FuZHJvaWQxFDASBgNVBAMTC1Rlc3QgU2VydmVyMIGf" + 770 "MA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCwoC6chqCI84rj1PrXuJgbiit4EV909zR6N0jNlYfg" + 771 "itwB39bP39wH03rFm8T59b3mbSptnGmCIpLZn25KPPFsYD3JJ+wFlmiUdEP9H05flfwtFQJnw9uT" + 772 "3rRIdYVMPcQ3RoZzwAMliGr882I2thIDbA6xjGU/1nRIdvk0LtxH3QIDAQABMA0GCSqGSIb3DQEB" + 773 "BAUAA4GBAJn+6YgUlY18Ie+0+Vt8oEi81DNi/bfPrAUAh63fhhBikx/3R9dl3wh09Z6p7cIdNxjW" + 774 "n2ll+cRW9eqF7z75F0Omm0C7/KAEPjukVbszmzeU5VqzkpSt0j84YWi+TfcHRrfvhLbrlmGITVpY" + 775 "ol5pHLDyqGmDs53pgwipWqsn/nEXEBgj3EoqPeqHbDf7YaP8h/5BSt0="; 776 777 protected int startServer(String name) { 778 String keys = useBKS ? SERVER_KEYS_BKS : SERVER_KEYS_JKS; 779 TestServer server = new TestServer(true, keys); 780 Thread serverThread = new Thread(server); 781 serverThread.start(); 782 try { 783 while (!serverReady) { 784 Thread.currentThread().sleep(50); 785 } 786 // give the server 100 millis to accept 787 Thread.currentThread().sleep(100); 788 } catch (InterruptedException e) { 789 // ignore 790 } 791 return server.sport; 792 } 793 794 /** 795 * Implements a test SSL socket server. It wait for a connection on a given 796 * port, requests client authentication (if specified), and read 256 bytes 797 * from the socket. 798 */ 799 class TestServer implements Runnable { 800 801 public static final int CLIENT_AUTH_NONE = 0; 802 803 public static final int CLIENT_AUTH_WANTED = 1; 804 805 public static final int CLIENT_AUTH_NEEDED = 2; 806 807 private TestTrustManager trustManager; 808 809 private Exception exception; 810 811 String keys; 812 813 private boolean provideKeys; 814 815 int sport; 816 817 public TestServer(boolean provideKeys, String keys) { 818 this.keys = keys; 819 this.provideKeys = provideKeys; 820 821 trustManager = new TestTrustManager(); 822 } 823 824 public void run() { 825 try { 826 KeyManager[] keyManagers = provideKeys ? getKeyManagers(keys) : null; 827 TrustManager[] trustManagers = new TrustManager[] { trustManager }; 828 829 SSLContext sslContext = SSLContext.getInstance("TLS"); 830 sslContext.init(keyManagers, trustManagers, null); 831 832 SSLServerSocket serverSocket = (SSLServerSocket)sslContext.getServerSocketFactory().createServerSocket(); 833 834 serverSocket.bind(new InetSocketAddress(port)); 835 sport = serverSocket.getLocalPort(); 836 serverReady = true; 837 838 SSLSocket clientSocket = (SSLSocket)serverSocket.accept(); 839 840 InputStream stream = clientSocket.getInputStream(); 841 842 for (int i = 0; i < 256; i++) { 843 int j = stream.read(); 844 if (i != j) { 845 throw new RuntimeException("Error reading socket, expected " + i + ", got " + j); 846 } 847 } 848 849 stream.close(); 850 clientSocket.close(); 851 serverSocket.close(); 852 853 } catch (Exception ex) { 854 exception = ex; 855 } 856 } 857 858 public Exception getException() { 859 return exception; 860 } 861 862 public X509Certificate[] getChain() { 863 return trustManager.getChain(); 864 } 865 866 } 867 868 /** 869 * Loads a keystore from a base64-encoded String. Returns the KeyManager[] 870 * for the result. 871 */ 872 private KeyManager[] getKeyManagers(String keys) throws Exception { 873 byte[] bytes = Base64.decode(keys.getBytes()); 874 InputStream inputStream = new ByteArrayInputStream(bytes); 875 876 KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType()); 877 keyStore.load(inputStream, PASSWORD.toCharArray()); 878 inputStream.close(); 879 880 String algorithm = KeyManagerFactory.getDefaultAlgorithm(); 881 KeyManagerFactory keyManagerFactory = KeyManagerFactory.getInstance(algorithm); 882 keyManagerFactory.init(keyStore, PASSWORD.toCharArray()); 883 884 return keyManagerFactory.getKeyManagers(); 885 } 886 887 private SSLSocket getSSLSocket() throws IOException { 888 SSLSocket ssl = null; 889 ssl = (SSLSocket) SSLSocketFactory.getDefault().createSocket(); 890 return ssl; 891 } 892 893 private SSLSocket getSSLSocket(InetAddress host, int port) throws IOException { 894 SSLSocket ssl = null; 895 ssl = (SSLSocket) SSLSocketFactory.getDefault().createSocket(host, port); 896 return ssl; 897 } 898 899 private SSLSocket getSSLSocket(String host, int port) throws UnknownHostException, IOException { 900 SSLSocket ssl = null; 901 ssl = (SSLSocket) SSLSocketFactory.getDefault().createSocket(host, port); 902 return ssl; 903 } 904 905 private SSLSocket getSSLSocket(InetAddress host, int port, InetAddress localHost, int localPort) throws IOException { 906 SSLSocket ssl = null; 907 ssl = (SSLSocket) SSLSocketFactory.getDefault().createSocket(host, port, localHost, localPort); 908 return ssl; 909 } 910 911 private SSLSocket getSSLSocket(String host, int port, InetAddress localHost, int localPort) throws UnknownHostException, IOException { 912 SSLSocket ssl = null; 913 ssl = (SSLSocket) SSLSocketFactory.getDefault().createSocket(host, port, localHost, localPort); 914 return ssl; 915 } 916 } 917