2015-11-14 16:15:33 +03:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2004-12-06 11:15:56 +03:00
|
|
|
begin
|
|
|
|
require "socket"
|
|
|
|
require "test/unit"
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2009-02-11 07:17:57 +03:00
|
|
|
class TestSocket_TCPSocket < Test::Unit::TestCase
|
2017-04-14 13:03:43 +03:00
|
|
|
def test_inspect
|
|
|
|
TCPServer.open("localhost", 0) {|server|
|
|
|
|
assert_match(/AF_INET/, server.inspect)
|
|
|
|
TCPSocket.open("localhost", server.addr[1]) {|client|
|
|
|
|
assert_match(/AF_INET/, client.inspect)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-04-06 06:39:44 +04:00
|
|
|
def test_initialize_failure
|
2013-07-19 07:35:53 +04:00
|
|
|
# These addresses are chosen from TEST-NET-1, TEST-NET-2, and TEST-NET-3.
|
|
|
|
# [RFC 5737]
|
2015-12-03 09:38:01 +03:00
|
|
|
# They are chosen because probably they are not used as a host address.
|
2013-07-19 07:35:53 +04:00
|
|
|
# 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-06 06:39:44 +04:00
|
|
|
|
2013-07-19 07:35:53 +04:00
|
|
|
server_addr = '127.0.0.1'
|
|
|
|
server_port = 80
|
2013-04-06 06:39:44 +04:00
|
|
|
|
|
|
|
begin
|
2013-07-19 07: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-06 06:39:44 +04:00
|
|
|
flunk "expected SystemCallError"
|
|
|
|
rescue SystemCallError => e
|
2013-07-19 07:35:53 +04:00
|
|
|
assert_match "for \"#{client_addr}\" port #{client_port}", e.message
|
2013-04-06 06:39:44 +04:00
|
|
|
end
|
2013-07-19 07:35:53 +04:00
|
|
|
ensure
|
|
|
|
t.close if t && !t.closed?
|
2013-04-06 06:39:44 +04:00
|
|
|
end
|
|
|
|
|
2007-11-18 10:18:56 +03:00
|
|
|
def test_recvfrom
|
2014-05-28 19: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 11:15:56 +03:00
|
|
|
}
|
|
|
|
end
|
2008-10-17 06:50:43 +04:00
|
|
|
|
|
|
|
def test_encoding
|
2014-05-28 19: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-17 06:50:43 +04:00
|
|
|
}
|
|
|
|
end
|
2015-03-12 06:03:04 +03:00
|
|
|
|
|
|
|
def test_accept_nonblock
|
|
|
|
TCPServer.open("localhost", 0) {|svr|
|
2015-06-02 05:18:44 +03:00
|
|
|
assert_raise(IO::WaitReadable) { svr.accept_nonblock }
|
2015-03-12 06:03:04 +03:00
|
|
|
assert_equal :wait_readable, svr.accept_nonblock(exception: false)
|
2015-06-02 05:18:44 +03:00
|
|
|
assert_raise(IO::WaitReadable) { svr.accept_nonblock(exception: true) }
|
2015-03-12 06:03:04 +03:00
|
|
|
}
|
|
|
|
end
|
2004-12-06 11:15:56 +03:00
|
|
|
end if defined?(TCPSocket)
|