summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-06-04 11:45:09 +0000
committerakr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2006-06-04 11:45:09 +0000
commitd3fde50de7eb4b0df8dc98e0d21df233be2d29c2 (patch)
tree41b097dcc90440d790a1123bed9c9d2d72b04b21 /test
parente0fd4ffc1c05be733e87fa42c0fb4fce2168bb28 (diff)
* ext/socket/socket.c: fix sockaddr_un handling.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@10212 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test')
-rw-r--r--test/socket/test_unix.rb128
1 files changed, 127 insertions, 1 deletions
diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb
index b1c0a38537..081100073a 100644
--- a/test/socket/test_unix.rb
+++ b/test/socket/test_unix.rb
@@ -1,9 +1,11 @@
begin
require "socket"
- require "test/unit"
rescue LoadError
end
+require "test/unit"
+require "tempfile"
+
class TestUNIXSocket < Test::Unit::TestCase
def test_fd_passing
r1, w = IO.pipe
@@ -27,4 +29,128 @@ class TestUNIXSocket < Test::Unit::TestCase
r2.close if r2 && !r2.closed?
end
end
+
+ def bound_unix_socket(klass)
+ tmpfile = Tempfile.new("testrubysock")
+ path = tmpfile.path
+ tmpfile.close(true)
+ yield klass.new(path), path
+ ensure
+ File.unlink path if path && File.socket?(path)
+ end
+
+ def test_addr
+ bound_unix_socket(UNIXServer) {|serv, path|
+ c = UNIXSocket.new(path)
+ s = serv.accept
+ assert_equal(["AF_UNIX", path], c.peeraddr)
+ assert_equal(["AF_UNIX", ""], c.addr)
+ assert_equal(["AF_UNIX", ""], s.peeraddr)
+ assert_equal(["AF_UNIX", path], s.addr)
+ assert_equal(path, s.path)
+ assert_equal("", c.path)
+ }
+ end
+
+ def test_noname_path
+ s1, s2 = UNIXSocket.pair
+ assert_equal("", s1.path)
+ assert_equal("", s2.path)
+ ensure
+ s1.close
+ s2.close
+ end
+
+ def test_noname_addr
+ s1, s2 = UNIXSocket.pair
+ assert_equal(["AF_UNIX", ""], s1.addr)
+ assert_equal(["AF_UNIX", ""], s2.addr)
+ ensure
+ s1.close
+ s2.close
+ end
+
+ def test_noname_peeraddr
+ s1, s2 = UNIXSocket.pair
+ assert_equal(["AF_UNIX", ""], s1.peeraddr)
+ assert_equal(["AF_UNIX", ""], s2.peeraddr)
+ ensure
+ s1.close
+ s2.close
+ end
+
+ def test_noname_unpack_sockaddr_un
+ s1, s2 = UNIXSocket.pair
+ assert_equal("", Socket.unpack_sockaddr_un(s1.getsockname))
+ assert_equal("", Socket.unpack_sockaddr_un(s2.getsockname))
+ assert_equal("", Socket.unpack_sockaddr_un(s1.getpeername))
+ assert_equal("", Socket.unpack_sockaddr_un(s2.getpeername))
+ ensure
+ s1.close
+ s2.close
+ end
+
+ def test_noname_recvfrom
+ s1, s2 = UNIXSocket.pair
+ s2.write("a")
+ assert_equal(["a", ["AF_UNIX", ""]], s1.recvfrom(10))
+ ensure
+ s1.close
+ s2.close
+ end
+
+ def test_noname_recvfrom_nonblock
+ s1, s2 = UNIXSocket.pair
+ s2.write("a")
+ IO.select [s1]
+ assert_equal(["a", ["AF_UNIX", ""]], s1.recvfrom_nonblock(10))
+ ensure
+ s1.close
+ s2.close
+ end
+
+ def test_too_long_path
+ assert_raise(ArgumentError) { Socket.sockaddr_un("a" * 300) }
+ assert_raise(ArgumentError) { UNIXServer.new("a" * 300) }
+ end
+
+ def test_nul
+ assert_raise(ArgumentError) { Socket.sockaddr_un("a\0b") }
+ assert_raise(ArgumentError) { UNIXServer.new("a\0b") }
+ end
+
+ def test_dgram_pair
+ s1, s2 = UNIXSocket.pair(Socket::SOCK_DGRAM)
+ assert_raise(Errno::EAGAIN) { s1.recvfrom_nonblock(10) }
+ s2.send("", 0)
+ s2.send("haha", 0)
+ s2.send("", 0)
+ s2.send("", 0)
+ assert_equal("", s1.recv(10))
+ assert_equal("haha", s1.recv(10))
+ assert_equal("", s1.recv(10))
+ assert_equal("", s1.recv(10))
+ assert_raise(Errno::EAGAIN) { s1.recvfrom_nonblock(10) }
+ ensure
+ s1.close
+ s2.close
+ end
+
+ def test_seqpacket_pair
+ s1, s2 = UNIXSocket.pair(Socket::SOCK_SEQPACKET)
+ assert_raise(Errno::EAGAIN) { s1.recvfrom_nonblock(10) }
+ s2.send("", 0)
+ s2.send("haha", 0)
+ s2.send("", 0)
+ s2.send("", 0)
+ assert_equal("", s1.recv(10))
+ assert_equal("haha", s1.recv(10))
+ assert_equal("", s1.recv(10))
+ assert_equal("", s1.recv(10))
+ assert_raise(Errno::EAGAIN) { s1.recvfrom_nonblock(10) }
+ ensure
+ s1.close
+ s2.close
+ end
+
end if defined?(UNIXSocket)