1 /* 2 * Copyright (c) 2006, 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 6427768 27 * @summary FtpURLConnection doesn't close FTP connection when login fails 28 * @modules java.base/sun.net.ftp 29 * @library ../www/ftptest/ 30 * @build FtpServer FtpCommandHandler FtpAuthHandler FtpFileSystemHandler 31 * @run main/othervm/timeout=500 B6427768 32 */ 33 34 import java.net.*; 35 import java.io.*; 36 37 public class B6427768 { 38 // Need to test when login fails, so AuthHandler should always return 39 // false 40 static class MyAuthHandler implements FtpAuthHandler { 41 public int authType() { 42 return 2; 43 } 44 45 public boolean authenticate(String user, String password) { 46 return false; 47 } 48 49 public boolean authenticate(String user, String password, String account) { 50 return false; 51 } 52 } 53 54 static class MyFileSystemHandler implements FtpFileSystemHandler { 55 private String currentDir = "/"; 56 57 public MyFileSystemHandler(String path) { 58 currentDir = path; 59 } 60 61 public boolean cd(String path) { 62 currentDir = path; 63 return true; 64 } 65 66 public boolean cdUp() { 67 return true; 68 } 69 70 public String pwd() { 71 return currentDir; 72 } 73 74 public InputStream getFile(String name) { 75 return null; 76 } 77 78 public long getFileSize(String name) { 79 return -1; 80 } 81 82 public boolean fileExists(String name) { 83 return false; 84 } 85 86 public InputStream listCurrentDir() { 87 return null; 88 } 89 90 public OutputStream putFile(String name) { 91 return null; 92 } 93 94 public boolean removeFile(String name) { 95 return false; 96 } 97 98 public boolean mkdir(String name) { 99 return false; 100 } 101 102 public boolean rename(String from, String to) { 103 return false; 104 } 105 } 106 107 public static void main(String[] args) throws IOException { 108 FtpServer server = new FtpServer(0); 109 int port = server.getLocalPort(); 110 server.setFileSystemHandler(new MyFileSystemHandler("/")); 111 server.setAuthHandler(new MyAuthHandler()); 112 server.start(); 113 URL url = new URL("ftp://user:passwd@localhost:" + port + "/foo.txt"); 114 URLConnection con = url.openConnection(); 115 // triggers the connection 116 try { 117 con.getInputStream(); 118 } catch(sun.net.ftp.FtpLoginException e) { 119 // Give some time to the client thread to properly terminate. 120 try { 121 Thread.sleep(2000); 122 } catch (InterruptedException ie) { 123 // shouldn't happen 124 } 125 if (server.activeClientsCount() > 0) { 126 // If there are still active clients attached to the FTP 127 // server, it means we didn't quit properly 128 server.killClients(); 129 throw new RuntimeException("URLConnection didn't close the ftp connection on failure to login"); 130 } 131 } finally { 132 server.terminate(); 133 } 134 } 135 }