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
37
38 public class SocketClientTest {
39 private static final String PROXY_HOST = "127.0.0.1";
40 private static final int PROXY_PORT = 1080;
41
42 private static final String LOCALHOST_ADDRESS = "127.0.0.1";
43 private static final String UNRESOLVED_HOST = "unresolved";
44 private static final int REMOTE_PORT = 21;
45
46 @Test
47 public void testConnectResolved() {
48 final SocketClient socketClient = new FTPClient();
49
50 assertThrows(IOException.class, () -> socketClient.connect(LOCALHOST_ADDRESS, REMOTE_PORT));
51 final InetSocketAddress remoteAddress = socketClient.getRemoteInetSocketAddress();
52 assertFalse(remoteAddress.isUnresolved());
53 assertEquals(LOCALHOST_ADDRESS, remoteAddress.getHostString());
54 assertEquals(REMOTE_PORT, remoteAddress.getPort());
55 }
56
57 @Test
58 public void testConnectUnresolved() {
59 final SocketClient socketClient = new FTPClient();
60
61 assertThrows(UnknownHostException.class, () -> socketClient.connect(UNRESOLVED_HOST, REMOTE_PORT, null, -1));
62 final InetSocketAddress remoteAddress = socketClient.getRemoteInetSocketAddress();
63 assertTrue(remoteAddress.isUnresolved());
64 assertEquals(UNRESOLVED_HOST, remoteAddress.getHostString());
65 assertEquals(REMOTE_PORT, remoteAddress.getPort());
66 }
67
68
69
70
71 @Test
72 public void testProxySettings() {
73 final SocketClient socketClient = new FTPClient();
74 assertNull(socketClient.getProxy());
75 final Proxy proxy = new Proxy(Proxy.Type.SOCKS, new InetSocketAddress(PROXY_HOST, PROXY_PORT));
76 socketClient.setProxy(proxy);
77 assertEquals(proxy, socketClient.getProxy());
78 assertFalse(socketClient.isConnected());
79 }
80 }