jdk/test/sun/net/ftp/B6427768.java
author mchung
Mon, 23 Nov 2015 12:44:43 -0800
changeset 34253 ba3946143842
parent 30820 0d4717a011d3
permissions -rw-r--r--
8140450: Implement JEP 259: Stack-Walking API Reviewed-by: coleenp, dfuchs, bchristi, psandoz, sspitsyn Contributed-by: Mandy Chung <mandy.chung@oracle.com>, Brent Christian <brent.christian@oracle.com>, Daniel Fuchs <daniel.fuchs@oracle.com>, Hamlin Li <huaming.li@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @bug 6427768
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 * @summary FtpURLConnection doesn't close FTP connection when login fails
30820
0d4717a011d3 8081347: Add @modules to jdk_core tests
mchung
parents: 5506
diff changeset
    28
 * @modules java.base/sun.net.ftp
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 * @library ../www/ftptest/
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @build FtpServer FtpCommandHandler FtpAuthHandler FtpFileSystemHandler
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @run main/othervm/timeout=500 B6427768
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
public class B6427768 {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    // Need to test when login fails, so AuthHandler should always return
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    // false
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static class MyAuthHandler implements FtpAuthHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        public int authType() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
                return 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        public boolean authenticate(String user, String password) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        public boolean authenticate(String user, String password, String account) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
                return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    static class MyFileSystemHandler implements FtpFileSystemHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        private String currentDir = "/";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        public MyFileSystemHandler(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                currentDir = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        public boolean cd(String path) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            currentDir = path;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        public boolean cdUp() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        public String pwd() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return currentDir;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        public InputStream getFile(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        public long getFileSize(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            return -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        public boolean fileExists(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        public InputStream listCurrentDir() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        public OutputStream putFile(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            return null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        public boolean removeFile(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        public boolean mkdir(String name) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        public boolean rename(String from, String to) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    public static void main(String[] args) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        FtpServer server = new FtpServer(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int port = server.getLocalPort();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        server.setFileSystemHandler(new MyFileSystemHandler("/"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        server.setAuthHandler(new MyAuthHandler());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        server.start();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        URL url = new URL("ftp://user:passwd@localhost:" + port + "/foo.txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        URLConnection con = url.openConnection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // triggers the connection
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
            con.getInputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        } catch(sun.net.ftp.FtpLoginException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
            // Give some time to the client thread to properly terminate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
                Thread.sleep(2000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            } catch (InterruptedException ie) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
                // shouldn't happen
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            if (server.activeClientsCount() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
                // If there are still active clients attached to the FTP
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                // server, it means we didn't quit properly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                server.killClients();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                throw new RuntimeException("URLConnection didn't close the ftp connection on failure to login");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        } finally {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            server.terminate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
}