1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.net;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.IOException;
26 import java.net.InetSocketAddress;
27 import java.net.Proxy;
28 import java.net.UnknownHostException;
29
30 import org.apache.commons.net.ftp.FTPClient;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 public class SocketClientTest {
37 private static final String PROXY_HOST = "127.0.0.1";
38 private static final int PROXY_PORT = 1080;
39
40 private static final String LOCALHOST_ADDRESS = "127.0.0.1";
41 private static final String UNRESOLVED_HOST = "unresolved";
42 private static final int REMOTE_PORT = 21;
43
44 @Test
45 public void testConnectResolved() {
46 final SocketClient socketClient = new FTPClient();
47
48 assertThrows(IOException.class, () -> socketClient.connect(LOCALHOST_ADDRESS, REMOTE_PORT));
49 final InetSocketAddress remoteAddress = socketClient.getRemoteInetSocketAddress();
50 assertFalse(remoteAddress.isUnresolved());
51 assertEquals(LOCALHOST_ADDRESS, remoteAddress.getHostString());
52 assertEquals(REMOTE_PORT, remoteAddress.getPort());
53 }
54
55 @Test
56 public void testConnectUnresolved() {
57 final SocketClient socketClient = new FTPClient();
58
59 assertThrows(UnknownHostException.class, () -> socketClient.connect(UNRESOLVED_HOST, REMOTE_PORT, null, -1));
60 final InetSocketAddress remoteAddress = socketClient.getRemoteInetSocketAddress();
61 assertTrue(remoteAddress.isUnresolved());
62 assertEquals(UNRESOLVED_HOST, remoteAddress.getHostString());
63 assertEquals(REMOTE_PORT, remoteAddress.getPort());
64 }
65
66
67
68
69 @Test
70 public void testProxySettings() {
71 final SocketClient socketClient = new FTPClient();
72 assertNull(socketClient.getProxy());
73 final Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
74 socketClient.setProxy(proxy);
75 assertEquals(proxy, socketClient.getProxy());
76 assertFalse(socketClient.isConnected());
77 }
78 }