Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/ftp/TestFtpClientNameListWithNull.java
41152 views
1
/*
2
* Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/*
25
* @test
26
* @bug 8022580
27
* @summary "null" should be treated as "current directory" in nameList()
28
* method of FtpClient
29
* @modules java.base/sun.net.ftp
30
* @run main TestFtpClientNameListWithNull
31
*/
32
33
34
import sun.net.ftp.FtpClient;
35
36
import java.io.BufferedReader;
37
import java.io.IOException;
38
import java.io.InputStreamReader;
39
import java.io.PrintWriter;
40
import java.net.InetAddress;
41
import java.net.InetSocketAddress;
42
import java.net.ServerSocket;
43
import java.net.Socket;
44
import java.net.SocketException;
45
46
47
public class TestFtpClientNameListWithNull {
48
49
private static volatile boolean commandHasArgs;
50
51
public static void main(String[] args) throws Exception {
52
try (FtpServer server = new FtpServer();
53
FtpClient client = FtpClient.create()) {
54
(new Thread(server)).start();
55
int port = server.getPort();
56
InetAddress loopback = InetAddress.getLoopbackAddress();
57
client.connect(new InetSocketAddress(loopback, port));
58
client.nameList(null);
59
} finally {
60
if (commandHasArgs) {
61
throw new RuntimeException("Test failed. NLST shouldn't have " +
62
"args if nameList parameter is null");
63
}
64
}
65
}
66
67
private static class FtpServer implements AutoCloseable, Runnable {
68
private final ServerSocket serverSocket;
69
70
FtpServer() throws IOException {
71
InetAddress loopback = InetAddress.getLoopbackAddress();
72
serverSocket = new ServerSocket();
73
serverSocket.bind(new InetSocketAddress(loopback, 0));
74
}
75
76
public void handleClient(Socket client) throws IOException {
77
boolean done = false;
78
String str;
79
80
client.setSoTimeout(2000);
81
BufferedReader in = new BufferedReader(new InputStreamReader(client.
82
getInputStream()));
83
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
84
out.println("220 FTP serverSocket is ready.");
85
while (!done) {
86
try {
87
str = in.readLine();
88
} catch (SocketException e) {
89
done = true;
90
continue;
91
}
92
String cmd = str.substring(0, str.indexOf(" ") > 0 ?
93
str.indexOf(" ") : str.length());
94
String args = (cmd.equals(str)) ?
95
"" : str.substring(str.indexOf(" "));
96
switch (cmd) {
97
case "QUIT":
98
out.println("221 Goodbye.");
99
out.flush();
100
done = true;
101
break;
102
case "EPSV":
103
if ("all".equalsIgnoreCase(args)) {
104
out.println("200 EPSV ALL command successful.");
105
continue;
106
}
107
out.println("229 Entering Extended Passive Mode " +
108
"(|||" + getPort() + "|)");
109
break;
110
case "NLST":
111
if (args.trim().length() != 0) {
112
commandHasArgs = true;
113
}
114
out.println("200 Command okay.");
115
break;
116
default:
117
out.println("500 unsupported command: " + str);
118
}
119
}
120
}
121
122
public int getPort() {
123
if (serverSocket != null) {
124
return serverSocket.getLocalPort();
125
}
126
return 0;
127
}
128
129
public void close() throws IOException {
130
if (serverSocket != null && !serverSocket.isClosed()) {
131
serverSocket.close();
132
}
133
}
134
135
@Override
136
public void run() {
137
try {
138
try (Socket client = serverSocket.accept()) {
139
handleClient(client);
140
}
141
} catch (IOException e) {
142
throw new RuntimeException("Problem in test execution", e);
143
}
144
}
145
}
146
}
147
148