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_UDPSocket < Test::Unit::TestCase
|
2009-01-01 18:15:31 +03:00
|
|
|
def test_open
|
|
|
|
assert_nothing_raised { UDPSocket.open {} }
|
|
|
|
assert_nothing_raised { UDPSocket.open(Socket::AF_INET) {} }
|
|
|
|
assert_nothing_raised { UDPSocket.open("AF_INET") {} }
|
2009-01-01 18:59:10 +03:00
|
|
|
assert_nothing_raised { UDPSocket.open(:AF_INET) {} }
|
2009-01-01 18:15:31 +03:00
|
|
|
end
|
|
|
|
|
2017-04-14 13:03:43 +03:00
|
|
|
def test_inspect
|
|
|
|
UDPSocket.open() {|sock|
|
|
|
|
assert_match(/AF_INET\b/, sock.inspect)
|
|
|
|
}
|
|
|
|
if Socket.const_defined?(:AF_INET6)
|
2018-10-13 08:18:49 +03:00
|
|
|
begin
|
|
|
|
UDPSocket.open(Socket::AF_INET6) {|sock|
|
|
|
|
assert_match(/AF_INET6\b/, sock.inspect)
|
|
|
|
}
|
|
|
|
rescue Errno::EAFNOSUPPORT
|
2021-12-28 14:29:43 +03:00
|
|
|
omit 'AF_INET6 not supported by kernel'
|
2018-10-13 08:18:49 +03:00
|
|
|
end
|
2017-04-14 13:03:43 +03:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2007-11-18 10:18:56 +03:00
|
|
|
def test_connect
|
2004-12-06 11:15:56 +03:00
|
|
|
s = UDPSocket.new
|
|
|
|
host = Object.new
|
2007-11-04 23:36:20 +03:00
|
|
|
class << host; self end.send(:define_method, :to_str) {
|
2004-12-06 11:15:56 +03:00
|
|
|
s.close
|
|
|
|
"127.0.0.1"
|
|
|
|
}
|
2007-11-18 10:18:56 +03:00
|
|
|
assert_raise(IOError, "[ruby-dev:25045]") {
|
2004-12-06 11:15:56 +03:00
|
|
|
s.connect(host, 1)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2007-11-18 10:18:56 +03:00
|
|
|
def test_bind
|
2004-12-06 11:15:56 +03:00
|
|
|
s = UDPSocket.new
|
|
|
|
host = Object.new
|
2007-11-04 23:36:20 +03:00
|
|
|
class << host; self end.send(:define_method, :to_str) {
|
2004-12-06 11:15:56 +03:00
|
|
|
s.close
|
|
|
|
"127.0.0.1"
|
|
|
|
}
|
2007-11-18 10:18:56 +03:00
|
|
|
assert_raise(IOError, "[ruby-dev:25057]") {
|
2004-12-06 11:15:56 +03:00
|
|
|
s.bind(host, 2000)
|
|
|
|
}
|
2013-06-26 07:09:54 +04:00
|
|
|
ensure
|
|
|
|
s.close if s && !s.closed?
|
2004-12-06 11:15:56 +03:00
|
|
|
end
|
2013-04-06 06:39:44 +04:00
|
|
|
|
|
|
|
def test_bind_addrinuse
|
|
|
|
host = "127.0.0.1"
|
|
|
|
|
|
|
|
in_use = UDPSocket.new
|
2013-06-26 07:09:54 +04:00
|
|
|
in_use.bind(host, 0)
|
|
|
|
port = in_use.addr[1]
|
2013-04-06 06:39:44 +04:00
|
|
|
|
|
|
|
s = UDPSocket.new
|
2013-04-09 00:27:01 +04:00
|
|
|
|
2015-06-02 05:18:44 +03:00
|
|
|
e = assert_raise(Errno::EADDRINUSE) do
|
2013-04-06 06:39:44 +04:00
|
|
|
s.bind(host, port)
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match "bind(2) for \"#{host}\" port #{port}", e.message
|
2013-06-26 07:09:54 +04:00
|
|
|
ensure
|
|
|
|
in_use.close if in_use
|
|
|
|
s.close if s
|
2013-04-06 06:39:44 +04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_too_long
|
|
|
|
u = UDPSocket.new
|
|
|
|
|
2015-06-02 05:18:44 +03:00
|
|
|
e = assert_raise(Errno::EMSGSIZE) do
|
2013-04-06 06:39:44 +04:00
|
|
|
u.send "\0" * 100_000, 0, "127.0.0.1", 7 # echo
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_match 'for "127.0.0.1" port 7', e.message
|
2013-06-26 07:09:54 +04:00
|
|
|
ensure
|
|
|
|
u.close if u
|
2013-04-06 06:39:44 +04:00
|
|
|
end
|
2015-10-10 02:52:23 +03:00
|
|
|
|
|
|
|
def test_bind_no_memory_leak
|
|
|
|
assert_no_memory_leak(["-rsocket"], <<-"end;", <<-"end;", rss: true)
|
|
|
|
s = UDPSocket.new
|
|
|
|
s.close
|
|
|
|
end;
|
|
|
|
100_000.times {begin s.bind("127.0.0.1", 1) rescue IOError; end}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_connect_no_memory_leak
|
|
|
|
assert_no_memory_leak(["-rsocket"], <<-"end;", <<-"end;", rss: true)
|
|
|
|
s = UDPSocket.new
|
|
|
|
s.close
|
|
|
|
end;
|
|
|
|
100_000.times {begin s.connect("127.0.0.1", 1) rescue IOError; end}
|
|
|
|
end;
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_send_no_memory_leak
|
|
|
|
assert_no_memory_leak(["-rsocket"], <<-"end;", <<-"end;", rss: true)
|
|
|
|
s = UDPSocket.new
|
|
|
|
s.close
|
|
|
|
end;
|
|
|
|
100_000.times {begin s.send("\0"*100, 0, "127.0.0.1", 1) rescue IOError; end}
|
|
|
|
end;
|
|
|
|
end
|
2004-12-06 11:15:56 +03:00
|
|
|
end if defined?(UDPSocket)
|