summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/maintainers.rdoc6
-rw-r--r--doc/standard_library.rdoc2
-rw-r--r--gems/bundled_gems1
-rw-r--r--lib/net/ftp.rb1544
-rw-r--r--lib/net/net-ftp.gemspec36
-rwxr-xr-xmisc/expand_tabs.rb1
-rw-r--r--test/net/ftp/test_buffered_socket.rb48
-rw-r--r--test/net/ftp/test_ftp.rb2636
-rw-r--r--test/net/ftp/test_mlsx_entry.rb98
-rw-r--r--tool/sync_default_gems.rb6
10 files changed, 4 insertions, 4374 deletions
diff --git a/doc/maintainers.rdoc b/doc/maintainers.rdoc
index 79c30b8732..965e9e8870 100644
--- a/doc/maintainers.rdoc
+++ b/doc/maintainers.rdoc
@@ -154,10 +154,6 @@ Yukihiro Matsumoto (matz)
Keiju ISHITSUKA (keiju)
https://github.com/ruby/mutex_m
https://rubygems.org/gems/mutex_m
-[lib/net/ftp.rb]
- Shugo Maeda (shugo)
- https://github.com/ruby/net-ftp
- https://rubygems.org/gems/net-ftp
[lib/net/http.rb, lib/net/https.rb]
NARUSE, Yui (naruse)
https://github.com/ruby/net-http
@@ -391,6 +387,8 @@ Yukihiro Matsumoto (matz)
https://github.com/ruby/rexml
[rss]
https://github.com/ruby/rss
+[net-ftp]
+ https://github.com/ruby/net-ftp
[matrix]
https://github.com/ruby/matrix
[prime]
diff --git a/doc/standard_library.rdoc b/doc/standard_library.rdoc
index e05eb6f62d..2c23c10deb 100644
--- a/doc/standard_library.rdoc
+++ b/doc/standard_library.rdoc
@@ -46,7 +46,6 @@ IRB:: Interactive Ruby command-line tool for REPL (Read Eval Print Loop)
OptionParser:: Ruby-oriented class for command-line option analysis
Logger:: Provides a simple logging utility for outputting messages
Mutex_m:: Mixin to extend objects to be handled like a Mutex
-Net::FTP:: Support for the File Transfer Protocol
Net::HTTP:: HTTP client api for Ruby
Net::IMAP:: Ruby client api for Internet Message Access Protocol
Net::POP3:: Ruby client library for POP3
@@ -109,6 +108,7 @@ Rake:: Ruby build program with capabilities similar to make
Test::Unit:: A compatibility layer for MiniTest
REXML:: An XML toolkit for Ruby
RSS:: Family of libraries that support various formats of XML "feeds"
+Net::FTP:: Support for the File Transfer Protocol
Matrix:: Represents a mathematical matrix.
Prime:: Prime numbers and factorization library
RBS:: RBS is a language to describe the structure of Ruby programs
diff --git a/gems/bundled_gems b/gems/bundled_gems
index b42c6794d3..e944d21f59 100644
--- a/gems/bundled_gems
+++ b/gems/bundled_gems
@@ -5,6 +5,7 @@ rake 13.0.3 https://github.com/ruby/rake
test-unit 3.4.1 https://github.com/test-unit/test-unit 3.4.1
rexml 3.2.5 https://github.com/ruby/rexml
rss 0.2.9 https://github.com/ruby/rss 0.2.9
+net-ftp 0.1.2 https://github.com/ruby/net-ftp
matrix 0.4.1 https://github.com/ruby/matrix
prime 0.1.2 https://github.com/ruby/prime
typeprof 0.14.1 https://github.com/ruby/typeprof
diff --git a/lib/net/ftp.rb b/lib/net/ftp.rb
deleted file mode 100644
index 68d5375ac7..0000000000
--- a/lib/net/ftp.rb
+++ /dev/null
@@ -1,1544 +0,0 @@
-# frozen_string_literal: true
-#
-# = net/ftp.rb - FTP Client Library
-#
-# Written by Shugo Maeda <shugo@ruby-lang.org>.
-#
-# Documentation by Gavin Sinclair, sourced from "Programming Ruby" (Hunt/Thomas)
-# and "Ruby In a Nutshell" (Matsumoto), used with permission.
-#
-# This library is distributed under the terms of the Ruby license.
-# You can freely distribute/modify this library.
-#
-# It is included in the Ruby standard library.
-#
-# See the Net::FTP class for an overview.
-#
-
-require "socket"
-require "monitor"
-require "net/protocol"
-require "time"
-begin
- require "openssl"
-rescue LoadError
-end
-
-module Net
-
- # :stopdoc:
- class FTPError < StandardError; end
- class FTPReplyError < FTPError; end
- class FTPTempError < FTPError; end
- class FTPPermError < FTPError; end
- class FTPProtoError < FTPError; end
- class FTPConnectionError < FTPError; end
- # :startdoc:
-
- #
- # This class implements the File Transfer Protocol. If you have used a
- # command-line FTP program, and are familiar with the commands, you will be
- # able to use this class easily. Some extra features are included to take
- # advantage of Ruby's style and strengths.
- #
- # == Example
- #
- # require 'net/ftp'
- #
- # === Example 1
- #
- # ftp = Net::FTP.new('example.com')
- # ftp.login
- # files = ftp.chdir('pub/lang/ruby/contrib')
- # files = ftp.list('n*')
- # ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
- # ftp.close
- #
- # === Example 2
- #
- # Net::FTP.open('example.com') do |ftp|
- # ftp.login
- # files = ftp.chdir('pub/lang/ruby/contrib')
- # files = ftp.list('n*')
- # ftp.getbinaryfile('nif.rb-0.91.gz', 'nif.gz', 1024)
- # end
- #
- # == Major Methods
- #
- # The following are the methods most likely to be useful to users:
- # - FTP.open
- # - #getbinaryfile
- # - #gettextfile
- # - #putbinaryfile
- # - #puttextfile
- # - #chdir
- # - #nlst
- # - #size
- # - #rename
- # - #delete
- #
- class FTP < Protocol
- include MonitorMixin
- if defined?(OpenSSL::SSL)
- include OpenSSL
- include SSL
- end
-
- # :stopdoc:
- VERSION = "0.1.2"
- FTP_PORT = 21
- CRLF = "\r\n"
- DEFAULT_BLOCKSIZE = BufferedIO::BUFSIZE
- @@default_passive = true
- # :startdoc:
-
- # When +true+, transfers are performed in binary mode. Default: +true+.
- attr_reader :binary
-
- # When +true+, the connection is in passive mode. Default: +true+.
- attr_accessor :passive
-
- # When +true+, all traffic to and from the server is written
- # to +$stdout+. Default: +false+.
- attr_accessor :debug_mode
-
- # Sets or retrieves the +resume+ status, which decides whether incomplete
- # transfers are resumed or restarted. Default: +false+.
- attr_accessor :resume
-
- # Number of seconds to wait for the connection to open. Any number
- # may be used, including Floats for fractional seconds. If the FTP
- # object cannot open a connection in this many seconds, it raises a
- # Net::OpenTimeout exception. The default value is +nil+.
- attr_accessor :open_timeout
-
- # Number of seconds to wait for the TLS handshake. Any number
- # may be used, including Floats for fractional seconds. If the FTP
- # object cannot complete the TLS handshake in this many seconds, it
- # raises a Net::OpenTimeout exception. The default value is +nil+.
- # If +ssl_handshake_timeout+ is +nil+, +open_timeout+ is used instead.
- attr_accessor :ssl_handshake_timeout
-
- # Number of seconds to wait for one block to be read (via one read(2)
- # call). Any number may be used, including Floats for fractional
- # seconds. If the FTP object cannot read data in this many seconds,
- # it raises a Timeout::Error exception. The default value is 60 seconds.
- attr_reader :read_timeout
-
- # Setter for the read_timeout attribute.
- def read_timeout=(sec)
- @sock.read_timeout = sec
- @read_timeout = sec
- end
-
- # The server's welcome message.
- attr_reader :welcome
-
- # The server's last response code.
- attr_reader :last_response_code
- alias lastresp last_response_code
-
- # The server's last response.
- attr_reader :last_response
-
- # When +true+, connections are in passive mode per default.
- # Default: +true+.
- def self.default_passive=(value)
- @@default_passive = value
- end
-
- # When +true+, connections are in passive mode per default.
- # Default: +true+.
- def self.default_passive
- @@default_passive
- end
-
- #
- # A synonym for <tt>FTP.new</tt>, but with a mandatory host parameter.
- #
- # If a block is given, it is passed the +FTP+ object, which will be closed
- # when the block finishes, or when an exception is raised.
- #
- def FTP.open(host, *args)
- if block_given?
- ftp = new(host, *args)
- begin
- yield ftp
- ensure
- ftp.close
- end
- else
- new(host, *args)
- end
- end
-
- # :call-seq:
- # Net::FTP.new(host = nil, options = {})
- #
- # Creates and returns a new +FTP+ object. If a +host+ is given, a connection
- # is made.
- #
- # +options+ is an option hash, each key of which is a symbol.
- #
- # The available options are:
- #
- # port:: Port number (default value is 21)
- # ssl:: If +options+[:ssl] is true, then an attempt will be made
- # to use SSL (now TLS) to connect to the server. For this
- # to work OpenSSL [OSSL] and the Ruby OpenSSL [RSSL]
- # extensions need to be installed. If +options+[:ssl] is a
- # hash, it's passed to OpenSSL::SSL::SSLContext#set_params
- # as parameters.
- # private_data_connection:: If true, TLS is used for data connections.
- # Default: +true+ when +options+[:ssl] is true.
- # username:: Username for login. If +options+[:username] is the string
- # "anonymous" and the +options+[:password] is +nil+,
- # "anonymous@" is used as a password.
- # password:: Password for login.
- # account:: Account information for ACCT.
- # passive:: When +true+, the connection is in passive mode. Default:
- # +true+.
- # open_timeout:: Number of seconds to wait for the connection to open.
- # See Net::FTP#open_timeout for details. Default: +nil+.
- # read_timeout:: Number of seconds to wait for one block to be read.
- # See Net::FTP#read_timeout for details. Default: +60+.
- # ssl_handshake_timeout:: Number of seconds to wait for the TLS
- # handshake.
- # See Net::FTP#ssl_handshake_timeout for
- # details. Default: +nil+.
- # debug_mode:: When +true+, all traffic to and from the server is
- # written to +$stdout+. Default: +false+.
- #
- def initialize(host = nil, user_or_options = {}, passwd = nil, acct = nil)
- super()
- begin
- options = user_or_options.to_hash
- rescue NoMethodError
- # for backward compatibility
- options = {}
- options[:username] = user_or_options
- options[:password] = passwd
- options[:account] = acct
- end
- @host = nil
- if options[:ssl]
- unless defined?(OpenSSL::SSL)
- raise "SSL extension not installed"
- end
- ssl_params = options[:ssl] == true ? {} : options[:ssl]
- @ssl_context = SSLContext.new
- @ssl_context.set_params(ssl_params)
- if defined?(VerifyCallbackProc)
- @ssl_context.verify_callback = VerifyCallbackProc
- end
- @ssl_context.session_cache_mode =
- OpenSSL::SSL::SSLContext::SESSION_CACHE_CLIENT |
- OpenSSL::SSL::SSLContext::SESSION_CACHE_NO_INTERNAL_STORE
- @ssl_context.session_new_cb = proc {|sock, sess| @ssl_session = sess }
- @ssl_session = nil
- if options[:private_data_connection].nil?
- @private_data_connection = true
- else
- @private_data_connection = options[:private_data_connection]
- end
- else
- @ssl_context = nil
- if options[:private_data_connection]
- raise ArgumentError,
- "private_data_connection can be set to true only when ssl is enabled"
- end
- @private_data_connection = false
- end
- @binary = true
- if options[:passive].nil?
- @passive = @@default_passive
- else
- @passive = options[:passive]
- end
- if options[:debug_mode].nil?
- @debug_mode = false
- else
- @debug_mode = options[:debug_mode]
- end
- @resume = false
- @bare_sock = @sock = NullSocket.new
- @logged_in = false
- @open_timeout = options[:open_timeout]
- @ssl_handshake_timeout = options[:ssl_handshake_timeout]
- @read_timeout = options[:read_timeout] || 60
- if host
- connect(host, options[:port] || FTP_PORT)
- if options[:username]
- login(options[:username], options[:password], options[:account])
- end
- end
- end
-
- # A setter to toggle transfers in binary mode.
- # +newmode+ is either +true+ or +false+
- def binary=(newmode)
- if newmode != @binary
- @binary = newmode
- send_type_command if @logged_in
- end
- end
-
- # Sends a command to destination host, with the current binary sendmode
- # type.
- #
- # If binary mode is +true+, then "TYPE I" (image) is sent, otherwise "TYPE
- # A" (ascii) is sent.
- def send_type_command # :nodoc:
- if @binary
- voidcmd("TYPE I")
- else
- voidcmd("TYPE A")
- end
- end
- private :send_type_command
-
- # Toggles transfers in binary mode and yields to a block.
- # This preserves your current binary send mode, but allows a temporary
- # transaction with binary sendmode of +newmode+.
- #
- # +newmode+ is either +true+ or +false+
- def with_binary(newmode) # :nodoc:
- oldmode = binary
- self.binary = newmode
- begin
- yield
- ensure
- self.binary = oldmode
- end
- end
- private :with_binary
-
- # Obsolete
- def return_code # :nodoc:
- warn("Net::FTP#return_code is obsolete and do nothing", uplevel: 1)
- return "\n"
- end
-
- # Obsolete
- def return_code=(s) # :nodoc:
- warn("Net::FTP#return_code= is obsolete and do nothing", uplevel: 1)
- end
-
- # Constructs a socket with +host+ and +port+.
- #
- # If SOCKSSocket is defined and the environment (ENV) defines
- # SOCKS_SERVER, then a SOCKSSocket is returned, else a Socket is
- # returned.
- def open_socket(host, port) # :nodoc:
- if defined? SOCKSSocket and ENV["SOCKS_SERVER"]
- @passive = true
- Timeout.timeout(@open_timeout, OpenTimeout) do
- SOCKSSocket.open(host, port)
- end
- else
- begin
- Socket.tcp host, port, nil, nil, connect_timeout: @open_timeout
- rescue Errno::ETIMEDOUT #raise Net:OpenTimeout instead for compatibility with previous versions
- raise Net::OpenTimeout, "Timeout to open TCP connection to "\
- "#{host}:#{port} (exceeds #{@open_timeout} seconds)"
- end
- end
- end
- private :open_socket
-
- def start_tls_session(sock)
- ssl_sock = SSLSocket.new(sock, @ssl_context)
- ssl_sock.sync_close = true
- ssl_sock.hostname = @host if ssl_sock.respond_to? :hostname=
- if @ssl_session &&
- Process.clock_gettime(Process::CLOCK_REALTIME) < @ssl_session.time.to_f + @ssl_session.timeout
- # ProFTPD returns 425 for data connections if session is not reused.
- ssl_sock.session = @ssl_session
- end
- ssl_socket_connect(ssl_sock, @ssl_handshake_timeout || @open_timeout)
- if @ssl_context.verify_mode != VERIFY_NONE
- ssl_sock.post_connection_check(@host)
- end
- return ssl_sock
- end
- private :start_tls_session
-
- #
- # Establishes an FTP connection to host, optionally overriding the default
- # port. If the environment variable +SOCKS_SERVER+ is set, sets up the
- # connection through a SOCKS proxy. Raises an exception (typically
- # <tt>Errno::ECONNREFUSED</tt>) if the connection cannot be established.
- #
- def connect(host, port = FTP_PORT)
- if @debug_mode
- print "connect: ", host, ", ", port, "\n"
- end
- synchronize do
- @host = host
- @bare_sock = open_socket(host, port)
- @sock = BufferedSocket.new(@bare_sock, read_timeout: @read_timeout)
- voidresp
- if @ssl_context
- begin
- voidcmd("AUTH TLS")
- ssl_sock = start_tls_session(@bare_sock)
- @sock = BufferedSSLSocket.new(ssl_sock, read_timeout: @read_timeout)
- if @private_data_connection
- voidcmd("PBSZ 0")
- voidcmd("PROT P")
- end
- rescue OpenSSL::SSL::SSLError, OpenTimeout
- @sock.close
- raise
- end
- end
- end
- end
-
- #
- # Set the socket used to connect to the FTP server.
- #
- # May raise FTPReplyError if +get_greeting+ is false.
- def set_socket(sock, get_greeting = true)
- synchronize do
- @sock = sock
- if get_greeting
- voidresp
- end
- end
- end
-
- # If string +s+ includes the PASS command (password), then the contents of
- # the password are cleaned from the string using "*"
- def sanitize(s) # :nodoc:
- if s =~ /^PASS /i
- return s[0, 5] + "*" * (s.length - 5)
- else
- return s
- end
- end
- private :sanitize
-
- # Ensures that +line+ has a control return / line feed (CRLF) and writes
- # it to the socket.
- def putline(line) # :nodoc:
- if @debug_mode
- print "put: ", sanitize(line), "\n"
- end
- if /[\r\n]/ =~ line
- raise ArgumentError, "A line must not contain CR or LF"
- end
- line = line + CRLF
- @sock.write(line)
- end
- private :putline
-
- # Reads a line from the sock. If EOF, then it will raise EOFError
- def getline # :nodoc:
- line = @sock.readline # if get EOF, raise EOFError
- line.sub!(/(\r\n|\n|\r)\z/n, "")
- if @debug_mode
- print "get: ", sanitize(line), "\n"
- end
- return line
- end
- private :getline
-
- # Receive a section of lines until the response code's match.
- def getmultiline # :nodoc:
- lines = []
- lines << getline
- code = lines.last.slice(/\A([0-9a-zA-Z]{3})-/, 1)
- if code
- delimiter = code + " "
- begin
- lines << getline
- end until lines.last.start_with?(delimiter)
- end
- return lines.join("\n") + "\n"
- end
- private :getmultiline
-
- # Receives a response from the destination host.
- #
- # Returns the response code or raises FTPTempError, FTPPermError, or
- # FTPProtoError
- def getresp # :nodoc:
- @last_response = getmultiline
- @last_response_code = @last_response[0, 3]
- case @last_response_code
- when /\A[123]/
- return @last_response
- when /\A4/
- raise FTPTempError, @last_response
- when /\A5/
- raise FTPPermError, @last_response
- else
- raise FTPProtoError, @last_response
- end
- end
- private :getresp
-
- # Receives a response.
- #
- # Raises FTPReplyError if the first position of the response code is not
- # equal 2.
- def voidresp # :nodoc:
- resp = getresp
- if !resp.start_with?("2")
- raise FTPReplyError, resp
- end
- end
- private :voidresp
-
- #
- # Sends a command and returns the response.
- #
- def sendcmd(cmd)
- synchronize do
- putline(cmd)
- return getresp
- end
- end
-
- #
- # Sends a command and expect a response beginning with '2'.
- #
- def voidcmd(cmd)
- synchronize do
- putline(cmd)
- voidresp
- end
- end
-
- # Constructs and send the appropriate PORT (or EPRT) command
- def sendport(host, port) # :nodoc:
- remote_address = @bare_sock.remote_address
- if remote_address.ipv4?
- cmd = "PORT " + (host.split(".") + port.divmod(256)).join(",")
- elsif remote_address.ipv6?
- cmd = sprintf("EPRT |2|%s|%d|", host, port)
- else
- raise FTPProtoError, host
- end
- voidcmd(cmd)
- end
- private :sendport
-
- # Constructs a TCPServer socket
- def makeport # :nodoc:
- Addrinfo.tcp(@bare_sock.local_address.ip_address, 0).listen
- end
- private :makeport
-
- # sends the appropriate command to enable a passive connection
- def makepasv # :nodoc:
- if @bare_sock.remote_address.ipv4?
- host, port = parse227(sendcmd("PASV"))
- else
- host, port = parse229(sendcmd("EPSV"))
- # host, port = parse228(sendcmd("LPSV"))
- end
- return host, port
- end
- private :makepasv
-
- # Constructs a connection for transferring data
- def transfercmd(cmd, rest_offset = nil) # :nodoc:
- if @passive
- host, port = makepasv
- begin
- conn = open_socket(host, port)
- if @resume and rest_offset
- resp = sendcmd("REST " + rest_offset.to_s)
- if !resp.start_with?("3")
- raise FTPReplyError, resp
- end
- end
- resp = sendcmd(cmd)
- # skip 2XX for some ftp servers
- resp = getresp if resp.start_with?("2")
- if !resp.start_with?("1")
- raise FTPReplyError, resp
- end
- ensure
- conn.close if conn && $!
- end
- else
- sock = makeport
- begin
- addr = sock.local_address
- sendport(addr.ip_address, addr.ip_port)
- if @resume and rest_offset
- resp = sendcmd("REST " + rest_offset.to_s)
- if !resp.start_with?("3")
- raise FTPReplyError, resp
- end
- end
- resp = sendcmd(cmd)
- # skip 2XX for some ftp servers
- resp = getresp if resp.start_with?("2")
- if !resp.start_with?("1")
- raise FTPReplyError, resp
- end
- conn, = sock.accept
- sock.shutdown(Socket::SHUT_WR) rescue nil
- sock.read rescue nil
- ensure
- sock.close
- end
- end
- if @private_data_connection
- return BufferedSSLSocket.new(start_tls_session(conn),
- read_timeout: @read_timeout)
- else
- return BufferedSocket.new(conn, read_timeout: @read_timeout)
- end
- end
- private :transfercmd
-
- #
- # Logs in to the remote host. The session must have been
- # previously connected. If +user+ is the string "anonymous" and
- # the +password+ is +nil+, "anonymous@" is used as a password. If
- # the +acct+ parameter is not +nil+, an FTP ACCT command is sent
- # following the successful login. Raises an exception on error
- # (typically <tt>Net::FTPPermError</tt>).
- #
- def login(user = "anonymous", passwd = nil, acct = nil)
- if user == "anonymous" and passwd == nil
- passwd = "anonymous@"
- end
-
- resp = ""
- synchronize do
- resp = sendcmd('USER ' + user)
- if resp.start_with?("3")
- raise FTPReplyError, resp if passwd.nil?
- resp = sendcmd('PASS ' + passwd)
- end
- if resp.start_with?("3")
- raise FTPReplyError, resp if acct.nil?
- resp = sendcmd('ACCT ' + acct)
- end
- end
- if !resp.start_with?("2")
- raise FTPReplyError, resp
- end
- @welcome = resp
- send_type_command
- @logged_in = true
- end
-
- #
- # Puts the connection into binary (image) mode, issues the given command,
- # and fetches the data returned, passing it to the associated block in
- # chunks of +blocksize+ characters. Note that +cmd+ is a server command
- # (such as "RETR myfile").
- #
- def retrbinary(cmd, blocksize, rest_offset = nil) # :yield: data
- synchronize do
- with_binary(true) do
- begin
- conn = transfercmd(cmd, rest_offset)
- while data = conn.read(blocksize)
- yield(data)
- end
- conn.shutdown(Socket::SHUT_WR) rescue nil
- conn.read_timeout = 1
- conn.read rescue nil
- ensure
- conn.close if conn
- end
- voidresp
- end
- end
- end
-
- #
- # Puts the connection into ASCII (text) mode, issues the given command, and
- # passes the resulting data, one line at a time, to the associated block. If
- # no block is given, prints the lines. Note that +cmd+ is a server command
- # (such as "RETR myfile").
- #
- def retrlines(cmd) # :yield: line
- synchronize do
- with_binary(false) do
- begin
- conn = transfercmd(cmd)
- while line = conn.gets
- yield(line.sub(/\r?\n\z/, ""), !line.match(/\n\z/).nil?)
- end
- conn.shutdown(Socket::SHUT_WR) rescue nil
- conn.read_timeout = 1
- conn.read rescue nil
- ensure
- conn.close if conn
- end
- voidresp
- end
- end
- end
-
- #
- # Puts the connection into binary (image) mode, issues the given server-side
- # command (such as "STOR myfile"), and sends the contents of the file named
- # +file+ to the server. If the optional block is given, it also passes it
- # the data, in chunks of +blocksize+ characters.
- #
- def storbinary(cmd, file, blocksize, rest_offset = nil) # :yield: data
- if rest_offset
- file.seek(rest_offset, IO::SEEK_SET)
- end
- synchronize do
- with_binary(true) do
- begin
- conn = transfercmd(cmd)
- while buf = file.read(blocksize)
- conn.write(buf)
- yield(buf) if block_given?
- end
- conn.shutdown(Socket::SHUT_WR) rescue nil
- conn.read_timeout = 1
- conn.read rescue nil
- ensure
- conn.close if conn
- end
- voidresp
- end
- end
- rescue Errno::EPIPE
- # EPIPE, in this case, means that the data connection was unexpectedly
- # terminated. Rather than just raising EPIPE to the caller, check the
- # response on the control connection. If getresp doesn't raise a more
- # appropriate exception, re-raise the original exception.
- getresp
- raise
- end
-
- #
- # Puts the connection into ASCII (text) mode, issues the given server-side
- # command (such as "STOR myfile"), and sends the contents of the file
- # named +file+ to the server, one line at a time. If the optional block is
- # given, it also passes it the lines.
- #
- def storlines(cmd, file) # :yield: line
- synchronize do
- with_binary(false) do
- begin
- conn = transfercmd(cmd)
- while buf = file.gets
- if buf[-2, 2] != CRLF
- buf = buf.chomp + CRLF
- end
- conn.write(buf)
- yield(buf) if block_given?
- end
- conn.shutdown(Socket::SHUT_WR) rescue nil
- conn.read_timeout = 1
- conn.read rescue nil
- ensure
- conn.close if conn
- end
- voidresp
- end
- end
- rescue Errno::EPIPE
- # EPIPE, in this case, means that the data connection was unexpectedly
- # terminated. Rather than just raising EPIPE to the caller, check the
- # response on the control connection. If getresp doesn't raise a more
- # appropriate exception, re-raise the original exception.
- getresp
- raise
- end
-
- #
- # Retrieves +remotefile+ in binary mode, storing the result in +localfile+.
- # If +localfile+ is nil, returns retrieved data.
- # If a block is supplied, it is passed the retrieved data in +blocksize+
- # chunks.
- #
- def getbinaryfile(remotefile, localfile = File.basename(remotefile),
- blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data
- f = nil
- result = nil
- if localfile
- if @resume
- rest_offset = File.size?(localfile)
- f = File.open(localfile, "a")
- else
- rest_offset = nil
- f = File.open(localfile, "w")
- end
- elsif !block_given?
- result = String.new
- end
- begin
- f&.binmode
- retrbinary("RETR #{remotefile}", blocksize, rest_offset) do |data|
- f&.write(data)
- block&.(data)
- result&.concat(data)
- end
- return result
- ensure
- f&.close
- end
- end
-
- #
- # Retrieves +remotefile+ in ASCII (text) mode, storing the result in
- # +localfile+.
- # If +localfile+ is nil, returns retrieved data.
- # If a block is supplied, it is passed the retrieved data one
- # line at a time.
- #
- def gettextfile(remotefile, localfile = File.basename(remotefile),
- &block) # :yield: line
- f = nil
- result = nil
- if localfile
- f = File.open(localfile, "w")
- elsif !block_given?
- result = String.new
- end
- begin
- retrlines("RETR #{remotefile}") do |line, newline|
- l = newline ? line + "\n" : line
- f&.print(l)
- block&.(line, newline)
- result&.concat(l)
- end
- return result
- ensure
- f&.close
- end
- end
-
- #
- # Retrieves +remotefile+ in whatever mode the session is set (text or
- # binary). See #gettextfile and #getbinaryfile.
- #
- def get(remotefile, localfile = File.basename(remotefile),
- blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data
- if @binary
- getbinaryfile(remotefile, localfile, blocksize, &block)
- else
- gettextfile(remotefile, localfile, &block)
- end
- end
-
- #
- # Transfers +localfile+ to the server in binary mode, storing the result in
- # +remotefile+. If a block is supplied, calls it, passing in the transmitted
- # data in +blocksize+ chunks.
- #
- def putbinaryfile(localfile, remotefile = File.basename(localfile),
- blocksize = DEFAULT_BLOCKSIZE, &block) # :yield: data
- if @resume
- begin
- rest_offset = size(remotefile)
- rescue Net::FTPPermError
- rest_offset = nil
- end
- else
- rest_offset = nil
- end
- f = File.open(localfile)
- begin
- f.binmode
- if rest_offset
- storbinary("APPE #{remotefile}", f, blocksize, rest_offset, &block)
- else
- storbinary("STOR #{remotefile}", f, blocksize, rest_offset, &block)
- end
- ensure
- f.close
- end
- end
-
- #
- # Transfers +localfile+ to the server in ASCII (text) mode, storing the result
- # in +remotefile+. If callback or an associated block is supplied, calls it,
- # passing in the transmitted data one line at a time.
- #
- def puttextfile(localfile, remotefile = File.basename(localfile), &block) # :yield: line
- f = File.open(localfile)
- begin
- storlines("STOR #{remotefile}", f, &block)
- ensure
- f.close
- end
- end
-
- #
- # Transfers +localfile+ to the server in whatever mode the session is set
- # (text or binary). See #puttextfile and #putbinaryfile.
- #
- def put(localfile, remotefile = File.basename(localfile),
- blocksize = DEFAULT_BLOCKSIZE, &block)
- if @binary
- putbinaryfile(localfile, remotefile, blocksize, &block)
- else
- puttextfile(localfile, remotefile, &block)
- end
- end
-
- #
- # Sends the ACCT command.
- #
- # This is a less common FTP command, to send account
- # information if the destination host requires it.
- #
- def acct(account)
- cmd = "ACCT " + account
- voidcmd(cmd)
- end
-
- #
- # Returns an array of filenames in the remote directory.
- #
- def nlst(dir = nil)
- cmd = "NLST"
- if dir
- cmd = "#{cmd} #{dir}"
- end
- files = []
- retrlines(cmd) do |line|
- files.push(line)
- end
- return files
- end
-
- #
- # Returns an array of file information in the directory (the output is like
- # `ls -l`). If a block is given, it iterates through the listing.
- #
- def list(*args, &block) # :yield: line
- cmd = "LIST"
- args.each do |arg|
- cmd = "#{cmd} #{arg}"
- end
- lines = []
- retrlines(cmd) do |line|
- lines << line
- end
- if block
- lines.each(&block)
- end
- return lines
- end
- alias ls list
- alias dir list
-
- #
- # MLSxEntry represents an entry in responses of MLST/MLSD.
- # Each entry has the facts (e.g., size, last modification time, etc.)
- # and the pathname.
- #
- class MLSxEntry
- attr_reader :facts, :pathname
-
- def initialize(facts, pathname)
- @facts = facts
- @pathname = pathname
- end
-
- standard_facts = %w(size modify create type unique perm
- lang media-type charset)
- standard_facts.each do |factname|
- define_method factname.gsub(/-/, "_") do
- facts[factname]
- end
- end
-
- #
- # Returns +true+ if the entry is a file (i.e., the value of the type
- # fact is file).
- #
- def file?
- return facts["type"] == "file"
- end
-
- #
- # Returns +true+ if the entry is a directory (i.e., the value of the
- # type fact is dir, cdir, or pdir).
- #
- def directory?
- if /\A[cp]?dir\z/.match(facts["type"])
- return true
- else
- return false
- end
- end
-
- #
- # Returns +true+ if the APPE command may be applied to the file.
- #
- def appendable?
- return facts["perm"].include?(?a)
- end
-
- #
- # Returns +true+ if files may be created in the directory by STOU,
- # STOR, APPE, and RNTO.
- #
- def creatable?
- return facts["perm"].include?(?c)
- end
-
- #
- # Returns +true+ if the file or directory may be deleted by DELE/RMD.
- #
- def deletable?
- return facts["perm"].include?(?d)
- end
-
- #
- # Returns +true+ if the directory may be entered by CWD/CDUP.
- #
- def enterable?
- return facts["perm"].include?(?e)
- end
-
- #
- # Returns +true+ if the file or directory may be renamed by RNFR.
- #
- def renamable?
- return facts["perm"].include?(?f)
- end
-
- #
- # Returns +true+ if the listing commands, LIST, NLST, and MLSD are
- # applied to the directory.
- #
- def listable?
- return facts["perm"].include?(?l)
- end
-
- #
- # Returns +true+ if the MKD command may be used to create a new
- # directory within the directory.
- #
- def directory_makable?
- return facts["perm"].include?(?m)
- end
-
- #
- # Returns +true+ if the objects in the directory may be deleted, or
- # the directory may be purged.
- #
- def purgeable?
- return facts["perm"].include?(?p)
- end
-
- #
- # Returns +true+ if the RETR command may be applied to the file.
- #
- def readable?
- return facts["perm"].include?(?r)
- end
-
- #
- # Returns +true+ if the STOR command may be applied to the file.
- #
- def writable?
- return facts["perm"].include?(?w)
- end
- end
-
- CASE_DEPENDENT_PARSER = ->(value) { value }
- CASE_INDEPENDENT_PARSER = ->(value) { value.downcase }
- DECIMAL_PARSER = ->(value) { value.to_i }
- OCTAL_PARSER = ->(value) { value.to_i(8) }
- TIME_PARSER = ->(value, local = false) {
- unless /\A(?<year>\d{4})(?<month>\d{2})(?<day>\d{2})
- (?<hour>\d{2})(?<min>\d{2})(?<sec>\d{2})
- (?:\.(?<fractions>\d{1,17}))?/x =~ value
- value = value[0, 97] + "..." if value.size > 100
- raise FTPProtoError, "invalid time-val: #{value}"
- end
- usec = ".#{fractions}".to_r * 1_000_000 if fractions
- Time.public_send(local ? :local : :utc, year, month, day, hour, min, sec, usec)
- }
- FACT_PARSERS = Hash.new(CASE_DEPENDENT_PARSER)
- FACT_PARSERS["size"] = DECIMAL_PARSER
- FACT_PARSERS["modify"] = TIME_PARSER
- FACT_PARSERS["create"] = TIME_PARSER
- FACT_PARSERS["type"] = CASE_INDEPENDENT_PARSER
- FACT_PARSERS["unique"] = CASE_DEPENDENT_PARSER
- FACT_PARSERS["perm"] = CASE_INDEPENDENT_PARSER
- FACT_PARSERS["lang"] = CASE_INDEPENDENT_PARSER
- FACT_PARSERS["media-type"] = CASE_INDEPENDENT_PARSER
- FACT_PARSERS["charset"] = CASE_INDEPENDENT_PARSER
- FACT_PARSERS["unix.mode"] = OCTAL_PARSER
- FACT_PARSERS["unix.owner"] = DECIMAL_PARSER
- FACT_PARSERS["unix.group"] = DECIMAL_PARSER
- FACT_PARSERS["unix.ctime"] = TIME_PARSER
- FACT_PARSERS["unix.atime"] = TIME_PARSER
-
- def parse_mlsx_entry(entry)
- facts, pathname = entry.chomp.split(/ /, 2)
- unless pathname
- raise FTPProtoError, entry
- end
- return MLSxEntry.new(
- facts.scan(/(.*?)=(.*?);/).each_with_object({}) {
- |(factname, value), h|
- name = factname.downcase
- h[name] = FACT_PARSERS[name].(value)
- },
- pathname)
- end
- private :parse_mlsx_entry
-
- #
- # Returns data (e.g., size, last modification time, entry type, etc.)
- # about the file or directory specified by +pathname+.
- # If +pathname+ is omitted, the current directory is assumed.
- #
- def mlst(pathname = nil)
- cmd = pathname ? "MLST #{pathname}" : "MLST"
- resp = sendcmd(cmd)
- if !resp.start_with?("250")
- raise FTPReplyError, resp
- end
- line = resp.lines[1]
- unless line
- raise FTPProtoError, resp
- end
- entry = line.sub(/\A(250-| *)/, "")
- return parse_mlsx_entry(entry)
- end
-
- #
- # Returns an array of the entries of the directory specified by
- # +pathname+.
- # Each entry has the facts (e.g., size, last modification time, etc.)
- # and the pathname.
- # If a block is given, it iterates through the listing.
- # If +pathname+ is omitted, the current directory is assumed.
- #
- def mlsd(pathname = nil, &block) # :yield: entry
- cmd = pathname ? "MLSD #{pathname}" : "MLSD"
- entries = []
- retrlines(cmd) do |line|
- entries << parse_mlsx_entry(line)
- end
- if block
- entries.each(&block)
- end
- return entries
- end
-
- #
- # Renames a file on the server.
- #
- def rename(fromname, toname)
- resp = sendcmd("RNFR #{fromname}")
- if !resp.start_with?("3")
- raise FTPReplyError, resp
- end
- voidcmd("RNTO #{toname}")
- end
-
- #
- # Deletes a file on the server.
- #
- def delete(filename)
- resp = sendcmd("DELE #{filename}")
- if resp.start_with?("250")
- return
- elsif resp.start_with?("5")
- raise FTPPermError, resp
- else
- raise FTPReplyError, resp
- end
- end
-
- #
- # Changes the (remote) directory.
- #
- def chdir(dirname)
- if dirname == ".."
- begin
- voidcmd("CDUP")
- return
- rescue FTPPermError => e
- if e.message[0, 3] != "500"
- raise e
- end
- end
- end
- cmd = "CWD #{dirname}"
- voidcmd(cmd)
- end
-
- def get_body(resp) # :nodoc:
- resp.slice(/\A[0-9a-zA-Z]{3} (.*)$/, 1)
- end
- private :get_body
-
- #
- # Returns the size of the given (remote) filename.
- #
- def size(filename)
- with_binary(true) do
- resp = sendcmd("SIZE #{filename}")
- if !resp.start_with?("213")
- raise FTPReplyError, resp
- end
- return get_body(resp).to_i
- end
- end
-
- #
- # Returns the last modification time of the (remote) file. If +local+ is
- # +true+, it is returned as a local time, otherwise it's a UTC time.
- #
- def mtime(filename, local = false)
- return TIME_PARSER.(mdtm(filename), local)
- end
-
- #
- # Creates a remote directory.
- #
- def mkdir(dirname)
- resp = sendcmd("MKD #{dirname}")
- return parse257(resp)
- end
-
- #
- # Removes a remote directory.
- #
- def rmdir(dirname)
- voidcmd("RMD #{dirname}")
- end
-
- #
- # Returns the current remote directory.
- #
- def pwd
- resp = sendcmd("PWD")
- return parse257(resp)
- end
- alias getdir pwd
-
- #
- # Returns system information.
- #
- def system
- resp = sendcmd("SYST")
- if !resp.start_with?("215")
- raise FTPReplyError, resp
- end
- return get_body(resp)
- end
-
- #
- # Aborts the previous command (ABOR command).
- #
- def abort
- line = "ABOR" + CRLF
- print "put: ABOR\n" if @debug_mode
- @sock.send(line, Socket::MSG_OOB)
- resp = getmultiline
- unless ["426", "226", "225"].include?(resp[0, 3])
- raise FTPProtoError, resp
- end
- return resp
- end
-
- #
- # Returns the status (STAT command).
- #
- # pathname:: when stat is invoked with pathname as a parameter it acts like
- # list but a lot faster and over the same tcp session.
- #
- def status(pathname = nil)
- line = pathname ? "STAT #{pathname}" : "STAT"
- if /[\r\n]/ =~ line
- raise ArgumentError, "A line must not contain CR or LF"
- end
- print "put: #{line}\n" if @debug_mode
- @sock.send(line + CRLF, Socket::MSG_OOB)
- return getresp
- end
-
- #
- # Returns the raw last modification time of the (remote) file in the format
- # "YYYYMMDDhhmmss" (MDTM command).
- #
- # Use +mtime+ if you want a parsed Time instance.
- #
- def mdtm(filename)
- resp = sendcmd("MDTM #{filename}")
- if resp.start_with?("213")
- return get_body(resp)
- end
- end
-
- #
- # Issues the HELP command.
- #
- def help(arg = nil)
- cmd = "HELP"
- if arg
- cmd = cmd + " " + arg
- end
- sendcmd(cmd)
- end
-
- #
- # Exits the FTP session.
- #
- def quit
- voidcmd("QUIT")
- end
-
- #
- # Issues a NOOP command.
- #
- # Does nothing except return a response.
- #
- def noop
- voidcmd("NOOP")
- end
-
- #
- # Issues a SITE command.
- #
- def site(arg)
- cmd = "SITE " + arg
- voidcmd(cmd)
- end
-
- #
- # Issues a FEAT command
- #
- # Returns an array of supported optional features
- #
- def features
- resp = sendcmd("FEAT")
- if !resp.start_with?("211")
- raise FTPReplyError, resp
- end
-
- feats = []
- resp.split("\n").each do |line|
- next if !line.start_with?(' ') # skip status lines
-
- feats << line.strip
- end
-
- return feats
- end
-
- #
- # Issues an OPTS command
- # - name Should be the name of the option to set
- # - params is any optional parameters to supply with the option
- #
- # example: option('UTF8', 'ON') => 'OPTS UTF8 ON'
- #
- def option(name, params = nil)
- cmd = "OPTS #{name}"
- cmd += " #{params}" if params
-
- voidcmd(cmd)
- end
-
- #
- # Closes the connection. Further operations are impossible until you open
- # a new connection with #connect.
- #
- def close
- if @sock and not @sock.closed?
- begin
- @sock.shutdown(Socket::SHUT_WR) rescue nil
- orig, self.read_timeout = self.read_timeout, 3
- @sock.read rescue nil
- ensure
- @sock.close
- self.read_timeout = orig
- end
- end
- end
-
- #
- # Returns +true+ if and only if the connection is closed.
- #
- def closed?
- @sock == nil or @sock.closed?
- end
-
- # handler for response code 227
- # (Entering Passive Mode (h1,h2,h3,h4,p1,p2))
- #
- # Returns host and port.
- def parse227(resp) # :nodoc:
- if !resp.start_with?("227")
- raise FTPReplyError, resp
- end
- if m = /\((?<host>\d+(?:,\d+){3}),(?<port>\d+,\d+)\)/.match(resp)
- return parse_pasv_ipv4_host(m["host"]), parse_pasv_port(m["port"])
- else
- raise FTPProtoError, resp
- end
- end
- private :parse227
-
- # handler for response code 228
- # (Entering Long Passive Mode)
- #
- # Returns host and port.
- def parse228(resp) # :nodoc:
- if !resp.start_with?("228")
- raise FTPReplyError, resp
- end
- if m = /\(4,4,(?<host>\d+(?:,\d+){3}),2,(?<port>\d+,\d+)\)/.match(resp)
- return parse_pasv_ipv4_host(m["host"]), parse_pasv_port(m["port"])
- elsif m = /\(6,16,(?<host>\d+(?:,\d+){15}),2,(?<port>\d+,\d+)\)/.match(resp)
- return parse_pasv_ipv6_host(m["host"]), parse_pasv_port(m["port"])
- else
- raise FTPProtoError, resp
- end
- end
- private :parse228
-
- def parse_pasv_ipv4_host(s)
- return s.tr(",", ".")
- end
- private :parse_pasv_ipv4_host
-
- def parse_pasv_ipv6_host(s)
- return s.split(/,/).map { |i|
- "%02x" % i.to_i
- }.each_slice(2).map(&:join).join(":")
- end
- private :parse_pasv_ipv6_host
-
- def parse_pasv_port(s)
- return s.split(/,/).map(&:to_i).inject { |x, y|
- (x << 8) + y
- }
- end
- private :parse_pasv_port
-
- # handler for response code 229
- # (Extended Passive Mode Entered)
- #
- # Returns host and port.
- def parse229(resp) # :nodoc:
- if !resp.start_with?("229")
- raise FTPReplyError, resp
- end
- if m = /\((?<d>[!-~])\k<d>\k<d>(?<port>\d+)\k<d>\)/.match(resp)
- return @bare_sock.remote_address.ip_address, m["port"].to_i
- else
- raise FTPProtoError, resp
- end
- end
- private :parse229
-
- # handler for response code 257
- # ("PATHNAME" created)
- #
- # Returns host and port.
- def parse257(resp) # :nodoc:
- if !resp.start_with?("257")
- raise FTPReplyError, resp
- end
- return resp.slice(/"(([^"]|"")*)"/, 1).to_s.gsub(/""/, '"')
- end
- private :parse257
-
- # :stopdoc:
- class NullSocket
- def read_timeout=(sec)
- end
-
- def closed?
- true
- end
-
- def close
- end
-
- def method_missing(mid, *args)
- raise FTPConnectionError, "not connected"
- end
- end
-
- class BufferedSocket < BufferedIO
- [:local_address, :remote_address, :addr, :peeraddr, :send, :shutdown].each do |method|
- define_method(method) { |*args|
- @io.__send__(method, *args)
- }
- end
-
- def read(len = nil)
- if len
- s = super(len, String.new, true)
- return s.empty? ? nil : s
- else
- result = String.new
- while s = super(DEFAULT_BLOCKSIZE, String.new, true)
- break if s.empty?
- result << s
- end
- return result
- end
- end
-
- def gets
- line = readuntil("\n", true)
- return line.empty? ? nil : line
- end
-
- def readline
- line = gets
- if line.nil?
- raise EOFError, "end of file reached"
- end
- return line
- end
- end
-
- if defined?(OpenSSL::SSL::SSLSocket)
- class BufferedSSLSocket < BufferedSocket
- def initialize(*args, **options)
- super
- @is_shutdown = false
- end
-
- def shutdown(*args)
- # SSL_shutdown() will be called from SSLSocket#close, and
- # SSL_shutdown() will send the "close notify" alert to the peer,
- # so shutdown(2) should not be called.
- @is_shutdown = true
- end
-
- def send(mesg, flags, dest = nil)
- # Ignore flags and dest.
- @io.write(mesg)
- end
-
- private
-
- def rbuf_fill
- if @is_shutdown
- raise EOFError, "shutdown has been called"
- else
- super
- end
- end
- end
- end
- # :startdoc:
- end
-end
-
-
-# Documentation comments:
-# - sourced from pickaxe and nutshell, with improvements (hopefully)
diff --git a/lib/net/net-ftp.gemspec b/lib/net/net-ftp.gemspec
deleted file mode 100644
index f5eb098135..0000000000
--- a/lib/net/net-ftp.gemspec
+++ /dev/null
@@ -1,36 +0,0 @@
-# frozen_string_literal: true
-
-name = File.basename(__FILE__, ".gemspec")
-version = ["lib", Array.new(name.count("-"), "..").join("/")].find do |dir|
- break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line|
- /^\s*VERSION\s*=\s*"(.*)"/ =~ line and break $1
- end rescue nil
-end
-
-Gem::Specification.new do |spec|
- spec.name = name
- spec.version = version
- spec.authors = ["Shugo Maeda"]
- spec.email = ["shugo@ruby-lang.org"]
-
- spec.summary = %q{Support for the File Transfer Protocol.}
- spec.description = %q{Support for the File Transfer Protocol.}
- spec.homepage = "https://github.com/ruby/net-ftp"
- spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
- spec.licenses = ["Ruby", "BSD-2-Clause"]
-
- spec.metadata["homepage_uri"] = spec.homepage
- spec.metadata["source_code_uri"] = spec.homepage
-
- # Specify which files should be added to the gem when it is released.
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
- spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
- `git ls-files -z 2>/dev/null`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
- end
- spec.bindir = "exe"
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
- spec.require_paths = ["lib"]
-
- spec.add_dependency "net-protocol"
- spec.add_dependency "time"
-end
diff --git a/misc/expand_tabs.rb b/misc/expand_tabs.rb
index 630e012996..3c5842b9a0 100755
--- a/misc/expand_tabs.rb
+++ b/misc/expand_tabs.rb
@@ -79,7 +79,6 @@ DEFAULT_GEM_LIBS = %w[
irb
logger
mutex_m
- net-ftp
net-http
net-imap
net-pop
diff --git a/test/net/ftp/test_buffered_socket.rb b/test/net/ftp/test_buffered_socket.rb
deleted file mode 100644
index 875c53f4e0..0000000000
--- a/test/net/ftp/test_buffered_socket.rb
+++ /dev/null
@@ -1,48 +0,0 @@
-# frozen_string_literal: true
-
-require "net/ftp"
-require "test/unit"
-require "ostruct"
-require "stringio"
-
-class BufferedSocketTest < Test::Unit::TestCase
- def test_gets_empty
- sock = create_buffered_socket("")
- assert_equal(nil, sock.gets)
- end
-
- def test_gets_one_line
- sock = create_buffered_socket("foo\n")
- assert_equal("foo\n", sock.gets)
- end
-
- def test_gets_one_line_without_term
- sock = create_buffered_socket("foo")
- assert_equal("foo", sock.gets)
- end
-
- def test_gets_two_lines
- sock = create_buffered_socket("foo\nbar\n")
- assert_equal("foo\n", sock.gets)
- assert_equal("bar\n", sock.gets)
- end
-
- def test_gets_two_lines_without_term
- sock = create_buffered_socket("foo\nbar")
- assert_equal("foo\n", sock.gets)
- assert_equal("bar", sock.gets)
- end
-
- def test_read_nil
- sock = create_buffered_socket("foo\nbar")
- assert_equal("foo\nbar", sock.read)
- assert_equal("", sock.read)
- end
-
- private
-
- def create_buffered_socket(s)
- io = StringIO.new(s)
- return Net::FTP::BufferedSocket.new(io)
- end
-end
diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb
deleted file mode 100644
index 5e0eb3dfe6..0000000000
--- a/test/net/ftp/test_ftp.rb
+++ /dev/null
@@ -1,2636 +0,0 @@
-# frozen_string_literal: true
-
-require "net/ftp"
-require "test/unit"
-require "ostruct"
-require "stringio"
-require "tempfile"
-require "tmpdir"
-
-class FTPTest < Test::Unit::TestCase
- SERVER_NAME = "localhost"
- SERVER_ADDR =
- begin
- Addrinfo.getaddrinfo(SERVER_NAME, 0, nil, :STREAM)[0].ip_address
- rescue SocketError
- "127.0.0.1"
- end
- CA_FILE = File.expand_path("../fixtures/cacert.pem", __dir__)
- SERVER_KEY = File.expand_path("../fixtures/server.key", __dir__)
- SERVER_CERT = File.expand_path("../fixtures/server.crt", __dir__)
-
- def setup
- @thread = nil
- @default_passive = Net::FTP.default_passive
- Net::FTP.default_passive = false
- end
-
- def teardown
- Net::FTP.default_passive = @default_passive
- if @thread
- @thread.join
- end
- end
-
- def test_not_connected
- ftp = Net::FTP.new
- assert_raise(Net::FTPConnectionError) do
- ftp.quit
- end
- end
-
- def test_closed_when_not_connected
- ftp = Net::FTP.new
- assert_equal(true, ftp.closed?)
- assert_nothing_raised(Net::FTPConnectionError) do
- ftp.close
- end
- end
-
- def test_connect_fail
- server = create_ftp_server { |sock|
- sock.print("421 Service not available, closing control connection.\r\n")
- }
- begin
- ftp = Net::FTP.new
- assert_raise(Net::FTPTempError){ ftp.connect(SERVER_ADDR, server.port) }
- ensure
- ftp.close if ftp
- server.close
- end
- end
-
- def test_parse227
- ftp = Net::FTP.new
- host, port = ftp.send(:parse227, "227 Entering Passive Mode (192,168,0,1,12,34)")
- assert_equal("192.168.0.1", host)
- assert_equal(3106, port)
- assert_raise(Net::FTPReplyError) do
- ftp.send(:parse227, "500 Syntax error")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse227, "227 Entering Passive Mode")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse227, "227 Entering Passive Mode (192,168,0,1,12,34,56)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse227, "227 Entering Passive Mode (192,168,0,1)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse227, "227 ) foo bar (")
- end
- end
-
- def test_parse228
- ftp = Net::FTP.new
- host, port = ftp.send(:parse228, "228 Entering Long Passive Mode (4,4,192,168,0,1,2,12,34)")
- assert_equal("192.168.0.1", host)
- assert_equal(3106, port)
- host, port = ftp.send(:parse228, "228 Entering Long Passive Mode (6,16,16,128,0,0,0,0,0,0,0,8,8,0,32,12,65,122,2,12,34)")
- assert_equal("1080:0000:0000:0000:0008:0800:200c:417a", host)
- assert_equal(3106, port)
- assert_raise(Net::FTPReplyError) do
- ftp.send(:parse228, "500 Syntax error")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse228, "228 Entering Passive Mode")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse228, "228 Entering Long Passive Mode (6,4,192,168,0,1,2,12,34)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse228, "228 Entering Long Passive Mode (4,4,192,168,0,1,3,12,34,56)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse228, "228 Entering Long Passive Mode (4,16,16,128,0,0,0,0,0,0,0,8,8,0,32,12,65,122,2,12,34)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse228, "228 Entering Long Passive Mode (6,16,16,128,0,0,0,0,0,0,0,8,8,0,32,12,65,122,3,12,34,56)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse228, "228 Entering Long Passive Mode (6,16,16,128,0,0,0,0,0,0,0,8,8,0,32,12,65,122,2,12,34,56)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse227, "227 ) foo bar (")
- end
- end
-
- def test_parse229
- ftp = Net::FTP.new
- sock = OpenStruct.new
- sock.remote_address = OpenStruct.new
- sock.remote_address.ip_address = "1080:0000:0000:0000:0008:0800:200c:417a"
- ftp.instance_variable_set(:@bare_sock, sock)
- host, port = ftp.send(:parse229, "229 Entering Passive Mode (|||3106|)")
- assert_equal("1080:0000:0000:0000:0008:0800:200c:417a", host)
- assert_equal(3106, port)
- host, port = ftp.send(:parse229, "229 Entering Passive Mode (!!!3106!)")
- assert_equal("1080:0000:0000:0000:0008:0800:200c:417a", host)
- assert_equal(3106, port)
- host, port = ftp.send(:parse229, "229 Entering Passive Mode (~~~3106~)")
- assert_equal("1080:0000:0000:0000:0008:0800:200c:417a", host)
- assert_equal(3106, port)
- assert_raise(Net::FTPReplyError) do
- ftp.send(:parse229, "500 Syntax error")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse229, "229 Entering Passive Mode")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse229, "229 Entering Passive Mode (|!!3106!)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse229, "229 Entering Passive Mode ( 3106 )")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse229, "229 Entering Passive Mode (\x7f\x7f\x7f3106\x7f)")
- end
- assert_raise(Net::FTPProtoError) do
- ftp.send(:parse229, "229 ) foo bar (")
- end
- end
-
- def test_parse_pasv_port
- ftp = Net::FTP.new
- assert_equal(12, ftp.send(:parse_pasv_port, "12"))
- assert_equal(3106, ftp.send(:parse_pasv_port, "12,34"))
- assert_equal(795192, ftp.send(:parse_pasv_port, "12,34,56"))
- assert_equal(203569230, ftp.send(:parse_pasv_port, "12,34,56,78"))
- end
-
- def test_login
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_login_fail1
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("502 Command not implemented.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- assert_raise(Net::FTPPermError){ ftp.login }
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_login_fail2
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("530 Not logged in.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- assert_raise(Net::FTPPermError){ ftp.login }
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_implicit_login
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("332 Need account for login.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new(SERVER_ADDR,
- port: server.port,
- username: "foo",
- password: "bar",
- account: "baz")
- assert_equal("USER foo\r\n", commands.shift)
- assert_equal("PASS bar\r\n", commands.shift)
- assert_equal("ACCT baz\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_s_open
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- Net::FTP.open(SERVER_ADDR, port: server.port, username: "anonymous") do
- end
- assert_equal("USER anonymous\r\n", commands.shift)
- assert_equal("PASS anonymous@\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- server.close
- end
- end
-
- def test_s_new_timeout_options
- ftp = Net::FTP.new
- assert_equal(nil, ftp.open_timeout)
- assert_equal(60, ftp.read_timeout)
-
- ftp = Net::FTP.new(nil, open_timeout: 123, read_timeout: 234)
- assert_equal(123, ftp.open_timeout)
- assert_equal(234, ftp.read_timeout)
- end
-
- # TODO: How can we test open_timeout? sleep before accept cannot delay
- # connections.
- def _test_open_timeout_exceeded
- commands = []
- server = create_ftp_server(0.2) { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.open_timeout = 0.1
- ftp.connect(SERVER_ADDR, server.port)
- assert_raise(Net::OpenTimeout) do
- ftp.login
- end
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_read_timeout_exceeded
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sleep(0.1)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sleep(2.0) # Net::ReadTimeout
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sleep(0.1)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout = 0.4
- ftp.connect(SERVER_ADDR, server.port)
- assert_raise(Net::ReadTimeout) do
- ftp.login
- end
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_read_timeout_not_exceeded
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sleep(0.1)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sleep(0.1)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sleep(0.1)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout = 1.0
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close
- assert_equal(1.0, ftp.read_timeout)
- end
- ensure
- server.close
- end
- end
-
- def test_list_read_timeout_exceeded
- commands = []
- list_lines = [
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 foo.txt",
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 bar.txt",
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 baz.txt"
- ]
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Here comes the directory listing.\r\n")
- begin
- conn = TCPSocket.new(host, port)
- list_lines.each_with_index do |l, i|
- if i == 1
- sleep(0.5)
- else
- sleep(0.1)
- end
- conn.print(l, "\r\n")
- end
- rescue Errno::EPIPE, Errno::ECONNRESET
- ensure
- assert_nil($!)
- conn.close
- end
- sock.print("226 Directory send OK.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout = 0.2
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_raise(Net::ReadTimeout) do
- ftp.list
- end
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("LIST\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_list_read_timeout_not_exceeded
- commands = []
- list_lines = [
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 foo.txt",
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 bar.txt",
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 baz.txt"
- ]
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Here comes the directory listing.\r\n")
- conn = TCPSocket.new(host, port)
- list_lines.each do |l|
- sleep(0.1)
- conn.print(l, "\r\n")
- end
- conn.close
- sock.print("226 Directory send OK.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout = 1.0
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(list_lines, ftp.list)
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("LIST\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_list_fail
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("553 Requested action not taken.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- [host, port]
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_raise(Net::FTPPermError){ ftp.list }
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("LIST\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_open_data_port_fail_no_leak
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- sock.print("421 Service not available, closing control connection.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_raise(Net::FTPTempError){ ftp.list }
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_retrbinary_read_timeout_exceeded
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- sleep(0.1)
- conn.print(binary_data[0,1024])
- sleep(1.0)
- conn.print(binary_data[1024, 1024]) rescue nil # may raise EPIPE or something
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout = 0.5
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = String.new
- assert_raise(Net::ReadTimeout) do
- ftp.retrbinary("RETR foo", 1024) do |s|
- buf << s
- end
- end
- assert_equal(1024, buf.bytesize)
- assert_equal(binary_data[0, 1024], buf)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close unless ftp.closed?
- end
- ensure
- server.close
- end
- end
-
- def test_retrbinary_read_timeout_not_exceeded
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- binary_data.scan(/.{1,1024}/nm) do |s|
- sleep(0.2)
- conn.print(s)
- end
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout = 1.0
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = String.new
- ftp.retrbinary("RETR foo", 1024) do |s|
- buf << s
- end
- assert_equal(binary_data.bytesize, buf.bytesize)
- assert_equal(binary_data, buf)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_retrbinary_fail
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("550 Requested action not taken.\r\n")
- [host, port]
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_raise(Net::FTPPermError){ ftp.retrbinary("RETR foo", 1024) }
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_getbinaryfile
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- binary_data.scan(/.{1,1024}/nm) do |s|
- conn.print(s)
- end
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = ftp.getbinaryfile("foo", nil)
- assert_equal(binary_data, buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_getbinaryfile_empty
- commands = []
- binary_data = ""
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = ftp.getbinaryfile("foo", nil)
- assert_equal(binary_data, buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_getbinaryfile_with_filename_and_block
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- binary_data.scan(/.{1,1024}/nm) do |s|
- conn.print(s)
- end
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- Tempfile.create("foo", external_encoding: "ASCII-8BIT") do |f|
- f.binmode
- buf = String.new
- res = ftp.getbinaryfile("foo", f.path) { |s|
- buf << s
- }
- assert_equal(nil, res)
- assert_equal(binary_data, buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_equal(binary_data, f.read)
- end
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_getbinaryfile_error
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- sock.print("450 No Dice\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.passive = true
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_raise(Net::FTPTempError) {ftp.getbinaryfile("foo", nil)}
- assert_match(/\A(PASV|EPSV)\r\n/, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_storbinary
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- stored_data = nil
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo\r\n")
- conn = TCPSocket.new(host, port)
- stored_data = conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.storbinary("STOR foo", StringIO.new(binary_data), 1024)
- assert_equal(binary_data, stored_data)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("STOR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_storbinary_fail
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("452 Requested file action aborted.\r\n")
- [host, port]
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_raise(Net::FTPTempError){ ftp.storbinary("STOR foo", StringIO.new(binary_data), 1024) }
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("STOR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_retrlines
- commands = []
- text_data = <<EOF.gsub(/\n/, "\r\n")
-foo
-bar
-baz
-EOF
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening TEXT mode data connection for foo (#{text_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- text_data.each_line do |l|
- conn.print(l)
- end
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = String.new
- ftp.retrlines("RETR foo") do |line|
- buf << line + "\r\n"
- end
- assert_equal(text_data.bytesize, buf.bytesize)
- assert_equal(text_data, buf)
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_gettextfile
- commands = []
- text_data = <<EOF.gsub(/\n/, "\r\n")
-foo
-bar
-baz
-EOF
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening TEXT mode data connection for foo (#{text_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- text_data.each_line do |l|
- conn.print(l)
- end
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = ftp.gettextfile("foo", nil)
- assert_equal(text_data.gsub(/\r\n/, "\n"), buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_gettextfile_with_filename_and_block
- commands = []
- text_data = <<EOF.gsub(/\n/, "\r\n")
-foo
-bar
-baz
-EOF
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening TEXT mode data connection for foo (#{text_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- text_data.each_line do |l|
- conn.print(l)
- end
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- Tempfile.create("foo", external_encoding: "ascii-8bit") do |f|
- buf = String.new
- res = ftp.gettextfile("foo", f.path) { |s|
- buf << s << "\n"
- }
- assert_equal(nil, res)
- assert_equal(text_data.gsub(/\r\n/, "\n"), buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_equal(buf, f.read)
- end
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_getbinaryfile_in_list
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join
- list_lines = [
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 foo.txt",
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 bar.txt",
- "-rw-r--r-- 1 0 0 0 Mar 30 11:22 baz.bin"
- ]
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Here comes the directory listing.\r\n")
- conn = TCPSocket.new(host, port)
- list_lines.each_with_index do |l, i|
- conn.print(l, "\r\n")
- end
- conn.close
- sock.print("226 Directory send OK.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- conn.print(binary_data)
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.list do |line|
- file = line.slice(/(\S*\.bin)\z/)
- if file
- data = ftp.getbinaryfile(file, nil)
- assert_equal(binary_data, data)
- end
- end
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("LIST\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR baz.bin\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_abort
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("225 No transfer to ABOR.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.abort
- assert_equal("ABOR\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_status
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("211 End of status\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.status
- assert_equal("STAT\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_status_path
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("213 End of status\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.status "/"
- assert_equal("STAT /\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_pathnames
- require 'pathname'
-
- commands = []
- server = create_ftp_server(0.2) { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("257 'foo' directory created.\r\n")
- commands.push(sock.gets)
- sock.print("250 CWD command successful.\r\n")
- commands.push(sock.gets)
- sock.print("250 CWD command successful.\r\n")
- commands.push(sock.gets)
- sock.print("250 RMD command successful.\r\n")
- commands.push(sock.gets)
- sock.print("213 test.txt Fri, 11 Jan 2013 11:20:41 -0500.\r\n")
- commands.push(sock.gets)
- sock.print("213 test.txt 16.\r\n")
- commands.push(sock.gets)
- sock.print("350 File exists, ready for destination name\r\n")
- commands.push(sock.gets)
- sock.print("250 RNTO command successful.\r\n")
- commands.push(sock.gets)
- sock.print("250 DELE command successful.\r\n")
- }
-
- begin
- begin
- dir = Pathname.new("foo")
- file = Pathname.new("test.txt")
- file2 = Pathname.new("test2.txt")
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- ftp.mkdir(dir)
- ftp.chdir(dir)
- ftp.chdir("..")
- ftp.rmdir(dir)
- ftp.mdtm(file)
- ftp.size(file)
- ftp.rename(file, file2)
- ftp.delete(file)
-
- # TODO: These commented tests below expose the error but don't test anything:
- # TypeError: no implicit conversion of Pathname into String
- # ftp.nlst(dir)
- # ftp.putbinaryfile(Pathname.new("/etc/hosts"), file2)
- # ftp.puttextfile(Pathname.new("/etc/hosts"), file2)
- # ftp.gettextfile(Pathname.new("/etc/hosts"), file2)
- # ftp.getbinaryfile(Pathname.new("/etc/hosts"), file2)
- # ftp.list(dir, dir, dir)
-
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_match(/\ATYPE /, commands.shift)
- assert_match(/\AMKD /, commands.shift)
- assert_match(/\ACWD /, commands.shift)
- assert_match(/\ACDUP/, commands.shift)
- assert_match(/\ARMD /, commands.shift)
- assert_match(/\AMDTM /, commands.shift)
- assert_match(/\ASIZE /, commands.shift)
- assert_match(/\ARNFR /, commands.shift)
- assert_match(/\ARNTO /, commands.shift)
- assert_match(/\ADELE /, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_getmultiline
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- sock.print("123- foo\r\n")
- sock.print("bar\r\n")
- sock.print(" 123 baz\r\n")
- sock.print("123 quux\r\n")
- sock.print("123 foo\r\n")
- sock.print("foo\r\n")
- sock.print("\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- assert_equal("123- foo\nbar\n 123 baz\n123 quux\n",
- ftp.send(:getmultiline))
- assert_equal("123 foo\n", ftp.send(:getmultiline))
- assert_equal("foo\n", ftp.send(:getmultiline))
- assert_equal("\n", ftp.send(:getmultiline))
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_size
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("213 12345\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- assert_equal(12345, ftp.size("foo.txt"))
- assert_match("SIZE foo.txt\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_mdtm
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("213 20150910161739\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- assert_equal("20150910161739", ftp.mdtm("foo.txt"))
- assert_match("MDTM foo.txt\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_mtime
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("213 20150910161739\r\n")
- commands.push(sock.gets)
- sock.print("213 20150910161739\r\n")
- commands.push(sock.gets)
- sock.print("213 20150910161739.123456\r\n")
- commands.push(sock.gets)
- sock.print("213 20150910161739.123\r\n")
- commands.push(sock.gets)
- sock.print("213 20150910161739.123456789\r\n")
- commands.push(sock.gets)
- sock.print("213 2015091016173\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- assert_equal(Time.utc(2015, 9, 10, 16, 17, 39), ftp.mtime("foo.txt"))
- assert_equal(Time.local(2015, 9, 10, 16, 17, 39),
- ftp.mtime("foo.txt", true))
- assert_equal(Time.utc(2015, 9, 10, 16, 17, 39, 123456),
- ftp.mtime("bar.txt"))
- assert_equal(Time.utc(2015, 9, 10, 16, 17, 39, 123000),
- ftp.mtime("bar.txt"))
- assert_equal(Time.utc(2015, 9, 10, 16, 17, 39,
- Rational(123456789, 1000)),
- ftp.mtime("bar.txt"))
- assert_raise(Net::FTPProtoError) do
- ftp.mtime("quux.txt")
- end
- assert_match("MDTM foo.txt\r\n", commands.shift)
- assert_match("MDTM foo.txt\r\n", commands.shift)
- assert_match("MDTM bar.txt\r\n", commands.shift)
- assert_match("MDTM bar.txt\r\n", commands.shift)
- assert_match("MDTM bar.txt\r\n", commands.shift)
- assert_match("MDTM quux.txt\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_system
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("215 UNIX Type: L8\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- assert_equal("UNIX Type: L8", ftp.system)
- assert_match("SYST\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_features
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("211-Features\r\n")
- sock.print(" LANG EN*\r\n")
- sock.print(" UTF8\r\n")
- sock.print("211 End\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- assert_equal(['LANG EN*', 'UTF8'], ftp.features)
- assert_equal("FEAT\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_features_not_implemented
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("502 Not Implemented\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- assert_raise(Net::FTPPermError) do
- ftp.features
- end
- assert_equal("FEAT\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
-
- end
-
- def test_option
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp)\r\n")
- commands.push(sock.gets)
- sock.print("200 OPTS UTF8 command successful\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.option("UTF8", "ON")
- assert_equal("OPTS UTF8 ON\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_option_not_implemented
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp)\r\n")
- commands.push(sock.gets)
- sock.print("502 Not implemented\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.connect(SERVER_ADDR, server.port)
- assert_raise(Net::FTPPermError) do
- ftp.option("UTF8", "ON")
- end
- assert_equal("OPTS UTF8 ON\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_mlst
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("250- Listing foo\r\n")
- sock.print(" Type=file;Unique=FC00U1E554A;Size=1234567;Modify=20131220035929;Perm=r;Unix.mode=0644;Unix.owner=122;Unix.group=0;Unix.ctime=20131220120140;Unix.atime=20131220131139; /foo\r\n")
- sock.print("250 End\r\n")
- commands.push(sock.gets)
- sock.print("250 Malformed response\r\n")
- commands.push(sock.gets)
- sock.print("250- Listing foo\r\n")
- sock.print("\r\n")
- sock.print("250 End\r\n")
- commands.push(sock.gets)
- sock.print("250- Listing foo\r\n")
- sock.print(" abc /foo\r\n")
- sock.print("250 End\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- entry = ftp.mlst("foo")
- assert_equal("/foo", entry.pathname)
- assert_equal("file", entry.facts["type"])
- assert_equal("FC00U1E554A", entry.facts["unique"])
- assert_equal(1234567, entry.facts["size"])
- assert_equal("r", entry.facts["perm"])
- assert_equal(0644, entry.facts["unix.mode"])
- assert_equal(122, entry.facts["unix.owner"])
- assert_equal(0, entry.facts["unix.group"])
- modify = entry.facts["modify"]
- assert_equal(2013, modify.year)
- assert_equal(12, modify.month)
- assert_equal(20, modify.day)
- assert_equal(3, modify.hour)
- assert_equal(59, modify.min)
- assert_equal(29, modify.sec)
- assert_equal(true, modify.utc?)
- ctime = entry.facts["unix.ctime"]
- assert_equal(12, ctime.hour)
- assert_equal(1, ctime.min)
- assert_equal(40, ctime.sec)
- atime = entry.facts["unix.atime"]
- assert_equal(13, atime.hour)
- assert_equal(11, atime.min)
- assert_equal(39, atime.sec)
- assert_match("MLST foo\r\n", commands.shift)
- assert_raise(Net::FTPProtoError) do
- ftp.mlst("foo")
- end
- assert_match("MLST foo\r\n", commands.shift)
- assert_raise(Net::FTPProtoError) do
- ftp.mlst("foo")
- end
- assert_match("MLST foo\r\n", commands.shift)
- entry = ftp.mlst("foo")
- assert_equal("/foo", entry.pathname)
- assert_match("MLST foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_mlsd
- commands = []
- entry_lines = [
- "Type=file;Unique=FC00U1E554A;Size=1234567;Modify=20131220035929.123456;Perm=r; foo bar",
- "Type=cdir;Unique=FC00U1E554B;Modify=20131220035929;Perm=flcdmpe; .",
- "Type=pdir;Unique=FC00U1E554C;Modify=20131220035929;Perm=flcdmpe; ..",
- ]
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Here comes the directory listing.\r\n")
- begin
- conn = TCPSocket.new(host, port)
- entry_lines.each do |l|
- conn.print(l, "\r\n")
- end
- rescue Errno::EPIPE
- ensure
- assert_nil($!)
- conn.close
- end
- sock.print("226 Directory send OK.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- entries = ftp.mlsd("/")
- assert_equal(3, entries.size)
- assert_equal("foo bar", entries[0].pathname)
- assert_equal(".", entries[1].pathname)
- assert_equal("..", entries[2].pathname)
- assert_equal("file", entries[0].facts["type"])
- assert_equal("cdir", entries[1].facts["type"])
- assert_equal("pdir", entries[2].facts["type"])
- assert_equal("flcdmpe", entries[1].facts["perm"])
- modify = entries[0].facts["modify"]
- assert_equal(2013, modify.year)
- assert_equal(12, modify.month)
- assert_equal(20, modify.day)
- assert_equal(3, modify.hour)
- assert_equal(59, modify.min)
- assert_equal(29, modify.sec)
- assert_equal(123456, modify.usec)
- assert_equal(true, modify.utc?)
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_match("MLSD /\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_parse257
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- assert_equal('/foo/bar',
- ftp.send(:parse257, '257 "/foo/bar" directory created'))
- assert_equal('/foo/bar"baz',
- ftp.send(:parse257, '257 "/foo/bar""baz" directory created'))
- assert_equal('/foo/x"y"z',
- ftp.send(:parse257, '257 "/foo/x""y""z" directory created'))
- assert_equal('/foo/bar',
- ftp.send(:parse257, '257 "/foo/bar" "comment"'))
- assert_equal('',
- ftp.send(:parse257, '257 "" directory created'))
- assert_equal('',
- ftp.send(:parse257, '257 directory created'))
- assert_raise(Net::FTPReplyError) do
- ftp.send(:parse257, "500 Syntax error")
- end
- end
-
- def test_putline_reject_crlf
- ftp = Net::FTP.new
- assert_raise(ArgumentError) do
- ftp.send(:putline, "\r")
- end
- assert_raise(ArgumentError) do
- ftp.send(:putline, "\n")
- end
- end
-
- if defined?(OpenSSL::SSL)
- def test_tls_unknown_ca
- assert_raise(OpenSSL::SSL::SSLError) do
- tls_test do |port|
- begin
- Net::FTP.new(SERVER_NAME,
- :port => port,
- :ssl => true)
- rescue SystemCallError
- skip $!
- end
- end
- end
- end
-
- def test_tls_with_ca_file
- assert_nothing_raised do
- tls_test do |port|
- begin
- Net::FTP.new(SERVER_NAME,
- :port => port,
- :ssl => { :ca_file => CA_FILE })
- rescue SystemCallError
- skip $!
- end
- end
- end
- end
-
- def test_tls_verify_none
- assert_nothing_raised do
- tls_test do |port|
- Net::FTP.new(SERVER_ADDR,
- :port => port,
- :ssl => { :verify_mode => OpenSSL::SSL::VERIFY_NONE })
- end
- end
- end
-
- def test_tls_post_connection_check
- assert_raise(OpenSSL::SSL::SSLError) do
- tls_test do |port|
- # SERVER_ADDR is different from the hostname in the certificate,
- # so the following code should raise a SSLError.
- Net::FTP.new(SERVER_ADDR,
- :port => port,
- :ssl => { :ca_file => CA_FILE })
- end
- end
- end
-
- def test_active_private_data_connection
- server = TCPServer.new(SERVER_ADDR, 0)
- port = server.addr[1]
- commands = []
- session_reused_for_data_connection = nil
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- @thread = Thread.start do
- sock = server.accept
- begin
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("234 AUTH success.\r\n")
- ctx = OpenSSL::SSL::SSLContext.new
- ctx.ca_file = CA_FILE
- ctx.key = File.open(SERVER_KEY) { |f|
- OpenSSL::PKey::RSA.new(f)
- }
- ctx.cert = File.open(SERVER_CERT) { |f|
- OpenSSL::X509::Certificate.new(f)
- }
- sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
- sock.sync_close = true
- begin
- sock.accept
- commands.push(sock.gets)
- sock.print("200 PSBZ success.\r\n")
- commands.push(sock.gets)
- sock.print("200 PROT success.\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- conn = OpenSSL::SSL::SSLSocket.new(conn, ctx)
- conn.sync_close = true
- conn.accept
- session_reused_for_data_connection = conn.session_reused?
- binary_data.scan(/.{1,1024}/nm) do |s|
- conn.print(s)
- end
- conn.close
- sock.print("226 Transfer complete.\r\n")
- rescue OpenSSL::SSL::SSLError
- end
- ensure
- sock.close
- server.close
- end
- end
- ftp = Net::FTP.new(SERVER_NAME,
- port: port,
- ssl: { ca_file: CA_FILE },
- passive: false)
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- begin
- assert_equal("AUTH TLS\r\n", commands.shift)
- assert_equal("PBSZ 0\r\n", commands.shift)
- assert_equal("PROT P\r\n", commands.shift)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = ftp.getbinaryfile("foo", nil)
- assert_equal(binary_data, buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- # FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
- # See https://github.com/openssl/openssl/pull/5967 for details.
- if OpenSSL::OPENSSL_LIBRARY_VERSION !~ /OpenSSL 1.1.0h|LibreSSL/
- assert_equal(true, session_reused_for_data_connection)
- end
- ensure
- ftp.close
- end
- end
-
- def test_passive_private_data_connection
- server = TCPServer.new(SERVER_ADDR, 0)
- port = server.addr[1]
- commands = []
- session_reused_for_data_connection = nil
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- @thread = Thread.start do
- sock = server.accept
- begin
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("234 AUTH success.\r\n")
- ctx = OpenSSL::SSL::SSLContext.new
- ctx.ca_file = CA_FILE
- ctx.key = File.open(SERVER_KEY) { |f|
- OpenSSL::PKey::RSA.new(f)
- }
- ctx.cert = File.open(SERVER_CERT) { |f|
- OpenSSL::X509::Certificate.new(f)
- }
- sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
- sock.sync_close = true
- begin
- sock.accept
- commands.push(sock.gets)
- sock.print("200 PSBZ success.\r\n")
- commands.push(sock.gets)
- sock.print("200 PROT success.\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- data_server = create_data_connection_server(sock)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = data_server.accept
- conn = OpenSSL::SSL::SSLSocket.new(conn, ctx)
- conn.sync_close = true
- conn.accept
- session_reused_for_data_connection = conn.session_reused?
- binary_data.scan(/.{1,1024}/nm) do |s|
- conn.print(s)
- end
- conn.close
- data_server.close
- sock.print("226 Transfer complete.\r\n")
- rescue OpenSSL::SSL::SSLError
- end
- ensure
- sock.close
- server.close
- end
- end
- ftp = Net::FTP.new(SERVER_NAME,
- port: port,
- ssl: { ca_file: CA_FILE },
- passive: true)
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- begin
- assert_equal("AUTH TLS\r\n", commands.shift)
- assert_equal("PBSZ 0\r\n", commands.shift)
- assert_equal("PROT P\r\n", commands.shift)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = ftp.getbinaryfile("foo", nil)
- assert_equal(binary_data, buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_match(/\A(PASV|EPSV)\r\n/, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- # FIXME: The new_session_cb is known broken for clients in OpenSSL 1.1.0h.
- if OpenSSL::OPENSSL_LIBRARY_VERSION !~ /OpenSSL 1.1.0h|LibreSSL/
- assert_equal(true, session_reused_for_data_connection)
- end
- ensure
- ftp.close
- end
- end
-
- def test_active_clear_data_connection
- server = TCPServer.new(SERVER_ADDR, 0)
- port = server.addr[1]
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- @thread = Thread.start do
- sock = server.accept
- begin
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("234 AUTH success.\r\n")
- ctx = OpenSSL::SSL::SSLContext.new
- ctx.ca_file = CA_FILE
- ctx.key = File.open(SERVER_KEY) { |f|
- OpenSSL::PKey::RSA.new(f)
- }
- ctx.cert = File.open(SERVER_CERT) { |f|
- OpenSSL::X509::Certificate.new(f)
- }
- sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
- sock.sync_close = true
- begin
- sock.accept
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- binary_data.scan(/.{1,1024}/nm) do |s|
- conn.print(s)
- end
- conn.close
- sock.print("226 Transfer complete.\r\n")
- rescue OpenSSL::SSL::SSLError
- end
- ensure
- sock.close
- server.close
- end
- end
- ftp = Net::FTP.new(SERVER_NAME,
- port: port,
- ssl: { ca_file: CA_FILE },
- private_data_connection: false,
- passive: false)
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- begin
- assert_equal("AUTH TLS\r\n", commands.shift)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = ftp.getbinaryfile("foo", nil)
- assert_equal(binary_data, buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close
- end
- end
-
- def test_passive_clear_data_connection
- server = TCPServer.new(SERVER_ADDR, 0)
- port = server.addr[1]
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- @thread = Thread.start do
- sock = server.accept
- begin
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("234 AUTH success.\r\n")
- ctx = OpenSSL::SSL::SSLContext.new
- ctx.ca_file = CA_FILE
- ctx.key = File.open(SERVER_KEY) { |f|
- OpenSSL::PKey::RSA.new(f)
- }
- ctx.cert = File.open(SERVER_CERT) { |f|
- OpenSSL::X509::Certificate.new(f)
- }
- sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
- sock.sync_close = true
- begin
- sock.accept
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- data_server = create_data_connection_server(sock)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for foo (#{binary_data.size} bytes)\r\n")
- conn = data_server.accept
- binary_data.scan(/.{1,1024}/nm) do |s|
- conn.print(s)
- end
- conn.close
- data_server.close
- sock.print("226 Transfer complete.\r\n")
- rescue OpenSSL::SSL::SSLError
- end
- ensure
- sock.close
- server.close
- end
- end
- ftp = Net::FTP.new(SERVER_NAME,
- port: port,
- ssl: { ca_file: CA_FILE },
- private_data_connection: false,
- passive: true)
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- begin
- assert_equal("AUTH TLS\r\n", commands.shift)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- buf = ftp.getbinaryfile("foo", nil)
- assert_equal(binary_data, buf)
- assert_equal(Encoding::ASCII_8BIT, buf.encoding)
- assert_match(/\A(PASV|EPSV)\r\n/, commands.shift)
- assert_equal("RETR foo\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close
- end
- end
-
- def test_tls_connect_timeout
- server = TCPServer.new(SERVER_ADDR, 0)
- port = server.addr[1]
- commands = []
- sock = nil
- @thread = Thread.start do
- sock = server.accept
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("234 AUTH success.\r\n")
- end
- begin
- assert_raise(Net::OpenTimeout) do
- Net::FTP.new(SERVER_NAME,
- port: port,
- ssl: { ca_file: CA_FILE },
- ssl_handshake_timeout: 0.1)
- end
- @thread.join
- ensure
- sock.close if sock
- server.close
- end
- end
- end
-
- def test_abort_tls
- return unless defined?(OpenSSL)
-
- commands = []
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("234 AUTH success.\r\n")
- ctx = OpenSSL::SSL::SSLContext.new
- ctx.ca_file = CA_FILE
- ctx.key = File.open(SERVER_KEY) { |f|
- OpenSSL::PKey::RSA.new(f)
- }
- ctx.cert = File.open(SERVER_CERT) { |f|
- OpenSSL::X509::Certificate.new(f)
- }
- sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
- sock.sync_close = true
- sock.accept
- commands.push(sock.gets)
- sock.print("200 PSBZ success.\r\n")
- commands.push(sock.gets)
- sock.print("200 PROT success.\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("225 No transfer to ABOR.\r\n")
- }
- begin
- begin
- ftp = Net::FTP.new(SERVER_NAME,
- port: server.port,
- ssl: { ca_file: CA_FILE })
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- assert_equal("AUTH TLS\r\n", commands.shift)
- assert_equal("PBSZ 0\r\n", commands.shift)
- assert_equal("PROT P\r\n", commands.shift)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.abort
- assert_equal("ABOR\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- rescue RuntimeError, LoadError
- # skip (require openssl)
- ensure
- ftp.close if ftp
- end
- ensure
- server.close
- end
- end
-
- def test_getbinaryfile_command_injection
- skip "| is not allowed in filename on Windows" if windows?
- [false, true].each do |resume|
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- binary_data.scan(/.{1,1024}/nm) do |s|
- conn.print(s)
- end
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- chdir_to_tmpdir do
- begin
- ftp = Net::FTP.new
- ftp.resume = resume
- ftp.read_timeout = (defined?(RubyVM::JIT) && RubyVM::JIT.enabled?) ? 300 : 0.2 # use large timeout for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.getbinaryfile("|echo hello")
- assert_equal(binary_data, File.binread("./|echo hello"))
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR |echo hello\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- end
- ensure
- server.close
- end
- end
- end
-
- def test_gettextfile_command_injection
- skip "| is not allowed in filename on Windows" if windows?
- commands = []
- text_data = <<EOF.gsub(/\n/, "\r\n")
-foo
-bar
-baz
-EOF
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening TEXT mode data connection for |echo hello (#{text_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- text_data.each_line do |l|
- conn.print(l)
- end
- conn.shutdown(Socket::SHUT_WR)
- conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- chdir_to_tmpdir do
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.gettextfile("|echo hello")
- assert_equal(text_data.gsub(/\r\n/, "\n"),
- File.binread("./|echo hello"))
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("RETR |echo hello\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- end
- ensure
- server.close
- end
- end
-
- def test_putbinaryfile_command_injection
- skip "| is not allowed in filename on Windows" if windows?
- commands = []
- binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
- received_data = nil
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening BINARY mode data connection for |echo hello (#{binary_data.size} bytes)\r\n")
- conn = TCPSocket.new(host, port)
- received_data = conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- }
- begin
- chdir_to_tmpdir do
- File.binwrite("./|echo hello", binary_data)
- begin
- ftp = Net::FTP.new
- ftp.read_timeout = defined?(RubyVM::JIT) && RubyVM::JIT.enabled? ? 300 : 0.2 # use large timeout for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.putbinaryfile("|echo hello")
- assert_equal(binary_data, received_data)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("STOR |echo hello\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- end
- ensure
- server.close
- end
- end
-
- def test_puttextfile_command_injection
- skip "| is not allowed in filename on Windows" if windows?
- commands = []
- received_data = nil
- server = create_ftp_server { |sock|
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("331 Please specify the password.\r\n")
- commands.push(sock.gets)
- sock.print("230 Login successful.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to ASCII mode.\r\n")
- line = sock.gets
- commands.push(line)
- host, port = process_port_or_eprt(sock, line)
- commands.push(sock.gets)
- sock.print("150 Opening TEXT mode data connection for |echo hello\r\n")
- conn = TCPSocket.new(host, port)
- received_data = conn.read
- conn.close
- sock.print("226 Transfer complete.\r\n")
- commands.push(sock.gets)
- sock.print("200 Switching to Binary mode.\r\n")
- }
- begin
- chdir_to_tmpdir do
- File.open("|echo hello", "w") do |f|
- f.puts("foo")
- f.puts("bar")
- f.puts("baz")
- end
- begin
- ftp = Net::FTP.new
- ftp.read_timeout *= 5 if defined?(RubyVM::JIT) && RubyVM::JIT.enabled? # for --jit-wait
- ftp.connect(SERVER_ADDR, server.port)
- ftp.login
- assert_match(/\AUSER /, commands.shift)
- assert_match(/\APASS /, commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- ftp.puttextfile("|echo hello")
- assert_equal(<<EOF.gsub(/\n/, "\r\n"), received_data)
-foo
-bar
-baz
-EOF
- assert_equal("TYPE A\r\n", commands.shift)
- assert_match(/\A(PORT|EPRT) /, commands.shift)
- assert_equal("STOR |echo hello\r\n", commands.shift)
- assert_equal("TYPE I\r\n", commands.shift)
- assert_equal(nil, commands.shift)
- ensure
- ftp.close if ftp
- end
- end
- ensure
- server.close
- end
- end
-
- def test_time_parser
- s = "20371231000000"
- assert_equal(Time.utc(2037, 12, 31, 0, 0, 0),
- Net::FTP::TIME_PARSER[s])
- s = "20371231000000.123456"
- assert_equal(Time.utc(2037, 12, 31, 0, 0, 0, 123456),
- Net::FTP::TIME_PARSER[s])
- s = "20371231000000." + "9" * 999999
- assert_equal(Time.utc(2037, 12, 31, 0, 0, 0,
- 99999999999999999r / 100000000000),
- Net::FTP::TIME_PARSER[s])
- e = assert_raise(Net::FTPProtoError) {
- Net::FTP::TIME_PARSER["x" * 999999]
- }
- assert_equal("invalid time-val: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...", e.message)
- end
-
- private
-
- def create_ftp_server(sleep_time = nil)
- server = TCPServer.new(SERVER_ADDR, 0)
- @thread = Thread.start do
- if sleep_time
- sleep(sleep_time)
- end
- sock = server.accept
- begin
- sock.setsockopt(Socket::SOL_SOCKET, Socket::SO_OOBINLINE, 1)
- yield(sock)
- sock.shutdown(Socket::SHUT_WR)
- sock.read unless sock.eof?
- ensure
- sock.close
- end
- end
- def server.port
- addr[1]
- end
- return server
- end
-
- def tls_test
- server = TCPServer.new(SERVER_ADDR, 0)
- port = server.addr[1]
- commands = []
- @thread = Thread.start do
- sock = server.accept
- begin
- sock.print("220 (test_ftp).\r\n")
- commands.push(sock.gets)
- sock.print("234 AUTH success.\r\n")
- ctx = OpenSSL::SSL::SSLContext.new
- ctx.ca_file = CA_FILE
- ctx.key = File.open(SERVER_KEY) { |f|
- OpenSSL::PKey::RSA.new(f)
- }
- ctx.cert = File.open(SERVER_CERT) { |f|
- OpenSSL::X509::Certificate.new(f)
- }
- sock = OpenSSL::SSL::SSLSocket.new(sock, ctx)
- sock.sync_close = true
- begin
- sock.accept
- commands.push(sock.gets)
- sock.print("200 PSBZ success.\r\n")
- commands.push(sock.gets)
- sock.print("200 PROT success.\r\n")
- rescue OpenSSL::SSL::SSLError, SystemCallError
- end
- ensure
- sock.close
- server.close
- end
- end
- ftp = yield(port)
- ftp.close
-
- assert_equal("AUTH TLS\r\n", commands.shift)
- assert_equal("PBSZ 0\r\n", commands.shift)
- assert_equal("PROT P\r\n", commands.shift)
- end
-
- def process_port_or_eprt(sock, line)
- case line
- when /\APORT (.*)/
- port_args = $1.split(/,/)
- host = port_args[0, 4].join(".")
- port = port_args[4, 2].map(&:to_i).inject {|x, y| (x << 8) + y}
- sock.print("200 PORT command successful.\r\n")
- return host, port
- when /\AEPRT \|2\|(.*?)\|(.*?)\|/
- host = $1
- port = $2.to_i
- sock.print("200 EPRT command successful.\r\n")
- return host, port
- else
- flunk "PORT or EPRT expected"
- end
- end
-
- def create_data_connection_server(sock)
- data_server = TCPServer.new(SERVER_ADDR, 0)
- port = data_server.local_address.ip_port
- if data_server.local_address.ipv4?
- sock.printf("227 Entering Passive Mode (127,0,0,1,%s).\r\n",
- port.divmod(256).join(","))
- elsif data_server.local_address.ipv6?
- sock.printf("229 Entering Extended Passive Mode (|||%d|)\r\n", port)
- else
- flunk "Invalid local address"
- end
- return data_server
- end
-
- def chdir_to_tmpdir
- Dir.mktmpdir do |dir|
- pwd = Dir.pwd
- Dir.chdir(dir)
- begin
- yield
- ensure
- Dir.chdir(pwd)
- end
- end
- end
-end
diff --git a/test/net/ftp/test_mlsx_entry.rb b/test/net/ftp/test_mlsx_entry.rb
deleted file mode 100644
index 92fe411548..0000000000
--- a/test/net/ftp/test_mlsx_entry.rb
+++ /dev/null
@@ -1,98 +0,0 @@
-# frozen_string_literal: true
-
-require "net/ftp"
-require "test/unit"
-require "ostruct"
-require "stringio"
-
-class MLSxEntryTest < Test::Unit::TestCase
- def test_file?
- assert_equal(true, Net::FTP::MLSxEntry.new({"type"=>"file"}, "foo").file?)
- assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"dir"}, "foo").file?)
- assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"cdir"}, "foo").file?)
- assert_equal(false, Net::FTP::MLSxEntry.new({"type"=>"pdir"}, "foo").file?)
- end
-
- def test_directory?
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"type"=>"file"}, "foo").directory?)
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"type"=>"dir"}, "foo").directory?)
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"type"=>"cdir"}, "foo").directory?)
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"type"=>"pdir"}, "foo").directory?)
- end
-
- def test_appendable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"a"}, "foo").appendable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").appendable?)
- end
-
- def test_creatable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"c"}, "foo").creatable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").creatable?)
- end
-
- def test_deletable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"d"}, "foo").deletable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").deletable?)
- end
-
- def test_enterable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"e"}, "foo").enterable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").enterable?)
- end
-
- def test_renamable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"f"}, "foo").renamable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").renamable?)
- end
-
- def test_listable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"l"}, "foo").listable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").listable?)
- end
-
- def test_directory_makable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"m"}, "foo").
- directory_makable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").
- directory_makable?)
- end
-
- def test_purgeable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"p"}, "foo").purgeable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").purgeable?)
- end
-
- def test_readable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"r"}, "foo").readable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").readable?)
- end
-
- def test_writable?
- assert_equal(true,
- Net::FTP::MLSxEntry.new({"perm"=>"w"}, "foo").writable?)
- assert_equal(false,
- Net::FTP::MLSxEntry.new({"perm"=>""}, "foo").writable?)
- end
-end
diff --git a/tool/sync_default_gems.rb b/tool/sync_default_gems.rb
index 5f95a3d80b..677b55a35c 100644
--- a/tool/sync_default_gems.rb
+++ b/tool/sync_default_gems.rb
@@ -52,7 +52,6 @@ REPOSITORIES = {
English: "ruby/English",
"net-protocol": "ruby/net-protocol",
"net-imap": "ruby/net-imap",
- "net-ftp": "ruby/net-ftp",
"net-http": "ruby/net-http",
bigdecimal: "ruby/bigdecimal",
optparse: "ruby/optparse",
@@ -267,11 +266,6 @@ def sync_default_gems(gem)
cp_r("#{upstream}/lib/net/imap", "lib/net")
cp_r("#{upstream}/test/net/imap", "test/net")
cp_r("#{upstream}/net-imap.gemspec", "lib/net/imap")
- when "net-ftp"
- rm_rf(%w[lib/net/ftp.rb lib/net/net-ftp.gemspec test/net/ftp])
- cp_r("#{upstream}/lib/net/ftp.rb", "lib/net")
- cp_r("#{upstream}/test/net/ftp", "test/net")
- cp_r("#{upstream}/net-ftp.gemspec", "lib/net")
when "net-http"
rm_rf(%w[lib/net/http.rb lib/net/http test/net/http])
cp_r("#{upstream}/lib/net/http.rb", "lib/net")