1 /* 2 * Copyright (c) 2016, 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 8022580 27 * @summary "null" should be treated as "current directory" in nameList() 28 * method of FtpClient 29 * @modules java.base/sun.net.ftp 30 * @run main TestFtpClientNameListWithNull 31 */ 32 33 34 import sun.net.ftp.FtpClient; 35 36 import java.io.BufferedReader; 37 import java.io.IOException; 38 import java.io.InputStreamReader; 39 import java.io.PrintWriter; 40 import java.net.InetSocketAddress; 41 import java.net.ServerSocket; 42 import java.net.Socket; 43 import java.net.SocketException; 44 45 46 public class TestFtpClientNameListWithNull { 47 48 private static volatile boolean commandHasArgs; 49 main(String[] args)50 public static void main(String[] args) throws Exception { 51 try (FtpServer server = new FtpServer(); 52 FtpClient client = FtpClient.create()) { 53 (new Thread(server)).start(); 54 int port = server.getPort(); 55 client.connect(new InetSocketAddress("localhost", port)); 56 client.nameList(null); 57 } finally { 58 if (commandHasArgs) { 59 throw new RuntimeException("Test failed. NLST shouldn't have " + 60 "args if nameList parameter is null"); 61 } 62 } 63 } 64 65 private static class FtpServer implements AutoCloseable, Runnable { 66 private final ServerSocket serverSocket; 67 FtpServer()68 FtpServer() throws IOException { 69 serverSocket = new ServerSocket(0); 70 } 71 handleClient(Socket client)72 public void handleClient(Socket client) throws IOException { 73 boolean done = false; 74 String str; 75 76 client.setSoTimeout(2000); 77 BufferedReader in = new BufferedReader(new InputStreamReader(client. 78 getInputStream())); 79 PrintWriter out = new PrintWriter(client.getOutputStream(), true); 80 out.println("220 FTP serverSocket is ready."); 81 while (!done) { 82 try { 83 str = in.readLine(); 84 } catch (SocketException e) { 85 done = true; 86 continue; 87 } 88 String cmd = str.substring(0, str.indexOf(" ") > 0 ? 89 str.indexOf(" ") : str.length()); 90 String args = (cmd.equals(str)) ? 91 "" : str.substring(str.indexOf(" ")); 92 switch (cmd) { 93 case "QUIT": 94 out.println("221 Goodbye."); 95 out.flush(); 96 done = true; 97 break; 98 case "EPSV": 99 if ("all".equalsIgnoreCase(args)) { 100 out.println("200 EPSV ALL command successful."); 101 continue; 102 } 103 out.println("229 Entering Extended Passive Mode " + 104 "(|||" + getPort() + "|)"); 105 break; 106 case "NLST": 107 if (args.trim().length() != 0) { 108 commandHasArgs = true; 109 } 110 out.println("200 Command okay."); 111 break; 112 default: 113 out.println("500 unsupported command: " + str); 114 } 115 } 116 } 117 getPort()118 public int getPort() { 119 if (serverSocket != null) { 120 return serverSocket.getLocalPort(); 121 } 122 return 0; 123 } 124 close()125 public void close() throws IOException { 126 if (serverSocket != null && !serverSocket.isClosed()) { 127 serverSocket.close(); 128 } 129 } 130 131 @Override run()132 public void run() { 133 try { 134 try (Socket client = serverSocket.accept()) { 135 handleClient(client); 136 } 137 } catch (IOException e) { 138 throw new RuntimeException("Problem in test execution", e); 139 } 140 } 141 } 142 } 143