9 class TestUNIXSocket < Test::Unit::TestCase
12 s1, s2 = UNIXSocket.pair
15 rescue NotImplementedError
16 assert_raise(NotImplementedError) { s2.recv_io }
20 assert_equal(r1.stat.ino, r2.stat.ino)
21 assert_not_equal(r1.fileno, r2.fileno)
23 assert_equal("a", r2.sysread(10))
29 r2.close if r2 && !r2.closed?
33 def bound_unix_socket(klass)
34 tmpfile = Tempfile.new("testrubysock")
37 yield klass.new(path), path
39 File.unlink path if path && File.socket?(path)
43 bound_unix_socket(UNIXServer) {|serv, path|
44 c = UNIXSocket.new(path)
46 assert_equal(["AF_UNIX", path], c.peeraddr)
47 assert_equal(["AF_UNIX", ""], c.addr)
48 assert_equal(["AF_UNIX", ""], s.peeraddr)
49 assert_equal(["AF_UNIX", path], s.addr)
50 assert_equal(path, s.path)
51 assert_equal("", c.path)
56 s1, s2 = UNIXSocket.pair
57 assert_equal("", s1.path)
58 assert_equal("", s2.path)
65 s1, s2 = UNIXSocket.pair
66 assert_equal(["AF_UNIX", ""], s1.addr)
67 assert_equal(["AF_UNIX", ""], s2.addr)
73 def test_noname_peeraddr
74 s1, s2 = UNIXSocket.pair
75 assert_equal(["AF_UNIX", ""], s1.peeraddr)
76 assert_equal(["AF_UNIX", ""], s2.peeraddr)
82 def test_noname_unpack_sockaddr_un
83 s1, s2 = UNIXSocket.pair
85 assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s1.getsockname) != ""
86 assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s1.getsockname) != ""
87 assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s2.getsockname) != ""
88 assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s1.getpeername) != ""
89 assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s2.getpeername) != ""
95 def test_noname_recvfrom
96 s1, s2 = UNIXSocket.pair
98 assert_equal(["a", ["AF_UNIX", ""]], s1.recvfrom(10))
104 def test_noname_recv_nonblock
105 s1, s2 = UNIXSocket.pair
108 assert_equal("a", s1.recv_nonblock(10))
114 def test_too_long_path
115 assert_raise(ArgumentError) { Socket.sockaddr_un("a" * 300) }
116 assert_raise(ArgumentError) { UNIXServer.new("a" * 300) }
120 assert_raise(ArgumentError) { Socket.sockaddr_un("a\0b") }
124 s1, s2 = UNIXSocket.pair(Socket::SOCK_DGRAM)
125 assert_raise(Errno::EAGAIN) { s1.recv_nonblock(10) }
130 assert_equal("", s1.recv(10))
131 assert_equal("haha", s1.recv(10))
132 assert_equal("", s1.recv(10))
133 assert_equal("", s1.recv(10))
134 assert_raise(Errno::EAGAIN) { s1.recv_nonblock(10) }
140 def test_epipe # [ruby-dev:34619]
141 s1, s2 = UNIXSocket.pair
142 s1.shutdown(Socket::SHUT_WR)
143 assert_raise(Errno::EPIPE) { s1.write "a" }
144 assert_equal(nil, s2.read(1))
146 assert_equal("a", s1.read(1))
149 end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM