2004-12-06 03:15:56 -05:00
|
|
|
begin
|
|
|
|
require "socket"
|
|
|
|
require "test/unit"
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-02-10 23:17:57 -05:00
|
|
|
class TestSocket_TCPSocket < Test::Unit::TestCase
|
2013-04-05 22:39:44 -04:00
|
|
|
def test_initialize_failure
|
2013-07-18 23:35:53 -04:00
|
|
|
# These addresses are chosen from TEST-NET-1, TEST-NET-2, and TEST-NET-3.
|
|
|
|
# [RFC 5737]
|
|
|
|
# They are choosen because probably they are not used as a host address.
|
|
|
|
# Anyway the addresses are used for bind() and should be failed.
|
|
|
|
# So no packets should be generated.
|
|
|
|
test_ip_addresses = [
|
|
|
|
'192.0.2.1', '192.0.2.42', # TEST-NET-1
|
|
|
|
'198.51.100.1', '198.51.100.42', # TEST-NET-2
|
|
|
|
'203.0.113.1', '203.0.113.42', # TEST-NET-3
|
|
|
|
]
|
|
|
|
begin
|
|
|
|
list = Socket.ip_address_list
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
test_ip_addresses -= list.reject {|ai| !ai.ipv4? }.map {|ai| ai.ip_address }
|
|
|
|
if test_ip_addresses.empty?
|
|
|
|
return
|
|
|
|
end
|
|
|
|
client_addr = test_ip_addresses.first
|
|
|
|
client_port = 8000
|
2013-04-05 22:39:44 -04:00
|
|
|
|
2013-07-18 23:35:53 -04:00
|
|
|
server_addr = '127.0.0.1'
|
|
|
|
server_port = 80
|
2013-04-05 22:39:44 -04:00
|
|
|
|
|
|
|
begin
|
2013-07-18 23:35:53 -04:00
|
|
|
# Since client_addr is not an IP address of this host,
|
|
|
|
# bind() in TCPSocket.new should fail as EADDRNOTAVAIL.
|
|
|
|
t = TCPSocket.new(server_addr, server_port, client_addr, client_port)
|
2013-04-05 22:39:44 -04:00
|
|
|
flunk "expected SystemCallError"
|
|
|
|
rescue SystemCallError => e
|
2013-07-18 23:35:53 -04:00
|
|
|
assert_match "for \"#{client_addr}\" port #{client_port}", e.message
|
2013-04-05 22:39:44 -04:00
|
|
|
end
|
2013-07-18 23:35:53 -04:00
|
|
|
ensure
|
|
|
|
t.close if t && !t.closed?
|
2013-04-05 22:39:44 -04:00
|
|
|
end
|
|
|
|
|
2007-11-18 02:18:56 -05:00
|
|
|
def test_recvfrom
|
2014-05-28 11:42:09 -04:00
|
|
|
TCPServer.open("localhost", 0) {|svr|
|
|
|
|
th = Thread.new {
|
|
|
|
c = svr.accept
|
|
|
|
c.write "foo"
|
|
|
|
c.close
|
|
|
|
}
|
|
|
|
addr = svr.addr
|
|
|
|
TCPSocket.open(addr[3], addr[1]) {|sock|
|
|
|
|
assert_equal(["foo", nil], sock.recvfrom(0x10000))
|
|
|
|
}
|
|
|
|
th.join
|
2004-12-06 03:15:56 -05:00
|
|
|
}
|
|
|
|
end
|
2008-10-16 22:50:43 -04:00
|
|
|
|
|
|
|
def test_encoding
|
2014-05-28 11:42:09 -04:00
|
|
|
TCPServer.open("localhost", 0) {|svr|
|
|
|
|
th = Thread.new {
|
|
|
|
c = svr.accept
|
|
|
|
c.write "foo\r\n"
|
|
|
|
c.close
|
|
|
|
}
|
|
|
|
addr = svr.addr
|
|
|
|
TCPSocket.open(addr[3], addr[1]) {|sock|
|
|
|
|
assert_equal(true, sock.binmode?)
|
|
|
|
s = sock.gets
|
|
|
|
assert_equal("foo\r\n", s)
|
|
|
|
assert_equal(Encoding.find("ASCII-8BIT"), s.encoding)
|
|
|
|
}
|
|
|
|
th.join
|
2008-10-16 22:50:43 -04:00
|
|
|
}
|
|
|
|
end
|
2004-12-06 03:15:56 -05:00
|
|
|
end if defined?(TCPSocket)
|