/external/apache-harmony/x-net/src/test/impl/java.injected/org/apache/harmony/xnet/provider/jsse/SSLSocketImplTest.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 1156 lines · 942 code · 93 blank · 121 comment · 128 complexity · 4ddd19b7de8b4b80da7eb8cdf3aa9d77 MD5 · raw file

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