Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/ftp/FtpURLConnectionLeak.java
38838 views
/*1* Copyright (c) 2016, 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 FtpURLConnectionLeak30*/31import java.io.FileNotFoundException;32import java.io.IOException;33import java.io.InputStream;34import java.io.OutputStream;35import java.net.URL;3637public class FtpURLConnectionLeak {3839public static void main(String[] args) throws Exception {40FtpServer server = new FtpServer(0);41server.setFileSystemHandler(new CustomFileSystemHandler("/"));42server.setAuthHandler(new MyAuthHandler());43int port = server.getLocalPort();44server.start();45URL url = new URL("ftp://localhost:" + port + "/filedoesNotExist.txt");46for (int i = 0; i < 3; i++) {47try {48InputStream stream = url.openStream();49} catch (FileNotFoundException expectedFirstTimeAround) {50// should always reach this point since the path does not exist51} catch (IOException expected) {52System.out.println("caught expected " + expected);53int times = 1;54do {55// give some time to close the connection...56System.out.println("sleeping... " + times);57Thread.sleep(times * 1000);58} while (server.activeClientsCount() > 0 && times++ < 5);5960if (server.activeClientsCount() > 0) {61server.killClients();62throw new RuntimeException("URLConnection didn't close the" +63" FTP connection on FileNotFoundException");64}65} finally {66server.terminate();67}68}69}7071static class CustomFileSystemHandler implements FtpFileSystemHandler {7273private String currentDir;7475public CustomFileSystemHandler(String path) {76currentDir = path;77}7879@Override80public boolean cd(String path) {81currentDir = path;82return true;83}8485@Override86public boolean cdUp() {87throw new UnsupportedOperationException("Not supported yet.");88}8990@Override91public String pwd() {92throw new UnsupportedOperationException("Not supported yet.");93}9495@Override96public boolean fileExists(String name) {97throw new UnsupportedOperationException("Not supported yet.");98}99100@Override101public InputStream getFile(String name) {102return null; //return null so that server will return 550 File not found.103}104105@Override106public long getFileSize(String name) {107throw new UnsupportedOperationException("Not supported yet.");108}109110@Override111public InputStream listCurrentDir() {112return null;113}114115@Override116public OutputStream putFile(String name) {117throw new UnsupportedOperationException("Not supported yet.");118}119120@Override121public boolean removeFile(String name) {122throw new UnsupportedOperationException("Not supported yet.");123}124125@Override126public boolean mkdir(String name) {127throw new UnsupportedOperationException("Not supported yet.");128}129130@Override131public boolean rename(String from, String to) {132throw new UnsupportedOperationException("Not supported yet.");133}134135}136137static class MyAuthHandler implements FtpAuthHandler {138139@Override140public int authType() {141return 0;142}143144@Override145public boolean authenticate(String user, String password) {146return true;147}148149@Override150public boolean authenticate(String user, String password, String account) {151return true;152}153}154}155156157