diff options
-rw-r--r-- | test/socket/test_socket.rb | 74 |
1 files changed, 74 insertions, 0 deletions
diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb index a3ce2b7ba0..d933d9c0cc 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -1,5 +1,6 @@ begin require "socket" + require "tmpdir" require "test/unit" rescue LoadError end @@ -71,4 +72,77 @@ class TestSocket < Test::Unit::TestCase assert(ai.ip?) } end + + def test_tcp + TCPServer.open(0) {|serv| + port, addr = Socket.unpack_sockaddr_in(serv.getsockname) + Socket.tcp(addr, port) {|s1| + s2 = serv.accept + begin + assert_equal(s2.remote_address.ip_unpack, s1.local_address.ip_unpack) + ensure + s2.close + end + } + } + end + + def random_port + # IANA suggests dynamic port for 49152 to 65535 + # http://www.iana.org/assignments/port-numbers + 49152 + rand(65535-49152+1) + end + + def test_tcp_server_sockets + port = random_port + begin + sockets = Socket.tcp_server_sockets(port) + rescue Errno::EADDRINUSE + return # not test failure + end + begin + sockets.each {|s| + assert_equal(port, s.local_address.ip_port) + } + ensure + sockets.each {|s| + s.close + } + end + end + + if defined? UNIXSocket + def test_unix + Dir.mktmpdir {|tmpdir| + path = "#{tmpdir}/sock" + UNIXServer.open(path) {|serv| + Socket.unix(path) {|s1| + s2 = serv.accept + begin + assert_equal(s2.remote_address.unix_path, s1.local_address.unix_path) + ensure + s2.close + end + } + } + } + end + + def test_unix_server_socket + Dir.mktmpdir {|tmpdir| + path = "#{tmpdir}/sock" + 2.times { + serv = Socket.unix_server_socket(path) + begin + assert_kind_of(Socket, serv) + assert(File.socket?(path)) + assert_equal(path, serv.local_address.unix_path) + ensure + serv.close + end + } + } + end + end + end if defined?(Socket) |