Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/ftp/B6427768.java
38838 views
/*1* Copyright (c) 2006, 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* @library ../www/ftptest/28* @build FtpServer FtpCommandHandler FtpAuthHandler FtpFileSystemHandler29* @run main/othervm/timeout=500 B642776830*/3132import java.net.*;33import java.io.*;3435public class B6427768 {36// Need to test when login fails, so AuthHandler should always return37// false38static class MyAuthHandler implements FtpAuthHandler {39public int authType() {40return 2;41}4243public boolean authenticate(String user, String password) {44return false;45}4647public boolean authenticate(String user, String password, String account) {48return false;49}50}5152static class MyFileSystemHandler implements FtpFileSystemHandler {53private String currentDir = "/";5455public MyFileSystemHandler(String path) {56currentDir = path;57}5859public boolean cd(String path) {60currentDir = path;61return true;62}6364public boolean cdUp() {65return true;66}6768public String pwd() {69return currentDir;70}7172public InputStream getFile(String name) {73return null;74}7576public long getFileSize(String name) {77return -1;78}7980public boolean fileExists(String name) {81return false;82}8384public InputStream listCurrentDir() {85return null;86}8788public OutputStream putFile(String name) {89return null;90}9192public boolean removeFile(String name) {93return false;94}9596public boolean mkdir(String name) {97return false;98}99100public boolean rename(String from, String to) {101return false;102}103}104105public static void main(String[] args) throws IOException {106FtpServer server = new FtpServer(0);107int port = server.getLocalPort();108server.setFileSystemHandler(new MyFileSystemHandler("/"));109server.setAuthHandler(new MyAuthHandler());110server.start();111URL url = new URL("ftp://user:passwd@localhost:" + port + "/foo.txt");112URLConnection con = url.openConnection();113// triggers the connection114try {115con.getInputStream();116} catch(sun.net.ftp.FtpLoginException e) {117// Give some time to the client thread to properly terminate.118try {119Thread.sleep(2000);120} catch (InterruptedException ie) {121// shouldn't happen122}123if (server.activeClientsCount() > 0) {124// If there are still active clients attached to the FTP125// server, it means we didn't quit properly126server.killClients();127throw new RuntimeException("URLConnection didn't close the ftp connection on failure to login");128}129} finally {130server.terminate();131}132}133}134135136