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

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk · Java · 464 lines · 392 code · 24 blank · 48 comment · 62 complexity · 5c2610fd2d393c4416bd2fa6e3c0dc53 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.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.net.InetAddress;
  22. import java.net.InetSocketAddress;
  23. import java.net.Socket;
  24. import java.util.Arrays;
  25. import javax.net.ssl.SSLServerSocket;
  26. import javax.net.ssl.SSLServerSocketFactory;
  27. import javax.net.ssl.SSLSocket;
  28. import javax.net.ssl.SSLSocketFactory;
  29. import junit.framework.Test;
  30. import junit.framework.TestCase;
  31. import junit.framework.TestSuite;
  32. /**
  33. * SSLSocketImplTest
  34. */
  35. public class SSLSocketFactoriesTest extends TestCase {
  36. // turn on/off the debug logging
  37. private boolean doLog = false;
  38. /**
  39. * Sets up the test case.
  40. */
  41. @Override
  42. public void setUp() throws Exception {
  43. super.setUp();
  44. if (doLog) {
  45. System.out.println("========================");
  46. System.out.println("====== Running the test: " + getName());
  47. System.out.println("========================");
  48. }
  49. }
  50. @Override
  51. public void tearDown() throws Exception {
  52. super.tearDown();
  53. }
  54. /**
  55. * Tests default initialized factories.
  56. */
  57. public void testDefaultInitialized() throws Exception {
  58. SSLServerSocketFactory ssfactory =
  59. (SSLServerSocketFactory) SSLServerSocketFactory.getDefault();
  60. SSLSocketFactory sfactory =
  61. (SSLSocketFactory) SSLSocketFactory.getDefault();
  62. assertNotNull(ssfactory.getDefaultCipherSuites());
  63. assertNotNull(ssfactory.getSupportedCipherSuites());
  64. assertNotNull(ssfactory.createServerSocket());
  65. assertNotNull(sfactory.getDefaultCipherSuites());
  66. assertNotNull(sfactory.getSupportedCipherSuites());
  67. assertNotNull(sfactory.createSocket());
  68. }
  69. public void testSocketCreation() throws Throwable {
  70. SSLSocketFactory socketFactory
  71. = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
  72. SSLServerSocketFactory serverSocketFactory
  73. = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
  74. String[] enabled = {"TLS_RSA_WITH_RC4_128_MD5"};
  75. for (int i=0; i<4; i++) {
  76. SSLServerSocket ssocket;
  77. switch (i) {
  78. case 0:
  79. if (doLog) {
  80. System.out.println(
  81. "*** ServerSocketFactory.createServerSocket()");
  82. }
  83. ssocket = (SSLServerSocket)
  84. serverSocketFactory.createServerSocket();
  85. ssocket.bind(null);
  86. break;
  87. case 1:
  88. if (doLog) {
  89. System.out.println(
  90. "*** ServerSocketFactory.createServerSocket(int)");
  91. }
  92. ssocket = (SSLServerSocket)
  93. serverSocketFactory.createServerSocket(0);
  94. break;
  95. case 2:
  96. if (doLog) {
  97. System.out.println(
  98. "*** ServerSocketFactory.createServerSocket(int,int)");
  99. }
  100. ssocket = (SSLServerSocket)
  101. serverSocketFactory.createServerSocket(0, 6);
  102. break;
  103. default:
  104. if (doLog) {
  105. System.out.println("*** ServerSocketFactory."
  106. + "createServerSocket(int,int,InetAddress)");
  107. }
  108. ssocket = (SSLServerSocket)
  109. serverSocketFactory.createServerSocket(0, 6, null);
  110. break;
  111. }
  112. ssocket.setUseClientMode(false);
  113. ssocket.setEnabledCipherSuites(enabled);
  114. for (int j=0; j<6; j++) {
  115. SSLSocket csocket;
  116. switch (j) {
  117. case 0:
  118. if (doLog) {
  119. System.out.println(
  120. "=== SocketFactory.createSocket()");
  121. }
  122. csocket = (SSLSocket) socketFactory.createSocket();
  123. csocket.connect(
  124. new InetSocketAddress("localhost",
  125. ssocket.getLocalPort()));
  126. break;
  127. case 1:
  128. if (doLog) {
  129. System.out.println(
  130. "=== SocketFactory.createSocket(String,int)");
  131. }
  132. csocket = (SSLSocket)
  133. socketFactory.createSocket("localhost",
  134. ssocket.getLocalPort());
  135. break;
  136. case 2:
  137. if (doLog) {
  138. System.out.println("=== SocketFactory.createSocket("
  139. + "String,int,InetAddress,int)");
  140. }
  141. csocket = (SSLSocket)
  142. socketFactory.createSocket("localhost",
  143. ssocket.getLocalPort(),
  144. InetAddress.getByName("localhost"), 0);
  145. break;
  146. case 3:
  147. if (doLog) {
  148. System.out.println("=== SocketFactory.createSocket("
  149. + "InetAddress,int)");
  150. }
  151. csocket = (SSLSocket) socketFactory.createSocket(
  152. InetAddress.getByName("localhost"),
  153. ssocket.getLocalPort());
  154. break;
  155. case 4:
  156. if (doLog) {
  157. System.out.println("=== SocketFactory.createSocket("
  158. + "InetAddress,int,InetAddress,int)");
  159. }
  160. csocket = (SSLSocket) socketFactory.createSocket(
  161. InetAddress.getByName("localhost"),
  162. ssocket.getLocalPort(),
  163. InetAddress.getByName("localhost"),
  164. 0);
  165. break;
  166. default:
  167. if (doLog) {
  168. System.out.println(
  169. "=== SSLSocketFactory.createSocket("
  170. + "socket,String,int,boolean)");
  171. }
  172. Socket socket = new Socket(
  173. InetAddress.getByName("localhost"),
  174. ssocket.getLocalPort());
  175. csocket = (SSLSocket) socketFactory.createSocket(
  176. socket, "localhost", ssocket.getLocalPort(),
  177. true);
  178. break;
  179. }
  180. csocket.setUseClientMode(true);
  181. csocket.setEnabledCipherSuites(enabled);
  182. doTest(ssocket, csocket);
  183. }
  184. }
  185. }
  186. /**
  187. * SSLSocketFactory.getSupportedCipherSuites() method testing.
  188. */
  189. public void testGetSupportedCipherSuites1() throws Exception {
  190. SSLSocketFactory socketFactory
  191. = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
  192. String[] supported = socketFactory.getSupportedCipherSuites();
  193. assertNotNull(supported);
  194. supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
  195. supported = socketFactory.getSupportedCipherSuites();
  196. for (int i=0; i<supported.length; i++) {
  197. if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
  198. fail("Modification of the returned result "
  199. + "causes the modification of the internal state");
  200. }
  201. }
  202. }
  203. /**
  204. * SSLServerSocketFactory.getSupportedCipherSuites() method testing.
  205. */
  206. public void testGetSupportedCipherSuites2() throws Exception {
  207. SSLServerSocketFactory serverSocketFactory
  208. = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
  209. String[] supported = serverSocketFactory.getSupportedCipherSuites();
  210. assertNotNull(supported);
  211. supported[0] = "NOT_SUPPORTED_CIPHER_SUITE";
  212. supported = serverSocketFactory.getSupportedCipherSuites();
  213. for (int i=0; i<supported.length; i++) {
  214. if ("NOT_SUPPORTED_CIPHER_SUITE".equals(supported[i])) {
  215. fail("Modification of the returned result "
  216. + "causes the modification of the internal state");
  217. }
  218. }
  219. }
  220. /**
  221. * SSLSocketFactory.getDefaultCipherSuites() method testing.
  222. */
  223. public void testGetDefaultCipherSuites1() throws Exception {
  224. SSLSocketFactory socketFactory
  225. = new SSLSocketFactoryImpl(JSSETestData.getSSLParameters());
  226. String[] supported = socketFactory.getSupportedCipherSuites();
  227. String[] defaultcs = socketFactory.getDefaultCipherSuites();
  228. assertNotNull(supported);
  229. assertNotNull(defaultcs);
  230. for (int i=0; i<defaultcs.length; i++) {
  231. found: {
  232. for (int j=0; j<supported.length; j++) {
  233. if (defaultcs[i].equals(supported[j])) {
  234. break found;
  235. }
  236. }
  237. fail("Default suite does not belong to the set "
  238. + "of supported cipher suites: " + defaultcs[i]);
  239. }
  240. }
  241. }
  242. /**
  243. * SSLServerSocketFactory.getDefaultCipherSuites() method testing.
  244. */
  245. public void testGetDefaultCipherSuites2() throws Exception {
  246. SSLServerSocketFactory serverSocketFactory
  247. = new SSLServerSocketFactoryImpl(JSSETestData.getSSLParameters());
  248. String[] supported = serverSocketFactory.getSupportedCipherSuites();
  249. String[] defaultcs = serverSocketFactory.getDefaultCipherSuites();
  250. assertNotNull(supported);
  251. assertNotNull(defaultcs);
  252. for (int i=0; i<defaultcs.length; i++) {
  253. found: {
  254. for (int j=0; j<supported.length; j++) {
  255. if (defaultcs[i].equals(supported[j])) {
  256. break found;
  257. }
  258. }
  259. fail("Default suite does not belong to the set "
  260. + "of supported cipher suites: " + defaultcs[i]);
  261. }
  262. }
  263. }
  264. /**
  265. * Performs SSL connection between the sockets
  266. * @return
  267. */
  268. public void doTest(SSLServerSocket ssocket, SSLSocket csocket)
  269. throws Throwable {
  270. final String server_message = "Hello from SSL Server Socket!";
  271. final String client_message = "Hello from SSL Socket!";
  272. Thread server = null;
  273. Thread client = null;
  274. final Throwable[] throwed = new Throwable[1];
  275. try {
  276. final SSLServerSocket ss = ssocket;
  277. final SSLSocket s = csocket;
  278. server = new Thread() {
  279. @Override
  280. public void run() {
  281. InputStream is = null;
  282. OutputStream os = null;
  283. SSLSocket s = null;
  284. try {
  285. s = (SSLSocket) ss.accept();
  286. if (doLog) {
  287. System.out.println("Socket accepted: " + s);
  288. }
  289. is = s.getInputStream();
  290. os = s.getOutputStream();
  291. // send the message to the client
  292. os.write(server_message.getBytes());
  293. // read the response
  294. byte[] buff = new byte[client_message.length()];
  295. int len = is.read(buff);
  296. if (doLog) {
  297. System.out.println("Received message of length "
  298. + len + ": '" + new String(buff, 0, len)+"'");
  299. }
  300. assertTrue("Read message does not equal to expected",
  301. Arrays.equals(client_message.getBytes(), buff));
  302. os.write(-1);
  303. assertEquals("Read data differs from expected",
  304. 255, is.read());
  305. if (doLog) {
  306. System.out.println("Server is closed: "
  307. +s.isClosed());
  308. }
  309. assertEquals("Returned value should be -1",
  310. // initiate an exchange of closure alerts
  311. -1, is.read());
  312. if (doLog) {
  313. System.out.println("Server is closed: "
  314. +s.isClosed());
  315. }
  316. assertEquals("Returned value should be -1",
  317. // initiate an exchange of closure alerts
  318. -1, is.read());
  319. } catch (Throwable e) {
  320. synchronized (throwed) {
  321. if (doLog) {
  322. e.printStackTrace();
  323. }
  324. if (throwed[0] == null) {
  325. throwed[0] = e;
  326. }
  327. }
  328. } finally {
  329. try {
  330. if (is != null) {
  331. is.close();
  332. }
  333. } catch (IOException ex) {}
  334. try {
  335. if (os != null) {
  336. os.close();
  337. }
  338. } catch (IOException ex) {}
  339. try {
  340. if (s != null) {
  341. s.close();
  342. }
  343. } catch (IOException ex) {}
  344. }
  345. }
  346. };
  347. client = new Thread() {
  348. @Override
  349. public void run() {
  350. InputStream is = null;
  351. OutputStream os = null;
  352. try {
  353. assertTrue("Client was not connected", s.isConnected());
  354. if (doLog) {
  355. System.out.println("Client connected");
  356. }
  357. is = s.getInputStream();
  358. os = s.getOutputStream();
  359. s.startHandshake();
  360. if (doLog) {
  361. System.out.println("Client: HS was done");
  362. }
  363. // read the message from the server
  364. byte[] buff = new byte[server_message.length()];
  365. int len = is.read(buff);
  366. if (doLog) {
  367. System.out.println("Received message of length "
  368. + len + ": '" + new String(buff, 0, len)+"'");
  369. }
  370. assertTrue("Read message does not equal to expected",
  371. Arrays.equals(server_message.getBytes(), buff));
  372. // send the response
  373. buff = (" "+client_message+" ").getBytes();
  374. os.write(buff, 1, buff.length-2);
  375. assertEquals("Read data differs from expected",
  376. 255, is.read());
  377. os.write(-1);
  378. if (doLog) {
  379. System.out.println("Client is closed: "
  380. +s.isClosed());
  381. }
  382. s.close();
  383. if (doLog) {
  384. System.out.println("Client is closed: "
  385. +s.isClosed());
  386. }
  387. } catch (Throwable e) {
  388. synchronized (throwed) {
  389. if (doLog) {
  390. e.printStackTrace();
  391. }
  392. if (throwed[0] == null) {
  393. throwed[0] = e;
  394. }
  395. }
  396. } finally {
  397. try {
  398. if (is != null) {
  399. is.close();
  400. }
  401. } catch (IOException ex) {}
  402. try {
  403. if (os != null) {
  404. os.close();
  405. }
  406. } catch (IOException ex) {}
  407. try {
  408. if (s != null) {
  409. s.close();
  410. }
  411. } catch (IOException ex) {}
  412. }
  413. }
  414. };
  415. server.start();
  416. client.start();
  417. while (server.isAlive() || client.isAlive()) {
  418. if (throwed[0] != null) {
  419. throw throwed[0];
  420. }
  421. try {
  422. Thread.sleep(500);
  423. } catch (Exception e) { }
  424. }
  425. } finally {
  426. if (server != null) {
  427. server.stop();
  428. }
  429. if (client != null) {
  430. client.stop();
  431. }
  432. }
  433. if (throwed[0] != null) {
  434. throw throwed[0];
  435. }
  436. }
  437. public static Test suite() {
  438. return new TestSuite(SSLSocketFactoriesTest.class);
  439. }
  440. }