Path: blob/master/test/jdk/sun/net/ftp/B6427768.java
41152 views
/*1* Copyright (c) 2006, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 642776826* @summary FtpURLConnection doesn't close FTP connection when login fails27* @modules java.base/sun.net.ftp28* @library ../www/ftptest/29* @build FtpServer FtpCommandHandler FtpAuthHandler FtpFileSystemHandler30* @run main/othervm/timeout=500 B642776831*/3233import java.net.*;34import java.io.*;3536public class B6427768 {37// Need to test when login fails, so AuthHandler should always return38// false39static class MyAuthHandler implements FtpAuthHandler {40public int authType() {41return 2;42}4344public boolean authenticate(String user, String password) {45return false;46}4748public boolean authenticate(String user, String password, String account) {49return false;50}51}5253static class MyFileSystemHandler implements FtpFileSystemHandler {54private String currentDir = "/";5556public MyFileSystemHandler(String path) {57currentDir = path;58}5960public boolean cd(String path) {61currentDir = path;62return true;63}6465public boolean cdUp() {66return true;67}6869public String pwd() {70return currentDir;71}7273public InputStream getFile(String name) {74return null;75}7677public long getFileSize(String name) {78return -1;79}8081public boolean fileExists(String name) {82return false;83}8485public InputStream listCurrentDir() {86return null;87}8889public OutputStream putFile(String name) {90return null;91}9293public boolean removeFile(String name) {94return false;95}9697public boolean mkdir(String name) {98return false;99}100101public boolean rename(String from, String to) {102return false;103}104}105106public static void main(String[] args) throws IOException {107InetAddress loopback = InetAddress.getLoopbackAddress();108FtpServer server = new FtpServer(loopback, 0);109int port = server.getLocalPort();110server.setFileSystemHandler(new MyFileSystemHandler("/"));111server.setAuthHandler(new MyAuthHandler());112server.start();113URL url = new URL("ftp://user:passwd@" + server.getAuthority() + "/foo.txt");114URLConnection con = url.openConnection(Proxy.NO_PROXY);115// triggers the connection116try {117con.getInputStream();118} catch(sun.net.ftp.FtpLoginException e) {119// Give some time to the client thread to properly terminate.120try {121Thread.sleep(2000);122} catch (InterruptedException ie) {123// shouldn't happen124}125if (server.activeClientsCount() > 0) {126// If there are still active clients attached to the FTP127// server, it means we didn't quit properly128server.killClients();129throw new RuntimeException("URLConnection didn't close the ftp connection on failure to login");130}131} finally {132server.terminate();133}134}135}136137138