1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.net.ftp;
19
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.assertTrue;
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.net.SocketException;
26 import java.net.URL;
27 import java.time.Duration;
28
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.io.output.NullOutputStream;
31 import org.apache.commons.net.PrintCommandListener;
32 import org.apache.ftpserver.FtpServer;
33 import org.apache.ftpserver.FtpServerFactory;
34 import org.apache.ftpserver.ftplet.FtpException;
35 import org.apache.ftpserver.ftplet.UserManager;
36 import org.apache.ftpserver.listener.ListenerFactory;
37 import org.apache.ftpserver.ssl.SslConfiguration;
38 import org.apache.ftpserver.ssl.SslConfigurationFactory;
39 import org.apache.ftpserver.usermanager.Md5PasswordEncryptor;
40 import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
41 import org.apache.ftpserver.usermanager.impl.BaseUser;
42 import org.junit.Assert;
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57 public abstract class AbstractFtpsTest {
58
59 private static int SocketPort;
60 private static FtpServer EmbeddedFtpServer;
61 protected static final boolean IMPLICIT = false;
62 protected static final long TEST_TIMEOUT = 10000;
63 private static final boolean TRACE_CALLS = Boolean.parseBoolean(System.getenv("TRACE_CALLS"));
64 private static final boolean ADD_LISTENER = Boolean.parseBoolean(System.getenv("ADD_LISTENER"));
65 private static final long startTime = System.nanoTime();
66
67
68
69
70
71
72 protected static String getTestHomeDirectory(final String defaultHome) {
73 return System.getProperty("test.basedir", defaultHome);
74 }
75
76
77
78
79
80
81
82
83
84
85 protected static synchronized void setupServer(final boolean implicit, final String userPropertiesResource, final String serverJksResourceResource,
86 final String defaultHome) throws FtpException {
87 if (EmbeddedFtpServer != null) {
88 return;
89 }
90
91 SocketPort = 0;
92 final FtpServerFactory serverFactory = new FtpServerFactory();
93 final PropertiesUserManagerFactory propertiesUserManagerFactory = new PropertiesUserManagerFactory();
94
95 propertiesUserManagerFactory.setPasswordEncryptor(new Md5PasswordEncryptor());
96 final URL userPropsResource = ClassLoader.getSystemClassLoader().getResource(userPropertiesResource);
97 Assert.assertNotNull(userPropertiesResource, userPropsResource);
98 propertiesUserManagerFactory.setUrl(userPropsResource);
99 final UserManager userManager = propertiesUserManagerFactory.createUserManager();
100 final BaseUser user = (BaseUser) userManager.getUserByName("test");
101
102
103 user.setHomeDirectory(getTestHomeDirectory(defaultHome));
104 serverFactory.setUserManager(userManager);
105 final ListenerFactory factory = new ListenerFactory();
106 factory.setPort(SocketPort);
107
108
109 final URL serverJksResource = ClassLoader.getSystemClassLoader().getResource(serverJksResourceResource);
110 Assert.assertNotNull(serverJksResourceResource, serverJksResource);
111
112 final SslConfigurationFactory sllConfigFactory = new SslConfigurationFactory();
113 final File keyStoreFile = FileUtils.toFile(serverJksResource);
114 Assert.assertTrue(keyStoreFile.toString(), keyStoreFile.exists());
115 sllConfigFactory.setKeystoreFile(keyStoreFile);
116 sllConfigFactory.setKeystorePassword("password");
117
118
119 final SslConfiguration sslConfiguration = sllConfigFactory.createSslConfiguration();
120 final NoProtocolSslConfigurationProxy noProtocolSslConfigurationProxy = new NoProtocolSslConfigurationProxy(sslConfiguration);
121 factory.setSslConfiguration(noProtocolSslConfigurationProxy);
122 factory.setImplicitSsl(implicit);
123
124
125 serverFactory.addListener("default", factory.createListener());
126
127
128 EmbeddedFtpServer = serverFactory.createServer();
129 EmbeddedFtpServer.start();
130 SocketPort = ((org.apache.ftpserver.impl.DefaultFtpServer) EmbeddedFtpServer).getListener("default").getPort();
131
132 trace("Server started");
133 }
134
135 protected static void trace(final String msg) {
136 if (TRACE_CALLS) {
137 System.err.println(msg + " " + (System.nanoTime() - startTime));
138 }
139 }
140
141 private final boolean endpointCheckingEnabled;
142
143 public AbstractFtpsTest(final boolean endpointCheckingEnabled, final String userPropertiesResource, final String serverJksResource) {
144 this.endpointCheckingEnabled = endpointCheckingEnabled;
145 }
146
147 protected void assertClientCode(final FTPSClient client) {
148 final int replyCode = client.getReplyCode();
149 assertTrue(FTPReply.isPositiveCompletion(replyCode));
150 }
151
152 protected FTPSClient loginClient() throws SocketException, IOException {
153 trace(">>loginClient");
154 final FTPSClient client = new FTPSClient(IMPLICIT);
155 if (ADD_LISTENER) {
156 client.addProtocolCommandListener(new PrintCommandListener(System.err));
157 }
158
159 client.setControlKeepAliveReplyTimeout(null);
160 assertEquals(0, client.getControlKeepAliveReplyTimeoutDuration().getSeconds());
161 client.setControlKeepAliveReplyTimeout(Duration.ofSeconds(60));
162 assertEquals(60, client.getControlKeepAliveReplyTimeoutDuration().getSeconds());
163
164 client.setControlKeepAliveTimeout(null);
165 assertEquals(0, client.getControlKeepAliveTimeoutDuration().getSeconds());
166 client.setControlKeepAliveTimeout(Duration.ofSeconds(61));
167 assertEquals(61, client.getControlKeepAliveTimeoutDuration().getSeconds());
168
169 client.setDataTimeout(null);
170 assertEquals(0, client.getDataTimeout().getSeconds());
171 client.setDataTimeout(Duration.ofSeconds(62));
172 assertEquals(62, client.getDataTimeout().getSeconds());
173
174 client.setEndpointCheckingEnabled(endpointCheckingEnabled);
175 client.connect("localhost", SocketPort);
176
177 assertClientCode(client);
178 assertEquals(SocketPort, client.getRemotePort());
179
180 try {
181
182
183 Thread.sleep(200);
184 } catch (final InterruptedException ignore) {
185
186 }
187 assertTrue(client.login("test", "test"));
188 assertClientCode(client);
189
190 client.setFileType(FTP.BINARY_FILE_TYPE);
191 assertClientCode(client);
192
193 client.execPBSZ(0);
194 assertClientCode(client);
195
196 client.execPROT("P");
197 assertClientCode(client);
198 trace("<<loginClient");
199 return client;
200 }
201
202 protected void retrieveFile(final String pathname) throws SocketException, IOException {
203 final FTPSClient client = loginClient();
204 try {
205
206
207 assertTrue(pathname, client.retrieveFile(pathname, NullOutputStream.INSTANCE));
208 assertTrue(pathname, client.retrieveFile(pathname, NullOutputStream.INSTANCE));
209 } finally {
210 client.disconnect();
211 }
212 }
213 }