aboutsummaryrefslogtreecommitdiffstats
path: root/test/socket
diff options
context:
space:
mode:
Diffstat (limited to 'test/socket')
-rw-r--r--test/socket/test_socket.rb16
-rw-r--r--test/socket/test_tcp.rb17
-rw-r--r--test/socket/test_udp.rb26
3 files changed, 59 insertions, 0 deletions
diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb
index 659ebabba3..61cb3bb066 100644
--- a/test/socket/test_socket.rb
+++ b/test/socket/test_socket.rb
@@ -70,6 +70,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_raises(Errno::EADDRINUSE) do
+ s.bind(Socket.sockaddr_in(port, "127.0.0.1"))
+ end
+
+ assert_match "bind(2) for \"127.0.0.1\" port #{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") }
diff --git a/test/socket/test_tcp.rb b/test/socket/test_tcp.rb
index 2634e20651..3cf1e3c36d 100644
--- a/test/socket/test_tcp.rb
+++ b/test/socket/test_tcp.rb
@@ -6,6 +6,23 @@ end
class TestSocket_TCPSocket < Test::Unit::TestCase
+ def test_initialize_failure
+ s = TCPServer.new("localhost", nil)
+ server_port = s.addr[1]
+
+ c = TCPSocket.new("localhost", server_port)
+ client_port = c.addr[1]
+
+ begin
+ # TCPServer.new uses SO_REUSEADDR so we must create a failure on the
+ # local address.
+ TCPSocket.new("localhost", server_port, "localhost", client_port)
+ flunk "expected SystemCallError"
+ rescue SystemCallError => e
+ assert_match "for \"localhost\" port #{client_port}", e.message
+ end
+ end
+
def test_recvfrom
svr = TCPServer.new("localhost", 0)
th = Thread.new {
diff --git a/test/socket/test_udp.rb b/test/socket/test_udp.rb
index fe329c8c16..82b5a5daa9 100644
--- a/test/socket/test_udp.rb
+++ b/test/socket/test_udp.rb
@@ -36,4 +36,30 @@ class TestSocket_UDPSocket < Test::Unit::TestCase
s.bind(host, 2000)
}
end
+
+ def test_bind_addrinuse
+ host = "127.0.0.1"
+ port = 2001
+
+ in_use = UDPSocket.new
+ in_use.bind(host, port)
+
+ s = UDPSocket.new
+
+ e = assert_raises(Errno::EADDRINUSE) do
+ s.bind(host, port)
+ end
+
+ assert_match "bind(2) for \"#{host}\" port #{port}", e.message
+ end
+
+ def test_send_too_long
+ u = UDPSocket.new
+
+ e = assert_raises Errno::EMSGSIZE do
+ u.send "\0" * 100_000, 0, "127.0.0.1", 7 # echo
+ end
+
+ assert_match 'for "127.0.0.1" port 7', e.message
+ end
end if defined?(UDPSocket)