Path: blob/master/test/jdk/sun/net/ftp/TestFtpClientNameListWithNull.java
41152 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 802258026* @summary "null" should be treated as "current directory" in nameList()27* method of FtpClient28* @modules java.base/sun.net.ftp29* @run main TestFtpClientNameListWithNull30*/313233import sun.net.ftp.FtpClient;3435import java.io.BufferedReader;36import java.io.IOException;37import java.io.InputStreamReader;38import java.io.PrintWriter;39import java.net.InetAddress;40import java.net.InetSocketAddress;41import java.net.ServerSocket;42import java.net.Socket;43import java.net.SocketException;444546public class TestFtpClientNameListWithNull {4748private static volatile boolean commandHasArgs;4950public static void main(String[] args) throws Exception {51try (FtpServer server = new FtpServer();52FtpClient client = FtpClient.create()) {53(new Thread(server)).start();54int port = server.getPort();55InetAddress loopback = InetAddress.getLoopbackAddress();56client.connect(new InetSocketAddress(loopback, port));57client.nameList(null);58} finally {59if (commandHasArgs) {60throw new RuntimeException("Test failed. NLST shouldn't have " +61"args if nameList parameter is null");62}63}64}6566private static class FtpServer implements AutoCloseable, Runnable {67private final ServerSocket serverSocket;6869FtpServer() throws IOException {70InetAddress loopback = InetAddress.getLoopbackAddress();71serverSocket = new ServerSocket();72serverSocket.bind(new InetSocketAddress(loopback, 0));73}7475public void handleClient(Socket client) throws IOException {76boolean done = false;77String str;7879client.setSoTimeout(2000);80BufferedReader in = new BufferedReader(new InputStreamReader(client.81getInputStream()));82PrintWriter out = new PrintWriter(client.getOutputStream(), true);83out.println("220 FTP serverSocket is ready.");84while (!done) {85try {86str = in.readLine();87} catch (SocketException e) {88done = true;89continue;90}91String cmd = str.substring(0, str.indexOf(" ") > 0 ?92str.indexOf(" ") : str.length());93String args = (cmd.equals(str)) ?94"" : str.substring(str.indexOf(" "));95switch (cmd) {96case "QUIT":97out.println("221 Goodbye.");98out.flush();99done = true;100break;101case "EPSV":102if ("all".equalsIgnoreCase(args)) {103out.println("200 EPSV ALL command successful.");104continue;105}106out.println("229 Entering Extended Passive Mode " +107"(|||" + getPort() + "|)");108break;109case "NLST":110if (args.trim().length() != 0) {111commandHasArgs = true;112}113out.println("200 Command okay.");114break;115default:116out.println("500 unsupported command: " + str);117}118}119}120121public int getPort() {122if (serverSocket != null) {123return serverSocket.getLocalPort();124}125return 0;126}127128public void close() throws IOException {129if (serverSocket != null && !serverSocket.isClosed()) {130serverSocket.close();131}132}133134@Override135public void run() {136try {137try (Socket client = serverSocket.accept()) {138handleClient(client);139}140} catch (IOException e) {141throw new RuntimeException("Problem in test execution", e);142}143}144}145}146147148