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    *      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 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   * @since 3.2
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       * A simple test to verify that the Proxy is being set.
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  }