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