Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/sun/net/ftp/TestFtpClientNameListWithNull.java
38838 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.InetSocketAddress;40import java.net.ServerSocket;41import java.net.Socket;42import java.net.SocketException;434445public class TestFtpClientNameListWithNull {4647private static volatile boolean commandHasArgs;4849public static void main(String[] args) throws Exception {50try (FtpServer server = new FtpServer();51FtpClient client = FtpClient.create()) {52(new Thread(server)).start();53int port = server.getPort();54client.connect(new InetSocketAddress("localhost", port));55client.nameList(null);56} finally {57if (commandHasArgs) {58throw new RuntimeException("Test failed. NLST shouldn't have " +59"args if nameList parameter is null");60}61}62}6364private static class FtpServer implements AutoCloseable, Runnable {65private final ServerSocket serverSocket;6667FtpServer() throws IOException {68serverSocket = new ServerSocket(0);69}7071public void handleClient(Socket client) throws IOException {72boolean done = false;73String str;7475client.setSoTimeout(2000);76BufferedReader in = new BufferedReader(new InputStreamReader(client.77getInputStream()));78PrintWriter out = new PrintWriter(client.getOutputStream(), true);79out.println("220 FTP serverSocket is ready.");80while (!done) {81try {82str = in.readLine();83} catch (SocketException e) {84done = true;85continue;86}87String cmd = str.substring(0, str.indexOf(" ") > 0 ?88str.indexOf(" ") : str.length());89String args = (cmd.equals(str)) ?90"" : str.substring(str.indexOf(" "));91switch (cmd) {92case "QUIT":93out.println("221 Goodbye.");94out.flush();95done = true;96break;97case "EPSV":98if ("all".equalsIgnoreCase(args)) {99out.println("200 EPSV ALL command successful.");100continue;101}102out.println("229 Entering Extended Passive Mode " +103"(|||" + getPort() + "|)");104break;105case "NLST":106if (args.trim().length() != 0) {107commandHasArgs = true;108}109out.println("200 Command okay.");110break;111default:112out.println("500 unsupported command: " + str);113}114}115}116117public int getPort() {118if (serverSocket != null) {119return serverSocket.getLocalPort();120}121return 0;122}123124public void close() throws IOException {125if (serverSocket != null && !serverSocket.isClosed()) {126serverSocket.close();127}128}129130@Override131public void run() {132try {133try (Socket client = serverSocket.accept()) {134handleClient(client);135}136} catch (IOException e) {137throw new RuntimeException("Problem in test execution", e);138}139}140}141}142143144