/platform/libcore/x-net/src/test/java/tests/api/javax/net/ssl/SSLSocketTest.java

https://github.com/lems111/Intercept-CM6-Kernel · Java · 892 lines · 659 code · 84 blank · 149 comment · 7 complexity · 771dcaff166c3aa9d89063a26a0a63f6 MD5 · raw file

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