add tests.
[ruby-svn.git] / test / socket / test_unix.rb
blob58350c4486d7e8113f760f90366a3665a563adeb
1 begin
2   require "socket"
3 rescue LoadError
4 end
6 require "test/unit"
7 require "tempfile"
9 class TestUNIXSocket < Test::Unit::TestCase
10   def test_fd_passing
11     r1, w = IO.pipe
12     s1, s2 = UNIXSocket.pair
13     begin
14       s1.send_io(nil)
15     rescue NotImplementedError
16       assert_raise(NotImplementedError) { s2.recv_io }
17     rescue TypeError
18       s1.send_io(r1)
19       r2 = s2.recv_io
20       assert_equal(r1.stat.ino, r2.stat.ino)
21       assert_not_equal(r1.fileno, r2.fileno)
22       w.syswrite "a"
23       assert_equal("a", r2.sysread(10))
24     ensure
25       s1.close
26       s2.close
27       w.close
28       r1.close
29       r2.close if r2 && !r2.closed?
30     end
31   end
33   def bound_unix_socket(klass)
34     tmpfile = Tempfile.new("testrubysock")
35     path = tmpfile.path
36     tmpfile.close(true)
37     yield klass.new(path), path
38   ensure
39     File.unlink path if path && File.socket?(path)
40   end
42   def test_addr
43     bound_unix_socket(UNIXServer) {|serv, path|
44       c = UNIXSocket.new(path)
45       s = serv.accept
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)
52     }
53   end
55   def test_noname_path
56     s1, s2 = UNIXSocket.pair
57     assert_equal("", s1.path)
58     assert_equal("", s2.path)
59   ensure
60     s1.close
61     s2.close
62   end
64   def test_noname_addr
65     s1, s2 = UNIXSocket.pair
66     assert_equal(["AF_UNIX", ""], s1.addr)
67     assert_equal(["AF_UNIX", ""], s2.addr)
68   ensure
69     s1.close
70     s2.close
71   end
73   def test_noname_peeraddr
74     s1, s2 = UNIXSocket.pair
75     assert_equal(["AF_UNIX", ""], s1.peeraddr)
76     assert_equal(["AF_UNIX", ""], s2.peeraddr)
77   ensure
78     s1.close
79     s2.close
80   end
82   def test_noname_unpack_sockaddr_un
83     s1, s2 = UNIXSocket.pair
84     n = nil
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) != ""
90   ensure
91     s1.close
92     s2.close
93   end
95   def test_noname_recvfrom
96     s1, s2 = UNIXSocket.pair
97     s2.write("a")
98     assert_equal(["a", ["AF_UNIX", ""]], s1.recvfrom(10))
99   ensure
100     s1.close
101     s2.close
102   end
104   def test_noname_recv_nonblock
105     s1, s2 = UNIXSocket.pair
106     s2.write("a")
107     IO.select [s1]
108     assert_equal("a", s1.recv_nonblock(10))
109   ensure
110     s1.close
111     s2.close
112   end
114   def test_too_long_path
115     assert_raise(ArgumentError) { Socket.sockaddr_un("a" * 300) }
116     assert_raise(ArgumentError) { UNIXServer.new("a" * 300) }
117   end
119   def test_nul
120     assert_raise(ArgumentError) { Socket.sockaddr_un("a\0b") }
121   end
123   def test_dgram_pair
124     s1, s2 = UNIXSocket.pair(Socket::SOCK_DGRAM)
125     assert_raise(Errno::EAGAIN) { s1.recv_nonblock(10) }
126     s2.send("", 0)
127     s2.send("haha", 0)
128     s2.send("", 0)
129     s2.send("", 0)
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) }
135   ensure
136     s1.close if s1
137     s2.close if s2
138   end
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))
145     s2.write "a"
146     assert_equal("a", s1.read(1))
147   end
149 end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM