* test/net/ftp/test_ftp.rb: add tests for getbinaryfile and
gettextfile. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@52209 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
a76fdea9fc
commit
19110fe19a
@ -1,3 +1,8 @@
|
|||||||
|
Thu Oct 22 11:11:16 2015 Shugo Maeda <shugo@ruby-lang.org>
|
||||||
|
|
||||||
|
* test/net/ftp/test_ftp.rb: add tests for getbinaryfile and
|
||||||
|
gettextfile.
|
||||||
|
|
||||||
Wed Oct 21 18:34:06 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
Wed Oct 21 18:34:06 2015 Nobuyoshi Nakada <nobu@ruby-lang.org>
|
||||||
|
|
||||||
* parse.y (parser_magic_comment): allow a sole magic comment without
|
* parse.y (parser_magic_comment): allow a sole magic comment without
|
||||||
|
@ -659,6 +659,105 @@ class FTPTest < Test::Unit::TestCase
|
|||||||
end
|
end
|
||||||
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)
|
||||||
|
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)
|
||||||
|
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 = 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)
|
||||||
|
buf = ftp.getbinaryfile("foo", nil)
|
||||||
|
assert_equal(binary_data, buf)
|
||||||
|
assert_equal(Encoding::ASCII_8BIT, buf.encoding)
|
||||||
|
assert_match(/\APORT /, 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)
|
||||||
|
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.shutdown(Socket::SHUT_WR)
|
||||||
|
conn.read
|
||||||
|
conn.close
|
||||||
|
sock.print("226 Transfer complete.\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)
|
||||||
|
buf = ftp.getbinaryfile("foo", nil)
|
||||||
|
assert_equal(binary_data, buf)
|
||||||
|
assert_equal(Encoding::ASCII_8BIT, buf.encoding)
|
||||||
|
assert_match(/\APORT /, 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_storbinary
|
def test_storbinary
|
||||||
commands = []
|
commands = []
|
||||||
binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
|
binary_data = (0..0xff).map {|i| i.chr}.join * 4 * 3
|
||||||
@ -807,6 +906,66 @@ EOF
|
|||||||
end
|
end
|
||||||
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)
|
||||||
|
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.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(/\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
|
def test_getbinaryfile_in_list
|
||||||
commands = []
|
commands = []
|
||||||
binary_data = (0..0xff).map {|i| i.chr}.join
|
binary_data = (0..0xff).map {|i| i.chr}.join
|
||||||
|
Loading…
x
Reference in New Issue
Block a user