/ojc-core/ftpbc/ftpbcimpl/test/com/sun/jbi/ftpbc/ftp/SocksSocketTest.java

https://bitbucket.org/ldassonville/openesb-components · Java · 631 lines · 363 code · 136 blank · 132 comment · 0 complexity · 96d41c437cf33ce91499905168a53531 MD5 · raw file

  1. /*
  2. * BEGIN_HEADER - DO NOT EDIT
  3. *
  4. * The contents of this file are subject to the terms
  5. * of the Common Development and Distribution License
  6. * (the "License"). You may not use this file except
  7. * in compliance with the License.
  8. *
  9. * You can obtain a copy of the license at
  10. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  11. * See the License for the specific language governing
  12. * permissions and limitations under the License.
  13. *
  14. * When distributing Covered Code, include this CDDL
  15. * HEADER in each file and include the License file at
  16. * https://open-jbi-components.dev.java.net/public/CDDLv1.0.html.
  17. * If applicable add the following below this CDDL HEADER,
  18. * with the fields enclosed by brackets "[]" replaced with
  19. * your own identifying information: Portions Copyright
  20. * [year] [name of copyright owner]
  21. */
  22. /*
  23. * @(#)SocksSocketTest.java
  24. *
  25. * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
  26. *
  27. * END_HEADER - DO NOT EDIT
  28. */
  29. package com.sun.jbi.ftpbc.ftp;
  30. import java.io.InputStream;
  31. import java.net.InetAddress;
  32. import java.net.Socket;
  33. import java.net.SocketImplFactory;
  34. import junit.framework.Test;
  35. import junit.framework.TestCase;
  36. import junit.framework.TestSuite;
  37. /*
  38. * JUnit based test.
  39. *
  40. *
  41. *
  42. *
  43. * @author Harry Liu (harry.liu@sun.com)
  44. *
  45. * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
  46. */
  47. public class SocksSocketTest extends TestCase {
  48. SocksSocket instance;
  49. public SocksSocketTest(String testName) {
  50. super(testName);
  51. }
  52. protected void setUp() throws Exception {
  53. super.setUp();
  54. // null Socks -- not depend on live Socks server
  55. instance = new SocksSocket(null, "www.sun.com", 80);
  56. }
  57. protected void tearDown() throws Exception {
  58. try {
  59. instance.close();
  60. } catch (Exception e) {
  61. ;
  62. }
  63. instance = null;
  64. super.tearDown();
  65. }
  66. public static Test suite() {
  67. TestSuite suite = new TestSuite(SocksSocketTest.class);
  68. return suite;
  69. }
  70. /**
  71. * Test of setSocketImplFactory method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  72. */
  73. public void testSetSocketImplFactory() throws Exception {
  74. System.out.println("setSocketImplFactory");
  75. SocketImplFactory factory = null;
  76. SocksSocket.setSocketImplFactory(factory);
  77. }
  78. /**
  79. * Test of close method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  80. */
  81. public void testClose() throws Exception {
  82. System.out.println("close");
  83. instance.close();
  84. }
  85. /**
  86. * Test of getInetAddress method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  87. */
  88. public void testGetInetAddress() throws Exception {
  89. System.out.println("getInetAddress");
  90. InetAddress result = instance.getInetAddress();
  91. assertNotNull(result);
  92. }
  93. /**
  94. * Test of getInputStream method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  95. */
  96. public void testGetInputStream() throws Exception {
  97. System.out.println("getInputStream");
  98. InputStream result = instance.getInputStream();
  99. assertNotNull(result);
  100. }
  101. /**
  102. * Test of getLocalAddress method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  103. */
  104. public void testGetLocalAddress() throws Exception {
  105. System.out.println("getLocalAddress");
  106. InetAddress result = instance.getLocalAddress();
  107. assertNotNull(result);
  108. }
  109. /**
  110. * Test of getLocalPort method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  111. */
  112. public void testGetLocalPort() throws Exception {
  113. System.out.println("getLocalPort");
  114. int result = instance.getLocalPort();
  115. assertTrue(result > 0);
  116. }
  117. /**
  118. * Test of getOutputStream method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  119. */
  120. public void testGetOutputStream() throws Exception {
  121. System.out.println("getOutputStream");
  122. assertNotNull(instance.getOutputStream());
  123. }
  124. /**
  125. * Test of getPort method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  126. */
  127. public void testGetPort() throws Exception {
  128. System.out.println("getPort");
  129. int expResult = 80;
  130. int result = instance.getPort();
  131. assertEquals(expResult, result);
  132. }
  133. /**
  134. * Test of getReceiveBufferSize method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  135. */
  136. public void testGetReceiveBufferSize() throws Exception {
  137. System.out.println("getReceiveBufferSize");
  138. int expResult = 8192;
  139. int result = instance.getReceiveBufferSize();
  140. assertEquals(expResult, result);
  141. }
  142. /**
  143. * Test of getSendBufferSize method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  144. */
  145. public void testGetSendBufferSize() throws Exception {
  146. System.out.println("getSendBufferSize");
  147. int expResult = 8192;
  148. int result = instance.getSendBufferSize();
  149. assertEquals(expResult, result);
  150. }
  151. /**
  152. * Test of getSoLinger method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  153. */
  154. public void testGetSoLinger() throws Exception {
  155. System.out.println("getSoLinger");
  156. int expResult = -1;
  157. int result = instance.getSoLinger();
  158. assertEquals(expResult, result);
  159. }
  160. /**
  161. * Test of getSoTimeout method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  162. */
  163. public void testGetSoTimeout() throws Exception {
  164. System.out.println("getSoTimeout");
  165. int expResult = 0;
  166. int result = instance.getSoTimeout();
  167. assertEquals(expResult, result);
  168. }
  169. /**
  170. * Test of getTcpNoDelay method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  171. */
  172. public void testGetTcpNoDelay() throws Exception {
  173. System.out.println("getTcpNoDelay");
  174. boolean expResult = false;
  175. boolean result = instance.getTcpNoDelay();
  176. assertEquals(expResult, result);
  177. }
  178. /**
  179. * Test of setReceiveBufferSize method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  180. */
  181. public void testSetReceiveBufferSize() throws Exception {
  182. System.out.println("setReceiveBufferSize");
  183. int size = 1024;
  184. instance.setReceiveBufferSize(size);
  185. assertEquals(size, instance.getReceiveBufferSize());
  186. try {
  187. size = 0;
  188. instance.setReceiveBufferSize(size);
  189. fail("An exception is expected - 0 size is invalid");
  190. } catch (Exception ex) {
  191. ex.printStackTrace();
  192. }
  193. try {
  194. size = -1;
  195. instance.setReceiveBufferSize(size);
  196. fail("An exception is expected - negative size is invalid");
  197. } catch (Exception ex) {
  198. ex.printStackTrace();
  199. }
  200. }
  201. /**
  202. * Test of setSendBufferSize method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  203. */
  204. public void testSetSendBufferSize() throws Exception {
  205. System.out.println("setSendBufferSize");
  206. int size = 1024;
  207. instance.setSendBufferSize(size);
  208. assertEquals(size, instance.getSendBufferSize());
  209. try {
  210. size = 0;
  211. instance.setSendBufferSize(size);
  212. fail("An exception is expected - 0 size is invalid");
  213. } catch (Exception ex) {
  214. ex.printStackTrace();
  215. }
  216. try {
  217. size = -1;
  218. instance.setSendBufferSize(size);
  219. fail("An exception is expected - negative size is invalid");
  220. } catch (Exception ex) {
  221. ex.printStackTrace();
  222. }
  223. }
  224. /**
  225. * Test of setSoLinger method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  226. */
  227. public void testSetSoLinger() throws Exception {
  228. System.out.println("setSoLinger");
  229. boolean on = true;
  230. int linger = 0;
  231. instance.setSoLinger(on, linger);
  232. assertEquals(linger, instance.getSoLinger());
  233. linger = 10;
  234. instance.setSoLinger(on, linger);
  235. assertEquals(linger, instance.getSoLinger());
  236. try {
  237. linger = -1;
  238. instance.setSoLinger(on, linger);
  239. fail("An exception is expected - invalid value");
  240. } catch (Exception ex) {
  241. ex.printStackTrace();
  242. }
  243. // false
  244. on = false;
  245. linger = 0;
  246. instance.setSoLinger(on, linger);
  247. assertEquals("Always return -1 when it is disabled", -1, instance.getSoLinger());
  248. linger = 10;
  249. instance.setSoLinger(on, linger);
  250. assertEquals("Always return -1 when it is disabled", -1, instance.getSoLinger());
  251. linger = -10;
  252. instance.setSoLinger(on, linger);
  253. assertEquals("Always return -1 when it is disabled", -1, instance.getSoLinger());
  254. }
  255. /**
  256. * Test of setSoTimeout method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  257. */
  258. public void testSetSoTimeout() throws Exception {
  259. System.out.println("setSoTimeout");
  260. int timeout = 0;
  261. instance.setSoTimeout(timeout);
  262. assertEquals(timeout, instance.getSoTimeout());
  263. timeout = 100;
  264. instance.setSoTimeout(timeout);
  265. assertEquals(timeout, instance.getSoTimeout());
  266. }
  267. /**
  268. * Test of setTcpNoDelay method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  269. */
  270. public void testSetTcpNoDelay() throws Exception {
  271. System.out.println("setTcpNoDelay");
  272. boolean on = true;
  273. instance.setTcpNoDelay(on);
  274. assertEquals(on, instance.getTcpNoDelay());
  275. }
  276. /**
  277. * Test of toString method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  278. */
  279. public void testToString() throws Exception {
  280. System.out.println("toString");
  281. String expResult = "";
  282. String result = instance.toString();
  283. assertNotNull(result);
  284. assertFalse(expResult.equals(result));
  285. }
  286. /**
  287. * Test of getKeepAlive method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  288. */
  289. public void testGetKeepAlive() throws Exception {
  290. System.out.println("getKeepAlive");
  291. boolean expResult = false;
  292. boolean result = instance.getKeepAlive();
  293. assertEquals(expResult, result);
  294. }
  295. /**
  296. * Test of setKeepAlive method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  297. */
  298. public void testSetKeepAlive() throws Exception {
  299. System.out.println("setKeepAlive");
  300. boolean on = true;
  301. instance.setKeepAlive(on);
  302. assertEquals(on, instance.getKeepAlive());
  303. }
  304. /**
  305. * Test of shutdownInput method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  306. */
  307. public void testShutdownInput() throws Exception {
  308. System.out.println("shutdownInput");
  309. instance.shutdownInput();
  310. }
  311. /**
  312. * Test of shutdownOutput method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  313. */
  314. public void testShutdownOutput() throws Exception {
  315. System.out.println("shutdownOutput");
  316. instance.shutdownOutput();
  317. }
  318. /**
  319. * Test of main method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  320. */
  321. public void testMain() throws Exception {
  322. System.out.println("main");
  323. String[] args = null;
  324. SocksSocket.main(args);
  325. }
  326. /**
  327. * Test of getSocks method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  328. */
  329. public void testGetSocks() throws Exception {
  330. System.out.println("getSocks");
  331. Socks expResult = null;
  332. Socks result = instance.getSocks();
  333. assertEquals(expResult, result);
  334. }
  335. /**
  336. * Test of getSocksList method, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  337. */
  338. public void testGetSocksList() throws Exception {
  339. System.out.println("getSocksList");
  340. SocksChain expResult = null;
  341. SocksChain result = instance.getSocksList();
  342. assertEquals(expResult, result);
  343. }
  344. /**
  345. * Test of constructor, of class com.sun.jbi.ftpbc.ftp.SocksSocket.
  346. */
  347. public void testSocksSocket() throws Exception {
  348. System.out.println("SocksSocket");
  349. SocksChain chain = null;
  350. Socks socks = null;
  351. SocksSocket ss = new SocksSocket(InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21);
  352. assertNotNull(ss);
  353. ss.close();
  354. ss = new SocksSocket(FtpTprops.FTP_TEST_HOST, 21);
  355. assertNotNull(ss);
  356. ss.close();
  357. ss = new SocksSocket(InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21, chain);
  358. assertNotNull(ss);
  359. ss.close();
  360. ss = new SocksSocket(socks, InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21);
  361. assertNotNull(ss);
  362. ss.close();
  363. ss = new SocksSocket(socks, FtpTprops.FTP_TEST_HOST, 21);
  364. assertNotNull(ss);
  365. ss.close();
  366. ss = new SocksSocket(FtpTprops.FTP_TEST_HOST, 21, chain);
  367. assertNotNull(ss);
  368. ss.close();
  369. try {
  370. int localPort = 4567;
  371. // the local port 4567, 4568, etc may be in use in test env,
  372. // so following constructors may or may not succeed.
  373. ss = new SocksSocket(InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21, InetAddress.getLocalHost(), localPort);
  374. assertNotNull(ss);
  375. ss.close();
  376. ss = new SocksSocket(FtpTprops.FTP_TEST_HOST, 21, InetAddress.getLocalHost(), localPort);
  377. assertNotNull(ss);
  378. ss.close();
  379. ss = new SocksSocket(InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21, InetAddress.getLocalHost(), localPort, chain);
  380. assertNotNull(ss);
  381. ss.close();
  382. ss = new SocksSocket(socks, InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21, InetAddress.getLocalHost(), localPort);
  383. assertNotNull(ss);
  384. ss.close();
  385. ss = new SocksSocket(socks, FtpTprops.FTP_TEST_HOST, 21, InetAddress.getLocalHost(), localPort);
  386. assertNotNull(ss);
  387. ss.close();
  388. ss = new SocksSocket(FtpTprops.FTP_TEST_HOST, 21, InetAddress.getLocalHost(), localPort, chain);
  389. assertNotNull(ss);
  390. ss.close();
  391. } catch (Exception e) {
  392. // the local port 4567, 4568, etc may be in use in test env,
  393. // so those tests may or may not succeed.
  394. e.printStackTrace();
  395. System.out.println("SocksSocket: Local Port is in use under test environment - it is ok for test.");
  396. }
  397. }
  398. public void testMore_WithWrongSocksServer() throws Exception {
  399. System.out.println("testMore_WithLiveSocksServer");
  400. Socks socks = new Socks("WrongSocksHost", 1080, "username", "password");
  401. try {
  402. new SocksSocket(socks, InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21);
  403. fail("An exception is expected - wrong socks server");
  404. } catch (Exception ex) {
  405. ex.printStackTrace();
  406. }
  407. try {
  408. new SocksSocket(socks, FtpTprops.FTP_TEST_HOST, 21);
  409. fail("An exception is expected - wrong socks server");
  410. } catch (Exception ex) {
  411. ex.printStackTrace();
  412. }
  413. int localPort = 4570;
  414. try {
  415. new SocksSocket(socks, InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21, InetAddress.getLocalHost(), localPort);
  416. fail("An exception is expected - wrong socks server");
  417. } catch (Exception ex) {
  418. ex.printStackTrace();
  419. }
  420. try {
  421. new SocksSocket(socks, FtpTprops.FTP_TEST_HOST, 21, InetAddress.getLocalHost(), localPort);
  422. fail("An exception is expected - wrong socks server");
  423. } catch (Exception ex) {
  424. ex.printStackTrace();
  425. }
  426. }
  427. public void testMore_WithLiveSocksServer() throws Exception {
  428. System.out.println("testMore_WithLiveSocksServer");
  429. String socksHost = "eurus.stc.com";
  430. int socksPort = 1080;
  431. // check
  432. try {
  433. new Socket(socksHost, socksPort).close();
  434. } catch (Exception e) {
  435. e.printStackTrace();
  436. System.out.println("testMore_WithLiveSocksServer: no live tests because no Socks is running on eurus.stc.com:1080");
  437. return;
  438. }
  439. System.out.println("testMore_WithLiveSocksServer: doing live tests because Socks is running on eurus.stc.com:1080");
  440. Socks socks = new Socks(socksHost, socksPort, "username", "password");
  441. SocksSocket ss = null;
  442. byte[] bytesIn = new byte[1024];
  443. int len = 0;
  444. // test 1
  445. ss = new SocksSocket(socks, InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21);
  446. assertNotNull(ss);
  447. ss.setSoTimeout(1000);
  448. len = ss.getInputStream().read(bytesIn);
  449. assertTrue(len > 0);
  450. System.out.println("Test 1: data gone through Socks is: " + new String(bytesIn).trim());
  451. ss.close();
  452. // test 2
  453. ss = new SocksSocket(socks, FtpTprops.FTP_TEST_HOST, 21);
  454. assertNotNull(ss);
  455. ss.setSoTimeout(1000);
  456. len = ss.getInputStream().read(bytesIn);
  457. assertTrue(len > 0);
  458. System.out.println("Test 2: data gone through Socks is: " + new String(bytesIn).trim());
  459. ss.close();
  460. // test 3
  461. int localPort = 4570;
  462. try {
  463. // for this constructor with local port, it is possible to fail if the local port is in use
  464. ss = new SocksSocket(socks, InetAddress.getByName(FtpTprops.FTP_TEST_HOST), 21, InetAddress.getLocalHost(), localPort);
  465. } catch (Exception e) {
  466. e.printStackTrace();
  467. System.out.println("Test 3: not performed because local port is in use");
  468. return;
  469. }
  470. assertNotNull(ss);
  471. ss.setSoTimeout(1000);
  472. len = ss.getInputStream().read(bytesIn);
  473. assertTrue(len > 0);
  474. System.out.println("Test 3: data gone through Socks is: " + new String(bytesIn).trim());
  475. ss.close();
  476. // test 4
  477. try {
  478. // for this constructor with local port, it is possible to fail if the local port is in use
  479. ss = new SocksSocket(socks, FtpTprops.FTP_TEST_HOST, 21, InetAddress.getLocalHost(), localPort);
  480. } catch (Exception e) {
  481. e.printStackTrace();
  482. System.out.println("Test 4: not performed because local port is in use");
  483. return;
  484. }
  485. assertNotNull(ss);
  486. ss.setSoTimeout(1000);
  487. len = ss.getInputStream().read(bytesIn);
  488. assertTrue(len > 0);
  489. System.out.println("Test 4: data gone through Socks is: " + new String(bytesIn).trim());
  490. ss.close();
  491. }
  492. public static void main(java.lang.String[] argList) {
  493. junit.textui.TestRunner.run(suite());
  494. }
  495. }