summaryrefslogtreecommitdiff
path: root/test/net
diff options
context:
space:
mode:
authorshugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-09-08 06:08:20 +0000
committershugo <shugo@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2015-09-08 06:08:20 +0000
commitd5d6b149df3d2d565859599334327e378e5b2a84 (patch)
tree4f15058ebb0e0f27776886b6a4144076946137b7 /test/net
parent49cbfc8359801c5adf22d00d31ab6a7882b9374a (diff)
* lib/net/ftp.rb (list): fetch all the lines before yielding a block
to allow other commands in the block. [Feature #11454] Patched by Srikanth Shreenivas. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@51791 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/net')
-rw-r--r--test/net/ftp/test_ftp.rb140
1 files changed, 140 insertions, 0 deletions
diff --git a/test/net/ftp/test_ftp.rb b/test/net/ftp/test_ftp.rb
index 618a9da..ad8ef5a 100644
--- a/test/net/ftp/test_ftp.rb
+++ b/test/net/ftp/test_ftp.rb
@@ -741,6 +741,146 @@ class FTPTest < Test::Unit::TestCase
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)
+ port_args = line.slice(/\APORT (.*)/, 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")
+ 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 |line|
+ conn.print(line)
+ 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.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(/\APORT /, 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)
+ port_args = line.slice(/\APORT (.*)/, 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")
+ 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)
+ port_args = line.slice(/\APORT (.*)/, 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")
+ 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(/\APORT /, commands.shift)
+ assert_equal("LIST\r\n", commands.shift)
+ assert_equal("TYPE I\r\n", commands.shift)
+ assert_match(/\APORT /, 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|