diff options
Diffstat (limited to 'test/socket/test_socket.rb')
| -rw-r--r-- | test/socket/test_socket.rb | 300 |
1 files changed, 259 insertions, 41 deletions
diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb index 659ebabba3..41aba32963 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -1,7 +1,10 @@ +# frozen_string_literal: true + begin require "socket" require "tmpdir" require "fcntl" + require "etc" require "test/unit" rescue LoadError end @@ -70,6 +73,22 @@ class TestSocket < Test::Unit::TestCase } end + def test_bind + Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|bound| + bound.bind(Socket.sockaddr_in(0, "127.0.0.1")) + addr = bound.getsockname + port, = Socket.unpack_sockaddr_in(addr) + + Socket.open(Socket::AF_INET, Socket::SOCK_STREAM, 0) {|s| + e = assert_raise(Errno::EADDRINUSE) do + s.bind(Socket.sockaddr_in(port, "127.0.0.1")) + end + + assert_match "bind(2) for 127.0.0.1:#{port}", e.message + } + } + end + def test_getaddrinfo # This should not send a DNS query because AF_UNIX. assert_raise(SocketError) { Socket.getaddrinfo("www.kame.net", 80, "AF_UNIX") } @@ -86,6 +105,8 @@ class TestSocket < Test::Unit::TestCase def test_getnameinfo assert_raise(SocketError) { Socket.getnameinfo(["AF_UNIX", 80, "0.0.0.0"]) } + assert_raise(ArgumentError) {Socket.getnameinfo(["AF_INET", "http\0", "example.net"])} + assert_raise(ArgumentError) {Socket.getnameinfo(["AF_INET", "http", "example.net\0"])} end def test_ip_address_list @@ -100,6 +121,15 @@ class TestSocket < Test::Unit::TestCase } end + def test_ip_address_list_include_localhost + begin + list = Socket.ip_address_list + rescue NotImplementedError + return + end + assert_includes list.map(&:ip_address), Addrinfo.tcp("localhost", 0).ip_address + end + def test_tcp TCPServer.open(0) {|serv| addr = serv.connect_address @@ -133,11 +163,19 @@ class TestSocket < Test::Unit::TestCase def random_port # IANA suggests dynamic port for 49152 to 65535 # http://www.iana.org/assignments/port-numbers - 49152 + rand(65535-49152+1) + case RUBY_PLATFORM + when /mingw|mswin/ + rand(50000..65535) + else + rand(49152..65535) + end end def errors_addrinuse - [Errno::EADDRINUSE] + errs = [Errno::EADDRINUSE] + # MinGW fails with "Errno::EACCES: Permission denied - bind(2) for 0.0.0.0:49721" + errs << Errno::EACCES if /mingw/ =~ RUBY_PLATFORM + errs end def test_tcp_server_sockets @@ -221,9 +259,12 @@ class TestSocket < Test::Unit::TestCase unix_server = Socket.unix_server_socket("#{tmpdir}/sock") tcp_servers.each {|s| addr = s.connect_address - assert_nothing_raised("connect to #{addr.inspect}") { + begin clients << addr.connect - } + rescue + # allow failure if the address is IPv6 + raise unless addr.ipv6? + end } addr = unix_server.connect_address assert_nothing_raised("connect to #{addr.inspect}") { @@ -299,10 +340,14 @@ class TestSocket < Test::Unit::TestCase end def test_udp_server + # http://rubyci.s3.amazonaws.com/rhel_zlinux/ruby-master/log/20230312T023302Z.fail.html.gz + # Errno::EHOSTUNREACH: No route to host - recvmsg(2) + omit if 'rhel_zlinux' == ENV['RUBYCI_NICKNAME'] + begin - ip_addrs = Socket.ip_address_list + ifaddrs = Socket.getifaddrs rescue NotImplementedError - skip "Socket.ip_address_list not implemented" + skip "Socket.getifaddrs not implemented" end ifconfig = nil @@ -310,26 +355,29 @@ class TestSocket < Test::Unit::TestCase famlies = {} sockets.each {|s| famlies[s.local_address.afamily] = s } nd6 = {} - ip_addrs.reject! {|ai| + ifaddrs.reject! {|ifa| + ai = ifa.addr + next true unless ai s = famlies[ai.afamily] next true unless s + next true if ai.ipv6_linklocal? # IPv6 link-local address is too troublesome in this test. case RUBY_PLATFORM when /linux/ if ai.ip_address.include?('%') and - (`uname -r`[/[0-9.]+/].split('.').map(&:to_i) <=> [2,6,18]) <= 0 + (Etc.uname[:release][/[0-9.]+/].split('.').map(&:to_i) <=> [2,6,18]) <= 0 # Cent OS 5.6 (2.6.18-238.19.1.el5xen) doesn't correctly work # sendmsg with pktinfo for link-local ipv6 addresses next true end when /freebsd/ - if ifr_name = ai.ip_address[/%(.*)/, 1] + if ifa.addr.ipv6_linklocal? # FreeBSD 9.0 with default setting (ipv6_activate_all_interfaces # is not YES) sets IFDISABLED to interfaces which don't have # global IPv6 address. # Link-local IPv6 addresses on those interfaces don't work. ulSIOCGIFINFO_IN6 = 3225971052 ulND6_IFF_IFDISABLED = 8 - in6_ondireq = ifr_name + in6_ondireq = ifa.name s.ioctl(ulSIOCGIFINFO_IN6, in6_ondireq) flag = in6_ondireq.unpack('A16L6').last next true if flag & ulND6_IFF_IFDISABLED != 0 @@ -343,7 +391,6 @@ class TestSocket < Test::Unit::TestCase # Mac OS X may sets IFDISABLED as FreeBSD does ulSIOCGIFFLAGS = 3223349521 ulSIOCGIFINFO_IN6 = 3224398156 - ulSIOCGIFAFLAG_IN6 = 3240126793 ulIFF_POINTOPOINT = 0x10 ulND6_IFF_IFDISABLED = 8 in6_ondireq = ifr_name @@ -354,11 +401,10 @@ class TestSocket < Test::Unit::TestCase in6_ifreq = [ifr_name,ai.to_sockaddr].pack('a16A*') s.ioctl(ulSIOCGIFFLAGS, in6_ifreq) next true if in6_ifreq.unpack('A16L1').last & ulIFF_POINTOPOINT != 0 - else - ifconfig ||= `/sbin/ifconfig` - next true if ifconfig.scan(/^(\w+):(.*(?:\n\t.*)*)/).find do|ifname, value| - value.include?(ai.ip_address) && value.include?('POINTOPOINT') - end + end + ifconfig ||= `/sbin/ifconfig` + next true if ifconfig.scan(/^(\w+):(.*(?:\n\t.*)*)/).find do |_ifname, value| + value.include?(ai.ip_address) && value.include?('POINTOPOINT') end end false @@ -377,24 +423,28 @@ class TestSocket < Test::Unit::TestCase } } - ip_addrs.each {|ai| + ifaddrs.each {|ifa| + ai = ifa.addr Addrinfo.udp(ai.ip_address, port).connect {|s| ping_p = false msg1 = "<<<#{ai.inspect}>>>" s.sendmsg msg1 unless IO.select([s], nil, nil, 10) nd6options = nd6.key?(ai) ? "nd6=%x " % nd6[ai] : '' - raise "no response from #{ai.inspect} #{nd6options}ping=#{ping_p}" + raise "no response from #{ifa.inspect} #{nd6options}ping=#{ping_p}" end msg2, addr = s.recvmsg - msg2, remote_address, local_address = Marshal.load(msg2) + msg2, _, _ = Marshal.load(msg2) assert_equal(msg1, msg2) assert_equal(ai.ip_address, addr.ip_address) } } rescue NotImplementedError, Errno::ENOSYS skipped = true - skip "need sendmsg and recvmsg" + skip "need sendmsg and recvmsg: #{$!}" + rescue RuntimeError + skipped = true + skip "UDP server is no response: #{$!}" ensure if th if skipped @@ -429,25 +479,47 @@ class TestSocket < Test::Unit::TestCase } end + def timestamp_retry_rw(s1, s2, t1, type) + IO.pipe do |r,w| + # UDP may not be reliable, keep sending until recvmsg returns: + th = Thread.new do + n = 0 + begin + s2.send("a", 0, s1.local_address) + n += 1 + end while IO.select([r], nil, nil, 0.1).nil? + n + end + timeout = (defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled? ? 120 : 30) # for --jit-wait + assert_equal([[s1],[],[]], IO.select([s1], nil, nil, timeout)) + msg, _, _, stamp = s1.recvmsg + assert_equal("a", msg) + assert(stamp.cmsg_is?(:SOCKET, type)) + w.close # stop th + n = th.value + n > 1 and + warn "UDP packet loss for #{type} over loopback, #{n} tries needed" + t2 = Time.now.strftime("%Y-%m-%d") + pat = Regexp.union([t1, t2].uniq) + assert_match(pat, stamp.inspect) + t = stamp.timestamp + assert_match(pat, t.strftime("%Y-%m-%d")) + stamp + end + end + def test_timestamp return if /linux|freebsd|netbsd|openbsd|solaris|darwin/ !~ RUBY_PLATFORM - return if !defined?(Socket::AncillaryData) + return if !defined?(Socket::AncillaryData) || !defined?(Socket::SO_TIMESTAMP) t1 = Time.now.strftime("%Y-%m-%d") stamp = nil Addrinfo.udp("127.0.0.1", 0).bind {|s1| Addrinfo.udp("127.0.0.1", 0).bind {|s2| s1.setsockopt(:SOCKET, :TIMESTAMP, true) - s2.send "a", 0, s1.local_address - msg, addr, rflags, stamp = s1.recvmsg - assert_equal("a", msg) - assert(stamp.cmsg_is?(:SOCKET, :TIMESTAMP)) + stamp = timestamp_retry_rw(s1, s2, t1, :TIMESTAMP) } } - t2 = Time.now.strftime("%Y-%m-%d") - pat = Regexp.union([t1, t2].uniq) - assert_match(pat, stamp.inspect) t = stamp.timestamp - assert_match(pat, t.strftime("%Y-%m-%d")) pat = /\.#{"%06d" % t.usec}/ assert_match(pat, stamp.inspect) end @@ -464,17 +536,10 @@ class TestSocket < Test::Unit::TestCase # SO_TIMESTAMPNS is available since Linux 2.6.22 return end - s2.send "a", 0, s1.local_address - msg, addr, rflags, stamp = s1.recvmsg - assert_equal("a", msg) - assert(stamp.cmsg_is?(:SOCKET, :TIMESTAMPNS)) + stamp = timestamp_retry_rw(s1, s2, t1, :TIMESTAMPNS) } } - t2 = Time.now.strftime("%Y-%m-%d") - pat = Regexp.union([t1, t2].uniq) - assert_match(pat, stamp.inspect) t = stamp.timestamp - assert_match(pat, t.strftime("%Y-%m-%d")) pat = /\.#{"%09d" % t.nsec}/ assert_match(pat, stamp.inspect) end @@ -487,7 +552,7 @@ class TestSocket < Test::Unit::TestCase Addrinfo.udp("127.0.0.1", 0).bind {|s2| s1.setsockopt(:SOCKET, :BINTIME, true) s2.send "a", 0, s1.local_address - msg, addr, rflags, stamp = s1.recvmsg + msg, _, _, stamp = s1.recvmsg assert_equal("a", msg) assert(stamp.cmsg_is?(:SOCKET, :BINTIME)) } @@ -509,15 +574,18 @@ class TestSocket < Test::Unit::TestCase begin sleep(0.1) end until serv_thread.stop? sock = TCPSocket.new("localhost", server.addr[1]) client_thread = Thread.new do - sock.readline + assert_raise(IOError, bug4390) { + sock.readline + } end begin sleep(0.1) end until client_thread.stop? Timeout.timeout(1) do sock.close sock = nil - assert_raise(IOError, bug4390) {client_thread.join} + client_thread.join end ensure + serv_thread.value.close server.close end @@ -535,7 +603,7 @@ class TestSocket < Test::Unit::TestCase # some platforms may not timeout when the listener queue overflows, # but we know Linux does with the default listen backlog of SOMAXCONN for # TCPServer. - assert_raises(Errno::ETIMEDOUT) do + assert_raise(Errno::ETIMEDOUT) do (Socket::SOMAXCONN*2).times do |i| sock = Socket.tcp(host, port, :connect_timeout => 0) assert_equal sock, IO.select(nil, [ sock ])[1][0], @@ -548,4 +616,154 @@ class TestSocket < Test::Unit::TestCase accepted.close if accepted sock.close if sock && ! sock.closed? end + + def test_getifaddrs + begin + list = Socket.getifaddrs + rescue NotImplementedError + return + end + list.each {|ifaddr| + assert_instance_of(Socket::Ifaddr, ifaddr) + } + end + + def test_connect_in_rescue + serv = Addrinfo.tcp(nil, 0).listen + addr = serv.connect_address + begin + raise "dummy error" + rescue + s = addr.connect + assert(!s.closed?) + end + ensure + serv.close if serv && !serv.closed? + s.close if s && !s.closed? + end + + def test_bind_in_rescue + begin + raise "dummy error" + rescue + s = Addrinfo.tcp(nil, 0).bind + assert(!s.closed?) + end + ensure + s.close if s && !s.closed? + end + + def test_listen_in_rescue + begin + raise "dummy error" + rescue + s = Addrinfo.tcp(nil, 0).listen + assert(!s.closed?) + end + ensure + s.close if s && !s.closed? + end + + def test_udp_server_sockets_in_rescue + begin + raise "dummy error" + rescue + ss = Socket.udp_server_sockets(0) + ss.each {|s| + assert(!s.closed?) + } + end + ensure + if ss + ss.each {|s| + s.close if !s.closed? + } + end + end + + def test_tcp_server_sockets_in_rescue + begin + raise "dummy error" + rescue + ss = Socket.tcp_server_sockets(0) + ss.each {|s| + assert(!s.closed?) + } + end + ensure + if ss + ss.each {|s| + s.close if !s.closed? + } + end + end + + def test_recvmsg_udp_no_arg + n = 4097 + s1 = Addrinfo.udp("127.0.0.1", 0).bind + s2 = s1.connect_address.connect + s2.send("a" * n, 0) + ret = s1.recvmsg + assert_equal n, ret[0].bytesize, '[ruby-core:71517] [Bug #11701]' + + s2.send("a" * n, 0) + IO.select([s1]) + ret = s1.recvmsg_nonblock + assert_equal n, ret[0].bytesize, 'non-blocking should also grow' + ensure + s1.close + s2.close + end + + def test_udp_read_truncation + s1 = Addrinfo.udp("127.0.0.1", 0).bind + s2 = s1.connect_address.connect + s2.send("a" * 100, 0) + ret = s1.read(10) + assert_equal "a" * 10, ret + s2.send("b" * 100, 0) + ret = s1.read(10) + assert_equal "b" * 10, ret + ensure + s1.close + s2.close + end + + def test_udp_recv_truncation + s1 = Addrinfo.udp("127.0.0.1", 0).bind + s2 = s1.connect_address.connect + s2.send("a" * 100, 0) + ret = s1.recv(10, Socket::MSG_PEEK) + assert_equal "a" * 10, ret + ret = s1.recv(10, 0) + assert_equal "a" * 10, ret + s2.send("b" * 100, 0) + ret = s1.recv(10, 0) + assert_equal "b" * 10, ret + ensure + s1.close + s2.close + end + + def test_udp_recvmsg_truncation + s1 = Addrinfo.udp("127.0.0.1", 0).bind + s2 = s1.connect_address.connect + s2.send("a" * 100, 0) + ret, addr, rflags = s1.recvmsg(10, Socket::MSG_PEEK) + assert_equal "a" * 10, ret + # AIX does not set MSG_TRUNC for a message partially read with MSG_PEEK. + assert_equal Socket::MSG_TRUNC, rflags & Socket::MSG_TRUNC if !rflags.nil? && /aix/ !~ RUBY_PLATFORM + ret, addr, rflags = s1.recvmsg(10, 0) + assert_equal "a" * 10, ret + assert_equal Socket::MSG_TRUNC, rflags & Socket::MSG_TRUNC if !rflags.nil? + s2.send("b" * 100, 0) + ret, addr, rflags = s1.recvmsg(10, 0) + assert_equal "b" * 10, ret + assert_equal Socket::MSG_TRUNC, rflags & Socket::MSG_TRUNC if !rflags.nil? + addr + ensure + s1.close + s2.close + end + end if defined?(Socket) |
