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

https://github.com/lems111/Intercept-CM6-Kernel · Java · 408 lines · 307 code · 44 blank · 57 comment · 10 complexity · f3cdeaeaffef32524e8eb22d3d8034cc 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. /**
  18. * @author Boris V. Kuznetsov
  19. * @version $Revision$
  20. */
  21. package tests.api.javax.net;
  22. import dalvik.annotation.TestTargetClass;
  23. import dalvik.annotation.TestLevel;
  24. import dalvik.annotation.TestTargetNew;
  25. import java.io.IOException;
  26. import java.net.InetAddress;
  27. import java.net.ServerSocket;
  28. import java.net.Socket;
  29. import java.net.SocketException;
  30. import java.net.UnknownHostException;
  31. import javax.net.SocketFactory;
  32. import junit.framework.TestCase;
  33. import tests.support.Support_PortManager;
  34. /**
  35. * Tests for <code>SocketFactory</code> class methods.
  36. */
  37. @TestTargetClass(SocketFactory.class)
  38. public class SocketFactoryTest extends TestCase {
  39. /**
  40. * @tests javax.net.SocketFactory#SocketFactory()
  41. */
  42. @TestTargetNew(
  43. level = TestLevel.COMPLETE,
  44. notes = "",
  45. method = "SocketFactory",
  46. args = {}
  47. )
  48. public void test_Constructor() {
  49. try {
  50. MySocketFactory sf = new MySocketFactory();
  51. } catch (Exception e) {
  52. fail("Unexpected exception " + e.toString());
  53. }
  54. }
  55. /**
  56. * @tests javax.net.SocketFactory#createSocket()
  57. */
  58. @TestTargetNew(
  59. level = TestLevel.SUFFICIENT,
  60. notes = "IOException check missed",
  61. method = "createSocket",
  62. args = {}
  63. )
  64. public final void test_createSocket_01() {
  65. SocketFactory sf = SocketFactory.getDefault();
  66. try {
  67. Socket s = sf.createSocket();
  68. assertNotNull(s);
  69. assertEquals(-1, s.getLocalPort());
  70. assertEquals(0, s.getPort());
  71. } catch (Exception e) {
  72. fail("Unexpected exception: " + e);
  73. }
  74. MySocketFactory msf = new MySocketFactory();
  75. try {
  76. msf.createSocket();
  77. fail("No expected SocketException");
  78. } catch (SocketException e) {
  79. } catch (IOException e) {
  80. fail(e.toString());
  81. }
  82. }
  83. /**
  84. * @tests javax.net.SocketFactory#createSocket(String host, int port)
  85. */
  86. @TestTargetNew(
  87. level = TestLevel.COMPLETE,
  88. notes = "",
  89. method = "createSocket",
  90. args = {String.class, int.class}
  91. )
  92. public final void test_createSocket_02() {
  93. SocketFactory sf = SocketFactory.getDefault();
  94. int portNumber = Support_PortManager.getNextPort();
  95. int sport = startServer("Cons String,I");
  96. int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
  97. try {
  98. Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport);
  99. assertNotNull(s);
  100. assertTrue("Failed to create socket", s.getPort() == sport);
  101. } catch (Exception e) {
  102. fail("Unexpected exception: " + e);
  103. }
  104. try {
  105. Socket s = sf.createSocket("bla-bla", sport);
  106. fail("UnknownHostException wasn't thrown");
  107. } catch (UnknownHostException uhe) {
  108. //expected
  109. } catch (Exception e) {
  110. fail(e + " was thrown instead of UnknownHostException");
  111. }
  112. for (int i = 0; i < invalidPorts.length; i++) {
  113. try {
  114. Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i]);
  115. fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
  116. } catch (IllegalArgumentException iae) {
  117. //expected
  118. } catch (Exception e) {
  119. fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
  120. }
  121. }
  122. try {
  123. Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), portNumber);
  124. fail("IOException wasn't thrown");
  125. } catch (IOException ioe) {
  126. //expected
  127. }
  128. SocketFactory f = SocketFactory.getDefault();
  129. try {
  130. Socket s = f.createSocket("localhost", 8082);
  131. fail("IOException wasn't thrown ...");
  132. } catch (IOException e) {
  133. }
  134. }
  135. /**
  136. * @tests javax.net.SocketFactory#createSocket(InetAddress host, int port)
  137. */
  138. @TestTargetNew(
  139. level = TestLevel.COMPLETE,
  140. notes = "",
  141. method = "createSocket",
  142. args = {InetAddress.class, int.class}
  143. )
  144. public final void test_createSocket_03() {
  145. SocketFactory sf = SocketFactory.getDefault();
  146. int portNumber = Support_PortManager.getNextPort();
  147. int sport = startServer("Cons InetAddress,I");
  148. int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
  149. try {
  150. Socket s = sf.createSocket(InetAddress.getLocalHost(), sport);
  151. assertNotNull(s);
  152. assertTrue("Failed to create socket", s.getPort() == sport);
  153. } catch (Exception e) {
  154. fail("Unexpected exception: " + e);
  155. }
  156. for (int i = 0; i < invalidPorts.length; i++) {
  157. try {
  158. Socket s = sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i]);
  159. fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
  160. } catch (IllegalArgumentException iae) {
  161. //expected
  162. } catch (Exception e) {
  163. fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
  164. }
  165. }
  166. try {
  167. Socket s = sf.createSocket(InetAddress.getLocalHost(), portNumber);
  168. fail("IOException wasn't thrown");
  169. } catch (IOException ioe) {
  170. //expected
  171. }
  172. SocketFactory f = SocketFactory.getDefault();
  173. try {
  174. Socket s = f.createSocket(InetAddress.getLocalHost(), 8081);
  175. fail("IOException wasn't thrown ...");
  176. } catch (IOException e) {
  177. }
  178. }
  179. /**
  180. * @tests javax.net.SocketFactory#createSocket(InetAddress address, int port,
  181. * InetAddress localAddress, int localPort)
  182. */
  183. @TestTargetNew(
  184. level = TestLevel.COMPLETE,
  185. notes = "",
  186. method = "createSocket",
  187. args = {InetAddress.class, int.class, InetAddress.class, int.class}
  188. )
  189. public final void test_createSocket_04() {
  190. SocketFactory sf = SocketFactory.getDefault();
  191. int portNumber = Support_PortManager.getNextPort();
  192. int sport = startServer("Cons InetAddress,I,InetAddress,I");
  193. int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
  194. try {
  195. Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
  196. InetAddress.getLocalHost(), portNumber);
  197. assertNotNull(s);
  198. assertTrue("1: Failed to create socket", s.getPort() == sport);
  199. assertTrue("2: Failed to create socket", s.getLocalPort() == portNumber);
  200. } catch (Exception e) {
  201. fail("Unexpected exception: " + e);
  202. }
  203. for (int i = 0; i < invalidPorts.length; i++) {
  204. try {
  205. Socket s = sf.createSocket(InetAddress.getLocalHost(), invalidPorts[i],
  206. InetAddress.getLocalHost(), portNumber);
  207. fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
  208. } catch (IllegalArgumentException iae) {
  209. //expected
  210. } catch (Exception e) {
  211. fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
  212. }
  213. try {
  214. Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
  215. InetAddress.getLocalHost(), invalidPorts[i]);
  216. fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
  217. } catch (IllegalArgumentException iae) {
  218. //expected
  219. } catch (Exception e) {
  220. fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
  221. }
  222. }
  223. try {
  224. Socket s = sf.createSocket(InetAddress.getLocalHost(), sport,
  225. InetAddress.getLocalHost(), portNumber);
  226. fail("IOException wasn't thrown");
  227. } catch (IOException ioe) {
  228. //expected
  229. }
  230. SocketFactory f = SocketFactory.getDefault();
  231. try {
  232. Socket s = f.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082);
  233. fail("IOException wasn't thrown ...");
  234. } catch (IOException e) {
  235. }
  236. }
  237. /**
  238. * @tests javax.net.SocketFactory#createSocket(String host, int port,
  239. * InetAddress localHost, int localPort)
  240. */
  241. @TestTargetNew(
  242. level = TestLevel.COMPLETE,
  243. notes = "",
  244. method = "createSocket",
  245. args = {String.class, int.class, InetAddress.class, int.class}
  246. )
  247. public final void test_createSocket_05() {
  248. SocketFactory sf = SocketFactory.getDefault();
  249. int portNumber = Support_PortManager.getNextPort();
  250. int sport = startServer("Cons String,I,InetAddress,I");
  251. int[] invalidPorts = {Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE};
  252. try {
  253. Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
  254. InetAddress.getLocalHost(), portNumber);
  255. assertNotNull(s);
  256. assertTrue("1: Failed to create socket", s.getPort() == sport);
  257. assertTrue("2: Failed to create socket", s.getLocalPort() == portNumber);
  258. } catch (Exception e) {
  259. fail("Unexpected exception: " + e);
  260. }
  261. portNumber = Support_PortManager.getNextPort();
  262. try {
  263. Socket s = sf.createSocket("bla-bla", sport, InetAddress.getLocalHost(), portNumber);
  264. fail("UnknownHostException wasn't thrown");
  265. } catch (UnknownHostException uhe) {
  266. //expected
  267. } catch (Exception e) {
  268. fail(e + " was thrown instead of UnknownHostException");
  269. }
  270. for (int i = 0; i < invalidPorts.length; i++) {
  271. portNumber = Support_PortManager.getNextPort();
  272. try {
  273. Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), invalidPorts[i],
  274. InetAddress.getLocalHost(), portNumber);
  275. fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
  276. } catch (IllegalArgumentException iae) {
  277. //expected
  278. } catch (Exception e) {
  279. fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
  280. }
  281. try {
  282. Socket s = sf.createSocket(InetAddress.getLocalHost().getHostName(), sport,
  283. InetAddress.getLocalHost(), invalidPorts[i]);
  284. fail("IllegalArgumentException wasn't thrown for " + invalidPorts[i]);
  285. } catch (IllegalArgumentException iae) {
  286. //expected
  287. } catch (Exception e) {
  288. fail(e + " was thrown instead of IllegalArgumentException for " + invalidPorts[i]);
  289. }
  290. }
  291. SocketFactory f = SocketFactory.getDefault();
  292. try {
  293. Socket s = f.createSocket("localhost", 8081, InetAddress.getLocalHost(), 8082);
  294. fail("IOException wasn't thrown ...");
  295. } catch (IOException e) {
  296. }
  297. }
  298. /**
  299. * @tests javax.net.SocketFactory#getDefault()
  300. */
  301. @TestTargetNew(
  302. level = TestLevel.COMPLETE,
  303. notes = "",
  304. method = "getDefault",
  305. args = {}
  306. )
  307. public final void test_getDefault() {
  308. SocketFactory sf = SocketFactory.getDefault();
  309. Socket s;
  310. try {
  311. s = sf.createSocket("localhost", 8082);
  312. s.close();
  313. } catch (IOException e) {
  314. }
  315. try {
  316. s = sf.createSocket("localhost", 8081, InetAddress.getLocalHost(), 8082);
  317. s.close();
  318. } catch (IOException e) {
  319. }
  320. try {
  321. s = sf.createSocket(InetAddress.getLocalHost(), 8081);
  322. s.close();
  323. } catch (IOException e) {
  324. }
  325. try {
  326. s = sf.createSocket(InetAddress.getLocalHost(), 8081, InetAddress.getLocalHost(), 8082);
  327. s.close();
  328. } catch (IOException e) {
  329. }
  330. }
  331. protected int startServer(String name) {
  332. int portNumber = Support_PortManager.getNextPort();
  333. ServerSocket ss = null;
  334. try {
  335. ss = new ServerSocket(portNumber);
  336. } catch (IOException e) {
  337. fail(name + ": " + e);
  338. }
  339. return ss.getLocalPort();
  340. }
  341. }
  342. class MySocketFactory extends SocketFactory {
  343. public MySocketFactory() {
  344. super();
  345. }
  346. @Override
  347. public Socket createSocket(String host, int port) throws IOException, UnknownHostException {
  348. return null;
  349. }
  350. @Override
  351. public Socket createSocket(String host, int port, InetAddress localHost, int localPort)
  352. throws IOException, UnknownHostException {
  353. return null;
  354. }
  355. @Override
  356. public Socket createSocket(InetAddress host, int port) throws IOException {
  357. return null;
  358. }
  359. @Override
  360. public Socket createSocket(InetAddress address, int port,
  361. InetAddress localAddress, int localPort) throws IOException {
  362. return null;
  363. }
  364. }