Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java
41149 views
1
/*
2
* Copyright (c) 2016, 2019, 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 7167293
27
* @summary FtpURLConnection doesn't close FTP connection when FileNotFoundException is thrown
28
* @library ../www/ftptest/
29
* @build FtpServer FtpCommandHandler FtpAuthHandler FtpFileSystemHandler
30
* @run main/othervm FtpURLConnectionLeak
31
*/
32
import java.io.FileNotFoundException;
33
import java.io.InputStream;
34
import java.io.OutputStream;
35
import java.net.InetAddress;
36
import java.net.URL;
37
import java.net.Proxy;
38
39
public class FtpURLConnectionLeak {
40
41
public static void main(String[] args) throws Exception {
42
InetAddress loopback = InetAddress.getLoopbackAddress();
43
FtpServer server = new FtpServer(loopback, 0);
44
server.setFileSystemHandler(new CustomFileSystemHandler("/"));
45
server.setAuthHandler(new MyAuthHandler());
46
String authority = server.getAuthority();
47
server.start();
48
URL url = new URL("ftp://" + authority + "/filedoesNotExist.txt");
49
try (server) {
50
for (int i = 0; i < 3; i++) {
51
try {
52
InputStream stream = url.openConnection(Proxy.NO_PROXY).getInputStream();
53
} catch (FileNotFoundException expected) {
54
// should always reach this point since the path does not exist
55
System.out.println("caught expected " + expected);
56
int times = 1;
57
do {
58
// give some time to close the connection...
59
System.out.println("sleeping... " + times);
60
Thread.sleep(times * 1000);
61
} while (server.activeClientsCount() > 0 && times++ < 5);
62
63
if (server.activeClientsCount() > 0) {
64
server.killClients();
65
throw new RuntimeException("URLConnection didn't close the" +
66
" FTP connection on FileNotFoundException");
67
}
68
}
69
}
70
}
71
}
72
73
static class CustomFileSystemHandler implements FtpFileSystemHandler {
74
75
private String currentDir;
76
77
public CustomFileSystemHandler(String path) {
78
currentDir = path;
79
}
80
81
@Override
82
public boolean cd(String path) {
83
currentDir = path;
84
return true;
85
}
86
87
@Override
88
public boolean cdUp() {
89
throw new UnsupportedOperationException("Not supported yet.");
90
}
91
92
@Override
93
public String pwd() {
94
throw new UnsupportedOperationException("Not supported yet.");
95
}
96
97
@Override
98
public boolean fileExists(String name) {
99
throw new UnsupportedOperationException("Not supported yet.");
100
}
101
102
@Override
103
public InputStream getFile(String name) {
104
return null; //return null so that server will return 550 File not found.
105
}
106
107
@Override
108
public long getFileSize(String name) {
109
throw new UnsupportedOperationException("Not supported yet.");
110
}
111
112
@Override
113
public InputStream listCurrentDir() {
114
return null;
115
}
116
117
@Override
118
public OutputStream putFile(String name) {
119
throw new UnsupportedOperationException("Not supported yet.");
120
}
121
122
@Override
123
public boolean removeFile(String name) {
124
throw new UnsupportedOperationException("Not supported yet.");
125
}
126
127
@Override
128
public boolean mkdir(String name) {
129
throw new UnsupportedOperationException("Not supported yet.");
130
}
131
132
@Override
133
public boolean rename(String from, String to) {
134
throw new UnsupportedOperationException("Not supported yet.");
135
}
136
137
}
138
139
static class MyAuthHandler implements FtpAuthHandler {
140
141
@Override
142
public int authType() {
143
return 0;
144
}
145
146
@Override
147
public boolean authenticate(String user, String password) {
148
return true;
149
}
150
151
@Override
152
public boolean authenticate(String user, String password, String account) {
153
return true;
154
}
155
}
156
}
157
158