Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/ftp/TestFtpTimeValue.java
41149 views
1
/*
2
* Copyright (c) 2020, 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 8255078
27
* @summary verify that datetime in MDTM and MLSD responses are properly parsed
28
* @library /test/lib
29
* @modules java.base/sun.net.ftp
30
* @build jdk.test.lib.Asserts
31
* @run main/othervm -Duser.timezone=UTC TestFtpTimeValue
32
* @run main/othervm -Duser.timezone=America/Los_Angeles TestFtpTimeValue
33
*/
34
35
import jdk.test.lib.Asserts;
36
import sun.net.ftp.FtpClient;
37
38
import java.io.*;
39
import java.net.*;
40
import java.util.Calendar;
41
import java.util.Date;
42
import java.util.GregorianCalendar;
43
import java.util.TimeZone;
44
45
46
public class TestFtpTimeValue {
47
private enum TestCase {
48
AmTimeNoMilli(2019, 4, 20, 10, 57, 13, 0),
49
AmTimeWithMilli(2019, 4, 20, 10, 57, 13, 50),
50
PmTimeNoMilli(2019, 4, 20, 22, 57, 13, 0),
51
PmTimeWithMilli(2019, 4, 20, 22, 57, 13, 50),
52
;
53
54
public final Date expectedCreated;
55
public final Date expectedModified;
56
public final String create;
57
public final String modify;
58
59
TestCase(int year, int month, int day, int hrs, int min, int sec, int milliseconds) {
60
var calendar = GregorianCalendar.getInstance(TimeZone.getTimeZone("GMT"));
61
// month is 0-based in Calendar
62
calendar.set(year, month - 1, day, hrs, min, sec);
63
calendar.set(Calendar.MILLISECOND, milliseconds);
64
expectedCreated = calendar.getTime();
65
var s = String.format("%4d%02d%02d%02d%02d%02d", year, month, day, hrs, min, sec);
66
if (milliseconds != 0) {
67
s += "." + String.format("%03d", milliseconds);
68
}
69
create = s;
70
71
calendar.add(GregorianCalendar.SECOND, 1);
72
expectedModified = calendar.getTime();
73
s = String.format("%4d%02d%02d%02d%02d%02d", year, month, day, hrs, min, sec + 1);
74
if (milliseconds != 0) {
75
s += "." + String.format("%03d", milliseconds);
76
}
77
modify = s;
78
}
79
}
80
81
public static void main(String[] args) throws Exception {
82
System.out.println("user.timezone: " + System.getProperty("user.timezone"));
83
try (FtpServer server = new FtpServer();
84
FtpClient client = FtpClient.create()) {
85
(new Thread(server)).start();
86
int port = server.getPort();
87
var loopback = InetAddress.getLoopbackAddress();
88
client.connect(new InetSocketAddress(loopback, port));
89
client.enablePassiveMode(true);
90
for (var testCase : TestCase.values()) {
91
Asserts.assertEQ(testCase.expectedModified, client.getLastModified(testCase.name()),
92
"wrong modified date from MDTM for " + testCase);
93
}
94
for (var it = client.listFiles(null); it.hasNext(); ) {
95
var e = it.next();
96
Asserts.assertEQ(TestCase.valueOf(e.getName()).expectedCreated, e.getCreated(),
97
"wrong created date from MLSD for " + e.getName());
98
Asserts.assertEQ(TestCase.valueOf(e.getName()).expectedModified, e.getLastModified(),
99
"wrong modified date from MLSD for " + e.getName());
100
}
101
}
102
}
103
104
private static class FtpServer implements AutoCloseable, Runnable {
105
private final ServerSocket serverSocket;
106
private final ServerSocket pasv;
107
108
FtpServer() throws IOException {
109
var loopback = InetAddress.getLoopbackAddress();
110
serverSocket = new ServerSocket();
111
serverSocket.bind(new InetSocketAddress(loopback, 0));
112
pasv = new ServerSocket();
113
pasv.bind(new InetSocketAddress(loopback, 0));
114
}
115
116
public void handleClient(Socket client) throws IOException {
117
String str;
118
119
client.setSoTimeout(2000);
120
BufferedReader in = new BufferedReader(new InputStreamReader(client.getInputStream()));
121
PrintWriter out = new PrintWriter(client.getOutputStream(), true);
122
out.println("220 FTP serverSocket is ready.");
123
boolean done = false;
124
while (!done) {
125
try {
126
str = in.readLine();
127
} catch (SocketException e) {
128
done = true;
129
continue;
130
}
131
String cmd = str.substring(0, str.indexOf(" ") > 0 ? str.indexOf(" ") : str.length());
132
String args = (cmd.equals(str)) ? "" : str.substring(str.indexOf(" ") + 1);
133
System.err.println("C> " + str);
134
switch (cmd) {
135
case "QUIT":
136
out.println("221 Goodbye.");
137
System.err.println("S> 221");
138
done = true;
139
break;
140
case "MDTM": {
141
var testCase = TestCase.valueOf(args);
142
out.println("213 " + testCase.modify);
143
System.err.println("S> 213");
144
break;
145
}
146
case "MLSD":
147
try (var socket = pasv.accept();
148
var dout = new PrintWriter(socket.getOutputStream(), true)) {
149
out.println("150 MLSD start");
150
System.err.println("S> 150");
151
for (var testCase : TestCase.values()) {
152
dout.printf("modify=%s;create=%s; %s%n",
153
testCase.modify, testCase.create, testCase.name());
154
}
155
}
156
out.println("226 MLSD done.");
157
System.err.println("S> 226");
158
break;
159
case "EPSV":
160
if ("all".equalsIgnoreCase(args)) {
161
out.println("200 EPSV ALL command successful.");
162
System.err.println("S> 200");
163
continue;
164
}
165
out.println("229 Entering Extended Passive Mode (|||" + pasv.getLocalPort() + "|)");
166
System.err.println("S> 229");
167
break;
168
default:
169
System.err.println("S> 500");
170
out.println("500 unsupported command: " + str);
171
}
172
}
173
}
174
175
public int getPort() {
176
return serverSocket.getLocalPort();
177
}
178
179
public void close() throws IOException {
180
serverSocket.close();
181
pasv.close();
182
}
183
184
@Override
185
public void run() {
186
try (Socket client = serverSocket.accept()) {
187
handleClient(client);
188
} catch (Throwable t) {
189
t.printStackTrace();
190
throw new RuntimeException("Problem in test execution", t);
191
}
192
}
193
}
194
}
195
196