View Javadoc
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    *      https://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 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   * A simple test class for SocketClient settings.
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       * A simple test to verify that the Proxy is being set.
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  }