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  
18  package org.apache.commons.net.ftp;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertNotNull;
22  import static org.junit.Assert.assertTrue;
23  
24  import java.io.IOException;
25  import java.net.SocketException;
26  import java.time.Instant;
27  import java.util.Calendar;
28  
29  import org.junit.BeforeClass;
30  import org.junit.Test;
31  import org.junit.runner.RunWith;
32  import org.junit.runners.Parameterized;
33  import org.junit.runners.Parameterized.Parameters;
34  
35  /**
36   * Tests {@link FTPSClient}.
37   * <p>
38   * To get our test cert to work on Java 11, this test must be run with:
39   * </p>
40   *
41   * <pre>
42   * -Djdk.tls.client.protocols="TLSv1.1"
43   * </pre>
44   * <p>
45   * This test does the above programmatically.
46   * </p>
47   */
48  @RunWith(Parameterized.class)
49  public class FTPSClientTest extends AbstractFtpsTest {
50  
51      private static final String USER_PROPS_RES = "org/apache/commons/net/ftpsserver/users.properties";
52  
53      private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks";
54  
55      @BeforeClass
56      public static void setupServer() throws Exception {
57          setupServer(IMPLICIT, USER_PROPS_RES, SERVER_JKS_RES, "target/test-classes/org/apache/commons/net/test-data");
58      }
59  
60      @Parameters(name = "endpointCheckingEnabled={0}")
61      public static Boolean[] testConstructurData() {
62          return new Boolean[] { Boolean.FALSE, Boolean.TRUE };
63      }
64  
65      public FTPSClientTest(final boolean endpointCheckingEnabled) {
66          super(endpointCheckingEnabled, null, null);
67      }
68  
69      @Test(timeout = TEST_TIMEOUT)
70      public void testHasFeature() throws SocketException, IOException {
71          trace(">>testHasFeature");
72          loginClient().disconnect();
73          trace("<<testHasFeature");
74      }
75  
76      private void testListFiles(final String pathname) throws SocketException, IOException {
77          final FTPSClient client = loginClient();
78          try {
79              // do it twice
80              assertNotNull(client.listFiles(pathname));
81              assertNotNull(client.listFiles(pathname));
82          } finally {
83              client.disconnect();
84          }
85      }
86  
87      @Test(timeout = TEST_TIMEOUT)
88      public void testListFilesPathNameEmpty() throws SocketException, IOException {
89          trace(">>testListFilesPathNameEmpty");
90          testListFiles("");
91          trace("<<testListFilesPathNameEmpty");
92      }
93  
94      @Test(timeout = TEST_TIMEOUT)
95      public void testListFilesPathNameJunk() throws SocketException, IOException {
96          trace(">>testListFilesPathNameJunk");
97          testListFiles("   Junk   ");
98          trace("<<testListFilesPathNameJunk");
99      }
100 
101     @Test(timeout = TEST_TIMEOUT)
102     public void testListFilesPathNameNull() throws SocketException, IOException {
103         trace(">>testListFilesPathNameNull");
104         testListFiles(null);
105         trace("<<testListFilesPathNameNull");
106     }
107 
108     @Test(timeout = TEST_TIMEOUT)
109     public void testListFilesPathNameRoot() throws SocketException, IOException {
110         trace(">>testListFilesPathNameRoot");
111         testListFiles("/");
112         trace("<<testListFilesPathNameRoot");
113     }
114 
115     @Test(timeout = TEST_TIMEOUT)
116     public void testMdtmCalendar() throws SocketException, IOException {
117         trace(">>testMdtmCalendar");
118         testMdtmCalendar("/file.txt");
119         trace("<<testMdtmCalendar");
120     }
121 
122     private void testMdtmCalendar(final String pathname) throws SocketException, IOException {
123         final FTPSClient client = loginClient();
124         try {
125             // do it twice
126             final Calendar mdtmCalendar1 = client.mdtmCalendar(pathname);
127             final Calendar mdtmCalendar2 = client.mdtmCalendar(pathname);
128             assertNotNull(mdtmCalendar1);
129             assertNotNull(mdtmCalendar2);
130             assertEquals(mdtmCalendar1, mdtmCalendar2);
131         } finally {
132             client.disconnect();
133         }
134     }
135 
136     @Test(timeout = TEST_TIMEOUT)
137     public void testMdtmFile() throws SocketException, IOException {
138         trace(">>testMdtmFile");
139         testMdtmFile("/file.txt");
140         trace("<<testMdtmFile");
141     }
142 
143     private void testMdtmFile(final String pathname) throws SocketException, IOException {
144         final FTPSClient client = loginClient();
145         try {
146             // do it twice
147             final FTPFile mdtmFile1 = client.mdtmFile(pathname);
148             final FTPFile mdtmFile2 = client.mdtmFile(pathname);
149             assertNotNull(mdtmFile1);
150             assertNotNull(mdtmFile2);
151             assertEquals(mdtmFile1.toString(), mdtmFile2.toString());
152         } finally {
153             client.disconnect();
154         }
155     }
156 
157     @Test(timeout = TEST_TIMEOUT)
158     public void testMdtmInstant() throws SocketException, IOException {
159         trace(">>testMdtmInstant");
160         testMdtmInstant("/file.txt");
161         trace("<<testMdtmInstant");
162     }
163 
164     private void testMdtmInstant(final String pathname) throws SocketException, IOException {
165         final FTPSClient client = loginClient();
166         try {
167             // do it twice
168             final Instant mdtmInstant1 = client.mdtmInstant(pathname);
169             final Instant mdtmInstant2 = client.mdtmInstant(pathname);
170             assertNotNull(mdtmInstant1);
171             assertNotNull(mdtmInstant2);
172             assertEquals(mdtmInstant1, mdtmInstant2);
173         } finally {
174             client.disconnect();
175         }
176     }
177 
178     @Test(timeout = TEST_TIMEOUT)
179     public void testOpenClose() throws SocketException, IOException {
180         trace(">>testOpenClose");
181         final FTPSClient ftpsClient = loginClient();
182         try {
183             assertTrue(ftpsClient.hasFeature("MODE"));
184             assertTrue(ftpsClient.hasFeature(FTPCmd.MODE));
185         } finally {
186             ftpsClient.disconnect();
187         }
188         trace("<<testOpenClose");
189     }
190 
191     @Test(timeout = TEST_TIMEOUT)
192     public void testRetrieveFilePathNameRoot() throws SocketException, IOException {
193         trace(">>testRetrieveFilePathNameRoot");
194         retrieveFile("/file.txt");
195         trace("<<testRetrieveFilePathNameRoot");
196     }
197 
198 }