Path: blob/master/test/jdk/sun/net/ftp/FtpURLConnectionLeak.java
41149 views
/*1* Copyright (c) 2016, 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 716729326* @summary FtpURLConnection doesn't close FTP connection when FileNotFoundException is thrown27* @library ../www/ftptest/28* @build FtpServer FtpCommandHandler FtpAuthHandler FtpFileSystemHandler29* @run main/othervm FtpURLConnectionLeak30*/31import java.io.FileNotFoundException;32import java.io.InputStream;33import java.io.OutputStream;34import java.net.InetAddress;35import java.net.URL;36import java.net.Proxy;3738public class FtpURLConnectionLeak {3940public static void main(String[] args) throws Exception {41InetAddress loopback = InetAddress.getLoopbackAddress();42FtpServer server = new FtpServer(loopback, 0);43server.setFileSystemHandler(new CustomFileSystemHandler("/"));44server.setAuthHandler(new MyAuthHandler());45String authority = server.getAuthority();46server.start();47URL url = new URL("ftp://" + authority + "/filedoesNotExist.txt");48try (server) {49for (int i = 0; i < 3; i++) {50try {51InputStream stream = url.openConnection(Proxy.NO_PROXY).getInputStream();52} catch (FileNotFoundException expected) {53// should always reach this point since the path does not exist54System.out.println("caught expected " + expected);55int times = 1;56do {57// give some time to close the connection...58System.out.println("sleeping... " + times);59Thread.sleep(times * 1000);60} while (server.activeClientsCount() > 0 && times++ < 5);6162if (server.activeClientsCount() > 0) {63server.killClients();64throw new RuntimeException("URLConnection didn't close the" +65" FTP connection on FileNotFoundException");66}67}68}69}70}7172static class CustomFileSystemHandler implements FtpFileSystemHandler {7374private String currentDir;7576public CustomFileSystemHandler(String path) {77currentDir = path;78}7980@Override81public boolean cd(String path) {82currentDir = path;83return true;84}8586@Override87public boolean cdUp() {88throw new UnsupportedOperationException("Not supported yet.");89}9091@Override92public String pwd() {93throw new UnsupportedOperationException("Not supported yet.");94}9596@Override97public boolean fileExists(String name) {98throw new UnsupportedOperationException("Not supported yet.");99}100101@Override102public InputStream getFile(String name) {103return null; //return null so that server will return 550 File not found.104}105106@Override107public long getFileSize(String name) {108throw new UnsupportedOperationException("Not supported yet.");109}110111@Override112public InputStream listCurrentDir() {113return null;114}115116@Override117public OutputStream putFile(String name) {118throw new UnsupportedOperationException("Not supported yet.");119}120121@Override122public boolean removeFile(String name) {123throw new UnsupportedOperationException("Not supported yet.");124}125126@Override127public boolean mkdir(String name) {128throw new UnsupportedOperationException("Not supported yet.");129}130131@Override132public boolean rename(String from, String to) {133throw new UnsupportedOperationException("Not supported yet.");134}135136}137138static class MyAuthHandler implements FtpAuthHandler {139140@Override141public int authType() {142return 0;143}144145@Override146public boolean authenticate(String user, String password) {147return true;148}149150@Override151public boolean authenticate(String user, String password, String account) {152return true;153}154}155}156157158