PageRenderTime 121ms CodeModel.GetById 45ms RepoModel.GetById 18ms app.codeStats 0ms

/luni/src/test/java/libcore/java/net/OldSocketTest.java

https://github.com/PAmoto/android_libcore
Java | 2623 lines | 2020 code | 325 blank | 278 comment | 81 complexity | 3db3c7a930b50371665dfa2c4b464775 MD5 | raw file
Possible License(s): JSON, BSD-3-Clause
  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 libcore.java.net;
  18. import java.io.IOException;
  19. import java.io.InputStream;
  20. import java.io.OutputStream;
  21. import java.net.ConnectException;
  22. import java.net.Inet4Address;
  23. import java.net.Inet6Address;
  24. import java.net.InetAddress;
  25. import java.net.InetSocketAddress;
  26. import java.net.Proxy;
  27. import java.net.ServerSocket;
  28. import java.net.Socket;
  29. import java.net.SocketAddress;
  30. import java.net.SocketException;
  31. import java.net.SocketImpl;
  32. import java.net.SocketTimeoutException;
  33. import java.net.UnknownHostException;
  34. import java.nio.channels.IllegalBlockingModeException;
  35. import java.nio.channels.SocketChannel;
  36. import java.security.Permission;
  37. import tests.support.Support_Configuration;
  38. import tests.support.Support_PortManager;
  39. public class OldSocketTest extends OldSocketTestCase {
  40. ServerSocket ss;
  41. Socket s;
  42. Thread t;
  43. SecurityManager sm = new SecurityManager() {
  44. public void checkPermission(Permission perm) {}
  45. public void checkConnect(String host, int port) {
  46. throw new SecurityException();
  47. }
  48. };
  49. public void test_Constructor() {
  50. // create the socket and then validate some basic state
  51. s = new Socket();
  52. assertFalse("new socket should not be connected", s.isConnected());
  53. assertFalse("new socket should not be bound", s.isBound());
  54. assertFalse("new socket should not be closed", s.isClosed());
  55. assertFalse("new socket should not be in InputShutdown", s
  56. .isInputShutdown());
  57. assertFalse("new socket should not be in OutputShutdown", s
  58. .isOutputShutdown());
  59. }
  60. public void test_ConstructorLjava_lang_StringI() throws IOException {
  61. // Test for method java.net.Socket(java.lang.String, int)
  62. int sport = startServer("Cons String,I");
  63. s = new Socket(InetAddress.getLocalHost().getHostName(), sport);
  64. assertTrue("Failed to create socket", s.getPort() == sport);
  65. //regression for HARMONY-946
  66. ServerSocket ss = null;
  67. Socket s = null;
  68. try {
  69. ss = new ServerSocket(0);
  70. s = new Socket("0.0.0.0", ss.getLocalPort());
  71. } finally {
  72. try {
  73. ss.close();
  74. } catch(Exception e) {
  75. //ignore
  76. }
  77. try {
  78. s.close();
  79. } catch(Exception e) {
  80. //ignore
  81. }
  82. }
  83. try {
  84. new Socket("unknown.host", 0);
  85. fail("UnknownHostException was not thrown.");
  86. } catch(UnknownHostException uhe) {
  87. //expected
  88. }
  89. Socket socket = null;
  90. try {
  91. socket = new Socket(InetAddress.getByName(null), sport);
  92. InetAddress address = socket.getLocalAddress();
  93. if (Boolean.getBoolean("java.net.preferIPv6Addresses")) {
  94. assertTrue(
  95. address.equals(InetAddress.getByName("::1")) ||
  96. address.equals(InetAddress.getByName("0:0:0:0:0:0:0:1")));
  97. } else {
  98. assertEquals(address, InetAddress.getByName("127.0.0.1"));
  99. }
  100. } finally {
  101. try {
  102. socket.close();
  103. } catch(Exception e) {}
  104. }
  105. }
  106. public void test_ConstructorLjava_lang_StringILjava_net_InetAddressI()
  107. throws IOException {
  108. // Test for method java.net.Socket(java.lang.String, int,
  109. // java.net.InetAddress, int)
  110. int sport = startServer("Cons String,I,InetAddress,I");
  111. int portNumber = Support_PortManager.getNextPort();
  112. s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
  113. InetAddress.getLocalHost(), portNumber);
  114. assertTrue("Failed to create socket", s.getPort() == sport);
  115. if (("true".equals(System.getProperty("java.net.preferIPv6Addresses")))
  116. && !("true".equals(System
  117. .getProperty("java.net.preferIPv4Stack")))) {
  118. // ALTERNATE IPv6 TEST
  119. if ("true".equals(System.getProperty("run.ipv6tests"))) {
  120. System.out
  121. .println("Running testConstructorLjava_lang_StringILjava_net_InetAddressI(OldSocketTest) with IPv6GlobalAddressJcl4: "
  122. + Support_Configuration.IPv6GlobalAddressJcl4);
  123. int testPort = Support_PortManager.getNextPort();
  124. Socket s1 = null, s2 = null;
  125. try {
  126. s1 = new Socket(
  127. Support_Configuration.IPv6GlobalAddressJcl4, 80,
  128. InetAddress.getLocalHost(), testPort);
  129. } catch (IOException e) {
  130. // check here if InetAddress.getLocalHost() is returning the
  131. // loopback address.
  132. // if so that is likely the cause of the failure
  133. String warning = "";
  134. try {
  135. InetAddress returnedLocalHost = InetAddress
  136. .getLocalHost();
  137. // don't use isLoopbackAddress for some configurations
  138. // as they do not have it
  139. if (returnedLocalHost.isLoopbackAddress()) {
  140. warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE";
  141. }
  142. } catch (Exception ex) {
  143. warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex;
  144. }
  145. fail("Exception creating 1st socket" + warning + ": " + e);
  146. }
  147. boolean exception = false;
  148. try {
  149. s2 = new Socket(
  150. Support_Configuration.IPv6GlobalAddressJcl4, 80,
  151. InetAddress.getLocalHost(), testPort);
  152. } catch (IOException e) {
  153. exception = true;
  154. }
  155. try {
  156. s1.close();
  157. if (!exception)
  158. s2.close();
  159. } catch (IOException e) {
  160. }
  161. assertTrue("Was able to create two sockets on same port",
  162. exception);
  163. }
  164. } else {
  165. int testPort = Support_PortManager.getNextPort();
  166. Socket s1 = null, s2 = null;
  167. int serverPort = ss.getLocalPort();
  168. try {
  169. s1 = new Socket("127.0.0.1", serverPort, InetAddress
  170. .getLocalHost(), testPort);
  171. } catch (IOException e) {
  172. e.printStackTrace();
  173. // check here if InetAddress.getLocalHost() is returning the
  174. // loopback address.
  175. // if so that is likely the cause of the failure
  176. String warning = "";
  177. try {
  178. InetAddress returnedLocalHost = InetAddress.getLocalHost();
  179. // don't use isLoopbackAddress for some configurations as
  180. // they do not have it
  181. if (returnedLocalHost.isLoopbackAddress()) {
  182. warning = " - WARNING RETURNED LOCAL HOST IS THE LOOPBACK ADDRESS - MACHINE IS LIKELY NOT CONFIGURED CORRECTLY - THIS LIKELY CAUSED THE FAILURE";
  183. }
  184. } catch (Exception ex) {
  185. warning = " - WARNING COULD NOT GET LOCAL HOST - " + ex;
  186. }
  187. fail("Exception creating 1st socket" + warning + ": " + e);
  188. }
  189. boolean exception = false;
  190. try {
  191. s2 = new Socket("127.0.0.1", serverPort, InetAddress
  192. .getLocalHost(), testPort);
  193. } catch (IOException e) {
  194. exception = true;
  195. }
  196. try {
  197. s1.close();
  198. if (!exception)
  199. s2.close();
  200. } catch (IOException e) {
  201. }
  202. assertTrue("Was able to create two sockets on same port", exception);
  203. }
  204. }
  205. public void test_ConstructorLjava_lang_StringIZ() throws IOException {
  206. // Test for method java.net.Socket(java.lang.String, int, boolean)
  207. int sport = startServer("Cons String,I,Z");
  208. s = new Socket(InetAddress.getLocalHost().getHostName(), sport, true);
  209. assertTrue("Failed to create socket", s.getPort() == sport);
  210. s = new Socket(InetAddress.getLocalHost().getHostName(), sport, false);
  211. }
  212. public void test_ConstructorLjava_net_InetAddressI() throws IOException {
  213. // Test for method java.net.Socket(java.net.InetAddress, int)
  214. int sport = startServer("Cons InetAddress,I");
  215. s = new Socket(InetAddress.getLocalHost(), sport);
  216. assertTrue("Failed to create socket", s.getPort() == sport);
  217. }
  218. public void test_ConstructorLjava_net_InetAddressILjava_net_InetAddressI()
  219. throws IOException {
  220. // Test for method java.net.Socket(java.net.InetAddress, int,
  221. // java.net.InetAddress, int)
  222. int sport = startServer("Cons InetAddress,I,InetAddress,I");
  223. int portNumber = Support_PortManager.getNextPort();
  224. s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
  225. InetAddress.getLocalHost(), portNumber);
  226. assertTrue("Failed to create socket", s.getLocalPort() == portNumber);
  227. }
  228. public void test_ConstructorLjava_net_InetAddressIZ() throws IOException {
  229. // Test for method java.net.Socket(java.net.InetAddress, int, boolean)
  230. int sport = startServer("Cons InetAddress,I,Z");
  231. s = new Socket(InetAddress.getLocalHost(), sport, true);
  232. assertTrue("Failed to create socket", s.getPort() == sport);
  233. s = new Socket(InetAddress.getLocalHost(), sport, false);
  234. }
  235. public void test_close() throws IOException {
  236. // Test for method void java.net.Socket.close()
  237. int sport = startServer("SServer close");
  238. int portNumber = Support_PortManager.getNextPort();
  239. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  240. try {
  241. s.setSoLinger(false, 100);
  242. } catch (IOException e) {
  243. handleException(e, SO_LINGER);
  244. }
  245. s.close();
  246. try {
  247. s.getOutputStream();
  248. fail("IOException was not thrown.");
  249. } catch (java.io.IOException e) {
  250. //expected
  251. }
  252. }
  253. public void test_getInetAddress() throws IOException {
  254. // Test for method java.net.InetAddress java.net.Socket.getInetAddress()
  255. int sport = startServer("SServer getInetAddress");
  256. int portNumber = Support_PortManager.getNextPort();
  257. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  258. assertTrue("Returned incorrect InetAddress", s.getInetAddress().equals(
  259. InetAddress.getLocalHost()));
  260. }
  261. public void test_getInputStream() throws IOException {
  262. // Simple fetch test
  263. ServerSocket server = new ServerSocket(0);
  264. Socket client = new Socket(InetAddress.getLocalHost(), server.getLocalPort());
  265. InputStream is = client.getInputStream();
  266. assertNotNull("Failed to get stream", is);
  267. is.close();
  268. client.close();
  269. server.close();
  270. }
  271. public void test_getKeepAlive() {
  272. try {
  273. int sport = startServer("SServer getKeepAlive");
  274. int portNumber = Support_PortManager.getNextPort();
  275. Socket theSocket = new Socket(InetAddress.getLocalHost(), sport,
  276. null, portNumber);
  277. theSocket.setKeepAlive(true);
  278. assertTrue("getKeepAlive false when it should be true", theSocket
  279. .getKeepAlive());
  280. theSocket.setKeepAlive(false);
  281. assertFalse("getKeepAlive true when it should be False", theSocket
  282. .getKeepAlive());
  283. theSocket.close();
  284. try {
  285. theSocket.setKeepAlive(false);
  286. fail("IOException was not thrown after calling setKeepAlive " +
  287. "method.");
  288. } catch(IOException ioe) {
  289. //expected
  290. }
  291. try {
  292. theSocket.getKeepAlive();
  293. fail("IOException was not thrown after calling getKeepAlive +" +
  294. "method.");
  295. } catch(IOException ioe) {
  296. //expected
  297. }
  298. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
  299. } catch (Exception e) {
  300. handleException(e, SO_KEEPALIVE);
  301. }
  302. }
  303. public void test_getLocalAddress() throws IOException {
  304. // Test for method java.net.InetAddress
  305. // java.net.Socket.getLocalAddress()
  306. int sport = startServer("SServer getLocAddress");
  307. int portNumber = Support_PortManager.getNextPort();
  308. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  309. assertEquals("Returned incorrect InetAddress",
  310. InetAddress.getLocalHost(), s.getLocalAddress());
  311. // now validate that behaviour when the any address is returned
  312. String preferIPv4StackValue = System
  313. .getProperty("java.net.preferIPv4Stack");
  314. String preferIPv6AddressesValue = System
  315. .getProperty("java.net.preferIPv6Addresses");
  316. s = new Socket();
  317. s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
  318. if (((preferIPv4StackValue == null) || preferIPv4StackValue
  319. .equalsIgnoreCase("false"))
  320. && (preferIPv6AddressesValue != null)
  321. && (preferIPv6AddressesValue.equals("true"))) {
  322. assertTrue(
  323. "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=false "
  324. + s.getLocalSocketAddress(),
  325. s.getLocalAddress() instanceof Inet6Address);
  326. } else {
  327. assertTrue(
  328. "ANY address not returned correctly (getLocalAddress) with preferIPv6Addresses=true, preferIPv4Stack=true "
  329. + s.getLocalSocketAddress(),
  330. s.getLocalAddress() instanceof Inet4Address);
  331. }
  332. s.close();
  333. }
  334. public void test_getLocalPort() throws IOException {
  335. // Test for method int java.net.Socket.getLocalPort()
  336. int sport = startServer("SServer getLocalPort");
  337. int portNumber = Support_PortManager.getNextPort();
  338. s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
  339. InetAddress.getLocalHost(), portNumber);
  340. assertTrue("Returned incorrect port", s.getLocalPort() == portNumber);
  341. }
  342. @SuppressWarnings("deprecation")
  343. public void test_getOutputStream() throws IOException {
  344. // Test for method java.io.OutputStream
  345. // java.net.Socket.getOutputStream()
  346. int sport = startServer("SServer getOutputStream");
  347. int portNumber = Support_PortManager.getNextPort();
  348. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  349. java.io.OutputStream os = s.getOutputStream();
  350. assertNotNull("Failed to get stream", os);
  351. os.write(1);
  352. s.close();
  353. // Regression test for harmony-2934
  354. s = new Socket("127.0.0.1", Support_PortManager.getNextPort(),
  355. false);
  356. OutputStream o = s.getOutputStream();
  357. o.write(1);
  358. try {
  359. Thread.sleep(1000);
  360. } catch (InterruptedException e) {
  361. }
  362. o.close();
  363. s.close();
  364. // Regression test for harmony-2942
  365. s = new Socket("0.0.0.0", Support_PortManager.getNextPort(),
  366. false);
  367. o = s.getOutputStream();
  368. o.write(1);
  369. try {
  370. Thread.sleep(1000);
  371. } catch (InterruptedException e) {
  372. }
  373. o.close();
  374. s.close();
  375. }
  376. public void test_getPort() throws IOException {
  377. // Test for method int java.net.Socket.getPort()
  378. int sport = startServer("SServer getPort");
  379. int portNumber = Support_PortManager.getNextPort();
  380. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  381. assertTrue("Returned incorrect port" + s.getPort(),
  382. s.getPort() == sport);
  383. }
  384. public void test_getSoLinger() {
  385. // Test for method int java.net.Socket.getSoLinger()
  386. int sport = startServer("SServer getSoLinger");
  387. try {
  388. int portNumber = Support_PortManager.getNextPort();
  389. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  390. s.setSoLinger(true, 200);
  391. assertEquals("Returned incorrect linger", 200, s.getSoLinger());
  392. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
  393. s.setSoLinger(false, 0);
  394. } catch (Exception e) {
  395. handleException(e, SO_LINGER);
  396. }
  397. try {
  398. int portNumber = Support_PortManager.getNextPort();
  399. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  400. s.close();
  401. try {
  402. s.getSoLinger();
  403. fail("SocketException was not thrown.");
  404. } catch(SocketException ioe) {
  405. //expected
  406. }
  407. } catch(Exception e) {
  408. fail("Unexpected exception was thrown: " + e.toString());
  409. }
  410. }
  411. public void test_getReceiveBufferSize() {
  412. try {
  413. int sport = startServer("SServer getReceiveBufferSize");
  414. int portNumber = Support_PortManager.getNextPort();
  415. s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
  416. null, portNumber);
  417. s.setReceiveBufferSize(130);
  418. assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130);
  419. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
  420. } catch (Exception e) {
  421. handleException(e, SO_RCVBUF);
  422. }
  423. try {
  424. Socket newSocket = new Socket();
  425. newSocket.close();
  426. try {
  427. newSocket.getReceiveBufferSize();
  428. fail("SocketException was not thrown.");
  429. } catch(SocketException e) {
  430. //expected
  431. }
  432. } catch(Exception e) {
  433. fail("Unexpected exception.");
  434. }
  435. }
  436. public void test_getSendBufferSize() {
  437. int sport = startServer("SServer setSendBufferSize");
  438. try {
  439. int portNumber = Support_PortManager.getNextPort();
  440. s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
  441. null, portNumber);
  442. s.setSendBufferSize(134);
  443. assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134);
  444. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
  445. } catch (Exception e) {
  446. handleException(e, SO_SNDBUF);
  447. }
  448. try {
  449. int portNumber = Support_PortManager.getNextPort();
  450. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  451. s.close();
  452. try {
  453. s.getSendBufferSize();
  454. fail("IOException was not thrown.");
  455. } catch(IOException ioe) {
  456. //expected
  457. }
  458. } catch(Exception e) {
  459. fail("Unexpected exception was thrown: " + e.toString());
  460. }
  461. }
  462. public void test_getSoTimeout() {
  463. // Test for method int java.net.Socket.getSoTimeout()
  464. int sport = startServer("SServer getSoTimeout");
  465. try {
  466. s = new Socket(InetAddress.getLocalHost(), sport);
  467. s.setSoTimeout(100);
  468. assertEquals("Returned incorrect sotimeout", 100, s.getSoTimeout());
  469. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
  470. } catch (Exception e) {
  471. handleException(e, SO_TIMEOUT);
  472. }
  473. try {
  474. int portNumber = Support_PortManager.getNextPort();
  475. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  476. s.close();
  477. try {
  478. s.getSoTimeout();
  479. fail("SocketException was not thrown.");
  480. } catch(SocketException ioe) {
  481. //expected
  482. }
  483. } catch(Exception e) {
  484. fail("Unexpected exception was thrown: " + e.toString());
  485. }
  486. }
  487. public void test_getTcpNoDelay() {
  488. // Test for method boolean java.net.Socket.getTcpNoDelay()
  489. int sport = startServer("SServer getTcpNoDelay");
  490. try {
  491. int portNumber = Support_PortManager.getNextPort();
  492. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  493. boolean bool = !s.getTcpNoDelay();
  494. s.setTcpNoDelay(bool);
  495. assertTrue("Failed to get no delay setting: " + s.getTcpNoDelay(),
  496. s.getTcpNoDelay() == bool);
  497. ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
  498. } catch (Exception e) {
  499. handleException(e, TCP_NODELAY);
  500. }
  501. try {
  502. int portNumber = Support_PortManager.getNextPort();
  503. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  504. s.close();
  505. try {
  506. s.getTcpNoDelay();
  507. fail("SocketException was not thrown.");
  508. } catch(SocketException ioe) {
  509. //expected
  510. }
  511. } catch(Exception e) {
  512. fail("Unexpected exception was thrown: " + e.toString());
  513. }
  514. }
  515. public void test_setKeepAliveZ() throws Exception {
  516. // There is not really a good test for this as it is there to detect
  517. // crashed machines. Just make sure we can set it
  518. try {
  519. int sport = startServer("SServer setKeepAlive");
  520. int portNumber = Support_PortManager.getNextPort();
  521. Socket theSocket = new Socket(InetAddress.getLocalHost(), sport,
  522. null, portNumber);
  523. theSocket.setKeepAlive(true);
  524. theSocket.setKeepAlive(false);
  525. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_KEEPALIVE);
  526. } catch (Exception e) {
  527. handleException(e, SO_KEEPALIVE);
  528. }
  529. // regression test for HARMONY-1136
  530. new TestSocket((SocketImpl) null).setKeepAlive(true);
  531. try {
  532. Socket theSocket = new Socket();
  533. theSocket.close();
  534. theSocket.setKeepAlive(true);
  535. fail("SocketException was not thrown.");
  536. } catch(SocketException ioe) {
  537. //expected
  538. }
  539. }
  540. class TestSocket extends Socket {
  541. public TestSocket(SocketImpl impl) throws SocketException {
  542. super(impl);
  543. }
  544. }
  545. public void test_setSocketImplFactoryLjava_net_SocketImplFactory() {
  546. // Test for method void
  547. // java.net.Socket.setSocketImplFactory(java.net.SocketImplFactory)
  548. // Cannot test as setting will cause the factory to be changed for
  549. // all subsequent sockets
  550. SecurityManager sm = new SecurityManager() {
  551. public void checkPermission(Permission perm) {
  552. }
  553. public void checkSetFactory() {
  554. throw new SecurityException();
  555. }
  556. };
  557. }
  558. public void test_setSendBufferSizeI() {
  559. try {
  560. int sport = startServer("SServer setSendBufferSizeI");
  561. int portNumber = Support_PortManager.getNextPort();
  562. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  563. s.setSendBufferSize(134);
  564. assertTrue("Incorrect buffer size", s.getSendBufferSize() >= 134);
  565. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_SNDBUF);
  566. } catch (Exception e) {
  567. handleException(e, SO_SNDBUF);
  568. }
  569. try {
  570. Socket theSocket = new Socket();
  571. theSocket.close();
  572. theSocket.setSendBufferSize(1);
  573. fail("SocketException was not thrown.");
  574. } catch(SocketException ioe) {
  575. //expected
  576. } catch(IOException ioe) {
  577. fail("IOException was thrown.");
  578. }
  579. }
  580. public void test_setReceiveBufferSizeI() {
  581. try {
  582. int sport = startServer("SServer setReceiveBufferSizeI");
  583. int portNumber = Support_PortManager.getNextPort();
  584. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  585. s.setReceiveBufferSize(130);
  586. assertTrue("Incorrect buffer size", s.getReceiveBufferSize() >= 130);
  587. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_RCVBUF);
  588. } catch (Exception e) {
  589. handleException(e, SO_RCVBUF);
  590. }
  591. try {
  592. Socket theSocket = new Socket();
  593. theSocket.close();
  594. theSocket.setReceiveBufferSize(1);
  595. fail("SocketException was not thrown.");
  596. } catch(SocketException ioe) {
  597. //expected
  598. } catch(IOException ioe) {
  599. fail("IOException was thrown.");
  600. }
  601. }
  602. public void test_setSoLingerZI() {
  603. // Test for method void java.net.Socket.setSoLinger(boolean, int)
  604. try {
  605. int sport = startServer("SServer setSoLingerZI");
  606. int portNumber = Support_PortManager.getNextPort();
  607. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  608. s.setSoLinger(true, 500);
  609. assertEquals("Set incorrect linger", 500, s.getSoLinger());
  610. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_LINGER);
  611. s.setSoLinger(false, 0);
  612. } catch (Exception e) {
  613. handleException(e, SO_LINGER);
  614. }
  615. try {
  616. Socket theSocket = new Socket();
  617. theSocket.close();
  618. theSocket.setSoLinger(true, 1);
  619. fail("SocketException was not thrown.");
  620. } catch(SocketException ioe) {
  621. //expected
  622. } catch(IOException ioe) {
  623. fail("IOException was thrown.");
  624. }
  625. }
  626. public void test_setSoTimeoutI() {
  627. // Test for method void java.net.Socket.setSoTimeout(int)
  628. try {
  629. int sport = startServer("SServer seSoTimeoutI");
  630. int portNumber = Support_PortManager.getNextPort();
  631. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  632. s.setSoTimeout(100);
  633. assertEquals("Set incorrect sotimeout", 100, s.getSoTimeout());
  634. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_TIMEOUT);
  635. } catch (Exception e) {
  636. handleException(e, SO_TIMEOUT);
  637. }
  638. try {
  639. Socket theSocket = new Socket();
  640. theSocket.close();
  641. theSocket.setSoTimeout(1);
  642. fail("SocketException was not thrown.");
  643. } catch(SocketException ioe) {
  644. //expected
  645. } catch(IOException ioe) {
  646. fail("IOException was thrown.");
  647. }
  648. }
  649. public void test_setTcpNoDelayZ() {
  650. // Test for method void java.net.Socket.setTcpNoDelay(boolean)
  651. try {
  652. int sport = startServer("SServer setTcpNoDelayZ");
  653. int portNumber = Support_PortManager.getNextPort();
  654. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  655. boolean bool;
  656. s.setTcpNoDelay(bool = !s.getTcpNoDelay());
  657. assertTrue("Failed to set no delay setting: " + s.getTcpNoDelay(),
  658. s.getTcpNoDelay() == bool);
  659. ensureExceptionThrownIfOptionIsUnsupportedOnOS(TCP_NODELAY);
  660. } catch (Exception e) {
  661. handleException(e, TCP_NODELAY);
  662. }
  663. try {
  664. Socket theSocket = new Socket();
  665. theSocket.close();
  666. theSocket.setTcpNoDelay(true);
  667. fail("SocketException was not thrown.");
  668. } catch(SocketException ioe) {
  669. //expected
  670. } catch(IOException ioe) {
  671. fail("IOException was thrown.");
  672. }
  673. }
  674. public void test_toString() throws IOException {
  675. // Test for method java.lang.String java.net.Socket.toString()
  676. int sport = startServer("SServer toString");
  677. int portNumber = Support_PortManager.getNextPort();
  678. s = new Socket(InetAddress.getLocalHost().getHostName(), sport,
  679. InetAddress.getLocalHost(), portNumber);
  680. assertTrue("Returned incorrect string: " + s.toString()
  681. + " localHost: " + InetAddress.getLocalHost(), s.toString()
  682. .equals(
  683. "Socket[addr=" + InetAddress.getLocalHost() + ",port="
  684. + s.getPort() + ",localport="
  685. + s.getLocalPort() + "]"));
  686. }
  687. // AndroidOnly: RI returns wrong value for EOF
  688. public void test_shutdownInput() throws Exception {
  689. InetAddress addr = InetAddress.getLocalHost();
  690. int port = Support_PortManager.getNextPort();
  691. ServerSocket serverSocket = new ServerSocket(port, 5, addr);
  692. Socket theSocket = new Socket(addr, port);
  693. Socket servSock = serverSocket.accept();
  694. InputStream theInput = theSocket.getInputStream();
  695. OutputStream theOutput = servSock.getOutputStream();
  696. // shutdown the input
  697. theSocket.shutdownInput();
  698. // send the regular data
  699. String sendString = new String("Test");
  700. theOutput.write(sendString.getBytes());
  701. theOutput.flush();
  702. // give things some time to settle
  703. Thread.sleep(1000);
  704. // RI fails here. It is a RI bug not to return 0 to indicate EOF
  705. assertEquals(0, theInput.available());
  706. theSocket.close();
  707. serverSocket.close();
  708. Socket socket = new Socket();
  709. socket.close();
  710. try {
  711. socket.shutdownInput();
  712. fail("IOException was not thrown.");
  713. } catch(IOException ioe) {
  714. //expected
  715. }
  716. }
  717. public void test_shutdownOutput() throws IOException {
  718. InetAddress addr = InetAddress.getLocalHost();
  719. int port = Support_PortManager.getNextPort();
  720. ServerSocket serverSocket = new ServerSocket(port, 5, addr);
  721. Socket theSocket = new Socket(addr, port);
  722. Socket servSock = serverSocket.accept();
  723. InputStream theInput = theSocket.getInputStream();
  724. OutputStream theOutput = servSock.getOutputStream();
  725. // shutdown the output
  726. servSock.shutdownOutput();
  727. // send the regular data
  728. String sendString = new String("Test");
  729. try {
  730. theOutput.write(sendString.getBytes());
  731. theOutput.flush();
  732. fail("No exception when writing on socket with output shutdown");
  733. } catch (Exception e) {
  734. }
  735. theSocket.close();
  736. serverSocket.close();
  737. try {
  738. theSocket.shutdownInput();
  739. fail("IOException was not thrown.");
  740. } catch(IOException ioe) {
  741. //expected
  742. }
  743. }
  744. public void test_getLocalSocketAddress() throws IOException {
  745. // set up server connect and then validate that we get the right
  746. // response for the local address
  747. int sport = startServer("SServer getLocSocketAddress");
  748. int portNumber = Support_PortManager.getNextPort();
  749. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  750. assertTrue(
  751. "Returned incorrect InetSocketAddress(1):"
  752. + s.getLocalSocketAddress().toString()
  753. + "Expected: "
  754. + (new InetSocketAddress(InetAddress.getLocalHost(),
  755. portNumber)).toString(), s
  756. .getLocalSocketAddress().equals(
  757. new InetSocketAddress(InetAddress
  758. .getLocalHost(), portNumber)));
  759. s.close();
  760. // now create a socket that is not bound and validate we get the
  761. // right answer
  762. Socket theSocket = new Socket();
  763. assertNull(
  764. "Returned incorrect InetSocketAddress -unbound socket- Expected null",
  765. theSocket.getLocalSocketAddress());
  766. // now bind the socket and make sure we get the right answer
  767. portNumber = Support_PortManager.getNextPort();
  768. theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
  769. portNumber));
  770. assertTrue(
  771. "Returned incorrect InetSocketAddress(2):"
  772. + theSocket.getLocalSocketAddress().toString()
  773. + "Expected: "
  774. + (new InetSocketAddress(InetAddress.getLocalHost(),
  775. portNumber)).toString(), theSocket
  776. .getLocalSocketAddress().equals(
  777. new InetSocketAddress(InetAddress
  778. .getLocalHost(), portNumber)));
  779. theSocket.close();
  780. // now validate that behaviour when the any address is returned
  781. s = new Socket();
  782. s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
  783. String preferIPv4StackValue = System
  784. .getProperty("java.net.preferIPv4Stack");
  785. String preferIPv6AddressesValue = System
  786. .getProperty("java.net.preferIPv6Addresses");
  787. if (((preferIPv4StackValue == null) || preferIPv4StackValue
  788. .equalsIgnoreCase("false"))
  789. && (preferIPv6AddressesValue != null)
  790. && (preferIPv6AddressesValue.equals("true"))) {
  791. assertTrue(
  792. "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false "
  793. + s.getLocalSocketAddress(),
  794. ((InetSocketAddress) s.getLocalSocketAddress())
  795. .getAddress() instanceof Inet6Address);
  796. } else {
  797. assertTrue(
  798. "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true "
  799. + s.getLocalSocketAddress(),
  800. ((InetSocketAddress) s.getLocalSocketAddress())
  801. .getAddress() instanceof Inet4Address);
  802. }
  803. s.close();
  804. // now validate the same for getLocalAddress
  805. s = new Socket();
  806. s.bind(new InetSocketAddress(InetAddress.getByName("0.0.0.0"), 0));
  807. if (((preferIPv4StackValue == null) || preferIPv4StackValue
  808. .equalsIgnoreCase("false"))
  809. && (preferIPv6AddressesValue != null)
  810. && (preferIPv6AddressesValue.equals("true"))) {
  811. assertTrue(
  812. "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=false "
  813. + s.getLocalSocketAddress(),
  814. ((InetSocketAddress) s.getLocalSocketAddress())
  815. .getAddress() instanceof Inet6Address);
  816. } else {
  817. assertTrue(
  818. "ANY address not returned correctly with preferIPv6Addresses=true, preferIPv4Stack=true "
  819. + s.getLocalSocketAddress(),
  820. ((InetSocketAddress) s.getLocalSocketAddress())
  821. .getAddress() instanceof Inet4Address);
  822. }
  823. s.close();
  824. }
  825. public void test_getRemoteSocketAddress() throws IOException {
  826. // set up server connect and then validate that we get the right
  827. // response for the remote address
  828. int sport = startServer("SServer getLocRemoteAddress");
  829. int portNumber = Support_PortManager.getNextPort();
  830. s = new Socket(InetAddress.getLocalHost(), sport, null, portNumber);
  831. assertTrue("Returned incorrect InetSocketAddress(1):"
  832. + s.getLocalSocketAddress().toString(),
  833. s.getRemoteSocketAddress()
  834. .equals(
  835. new InetSocketAddress(InetAddress
  836. .getLocalHost(), sport)));
  837. s.close();
  838. // now create one that is not connect and validate that we get the
  839. // right answer
  840. Socket theSocket = new Socket();
  841. portNumber = Support_PortManager.getNextPort();
  842. theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
  843. portNumber));
  844. assertNull("Returned incorrect InetSocketAddress -unconnected socket:"
  845. + "Expected: NULL", theSocket.getRemoteSocketAddress());
  846. // now connect and validate we get the right answer
  847. theSocket.connect(new InetSocketAddress(InetAddress.getLocalHost(),
  848. sport));
  849. assertTrue("Returned incorrect InetSocketAddress(2):"
  850. + theSocket.getRemoteSocketAddress().toString(),
  851. theSocket.getRemoteSocketAddress()
  852. .equals(
  853. new InetSocketAddress(InetAddress
  854. .getLocalHost(), sport)));
  855. theSocket.close();
  856. }
  857. public void test_isBound() throws IOException {
  858. InetAddress addr = InetAddress.getLocalHost();
  859. int port = Support_PortManager.getNextPort();
  860. ServerSocket serverSocket = new ServerSocket(port, 5, addr);
  861. Socket theSocket = new Socket(addr, port);
  862. Socket servSock = serverSocket.accept();
  863. assertTrue("Socket indicated not bound when it should be (1)",
  864. theSocket.isBound());
  865. theSocket.close();
  866. serverSocket.close();
  867. // now do it with the new constructors and revalidate. Connect causes
  868. // the socket to be bound
  869. InetSocketAddress theAddress = new InetSocketAddress(InetAddress
  870. .getLocalHost(), Support_PortManager.getNextPort());
  871. theSocket = new Socket();
  872. assertFalse("Socket indicated bound when it was not (2)", theSocket
  873. .isBound());
  874. serverSocket = new ServerSocket();
  875. serverSocket.bind(theAddress);
  876. theSocket.connect(theAddress);
  877. servSock = serverSocket.accept();
  878. assertTrue("Socket indicated not bound when it should be (2)",
  879. theSocket.isBound());
  880. theSocket.close();
  881. serverSocket.close();
  882. // now test when we bind explicitly
  883. InetSocketAddress theLocalAddress = new InetSocketAddress(InetAddress
  884. .getLocalHost(), Support_PortManager.getNextPort());
  885. theSocket = new Socket();
  886. assertFalse("Socket indicated bound when it was not (3)", theSocket
  887. .isBound());
  888. theSocket.bind(theLocalAddress);
  889. assertTrue("Socket indicated not bound when it should be (3a)",
  890. theSocket.isBound());
  891. theSocket.close();
  892. assertTrue("Socket indicated not bound when it should be (3b)",
  893. theSocket.isBound());
  894. }
  895. public void test_isConnected() throws IOException {
  896. InetAddress addr = InetAddress.getLocalHost();
  897. int port = Support_PortManager.getNextPort();
  898. ServerSocket serverSocket = new ServerSocket(port, 5, addr);
  899. Socket theSocket = new Socket(addr, port);
  900. Socket servSock = serverSocket.accept();
  901. assertTrue("Socket indicated not connected when it should be",
  902. theSocket.isConnected());
  903. theSocket.close();
  904. serverSocket.close();
  905. // now do it with the new constructors and revalidate
  906. InetSocketAddress theAddress = new InetSocketAddress(InetAddress
  907. .getLocalHost(), Support_PortManager.getNextPort());
  908. theSocket = new Socket();
  909. assertFalse("Socket indicated connected when it was not", theSocket
  910. .isConnected());
  911. serverSocket = new ServerSocket();
  912. serverSocket.bind(theAddress);
  913. theSocket.connect(theAddress);
  914. servSock = serverSocket.accept();
  915. assertTrue("Socket indicated not connected when it should be",
  916. theSocket.isConnected());
  917. theSocket.close();
  918. serverSocket.close();
  919. }
  920. public void test_isClosed() throws IOException {
  921. InetAddress addr = InetAddress.getLocalHost();
  922. int port = Support_PortManager.getNextPort();
  923. ServerSocket serverSocket = new ServerSocket(port, 5, addr);
  924. Socket theSocket = new Socket(addr, port);
  925. Socket servSock = serverSocket.accept();
  926. // validate isClosed returns expected values
  927. assertFalse("Socket should indicate it is not closed(1):", theSocket
  928. .isClosed());
  929. theSocket.close();
  930. assertTrue("Socket should indicate it is closed(1):", theSocket
  931. .isClosed());
  932. theSocket = new Socket(addr, port);
  933. assertFalse("Socket should indicate it is not closed(2):", theSocket
  934. .isClosed());
  935. theSocket.close();
  936. assertTrue("Socket should indicate it is closed(2):", theSocket
  937. .isClosed());
  938. // validate that isClosed works ok for sockets returned from
  939. // ServerSocket.accept()
  940. assertFalse("Server Socket should indicate it is not closed:", servSock
  941. .isClosed());
  942. servSock.close();
  943. assertTrue("Server Socket should indicate it is closed:", servSock
  944. .isClosed());
  945. }
  946. public void test_bindLjava_net_SocketAddress() throws IOException {
  947. class mySocketAddress extends SocketAddress {
  948. public mySocketAddress() {
  949. }
  950. }
  951. // Address we cannot bind to
  952. Socket theSocket = new Socket();
  953. try {
  954. theSocket.bind(new InetSocketAddress(InetAddress
  955. .getByAddress(Support_Configuration.nonLocalAddressBytes),
  956. Support_PortManager.getNextPort()));
  957. fail("No exception when binding to bad address:"
  958. + theSocket.getLocalSocketAddress().toString());
  959. } catch (IOException ex) {
  960. }
  961. theSocket.close();
  962. // now create a socket that is not bound and then bind it
  963. theSocket = new Socket();
  964. int portNumber = Support_PortManager.getNextPort();
  965. theSocket.bind(new InetSocketAddress(InetAddress.getLocalHost(),
  966. portNumber));
  967. // validate that the localSocketAddress reflects the address we
  968. // bound to
  969. assertTrue(
  970. "Local address not correct after bind:"
  971. + theSocket.getLocalSocketAddress().toString()
  972. + " Expected: "
  973. + (new InetSocketAddress(InetAddress.getLocalHost(),
  974. portNumber)).toString(), theSocket
  975. .getLocalSocketAddress().equals(
  976. new InetSocketAddress(InetAddress
  977. .getLocalHost(), portNumber)));
  978. // make sure we can now connect and that connections appear to come
  979. // from the address we bound to.
  980. InetSocketAddress theAddress = new InetSocketAddress(InetAddress
  981. .getLocalHost(), Support_PortManager.getNextPort());
  982. ServerSocket serverSocket = new ServerSocket();
  983. serverSocket.bind(theAddress);
  984. theSocket.connect(theAddress);
  985. Socket servSock = serverSocket.accept();
  986. assertTrue(
  987. "Returned Remote address from server connected to does not match expected local address:"
  988. + servSock.getRemoteSocketAddress().toString()
  989. + " Expected: "
  990. + (new InetSocketAddress(InetAddress.getLocalHost(),
  991. portNumber)).toString(), servSock
  992. .getRemoteSocketAddress().equals(
  993. new InetSocketAddress(InetAddress
  994. .getLocalHost(), portNumber)));
  995. theSocket.close();
  996. servSock.close();
  997. serverSocket.close();
  998. // validate if we pass in null that it picks an address for us and
  999. // all is ok
  1000. theSocket = new Socket();
  1001. theSocket.bind(null);
  1002. assertNotNull("Bind with null did not work", theSocket
  1003. .getLocalSocketAddress());
  1004. theSocket.close();
  1005. // now check the error conditions
  1006. // Address that we have already bound to
  1007. theSocket = new Socket();
  1008. Socket theSocket2 = new Socket();
  1009. try {
  1010. theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
  1011. Support_PortManager.getNextPort());
  1012. theSocket.bind(theAddress);
  1013. theSocket2.bind(theAddress);
  1014. fail("No exception binding to address that is not available");
  1015. } catch (IOException ex) {
  1016. }
  1017. theSocket.close();
  1018. theSocket2.close();
  1019. // unsupported SocketAddress subclass
  1020. theSocket = new Socket();
  1021. try {
  1022. theSocket.bind(new mySocketAddress());
  1023. fail("No exception when binding using unsupported SocketAddress subclass");
  1024. } catch (IllegalArgumentException ex) {
  1025. }
  1026. theSocket.close();
  1027. }
  1028. public void test_bindLjava_net_SocketAddress_Proxy() throws IOException {
  1029. //The Proxy will not impact on the bind operation.It can be assigned with any address.
  1030. Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress("127.0.0.1", 0));
  1031. Socket socket = new Socket(proxy);
  1032. try {
  1033. InetAddress address = InetAddress.getByName("localhost");
  1034. int port = 0;
  1035. socket.bind(new InetSocketAddress(address, port));
  1036. assertEquals(address, socket.getLocalAddress());
  1037. assertTrue(port!=socket.getLocalPort());
  1038. } finally {
  1039. socket.close();
  1040. }
  1041. }
  1042. public void test_connectLjava_net_SocketAddress() throws Exception {
  1043. // needed for some tests
  1044. class mySocketAddress extends SocketAddress {
  1045. public mySocketAddress() {
  1046. }
  1047. }
  1048. class SocketCloser extends Thread {
  1049. int timeout = 0;
  1050. Socket theSocket = null;
  1051. public void run() {
  1052. try {
  1053. Thread.sleep(timeout);
  1054. theSocket.close();
  1055. } catch (Exception e) {
  1056. }
  1057. ;
  1058. return;
  1059. }
  1060. public SocketCloser(int timeout, Socket theSocket) {
  1061. this.timeout = timeout;
  1062. this.theSocket = theSocket;
  1063. }
  1064. }
  1065. // start by validating the error checks
  1066. int portNumber = Support_PortManager.getNextPort();
  1067. Socket theSocket = null;
  1068. ServerSocket serverSocket = null;
  1069. SocketAddress theAddress = null;
  1070. SocketAddress nonConnectableAddress = null;
  1071. SocketAddress nonReachableAddress = null;
  1072. SocketAddress invalidType = null;
  1073. // byte[] theBytes = {-1,-1,-1,-1};
  1074. byte[] theBytes = { 0, 0, 0, 0 };
  1075. theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
  1076. portNumber);
  1077. nonConnectableAddress = new InetSocketAddress(InetAddress
  1078. .getByAddress(theBytes), portNumber);
  1079. nonReachableAddress = new InetSocketAddress(InetAddress
  1080. .getByName(Support_Configuration.ResolvedNotExistingHost),
  1081. portNumber);
  1082. invalidType = new mySocketAddress();
  1083. try {
  1084. theSocket = new Socket();
  1085. theSocket.connect(null);
  1086. fail("No exception after null address passed in");
  1087. } catch (Exception e) {
  1088. assertTrue("Wrong exception null address passed in: "
  1089. + e.toString(), (e instanceof IllegalArgumentException));
  1090. }
  1091. try {
  1092. theSocket = new Socket();
  1093. theSocket.connect(invalidType);
  1094. fail("No exception when invalid socket address type passed in: ");
  1095. } catch (Exception e) {
  1096. assertTrue(
  1097. "Wrong exception when when invalid socket address type passed in: "
  1098. + e.toString(),
  1099. (e instanceof IllegalArgumentException));
  1100. }
  1101. try {
  1102. theSocket = new Socket();
  1103. theSocket.connect(nonConnectableAddress);
  1104. fail("No exception when non Connectable Address passed in: ");
  1105. } catch (Exception e) {
  1106. assertTrue(
  1107. "Wrong exception when non Connectable Address passed in: "
  1108. + e.toString(), (e instanceof ConnectException));
  1109. }
  1110. // now validate that we get a connect exception if we try to connect to
  1111. // an address on which nobody is listening
  1112. try {
  1113. theSocket = new Socket();
  1114. theSocket.connect(theAddress);
  1115. theSocket.close();
  1116. fail("No exception when connecting to address nobody listening on: ");
  1117. } catch (Exception e) {
  1118. assertTrue(
  1119. "Wrong exception when connecting to address nobody listening on: "
  1120. + e.toString(), (e instanceof ConnectException));
  1121. }
  1122. // now validate that we can actually connect when somebody is listening
  1123. theSocket = new Socket();
  1124. serverSocket = new ServerSocket();
  1125. serverSocket.bind(theAddress);
  1126. theSocket.connect(theAddress);
  1127. theSocket.close();
  1128. serverSocket.close();
  1129. // now validate that we can actually connect when somebody is listening
  1130. theSocket = new Socket();
  1131. serverSocket = new ServerSocket();
  1132. serverSocket.bind(theAddress);
  1133. theSocket.connect(theAddress);
  1134. // validate that when a socket is connected that it answers
  1135. // correctly to related queries
  1136. assertTrue("Socket did not returned connected when it is: ", theSocket
  1137. .isConnected());
  1138. assertFalse("Socket returned closed when it should be connected ",
  1139. theSocket.isClosed());
  1140. assertTrue("Socket returned not bound when it should be: ", theSocket
  1141. .isBound());
  1142. assertFalse(
  1143. "Socket returned input Shutdown when it should be connected ",
  1144. theSocket.isInputShutdown());
  1145. assertFalse(
  1146. "Socket returned output Shutdown when it should be connected ",
  1147. theSocket.isOutputShutdown());
  1148. assertTrue("Local port on connected socket was 0", theSocket
  1149. .getLocalPort() != 0);
  1150. theSocket.close();
  1151. serverSocket.close();
  1152. // now validate that we get the right exception if we connect when we
  1153. // are already connected
  1154. try {
  1155. theSocket = new Socket();
  1156. serverSocket = new ServerSocket();
  1157. serverSocket.bind(theAddress);
  1158. theSocket.connect(theAddress);
  1159. theSocket.connect(theAddress);
  1160. theSocket.close();
  1161. serverSocket.close();
  1162. fail("No exception when we try to connect on a connected socket: ");
  1163. } catch (Exception e) {
  1164. assertTrue(
  1165. "Wrong exception when connecting on socket that is allready connected"
  1166. + e.toString(), (e instanceof SocketException));
  1167. assertFalse(
  1168. "Wrong exception when connecting on socket that is allready connected"
  1169. + e.toString(),
  1170. (e instanceof SocketTimeoutException));
  1171. try {
  1172. theSocket.close();
  1173. serverSocket.close();
  1174. } catch (Exception e2) {
  1175. }
  1176. }
  1177. // now validate that connected socket can be used to read/write
  1178. theSocket = new Socket();
  1179. serverSocket = new ServerSocket();
  1180. serverSocket.bind(theAddress);
  1181. theSocket.connect(theAddress);
  1182. Socket servSock = serverSocket.accept();
  1183. InputStream theInput = theSocket.getInputStream();
  1184. OutputStream theOutput = servSock.getOutputStream();
  1185. InputStream theInput2 = servSock.getInputStream();
  1186. OutputStream theOutput2 = theSocket.getOutputStream();
  1187. String sendString = new String("Test");
  1188. theOutput.write(sendString.getBytes());
  1189. theOutput.flush();
  1190. Thread.sleep(1000);
  1191. int totalBytesRead = 0;
  1192. byte[] myBytes = new byte[100];
  1193. while (theInput.available() > 0) {
  1194. int bytesRead = theInput.read(myBytes, totalBytesRead,
  1195. myBytes.length - totalBytesRead);
  1196. totalBytesRead = totalBytesRead + bytesRead;
  1197. }
  1198. String receivedString = new String(myBytes, 0, totalBytesRead);
  1199. assertTrue("Could not recv on socket connected with timeout:"
  1200. + receivedString + ":" + sendString, receivedString
  1201. .equals(sendString));
  1202. sendString = new String("SEND - Test");
  1203. theOutput2.write(sendString.getBytes());
  1204. theOutput2.flush();
  1205. Thread.sleep(1000);
  1206. totalBytesRead = 0;
  1207. myBytes = new byte[100];
  1208. while (theInput2.available() > 0) {
  1209. int bytesRead = theInput2.read(myBytes, totalBytesRead,
  1210. myBytes.length - totalBytesRead);
  1211. totalBytesRead = totalBytesRead + bytesRead;
  1212. }
  1213. receivedString = new String(myBytes, 0, totalBytesRead);
  1214. assertTrue("Could not send on socket connected with timeout:"
  1215. + receivedString + ":" + sendString, receivedString
  1216. .equals(sendString));
  1217. theSocket.close();
  1218. serverSocket.close();
  1219. SocketChannel channel = SocketChannel.open();
  1220. channel.configureBlocking(false);
  1221. Socket socket = channel.socket();
  1222. int port = Support_PortManager.getNextPort();
  1223. try {
  1224. socket.connect( new InetSocketAddress(InetAddress.getLocalHost(),
  1225. Support_PortManager.getNextPort()));
  1226. fail("IllegalBlockingModeException was not thrown.");
  1227. } catch(IllegalBlockingModeException ibme) {
  1228. //expected
  1229. }
  1230. }
  1231. public void test_connectLjava_net_SocketAddressI() throws Exception {
  1232. // needed for some tests
  1233. class mySocketAddress extends SocketAddress {
  1234. public mySocketAddress() {
  1235. }
  1236. }
  1237. class SocketCloser extends Thread {
  1238. int timeout = 0;
  1239. Socket theSocket = null;
  1240. public void run() {
  1241. try {
  1242. Thread.sleep(timeout);
  1243. theSocket.close();
  1244. } catch (Exception e) {
  1245. }
  1246. return;
  1247. }
  1248. public SocketCloser(int timeout, Socket theSocket) {
  1249. this.timeout = timeout;
  1250. this.theSocket = theSocket;
  1251. }
  1252. }
  1253. class SocketConnector extends Thread {
  1254. int timeout = 0;
  1255. Socket theSocket = null;
  1256. SocketAddress address = null;
  1257. public void run() {
  1258. try {
  1259. theSocket.connect(address, timeout);
  1260. } catch (Exception e) {
  1261. }
  1262. return;
  1263. }
  1264. public SocketConnector(int timeout, Socket theSocket,
  1265. SocketAddress address) {
  1266. this.timeout = timeout;
  1267. this.theSocket = theSocket;
  1268. this.address = address;
  1269. }
  1270. }
  1271. // start by validating the error checks
  1272. int portNumber = Support_PortManager.getNextPort();
  1273. Socket theSocket = null;
  1274. ServerSocket serverSocket = null;
  1275. SocketAddress theAddress = null;
  1276. SocketAddress nonConnectableAddress = null;
  1277. SocketAddress nonReachableAddress = null;
  1278. SocketAddress nonListeningAddress = null;
  1279. SocketAddress invalidType = null;
  1280. byte[] theBytes = { 0, 0, 0, 0 };
  1281. theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
  1282. portNumber);
  1283. nonConnectableAddress = new InetSocketAddress(InetAddress
  1284. .getByAddress(theBytes), portNumber);
  1285. nonReachableAddress = new InetSocketAddress(InetAddress
  1286. .getByName(Support_Configuration.ResolvedNotExistingHost),
  1287. portNumber);
  1288. // make sure we get another port
  1289. Thread.sleep(7000);
  1290. nonListeningAddress = new InetSocketAddress(InetAddress.getLocalHost(),
  1291. Support_PortManager.getNextPort());
  1292. invalidType = new mySocketAddress();
  1293. try {
  1294. theSocket = new Socket();
  1295. theSocket.connect(theAddress, -100);
  1296. fail("No exception after negative timeout passed in");
  1297. } catch (Exception e) {
  1298. assertTrue("Wrong exception when negative timeout passed in: "
  1299. + e.toString(), (e instanceof IllegalArgumentException));
  1300. }
  1301. try {
  1302. theSocket = new Socket();
  1303. theSocket.connect(null, 0);
  1304. fail("No exception after null address passed in");
  1305. } catch (Exception e) {
  1306. assertTrue("Wrong exception null address passed in: "
  1307. + e.toString(), (e instanceof IllegalArgumentException));
  1308. }
  1309. try {
  1310. theSocket = new Socket();
  1311. theSocket.connect(invalidType, 100000);
  1312. fail("No exception when invalid socket address type passed in: ");
  1313. } catch (Exception e) {
  1314. assertTrue(
  1315. "Wrong exception when when invalid socket address type passed in: "
  1316. + e.toString(),
  1317. (e instanceof IllegalArgumentException));
  1318. }
  1319. try {
  1320. theSocket = new Socket();
  1321. theSocket.connect(nonConnectableAddress, 100000);
  1322. fail("No exception when non Connectable Address passed in: ");
  1323. } catch (Exception e) {
  1324. assertTrue(
  1325. "Wrong exception when non Connectable Address passed in: "
  1326. + e.toString(), (e instanceof SocketException));
  1327. }
  1328. // now validate that we get a connect exception if we try to connect to
  1329. // an address on which nobody is listening
  1330. try {
  1331. theSocket = new Socket();
  1332. theSocket.connect(theAddress, 0);
  1333. theSocket.close();
  1334. fail("No timeout:No exception when connecting to address nobody listening on: ");
  1335. } catch (Exception e) {
  1336. assertTrue(
  1337. "No timeout:Wrong exception when connecting to address nobody listening on: "
  1338. + e.toString(), (e instanceof ConnectException));
  1339. }
  1340. // now validate that we can actually connect when somebody is listening
  1341. theSocket = new Socket();
  1342. serverSocket = new ServerSocket();
  1343. serverSocket.bind(theAddress);
  1344. theSocket.connect(theAddress, 0);
  1345. theSocket.close();
  1346. serverSocket.close();
  1347. // now validate that we get a connect exception if we try to connect to
  1348. // an address on which nobody is listening
  1349. try {
  1350. theSocket = new Socket();
  1351. theSocket.connect(nonListeningAddress, 100000);
  1352. theSocket.close();
  1353. fail("No exception when connecting to address nobody listening on: ");
  1354. } catch (Exception e) {
  1355. assertTrue(
  1356. "Wrong exception when connecting to address nobody listening on: "
  1357. + e.toString(), (e instanceof ConnectException));
  1358. }
  1359. // now validate that we get a interrupted exception if we try to connect
  1360. // to an address on which nobody is accepting connections and the
  1361. // timeout expired
  1362. try {
  1363. theSocket = new Socket();
  1364. theSocket.connect(nonReachableAddress, 200);
  1365. theSocket.close();
  1366. fail("No interrupted exception when connecting to address nobody listening on with short timeout 200: ");
  1367. } catch (Exception e) {
  1368. assertTrue(
  1369. "Wrong exception when connecting to address nobody listening on with short timeout 200: "
  1370. + e.toString(),
  1371. (e instanceof SocketTimeoutException));
  1372. }
  1373. // now validate that we get a interrupted exception if we try to connect
  1374. // to an address on which nobody is accepting connections and the
  1375. // timeout expired
  1376. try {
  1377. theSocket = new Socket();
  1378. theSocket.connect(nonReachableAddress, 40);
  1379. theSocket.close();
  1380. fail("No interrupted exception when connecting to address nobody listening on with short timeout 40: ");
  1381. } catch (Exception e) {
  1382. assertTrue(
  1383. "Wrong exception when connecting to address nobody listening on with short timeout 40: "
  1384. + e.toString(),
  1385. (e instanceof SocketTimeoutException));
  1386. }
  1387. // now validate that we can actually connect when somebody is listening
  1388. new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager
  1389. .getNextPort());
  1390. theSocket = new Socket();
  1391. serverSocket = new ServerSocket();
  1392. serverSocket.bind(theAddress);
  1393. theSocket.connect(theAddress, 100000);
  1394. // validate that when a socket is connected that it answers
  1395. // correctly to related queries
  1396. assertTrue("Socket did not returned connected when it is: ", theSocket
  1397. .isConnected());
  1398. assertFalse("Socket returned closed when it should be connected ",
  1399. theSocket.isClosed());
  1400. assertTrue("Socket returned not bound when it should be: ", theSocket
  1401. .isBound());
  1402. assertFalse(
  1403. "Socket returned input Shutdown when it should be connected ",
  1404. theSocket.isInputShutdown());
  1405. assertFalse(
  1406. "Socket returned output Shutdown when it should be connected ",
  1407. theSocket.isOutputShutdown());
  1408. assertTrue("Local port on connected socket was 0", theSocket
  1409. .getLocalPort() != 0);
  1410. theSocket.close();
  1411. serverSocket.close();
  1412. // now validate that we get the right exception if we connect when we
  1413. // are already connected
  1414. try {
  1415. new InetSocketAddress(InetAddress.getLocalHost(),
  1416. Support_PortManager.getNextPort());
  1417. theSocket = new Socket();
  1418. serverSocket = new ServerSocket();
  1419. serverSocket.bind(theAddress);
  1420. theSocket.connect(theAddress, 100000);
  1421. theSocket.connect(theAddress, 100000);
  1422. theSocket.close();
  1423. serverSocket.close();
  1424. fail("No exception when we try to connect on a connected socket: ");
  1425. } catch (Exception e) {
  1426. assertTrue(
  1427. "Wrong exception when connecting on socket that is already connected"
  1428. + e.toString(), (e instanceof SocketException));
  1429. assertFalse(
  1430. "Wrong exception when connecting on socket that is already connected"
  1431. + e.toString(),
  1432. (e instanceof SocketTimeoutException));
  1433. try {
  1434. theSocket.close();
  1435. serverSocket.close();
  1436. } catch (Exception e2) {
  1437. }
  1438. }
  1439. // now validate that connected socket can be used to read/write
  1440. new InetSocketAddress(InetAddress.getLocalHost(), Support_PortManager
  1441. .getNextPort());
  1442. theSocket = new Socket();
  1443. serverSocket = new ServerSocket();
  1444. serverSocket.bind(theAddress);
  1445. theSocket.connect(theAddress, 100000);
  1446. Socket servSock = serverSocket.accept();
  1447. InputStream theInput = theSocket.getInputStream();
  1448. OutputStream theOutput = servSock.getOutputStream();
  1449. InputStream theInput2 = servSock.getInputStream();
  1450. OutputStream theOutput2 = theSocket.getOutputStream();
  1451. String sendString = new String("Test");
  1452. theOutput.write(sendString.getBytes());
  1453. theOutput.flush();
  1454. Thread.sleep(1000);
  1455. int totalBytesRead = 0;
  1456. byte[] myBytes = new byte[100];
  1457. while (theInput.available() > 0) {
  1458. int bytesRead = theInput.read(myBytes, totalBytesRead,
  1459. myBytes.length - totalBytesRead);
  1460. totalBytesRead = totalBytesRead + bytesRead;
  1461. }
  1462. String receivedString = new String(myBytes, 0, totalBytesRead);
  1463. assertTrue("Could not recv on socket connected with timeout:"
  1464. + receivedString + ":" + sendString, receivedString
  1465. .equals(sendString));
  1466. sendString = new String("SEND - Test");
  1467. theOutput2.write(sendString.getBytes());
  1468. theOutput2.flush();
  1469. totalBytesRead = 0;
  1470. myBytes = new byte[100];
  1471. Thread.sleep(1000);
  1472. while (theInput2.available() > 0) {
  1473. int bytesRead = theInput2.read(myBytes, totalBytesRead,
  1474. myBytes.length - totalBytesRead);
  1475. totalBytesRead = totalBytesRead + bytesRead;
  1476. }
  1477. receivedString = new String(myBytes, 0, totalBytesRead);
  1478. assertTrue("Could not send on socket connected with timeout:"
  1479. + receivedString + ":" + sendString, receivedString
  1480. .equals(sendString));
  1481. theSocket.close();
  1482. serverSocket.close();
  1483. // now try to set options while we are connecting
  1484. theSocket = new Socket();
  1485. SocketConnector connector = new SocketConnector(5000, theSocket,
  1486. nonReachableAddress);
  1487. connector.start();
  1488. theSocket.setSoTimeout(100);
  1489. Thread.sleep(10);
  1490. assertEquals("Socket option not set during connect: 10 ", 100,
  1491. theSocket.getSoTimeout());
  1492. Thread.sleep(50);
  1493. theSocket.setSoTimeout(200);
  1494. assertEquals("Socket option not set during connect: 50 ", 200,
  1495. theSocket.getSoTimeout());
  1496. Thread.sleep(5000);
  1497. theSocket.close();
  1498. SocketChannel channel = SocketChannel.open();
  1499. channel.configureBlocking(false);
  1500. Socket socket = channel.socket();
  1501. int port = Support_PortManager.getNextPort();
  1502. try {
  1503. socket.connect( new InetSocketAddress(InetAddress.getLocalHost(),
  1504. Support_PortManager.getNextPort()), port);
  1505. fail("IllegalBlockingModeException was not thrown.");
  1506. } catch(IllegalBlockingModeException ibme) {
  1507. //expected
  1508. }
  1509. }
  1510. public void test_isInputShutdown() throws IOException {
  1511. InetSocketAddress theAddress = new InetSocketAddress(InetAddress
  1512. .getLocalHost(), Support_PortManager.getNextPort());
  1513. Socket theSocket = new Socket();
  1514. ServerSocket serverSocket = new ServerSocket();
  1515. serverSocket.bind(theAddress);
  1516. theSocket.connect(theAddress);
  1517. Socket servSock = serverSocket.accept();
  1518. InputStream theInput = theSocket.getInputStream();
  1519. OutputStream theOutput = servSock.getOutputStream();
  1520. // make sure we get the right answer with newly connected socket
  1521. assertFalse("Socket indicated input shutdown when it should not have",
  1522. theSocket.isInputShutdown());
  1523. // shutdown the output
  1524. theSocket.shutdownInput();
  1525. // make sure we get the right answer once it is shut down
  1526. assertTrue(
  1527. "Socket indicated input was NOT shutdown when it should have been",
  1528. theSocket.isInputShutdown());
  1529. theSocket.close();
  1530. serverSocket.close();
  1531. // make sure we get the right answer for closed sockets
  1532. assertFalse(
  1533. "Socket indicated input was shutdown when socket was closed",
  1534. servSock.isInputShutdown());
  1535. }
  1536. public void test_isOutputShutdown() throws IOException {
  1537. InetSocketAddress theAddress = new InetSocketAddress(InetAddress
  1538. .getLocalHost(), Support_PortManager.getNextPort());
  1539. Socket theSocket = new Socket();
  1540. ServerSocket serverSocket = new ServerSocket();
  1541. serverSocket.bind(theAddress);
  1542. theSocket.connect(theAddress);
  1543. Socket servSock = serverSocket.accept();
  1544. InputStream theInput = theSocket.getInputStream();
  1545. OutputStream theOutput = servSock.getOutputStream();
  1546. // make sure we get the right answer with newly connected socket
  1547. assertFalse("Socket indicated output shutdown when it should not have",
  1548. servSock.isOutputShutdown());
  1549. // shutdown the output
  1550. servSock.shutdownOutput();
  1551. // make sure we get the right answer once it is shut down
  1552. assertTrue(
  1553. "Socket indicated output was NOT shutdown when it should have been",
  1554. servSock.isOutputShutdown());
  1555. theSocket.close();
  1556. serverSocket.close();
  1557. // make sure we get the right answer for closed sockets
  1558. assertFalse(
  1559. "Socket indicated output was output shutdown when the socket was closed",
  1560. theSocket.isOutputShutdown());
  1561. }
  1562. public void test_setReuseAddressZ() {
  1563. try {
  1564. InetAddress allAddresses[] = InetAddress.getAllByName(InetAddress
  1565. .getLocalHost().getHostName());
  1566. if (allAddresses.length > 1) {
  1567. InetSocketAddress theAddress = new InetSocketAddress(
  1568. InetAddress.getLocalHost(), Support_PortManager
  1569. .getNextPort());
  1570. ServerSocket serverSocket = new ServerSocket();
  1571. serverSocket.bind(theAddress);
  1572. // try to bind to port address that is already in use with
  1573. // reuseAddress = false.
  1574. // On windows platforms the bind is allowed even then
  1575. // reUseAddress is false (ONLY IF BOTH SOCKETS
  1576. // ARE IPV4 Sockets) so our test uses the platform to determine
  1577. // what the expected result is. It seems that on linux
  1578. // platforms we also don't get an exception.
  1579. InetSocketAddress theLocalAddress = new InetSocketAddress(
  1580. (InetAddress) allAddresses[1], Support_PortManager
  1581. .getNextPort());
  1582. InetSocketAddress theOtherLocalAddress = new InetSocketAddress(
  1583. (InetAddress) allAddresses[0], theLocalAddress
  1584. .getPort());
  1585. Socket theSocket = new Socket();
  1586. theSocket.setReuseAddress(false);
  1587. theSocket.bind(theLocalAddress);
  1588. Socket theSocket2 = null;
  1589. String platform = System.getProperty("os.name");
  1590. try {
  1591. theSocket2 = new Socket();
  1592. theSocket2.setReuseAddress(false);
  1593. theSocket2.bind(theOtherLocalAddress);
  1594. if ((!platform.startsWith("Linux"))
  1595. && ((!platform.startsWith("Windows")) ||
  1596. // for windows we don't get an exception with
  1597. // setreuse set to false unless one of the
  1598. // addresses we bind to is an IPv6 address and we
  1599. // are therefore using the IPv6 stack.
  1600. !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
  1601. fail("No exception when setReuseAddress is false and we bind:"
  1602. + theLocalAddress.toString()
  1603. + ":"
  1604. + theOtherLocalAddress.toString());
  1605. }
  1606. } catch (IOException ex) {
  1607. if ((platform.startsWith("Linux"))
  1608. || ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
  1609. fail("Got unexpected exception when binding with setReuseAddress false on windows platform:"
  1610. + theAddress.toString() + ":" + ex.toString());
  1611. }
  1612. }
  1613. theSocket.close();
  1614. theSocket2.close();
  1615. // try to bind to port that is already in use with reuseAddress
  1616. // = true
  1617. theLocalAddress = new InetSocketAddress(
  1618. (InetAddress) allAddresses[0], Support_PortManager
  1619. .getNextPort());
  1620. theOtherLocalAddress = new InetSocketAddress(
  1621. (InetAddress) allAddresses[1], theLocalAddress
  1622. .getPort());
  1623. theSocket = new Socket();
  1624. theSocket.setReuseAddress(true);
  1625. theSocket.bind(theLocalAddress);
  1626. try {
  1627. theSocket2 = new Socket();
  1628. theSocket2.setReuseAddress(true);
  1629. theSocket2.bind(theOtherLocalAddress);
  1630. theSocket2.close();
  1631. } catch (IOException ex) {
  1632. fail("IOException when setReuseAddress is true and we bind :"
  1633. + ex.toString());
  1634. }
  1635. theSocket.close();
  1636. serverSocket.close();
  1637. // try with default behavior which should be the same on all
  1638. // platforms
  1639. theLocalAddress = new InetSocketAddress(
  1640. (InetAddress) allAddresses[0], Support_PortManager
  1641. .getNextPort());
  1642. theOtherLocalAddress = new InetSocketAddress(
  1643. (InetAddress) allAddresses[1], theLocalAddress
  1644. .getPort());
  1645. theSocket = new Socket();
  1646. theSocket.bind(theLocalAddress);
  1647. try {
  1648. theSocket2 = new Socket();
  1649. theSocket2.bind(theOtherLocalAddress);
  1650. theSocket2.close();
  1651. if ((!platform.startsWith("Linux"))
  1652. && ((!platform.startsWith("Windows")) || !((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
  1653. fail("No exception when setReuseAddress is default and we bind:"
  1654. + theLocalAddress.toString()
  1655. + ":"
  1656. + theOtherLocalAddress.toString());
  1657. }
  1658. } catch (IOException ex) {
  1659. if ((platform.startsWith("Linux"))
  1660. || ((platform.startsWith("Windows")) && ((((InetAddress) allAddresses[0]) instanceof Inet4Address) && (((InetAddress) allAddresses[1]) instanceof Inet4Address)))) {
  1661. fail("Got unexpected exception when binding with setReuseAddress default on windows platform:"
  1662. + theAddress.toString() + ":" + ex.toString());
  1663. }
  1664. }
  1665. theSocket.close();
  1666. serverSocket.close();
  1667. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
  1668. }
  1669. } catch (Exception e) {
  1670. handleException(e, SO_REUSEADDR);
  1671. }
  1672. try {
  1673. Socket theSocket = new Socket();
  1674. theSocket.close();
  1675. theSocket.setReuseAddress(true);
  1676. fail("SocketException was not thrown.");
  1677. } catch(SocketException ioe) {
  1678. //expected
  1679. } catch(IOException ioe) {
  1680. fail("IOException was thrown.");
  1681. }
  1682. }
  1683. public void test_getReuseAddress() {
  1684. try {
  1685. Socket theSocket = new Socket();
  1686. theSocket.setReuseAddress(true);
  1687. assertTrue("getReuseAddress false when it should be true",
  1688. theSocket.getReuseAddress());
  1689. theSocket.setReuseAddress(false);
  1690. assertFalse("getReuseAddress true when it should be False",
  1691. theSocket.getReuseAddress());
  1692. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_REUSEADDR);
  1693. } catch (Exception e) {
  1694. handleException(e, SO_REUSEADDR);
  1695. }
  1696. try {
  1697. Socket newSocket = new Socket();
  1698. newSocket.close();
  1699. try {
  1700. newSocket.getReuseAddress();
  1701. fail("SocketException was not thrown.");
  1702. } catch(SocketException e) {
  1703. //expected
  1704. }
  1705. } catch(Exception e) {
  1706. fail("Unexpected exception.");
  1707. }
  1708. }
  1709. public void test_setOOBInlineZ() {
  1710. // mostly tested in getOOBInline. Just set to make sure call works ok
  1711. try {
  1712. new InetSocketAddress(InetAddress.getLocalHost(),
  1713. Support_PortManager.getNextPort());
  1714. Socket theSocket = new Socket();
  1715. theSocket.setOOBInline(true);
  1716. assertTrue("expected OOBIline to be true", theSocket.getOOBInline());
  1717. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE);
  1718. } catch (Exception e) {
  1719. handleException(e, SO_OOBINLINE);
  1720. }
  1721. try {
  1722. Socket theSocket = new Socket();
  1723. theSocket.close();
  1724. theSocket.setOOBInline(true);
  1725. fail("SocketException was not thrown.");
  1726. } catch(SocketException ioe) {
  1727. //expected
  1728. } catch(IOException ioe) {
  1729. fail("IOException was thrown.");
  1730. }
  1731. }
  1732. public void test_getOOBInline() {
  1733. try {
  1734. new InetSocketAddress(InetAddress.getLocalHost(),
  1735. Support_PortManager.getNextPort());
  1736. Socket theSocket = new Socket();
  1737. // validate that value reflects what we set it to true after true,
  1738. // false after false and false after false false
  1739. theSocket.setOOBInline(true);
  1740. assertTrue("expected OOBIline to be true", theSocket.getOOBInline());
  1741. theSocket.setOOBInline(false);
  1742. assertFalse("expected OOBIline to be true", theSocket
  1743. .getOOBInline());
  1744. theSocket.setOOBInline(false);
  1745. assertFalse("expected OOBIline to be true", theSocket
  1746. .getOOBInline());
  1747. theSocket.close();
  1748. try {
  1749. theSocket.getOOBInline();
  1750. fail("SocketException was not thrown.");
  1751. } catch(SocketException se) {
  1752. //expected
  1753. }
  1754. ensureExceptionThrownIfOptionIsUnsupportedOnOS(SO_OOBINLINE);
  1755. } catch (Exception e) {
  1756. handleException(e, SO_OOBINLINE);
  1757. }
  1758. }
  1759. public void test_setTrafficClassI() {
  1760. try {
  1761. int IPTOS_LOWCOST = 0x2;
  1762. int IPTOS_THROUGHPUT = 0x8;
  1763. new InetSocketAddress(InetAddress.getLocalHost(),
  1764. Support_PortManager.getNextPort());
  1765. Socket theSocket = new Socket();
  1766. // validate that value set must be between 0 and 255
  1767. try {
  1768. theSocket.setTrafficClass(256);
  1769. fail("No exception was thrown when traffic class set to 256");
  1770. } catch (IllegalArgumentException e) {
  1771. }
  1772. try {
  1773. theSocket.setTrafficClass(-1);
  1774. fail("No exception was thrown when traffic class set to -1");
  1775. } catch (IllegalArgumentException e) {
  1776. }
  1777. // now validate that we can set it to some good values
  1778. theSocket.setTrafficClass(IPTOS_LOWCOST);
  1779. theSocket.setTrafficClass(IPTOS_THROUGHPUT);
  1780. ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS);
  1781. } catch (Exception e) {
  1782. handleException(e, IP_TOS);
  1783. }
  1784. try {
  1785. Socket theSocket = new Socket();
  1786. theSocket.close();
  1787. theSocket.setTrafficClass(0);
  1788. fail("SocketException was not thrown.");
  1789. } catch(SocketException ioe) {
  1790. //expected
  1791. } catch(IOException ioe) {
  1792. fail("IOException was thrown.");
  1793. }
  1794. }
  1795. public void test_getTrafficClass() {
  1796. try {
  1797. new InetSocketAddress(InetAddress.getLocalHost(),
  1798. Support_PortManager.getNextPort());
  1799. Socket theSocket = new Socket();
  1800. /*
  1801. * we cannot actually check that the values are set as if a platform
  1802. * does not support the option then it may come back unset even
  1803. * though we set it so just get the value to make sure we can get it
  1804. */
  1805. int trafficClass = theSocket.getTrafficClass();
  1806. ensureExceptionThrownIfOptionIsUnsupportedOnOS(IP_TOS);
  1807. } catch (Exception e) {
  1808. handleException(e, IP_TOS);
  1809. }
  1810. }
  1811. public void test_getChannel() throws Exception {
  1812. assertNull(new Socket().getChannel());
  1813. SocketChannel channel = SocketChannel.open();
  1814. Socket socket = channel.socket();
  1815. assertEquals(channel, socket.getChannel());
  1816. socket.close();
  1817. channel.close();
  1818. }
  1819. public void test_sendUrgentDataI() {
  1820. // Some platforms may not support urgent data in this case we will not
  1821. // run these tests. For now run on all platforms until we find those
  1822. // that do not support urgent data
  1823. String platform = System.getProperty("os.name");
  1824. if (!platform.equals("Dummy")) {
  1825. // validate that when OOBInline is false that any urgent data
  1826. // is silently ignored
  1827. String urgentData = "U";
  1828. try {
  1829. InetSocketAddress theAddress = new InetSocketAddress(
  1830. InetAddress.getLocalHost(), Support_PortManager
  1831. .getNextPort());
  1832. Socket theSocket = new Socket();
  1833. ServerSocket serverSocket = new ServerSocket();
  1834. serverSocket.bind(theAddress);
  1835. theSocket.connect(theAddress);
  1836. Socket servSock = serverSocket.accept();
  1837. InputStream theInput = theSocket.getInputStream();
  1838. OutputStream theOutput = servSock.getOutputStream();
  1839. // send the regular data
  1840. String sendString = new String("Test");
  1841. theOutput.write(sendString.getBytes());
  1842. theOutput.flush();
  1843. // send the urgent data which should not be received
  1844. theSocket.setOOBInline(false);
  1845. servSock.sendUrgentData(urgentData.getBytes()[0]);
  1846. theOutput.write(sendString.getBytes());
  1847. theOutput.flush();
  1848. // give things some time to settle
  1849. Thread.sleep(1000);
  1850. int totalBytesRead = 0;
  1851. byte[] myBytes = new byte[100];
  1852. while (theInput.available() > 0) {
  1853. int bytesRead = theInput.read(myBytes, totalBytesRead,
  1854. myBytes.length - totalBytesRead);
  1855. totalBytesRead = totalBytesRead + bytesRead;
  1856. }
  1857. String receivedString = new String(myBytes, 0, totalBytesRead);
  1858. //assertTrue("Urgent Data seems to have been received:"
  1859. // + receivedString + ":" + sendString, receivedString
  1860. // .equals(sendString + sendString));
  1861. theSocket.close();
  1862. serverSocket.close();
  1863. // now validate that urgent data is received as expected. Expect
  1864. // that it should be between the two writes.
  1865. theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
  1866. Support_PortManager.getNextPort());
  1867. theSocket = new Socket();
  1868. serverSocket = new ServerSocket();
  1869. serverSocket.bind(theAddress);
  1870. theSocket.connect(theAddress);
  1871. servSock = serverSocket.accept();
  1872. theInput = theSocket.getInputStream();
  1873. theOutput = servSock.getOutputStream();
  1874. // send the regular data
  1875. sendString = new String("Test - Urgent Data");
  1876. theOutput.write(sendString.getBytes());
  1877. theOutput.flush();
  1878. // send the urgent data which should be received
  1879. theSocket.setOOBInline(true);
  1880. servSock.sendUrgentData(urgentData.getBytes()[0]);
  1881. theOutput.write(sendString.getBytes());
  1882. theOutput.flush();
  1883. Thread.sleep(1000);
  1884. totalBytesRead = 0;
  1885. myBytes = new byte[100];
  1886. while (theInput.available() > 0) {
  1887. int bytesRead = theInput.read(myBytes, totalBytesRead,
  1888. myBytes.length - totalBytesRead);
  1889. totalBytesRead = totalBytesRead + bytesRead;
  1890. }
  1891. receivedString = new String(myBytes, 0, totalBytesRead);
  1892. assertTrue("Urgent Data was not received with one urgent byte:"
  1893. + receivedString + ":" + sendString + urgentData
  1894. + sendString, receivedString.equals(sendString
  1895. + urgentData + sendString));
  1896. theSocket.close();
  1897. serverSocket.close();
  1898. // now test case where we try to send two urgent bytes.
  1899. theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
  1900. Support_PortManager.getNextPort());
  1901. theSocket = new Socket();
  1902. serverSocket = new ServerSocket();
  1903. serverSocket.bind(theAddress);
  1904. theSocket.connect(theAddress);
  1905. servSock = serverSocket.accept();
  1906. theInput = theSocket.getInputStream();
  1907. theOutput = servSock.getOutputStream();
  1908. // send the regular data
  1909. sendString = new String("Test - Urgent Data");
  1910. theOutput.write(sendString.getBytes());
  1911. theOutput.flush();
  1912. // send the urgent data which should not be received
  1913. theSocket.setOOBInline(true);
  1914. servSock.sendUrgentData(urgentData.getBytes()[0]);
  1915. servSock.sendUrgentData(urgentData.getBytes()[0]);
  1916. theOutput.write(sendString.getBytes());
  1917. theOutput.flush();
  1918. Thread.sleep(1000);
  1919. totalBytesRead = 0;
  1920. myBytes = new byte[100];
  1921. while (theInput.available() > 0) {
  1922. int bytesRead = theInput.read(myBytes, totalBytesRead,
  1923. myBytes.length - totalBytesRead);
  1924. totalBytesRead = totalBytesRead + bytesRead;
  1925. }
  1926. receivedString = new String(myBytes, 0, totalBytesRead);
  1927. assertTrue(
  1928. "Did not get right byte of urgent data when two sent:"
  1929. + receivedString + ":" + sendString
  1930. + urgentData + urgentData + sendString,
  1931. receivedString.equals(sendString + urgentData
  1932. + urgentData + sendString));
  1933. theSocket.close();
  1934. serverSocket.close();
  1935. /*
  1936. * TODO : These do not currently pass on XP SP2 and Server 2003
  1937. */
  1938. if (!platform.startsWith("Windows")) {
  1939. // now test the case were we send turn the OOBInline on/off
  1940. theAddress = new InetSocketAddress(InetAddress
  1941. .getLocalHost(), Support_PortManager.getNextPort());
  1942. theSocket = new Socket();
  1943. serverSocket = new ServerSocket();
  1944. serverSocket.bind(theAddress);
  1945. theSocket.connect(theAddress);
  1946. servSock = serverSocket.accept();
  1947. theInput = theSocket.getInputStream();
  1948. theOutput = servSock.getOutputStream();
  1949. // send the regular data
  1950. sendString = new String("Test - Urgent Data");
  1951. theOutput.write(sendString.getBytes());
  1952. theOutput.flush();
  1953. // send the urgent data which should be received
  1954. theSocket.setOOBInline(true);
  1955. servSock.sendUrgentData(urgentData.getBytes()[0]);
  1956. theOutput.write(sendString.getBytes());
  1957. theOutput.flush();
  1958. Thread.sleep(1000);
  1959. totalBytesRead = 0;
  1960. myBytes = new byte[100];
  1961. while (theInput.available() > 0) {
  1962. int bytesRead = theInput.read(myBytes, totalBytesRead,
  1963. myBytes.length - totalBytesRead);
  1964. totalBytesRead = totalBytesRead + bytesRead;
  1965. }
  1966. receivedString = new String(myBytes, 0, totalBytesRead);
  1967. assertTrue(
  1968. "Did not get urgent data when turning on/off(1):"
  1969. + receivedString + ":" + sendString
  1970. + urgentData + sendString, receivedString
  1971. .equals(sendString + urgentData
  1972. + sendString));
  1973. // send the regular data
  1974. sendString = new String("Test - Urgent Data");
  1975. theOutput.write(sendString.getBytes());
  1976. theOutput.flush();
  1977. // send the urgent data which should not be received
  1978. theSocket.setOOBInline(false);
  1979. servSock.sendUrgentData(urgentData.getBytes()[0]);
  1980. // send trailing data
  1981. theOutput.write(sendString.getBytes());
  1982. theOutput.flush();
  1983. Thread.sleep(1000);
  1984. totalBytesRead = 0;
  1985. myBytes = new byte[100];
  1986. while (theInput.available() > 0) {
  1987. int bytesRead = theInput.read(myBytes, totalBytesRead,
  1988. myBytes.length - totalBytesRead);
  1989. totalBytesRead = totalBytesRead + bytesRead;
  1990. }
  1991. receivedString = new String(myBytes, 0, totalBytesRead);
  1992. //assertTrue(
  1993. // "Got unexpected data data when turning on/off(2):"
  1994. // + receivedString + ":" + sendString
  1995. // + sendString, receivedString
  1996. // .equals(sendString + sendString));
  1997. // now turn back on and get data. Here we also
  1998. // get the previously sent byte of urgent data as it is
  1999. // still in the urgent buffer
  2000. // send the regular data
  2001. sendString = new String("Test - Urgent Data");
  2002. theOutput.write(sendString.getBytes());
  2003. theOutput.flush();
  2004. // send the urgent data which should be received again
  2005. theSocket.setOOBInline(true);
  2006. servSock.sendUrgentData(urgentData.getBytes()[0]);
  2007. theOutput.write(sendString.getBytes());
  2008. theOutput.flush();
  2009. Thread.sleep(1000);
  2010. totalBytesRead = 0;
  2011. myBytes = new byte[100];
  2012. while (theInput.available() > 0) {
  2013. int bytesRead = theInput.read(myBytes, totalBytesRead,
  2014. myBytes.length - totalBytesRead);
  2015. totalBytesRead = totalBytesRead + bytesRead;
  2016. }
  2017. receivedString = new String(myBytes, 0, totalBytesRead);
  2018. // depending on the platform we may get the previously sent
  2019. // urgent data or not (examples windows-yes, Linux-no).
  2020. // So accept either so long as we get the urgent data from
  2021. // when it was on.
  2022. //assertTrue(
  2023. // "Did not get urgent data when turning on/off(3) GOT:"
  2024. // + receivedString + ":Expected" + urgentData
  2025. // + sendString + urgentData + sendString
  2026. // + ":OR:" + sendString + urgentData
  2027. // + sendString,
  2028. // (receivedString.equals(urgentData + sendString
  2029. // + urgentData + sendString) || receivedString
  2030. // .equals(sendString + urgentData
  2031. // + sendString)));
  2032. theSocket.close();
  2033. serverSocket.close();
  2034. }
  2035. // now test the case where there is only urgent data
  2036. theAddress = new InetSocketAddress(InetAddress.getLocalHost(),
  2037. Support_PortManager.getNextPort());
  2038. theSocket = new Socket();
  2039. serverSocket = new ServerSocket();
  2040. serverSocket.bind(theAddress);
  2041. theSocket.connect(theAddress);
  2042. servSock = serverSocket.accept();
  2043. theInput = theSocket.getInputStream();
  2044. theOutput = servSock.getOutputStream();
  2045. // send the urgent data which should not be received.
  2046. theSocket.setOOBInline(true);
  2047. servSock.sendUrgentData(urgentData.getBytes()[0]);
  2048. Thread.sleep(1000);
  2049. totalBytesRead = 0;
  2050. myBytes = new byte[100];
  2051. while (theInput.available() > 0) {
  2052. int bytesRead = theInput.read(myBytes, totalBytesRead,
  2053. myBytes.length - totalBytesRead);
  2054. totalBytesRead = totalBytesRead + bytesRead;
  2055. }
  2056. receivedString = new String(myBytes, 0, totalBytesRead);
  2057. assertTrue("Did not get urgent data only urgent data sent:"
  2058. + receivedString + ":" + urgentData, receivedString
  2059. .equals(urgentData));
  2060. } catch (Exception e) {
  2061. // for platforms that do not support urgent data we expect an
  2062. // exception. For the others report an error.
  2063. // TODO : Need to introduce a better test for the exception
  2064. // so that the failure only occurs on platforms that support
  2065. // urgent data
  2066. fail("Platform:" + platform
  2067. + ": Got exception during sendUrgent data tests"
  2068. + e.toString());
  2069. }
  2070. }
  2071. try {
  2072. Socket theSocket = new Socket();
  2073. theSocket.close();
  2074. theSocket.sendUrgentData(0);
  2075. fail("IOException was not thrown.");
  2076. } catch(IOException ioe) {
  2077. //expected
  2078. }
  2079. }
  2080. public void test_setPerformancePreference_Int_Int_Int() throws Exception {
  2081. Socket theSocket = new Socket();
  2082. theSocket.setPerformancePreferences(1, 1, 1);
  2083. }
  2084. public void test_ConstructorLjava_net_Proxy_Exception() {
  2085. SocketAddress addr1 = InetSocketAddress.createUnresolved("127.0.0.1",
  2086. 80);
  2087. SocketAddress addr2 = new InetSocketAddress("localhost", 80);
  2088. Proxy proxy1 = new Proxy(Proxy.Type.HTTP, addr1);
  2089. // IllegalArgumentException test
  2090. try {
  2091. new Socket(proxy1);
  2092. fail("should throw IllegalArgumentException");
  2093. } catch (IllegalArgumentException e) {
  2094. // expected
  2095. }
  2096. Proxy proxy2 = new Proxy(Proxy.Type.SOCKS, addr1);
  2097. // should not throw any exception
  2098. new Socket(proxy2);
  2099. new Socket(Proxy.NO_PROXY);
  2100. try {
  2101. new Socket((Proxy) null);
  2102. fail("IllegalArgumentException was not thrown.");
  2103. } catch(IllegalArgumentException iae) {
  2104. //expected
  2105. }
  2106. }
  2107. public void test_ConstructorLSocketImpl() {
  2108. MockSocketImpl msi = new MockSocketImpl();
  2109. try {
  2110. new TestSocket(msi);
  2111. } catch (SocketException e) {
  2112. fail("SocketException was thrown.");
  2113. }
  2114. }
  2115. public void test_connect_unknownhost() throws Exception {
  2116. Socket socket = new Socket();
  2117. InetSocketAddress socketAddress = new InetSocketAddress("unknownhost", 12345);
  2118. try {
  2119. socket.connect(socketAddress);
  2120. fail("Should throw IOException");
  2121. } catch (IOException e) {
  2122. // expected
  2123. }
  2124. }
  2125. public void test_connect_unresolved_unknown() throws Exception {
  2126. Socket socket = new Socket();
  2127. InetSocketAddress unresolved = InetSocketAddress.createUnresolved("unknownhost", 12345);
  2128. try {
  2129. socket.connect(unresolved);
  2130. fail("Should throw IOException");
  2131. } catch (IOException e) {
  2132. // expected
  2133. }
  2134. }
  2135. public void test_connect_unresolved() throws Exception {
  2136. Socket socket = new Socket();
  2137. InetSocketAddress unresolvedSocketAddress = InetSocketAddress.createUnresolved(
  2138. Support_Configuration.SocksServerTestHost,
  2139. Support_Configuration.SocksServerTestPort);
  2140. try {
  2141. socket.connect(unresolvedSocketAddress);
  2142. fail("Should throw IOException");
  2143. } catch (IOException e) {
  2144. // expected
  2145. }
  2146. }
  2147. public void test_getOutputStream_shutdownOutput() throws Exception {
  2148. // regression test for Harmony-873
  2149. ServerSocket ss = new ServerSocket(0);
  2150. Socket s = new Socket("127.0.0.1", ss.getLocalPort());
  2151. ss.accept();
  2152. s.shutdownOutput();
  2153. try {
  2154. s.getOutputStream();
  2155. fail("should throw SocketException");
  2156. } catch (IOException e) {
  2157. // expected
  2158. } finally {
  2159. s.close();
  2160. }
  2161. SocketChannel channel = SocketChannel.open(
  2162. new InetSocketAddress(ss.getInetAddress(), ss.getLocalPort()));
  2163. channel.configureBlocking(false);
  2164. ss.accept();
  2165. Socket socket = channel.socket();
  2166. OutputStream out = null;
  2167. try {
  2168. out = socket.getOutputStream();
  2169. out.write(1);
  2170. fail("IllegalBlockingModeException was not thrown.");
  2171. } catch(IllegalBlockingModeException ibme) {
  2172. //expected
  2173. } finally {
  2174. if(out != null) out.close();
  2175. socket.close();
  2176. channel.close();
  2177. }
  2178. }
  2179. public void test_shutdownInputOutput_twice() throws Exception {
  2180. // regression test for Harmony-2944
  2181. Socket s = new Socket("0.0.0.0", 0, false);
  2182. s.shutdownInput();
  2183. try {
  2184. s.shutdownInput();
  2185. fail("should throw SocketException");
  2186. } catch (SocketException se) {
  2187. // expected
  2188. }
  2189. s.shutdownOutput();
  2190. try {
  2191. s.shutdownOutput();
  2192. fail("should throw SocketException");
  2193. } catch (SocketException se) {
  2194. // expected
  2195. }
  2196. }
  2197. /**
  2198. * Sets up the fixture, for example, open a network connection. This method
  2199. * is called before a test is executed.
  2200. *
  2201. * @throws Exception
  2202. */
  2203. protected void setUp() throws Exception {
  2204. super.setUp();
  2205. }
  2206. /**
  2207. * Tears down the fixture, for example, close a network connection. This
  2208. * method is called after a test is executed.
  2209. */
  2210. protected void tearDown() {
  2211. try {
  2212. if (s != null)
  2213. s.close();
  2214. } catch (Exception e) {
  2215. }
  2216. try {
  2217. if (ss != null)
  2218. ss.close();
  2219. } catch (Exception e) {
  2220. }
  2221. try {
  2222. if (t != null)
  2223. t.interrupt();
  2224. } catch (Exception e) {
  2225. }
  2226. }
  2227. static class MockSecurityManager extends SecurityManager {
  2228. public void checkConnect(String host, int port) {
  2229. if ("127.0.0.1".equals(host)) {
  2230. throw new SecurityException("permission is not allowed");
  2231. }
  2232. }
  2233. public void checkPermission(Permission permission) {
  2234. return;
  2235. }
  2236. }
  2237. /**
  2238. *
  2239. */
  2240. protected int startServer(String name) {
  2241. int portNumber = Support_PortManager.getNextPort();
  2242. try {
  2243. ss = new ServerSocket(portNumber, 5);
  2244. } catch (IOException e) {
  2245. fail(name + ": " + e);
  2246. }
  2247. return ss.getLocalPort();
  2248. }
  2249. class MockSocketImpl extends SocketImpl {
  2250. public MockSocketImpl() {
  2251. super();
  2252. }
  2253. @Override
  2254. protected void accept(SocketImpl arg0) throws IOException {
  2255. }
  2256. @Override
  2257. protected int available() throws IOException {
  2258. return 0;
  2259. }
  2260. @Override
  2261. protected void bind(InetAddress arg0, int arg1) throws IOException {
  2262. }
  2263. @Override
  2264. protected void close() throws IOException {
  2265. }
  2266. @Override
  2267. protected void connect(String arg0, int arg1) throws IOException {
  2268. }
  2269. @Override
  2270. protected void connect(InetAddress arg0, int arg1) throws IOException {
  2271. }
  2272. @Override
  2273. protected void connect(SocketAddress arg0, int arg1) throws IOException {
  2274. }
  2275. @Override
  2276. protected void create(boolean arg0) throws IOException {
  2277. }
  2278. @Override
  2279. protected InputStream getInputStream() throws IOException {
  2280. return null;
  2281. }
  2282. @Override
  2283. protected OutputStream getOutputStream() throws IOException {
  2284. return null;
  2285. }
  2286. @Override
  2287. protected void listen(int arg0) throws IOException {
  2288. }
  2289. @Override
  2290. protected void sendUrgentData(int arg0) throws IOException {
  2291. }
  2292. public Object getOption(int arg0) throws SocketException {
  2293. return null;
  2294. }
  2295. public void setOption(int arg0, Object arg1) throws SocketException {
  2296. }
  2297. }
  2298. }