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  
18  package org.apache.commons.net.ftp;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertNotNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  import java.net.SocketException;
26  import java.time.Instant;
27  import java.util.Calendar;
28  import java.util.stream.Stream;
29  
30  import org.junit.jupiter.api.BeforeAll;
31  import org.junit.jupiter.api.Timeout;
32  import org.junit.jupiter.params.ParameterizedTest;
33  import org.junit.jupiter.params.provider.MethodSource;
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  class FTPSClientTest extends AbstractFtpsTest {
49  
50      private static final String USER_PROPS_RES = "org/apache/commons/net/ftpsserver/users.properties";
51  
52      private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks";
53  
54      private static Stream<Boolean> endpointCheckingEnabledSource() {
55          return Stream.of(Boolean.FALSE, Boolean.TRUE);
56      }
57  
58      @BeforeAll
59      public static void setupServer() throws Exception {
60          setupServer(IMPLICIT, USER_PROPS_RES, SERVER_JKS_RES, "target/test-classes/org/apache/commons/net/test-data");
61      }
62  
63      @ParameterizedTest(name = "endpointCheckingEnabled={0}")
64      @MethodSource("endpointCheckingEnabledSource")
65      @Timeout(TEST_TIMEOUT)
66      void testHasFeature(final boolean endpointCheckingEnabled) throws SocketException, IOException {
67          setEndpointCheckingEnabled(endpointCheckingEnabled);
68          trace(">>testHasFeature");
69          loginClient().disconnect();
70          trace("<<testHasFeature");
71      }
72  
73      private void testListFiles(final String pathname) throws SocketException, IOException {
74          final FTPSClient client = loginClient();
75          try {
76              // do it twice
77              assertNotNull(client.listFiles(pathname));
78              assertNotNull(client.listFiles(pathname));
79          } finally {
80              client.disconnect();
81          }
82      }
83  
84      @ParameterizedTest(name = "endpointCheckingEnabled={0}")
85      @MethodSource("endpointCheckingEnabledSource")
86      @Timeout(TEST_TIMEOUT)
87      void testListFilesPathNameEmpty(final boolean endpointCheckingEnabled) throws SocketException, IOException {
88          setEndpointCheckingEnabled(endpointCheckingEnabled);
89          trace(">>testListFilesPathNameEmpty");
90          testListFiles("");
91          trace("<<testListFilesPathNameEmpty");
92      }
93  
94      @ParameterizedTest(name = "endpointCheckingEnabled={0}")
95      @MethodSource("endpointCheckingEnabledSource")
96      @Timeout(TEST_TIMEOUT)
97      void testListFilesPathNameJunk(final boolean endpointCheckingEnabled) throws SocketException, IOException {
98          setEndpointCheckingEnabled(endpointCheckingEnabled);
99          trace(">>testListFilesPathNameJunk");
100         testListFiles("   Junk   ");
101         trace("<<testListFilesPathNameJunk");
102     }
103 
104     @ParameterizedTest(name = "endpointCheckingEnabled={0}")
105     @MethodSource("endpointCheckingEnabledSource")
106     @Timeout(TEST_TIMEOUT)
107     void testListFilesPathNameNull(final boolean endpointCheckingEnabled) throws SocketException, IOException {
108         setEndpointCheckingEnabled(endpointCheckingEnabled);
109         trace(">>testListFilesPathNameNull");
110         testListFiles(null);
111         trace("<<testListFilesPathNameNull");
112     }
113 
114     @ParameterizedTest(name = "endpointCheckingEnabled={0}")
115     @MethodSource("endpointCheckingEnabledSource")
116     @Timeout(TEST_TIMEOUT)
117     void testListFilesPathNameRoot(final boolean endpointCheckingEnabled) throws SocketException, IOException {
118         setEndpointCheckingEnabled(endpointCheckingEnabled);
119         trace(">>testListFilesPathNameRoot");
120         testListFiles("/");
121         trace("<<testListFilesPathNameRoot");
122     }
123 
124     @ParameterizedTest(name = "endpointCheckingEnabled={0}")
125     @MethodSource("endpointCheckingEnabledSource")
126     @Timeout(TEST_TIMEOUT)
127     void testMdtmCalendar(final boolean endpointCheckingEnabled) throws SocketException, IOException {
128         setEndpointCheckingEnabled(endpointCheckingEnabled);
129         trace(">>testMdtmCalendar");
130         testMdtmCalendar("/file.txt");
131         trace("<<testMdtmCalendar");
132     }
133 
134     private void testMdtmCalendar(final String pathname) throws SocketException, IOException {
135         final FTPSClient client = loginClient();
136         try {
137             // do it twice
138             final Calendar mdtmCalendar1 = client.mdtmCalendar(pathname);
139             final Calendar mdtmCalendar2 = client.mdtmCalendar(pathname);
140             assertNotNull(mdtmCalendar1);
141             assertNotNull(mdtmCalendar2);
142             assertEquals(mdtmCalendar1, mdtmCalendar2);
143         } finally {
144             client.disconnect();
145         }
146     }
147 
148     @ParameterizedTest(name = "endpointCheckingEnabled={0}")
149     @MethodSource("endpointCheckingEnabledSource")
150     @Timeout(TEST_TIMEOUT)
151     void testMdtmFile(final boolean endpointCheckingEnabled) throws SocketException, IOException {
152         setEndpointCheckingEnabled(endpointCheckingEnabled);
153         trace(">>testMdtmFile");
154         testMdtmFile("/file.txt");
155         trace("<<testMdtmFile");
156     }
157 
158     private void testMdtmFile(final String pathname) throws SocketException, IOException {
159         final FTPSClient client = loginClient();
160         try {
161             // do it twice
162             final FTPFile mdtmFile1 = client.mdtmFile(pathname);
163             final FTPFile mdtmFile2 = client.mdtmFile(pathname);
164             assertNotNull(mdtmFile1);
165             assertNotNull(mdtmFile2);
166             assertEquals(mdtmFile1.toString(), mdtmFile2.toString());
167         } finally {
168             client.disconnect();
169         }
170     }
171 
172     @ParameterizedTest(name = "endpointCheckingEnabled={0}")
173     @MethodSource("endpointCheckingEnabledSource")
174     @Timeout(TEST_TIMEOUT)
175     void testMdtmInstant(final boolean endpointCheckingEnabled) throws SocketException, IOException {
176         setEndpointCheckingEnabled(endpointCheckingEnabled);
177         trace(">>testMdtmInstant");
178         testMdtmInstant("/file.txt");
179         trace("<<testMdtmInstant");
180     }
181 
182     private void testMdtmInstant(final String pathname) throws SocketException, IOException {
183         final FTPSClient client = loginClient();
184         try {
185             // do it twice
186             final Instant mdtmInstant1 = client.mdtmInstant(pathname);
187             final Instant mdtmInstant2 = client.mdtmInstant(pathname);
188             assertNotNull(mdtmInstant1);
189             assertNotNull(mdtmInstant2);
190             assertEquals(mdtmInstant1, mdtmInstant2);
191         } finally {
192             client.disconnect();
193         }
194     }
195 
196     @ParameterizedTest(name = "endpointCheckingEnabled={0}")
197     @MethodSource("endpointCheckingEnabledSource")
198     @Timeout(TEST_TIMEOUT)
199     void testOpenClose(final boolean endpointCheckingEnabled) throws SocketException, IOException {
200         setEndpointCheckingEnabled(endpointCheckingEnabled);
201         trace(">>testOpenClose");
202         final FTPSClient ftpsClient = loginClient();
203         try {
204             assertTrue(ftpsClient.hasFeature("MODE"));
205             assertTrue(ftpsClient.hasFeature(FTPCmd.MODE));
206         } finally {
207             ftpsClient.disconnect();
208         }
209         trace("<<testOpenClose");
210     }
211 
212     @ParameterizedTest(name = "endpointCheckingEnabled={0}")
213     @MethodSource("endpointCheckingEnabledSource")
214     @Timeout(TEST_TIMEOUT)
215     void testRetrieveFilePathNameRoot(final boolean endpointCheckingEnabled) throws SocketException, IOException {
216         setEndpointCheckingEnabled(endpointCheckingEnabled);
217         trace(">>testRetrieveFilePathNameRoot");
218         retrieveFile("/file.txt");
219         trace("<<testRetrieveFilePathNameRoot");
220     }
221 
222 }