2005-06-03 22:40:40 -04:00
|
|
|
begin
|
|
|
|
require "socket"
|
|
|
|
rescue LoadError
|
|
|
|
end
|
|
|
|
|
2006-06-04 07:45:09 -04:00
|
|
|
require "test/unit"
|
|
|
|
require "tempfile"
|
2009-01-05 08:40:42 -05:00
|
|
|
require "tmpdir"
|
2011-06-13 10:11:23 -04:00
|
|
|
require "thread"
|
|
|
|
require "io/nonblock"
|
2006-06-04 07:45:09 -04:00
|
|
|
|
2009-02-10 23:17:57 -05:00
|
|
|
class TestSocket_UNIXSocket < Test::Unit::TestCase
|
2005-06-03 22:40:40 -04:00
|
|
|
def test_fd_passing
|
|
|
|
r1, w = IO.pipe
|
|
|
|
s1, s2 = UNIXSocket.pair
|
|
|
|
begin
|
2005-07-02 09:45:08 -04:00
|
|
|
s1.send_io(nil)
|
2005-06-03 22:40:40 -04:00
|
|
|
rescue NotImplementedError
|
|
|
|
assert_raise(NotImplementedError) { s2.recv_io }
|
2005-07-02 09:45:08 -04:00
|
|
|
rescue TypeError
|
|
|
|
s1.send_io(r1)
|
|
|
|
r2 = s2.recv_io
|
|
|
|
assert_equal(r1.stat.ino, r2.stat.ino)
|
|
|
|
assert_not_equal(r1.fileno, r2.fileno)
|
2011-11-03 07:15:15 -04:00
|
|
|
assert(r2.close_on_exec?)
|
2005-07-02 09:45:08 -04:00
|
|
|
w.syswrite "a"
|
|
|
|
assert_equal("a", r2.sysread(10))
|
|
|
|
ensure
|
|
|
|
s1.close
|
|
|
|
s2.close
|
|
|
|
w.close
|
|
|
|
r1.close
|
|
|
|
r2.close if r2 && !r2.closed?
|
2005-06-03 22:40:40 -04:00
|
|
|
end
|
|
|
|
end
|
2006-06-04 07:45:09 -04:00
|
|
|
|
2009-02-01 21:51:02 -05:00
|
|
|
def test_fd_passing_n
|
|
|
|
io_ary = []
|
|
|
|
return if !defined?(Socket::SCM_RIGHTS)
|
|
|
|
io_ary.concat IO.pipe
|
|
|
|
io_ary.concat IO.pipe
|
|
|
|
io_ary.concat IO.pipe
|
|
|
|
send_io_ary = []
|
|
|
|
io_ary.each {|io|
|
|
|
|
send_io_ary << io
|
|
|
|
UNIXSocket.pair {|s1, s2|
|
|
|
|
begin
|
|
|
|
ret = s1.sendmsg("\0", 0, nil, [Socket::SOL_SOCKET, Socket::SCM_RIGHTS,
|
2010-01-25 17:08:29 -05:00
|
|
|
send_io_ary.map {|io2| io2.fileno }.pack("i!*")])
|
2009-02-01 21:51:02 -05:00
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
assert_equal(1, ret)
|
2009-02-27 09:23:54 -05:00
|
|
|
ret = s2.recvmsg(:scm_rights=>true)
|
2009-02-01 21:51:02 -05:00
|
|
|
data, srcaddr, flags, *ctls = ret
|
|
|
|
recv_io_ary = []
|
|
|
|
ctls.each {|ctl|
|
|
|
|
next if ctl.level != Socket::SOL_SOCKET || ctl.type != Socket::SCM_RIGHTS
|
2009-02-19 07:13:38 -05:00
|
|
|
recv_io_ary.concat ctl.unix_rights
|
2009-02-01 21:51:02 -05:00
|
|
|
}
|
|
|
|
assert_equal(send_io_ary.length, recv_io_ary.length)
|
|
|
|
send_io_ary.length.times {|i|
|
|
|
|
assert_not_equal(send_io_ary[i].fileno, recv_io_ary[i].fileno)
|
|
|
|
assert(File.identical?(send_io_ary[i], recv_io_ary[i]))
|
2011-11-03 07:15:15 -04:00
|
|
|
assert(recv_io_ary[i].close_on_exec?)
|
2009-02-01 21:51:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
io_ary.each {|io| io.close if !io.closed? }
|
|
|
|
end
|
|
|
|
|
2009-02-25 09:19:24 -05:00
|
|
|
def test_fd_passing_n2
|
|
|
|
io_ary = []
|
|
|
|
return if !defined?(Socket::SCM_RIGHTS)
|
2009-03-02 07:14:35 -05:00
|
|
|
return if !defined?(Socket::AncillaryData)
|
2009-02-25 09:19:24 -05:00
|
|
|
io_ary.concat IO.pipe
|
|
|
|
io_ary.concat IO.pipe
|
|
|
|
io_ary.concat IO.pipe
|
|
|
|
send_io_ary = []
|
|
|
|
io_ary.each {|io|
|
|
|
|
send_io_ary << io
|
|
|
|
UNIXSocket.pair {|s1, s2|
|
|
|
|
begin
|
|
|
|
ancdata = Socket::AncillaryData.unix_rights(*send_io_ary)
|
|
|
|
ret = s1.sendmsg("\0", 0, nil, ancdata)
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
assert_equal(1, ret)
|
2009-02-27 09:23:54 -05:00
|
|
|
ret = s2.recvmsg(:scm_rights=>true)
|
2009-02-25 09:19:24 -05:00
|
|
|
data, srcaddr, flags, *ctls = ret
|
|
|
|
recv_io_ary = []
|
|
|
|
ctls.each {|ctl|
|
|
|
|
next if ctl.level != Socket::SOL_SOCKET || ctl.type != Socket::SCM_RIGHTS
|
|
|
|
recv_io_ary.concat ctl.unix_rights
|
|
|
|
}
|
|
|
|
assert_equal(send_io_ary.length, recv_io_ary.length)
|
|
|
|
send_io_ary.length.times {|i|
|
|
|
|
assert_not_equal(send_io_ary[i].fileno, recv_io_ary[i].fileno)
|
|
|
|
assert(File.identical?(send_io_ary[i], recv_io_ary[i]))
|
2011-11-03 07:15:15 -04:00
|
|
|
assert(recv_io_ary[i].close_on_exec?)
|
2009-02-25 09:19:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ensure
|
|
|
|
io_ary.each {|io| io.close if !io.closed? }
|
|
|
|
end
|
|
|
|
|
2011-06-13 10:11:23 -04:00
|
|
|
def test_fd_passing_race_condition
|
|
|
|
r1, w = IO.pipe
|
|
|
|
s1, s2 = UNIXSocket.pair
|
|
|
|
s1.nonblock = s2.nonblock = true
|
|
|
|
lock = Mutex.new
|
|
|
|
nr = 0
|
|
|
|
x = 2
|
|
|
|
y = 1000
|
|
|
|
begin
|
|
|
|
s1.send_io(nil)
|
|
|
|
rescue NotImplementedError
|
|
|
|
assert_raise(NotImplementedError) { s2.recv_io }
|
|
|
|
rescue TypeError
|
|
|
|
thrs = x.times.map do
|
|
|
|
Thread.new do
|
|
|
|
y.times do
|
|
|
|
s2.recv_io.close
|
|
|
|
lock.synchronize { nr += 1 }
|
|
|
|
end
|
2011-06-13 10:12:23 -04:00
|
|
|
true
|
2011-06-13 10:11:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
(x * y).times { s1.send_io r1 }
|
2011-06-13 10:12:23 -04:00
|
|
|
assert_equal([true]*x, thrs.map { |t| t.value })
|
2011-06-13 10:11:23 -04:00
|
|
|
assert_equal x * y, nr
|
|
|
|
ensure
|
|
|
|
s1.close
|
|
|
|
s2.close
|
|
|
|
w.close
|
|
|
|
r1.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-02-01 21:51:02 -05:00
|
|
|
def test_sendmsg
|
|
|
|
return if !defined?(Socket::SCM_RIGHTS)
|
|
|
|
IO.pipe {|r1, w|
|
|
|
|
UNIXSocket.pair {|s1, s2|
|
|
|
|
begin
|
|
|
|
ret = s1.sendmsg("\0", 0, nil, [Socket::SOL_SOCKET, Socket::SCM_RIGHTS, [r1.fileno].pack("i!")])
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
assert_equal(1, ret)
|
|
|
|
r2 = s2.recv_io
|
|
|
|
begin
|
|
|
|
assert(File.identical?(r1, r2))
|
2011-11-03 07:15:15 -04:00
|
|
|
assert(r2.close_on_exec?)
|
2009-02-01 21:51:02 -05:00
|
|
|
ensure
|
|
|
|
r2.close
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-25 09:19:24 -05:00
|
|
|
def test_sendmsg_ancillarydata_int
|
2009-02-01 21:51:02 -05:00
|
|
|
return if !defined?(Socket::SCM_RIGHTS)
|
|
|
|
return if !defined?(Socket::AncillaryData)
|
|
|
|
IO.pipe {|r1, w|
|
|
|
|
UNIXSocket.pair {|s1, s2|
|
|
|
|
begin
|
2009-02-09 05:49:16 -05:00
|
|
|
ad = Socket::AncillaryData.int(:UNIX, :SOCKET, :RIGHTS, r1.fileno)
|
2009-02-01 21:51:02 -05:00
|
|
|
ret = s1.sendmsg("\0", 0, nil, ad)
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
assert_equal(1, ret)
|
|
|
|
r2 = s2.recv_io
|
|
|
|
begin
|
|
|
|
assert(File.identical?(r1, r2))
|
|
|
|
ensure
|
|
|
|
r2.close
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-25 09:19:24 -05:00
|
|
|
def test_sendmsg_ancillarydata_unix_rights
|
|
|
|
return if !defined?(Socket::SCM_RIGHTS)
|
|
|
|
return if !defined?(Socket::AncillaryData)
|
|
|
|
IO.pipe {|r1, w|
|
|
|
|
UNIXSocket.pair {|s1, s2|
|
|
|
|
begin
|
|
|
|
ad = Socket::AncillaryData.unix_rights(r1)
|
|
|
|
ret = s1.sendmsg("\0", 0, nil, ad)
|
|
|
|
rescue NotImplementedError
|
|
|
|
return
|
|
|
|
end
|
|
|
|
assert_equal(1, ret)
|
|
|
|
r2 = s2.recv_io
|
|
|
|
begin
|
|
|
|
assert(File.identical?(r1, r2))
|
|
|
|
ensure
|
|
|
|
r2.close
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-01 21:51:02 -05:00
|
|
|
def test_recvmsg
|
|
|
|
return if !defined?(Socket::SCM_RIGHTS)
|
2010-04-28 04:54:07 -04:00
|
|
|
return if !defined?(Socket::AncillaryData)
|
2009-02-01 21:51:02 -05:00
|
|
|
IO.pipe {|r1, w|
|
|
|
|
UNIXSocket.pair {|s1, s2|
|
|
|
|
s1.send_io(r1)
|
2009-02-27 09:23:54 -05:00
|
|
|
ret = s2.recvmsg(:scm_rights=>true)
|
2009-02-01 21:51:02 -05:00
|
|
|
data, srcaddr, flags, *ctls = ret
|
|
|
|
assert_equal("\0", data)
|
|
|
|
if flags == nil
|
|
|
|
# struct msghdr is 4.3BSD style (msg_accrights field).
|
|
|
|
assert_instance_of(Array, ctls)
|
|
|
|
assert_equal(0, ctls.length)
|
|
|
|
else
|
|
|
|
# struct msghdr is POSIX/4.4BSD style (msg_control field).
|
|
|
|
assert_equal(0, flags & (Socket::MSG_TRUNC|Socket::MSG_CTRUNC))
|
2009-02-05 06:01:43 -05:00
|
|
|
assert_instance_of(Addrinfo, srcaddr)
|
2009-02-01 21:51:02 -05:00
|
|
|
assert_instance_of(Array, ctls)
|
|
|
|
assert_equal(1, ctls.length)
|
2009-02-18 13:43:15 -05:00
|
|
|
ctl = ctls[0]
|
|
|
|
assert_instance_of(Socket::AncillaryData, ctl)
|
|
|
|
assert_equal(Socket::SOL_SOCKET, ctl.level)
|
|
|
|
assert_equal(Socket::SCM_RIGHTS, ctl.type)
|
|
|
|
assert_instance_of(String, ctl.data)
|
2009-02-19 07:13:38 -05:00
|
|
|
ios = ctl.unix_rights
|
2009-02-18 13:43:15 -05:00
|
|
|
assert_equal(1, ios.length)
|
|
|
|
r2 = ios[0]
|
2009-02-01 21:51:02 -05:00
|
|
|
begin
|
|
|
|
assert(File.identical?(r1, r2))
|
2011-11-03 07:15:15 -04:00
|
|
|
assert(r2.close_on_exec?)
|
2009-02-01 21:51:02 -05:00
|
|
|
ensure
|
|
|
|
r2.close
|
|
|
|
end
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2006-06-04 07:45:09 -04:00
|
|
|
def bound_unix_socket(klass)
|
2012-04-23 09:09:48 -04:00
|
|
|
tmpfile = Tempfile.new("s")
|
2006-06-04 07:45:09 -04:00
|
|
|
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
|
|
|
|
|
2011-11-03 07:15:15 -04:00
|
|
|
def test_cloexec
|
|
|
|
bound_unix_socket(UNIXServer) {|serv, path|
|
|
|
|
c = UNIXSocket.new(path)
|
|
|
|
s = serv.accept
|
|
|
|
assert(serv.close_on_exec?)
|
|
|
|
assert(c.close_on_exec?)
|
|
|
|
assert(s.close_on_exec?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2006-06-04 07:45:09 -04:00
|
|
|
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
|
2006-06-07 06:57:26 -04:00
|
|
|
n = nil
|
|
|
|
assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s1.getsockname) != ""
|
|
|
|
assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s1.getsockname) != ""
|
|
|
|
assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s2.getsockname) != ""
|
|
|
|
assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s1.getpeername) != ""
|
|
|
|
assert_equal("", Socket.unpack_sockaddr_un(n)) if (n = s2.getpeername) != ""
|
2006-06-04 07:45:09 -04:00
|
|
|
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
|
|
|
|
|
2006-06-26 12:39:59 -04:00
|
|
|
def test_noname_recv_nonblock
|
2006-06-04 07:45:09 -04:00
|
|
|
s1, s2 = UNIXSocket.pair
|
|
|
|
s2.write("a")
|
|
|
|
IO.select [s1]
|
2006-06-26 12:39:59 -04:00
|
|
|
assert_equal("a", s1.recv_nonblock(10))
|
2006-06-04 07:45:09 -04:00
|
|
|
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
|
|
|
|
|
2013-01-28 19:32:05 -05:00
|
|
|
def test_abstract_namespace
|
|
|
|
return if /linux/ !~ RUBY_PLATFORM
|
|
|
|
addr = Socket.pack_sockaddr_un("\0foo")
|
|
|
|
assert_match(/\0foo\z/, addr)
|
|
|
|
assert_equal("\0foo", Socket.unpack_sockaddr_un(addr))
|
|
|
|
end
|
2006-06-04 07:45:09 -04:00
|
|
|
|
|
|
|
def test_dgram_pair
|
|
|
|
s1, s2 = UNIXSocket.pair(Socket::SOCK_DGRAM)
|
2013-04-08 15:58:55 -04:00
|
|
|
begin
|
|
|
|
s1.recv_nonblock(10)
|
|
|
|
fail
|
|
|
|
rescue => e
|
|
|
|
assert(IO::EAGAINWaitReadable === e)
|
|
|
|
assert(IO::WaitReadable === e)
|
|
|
|
end
|
2006-06-04 07:45:09 -04:00
|
|
|
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))
|
2013-04-08 15:58:55 -04:00
|
|
|
assert_raise(IO::EAGAINWaitReadable) { s1.recv_nonblock(10) }
|
2006-06-04 07:45:09 -04:00
|
|
|
ensure
|
2006-06-04 08:15:01 -04:00
|
|
|
s1.close if s1
|
|
|
|
s2.close if s2
|
2006-06-04 07:45:09 -04:00
|
|
|
end
|
|
|
|
|
2008-06-30 09:50:13 -04:00
|
|
|
def test_epipe # [ruby-dev:34619]
|
|
|
|
s1, s2 = UNIXSocket.pair
|
|
|
|
s1.shutdown(Socket::SHUT_WR)
|
|
|
|
assert_raise(Errno::EPIPE) { s1.write "a" }
|
2008-06-30 10:05:20 -04:00
|
|
|
assert_equal(nil, s2.read(1))
|
2008-06-30 09:50:13 -04:00
|
|
|
s2.write "a"
|
|
|
|
assert_equal("a", s1.read(1))
|
|
|
|
end
|
|
|
|
|
2008-12-31 10:05:16 -05:00
|
|
|
def test_socket_pair_with_block
|
|
|
|
pair = nil
|
|
|
|
ret = Socket.pair(Socket::AF_UNIX, Socket::SOCK_STREAM, 0) {|s1, s2|
|
|
|
|
pair = [s1, s2]
|
|
|
|
:return_value
|
|
|
|
}
|
|
|
|
assert_equal(:return_value, ret)
|
|
|
|
assert_kind_of(Socket, pair[0])
|
|
|
|
assert_kind_of(Socket, pair[1])
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_unix_socket_pair_with_block
|
|
|
|
pair = nil
|
|
|
|
UNIXSocket.pair {|s1, s2|
|
|
|
|
pair = [s1, s2]
|
|
|
|
}
|
|
|
|
assert_kind_of(UNIXSocket, pair[0])
|
|
|
|
assert_kind_of(UNIXSocket, pair[1])
|
|
|
|
end
|
|
|
|
|
2011-11-03 07:15:15 -04:00
|
|
|
def test_unix_socket_pair_close_on_exec
|
|
|
|
pair = nil
|
|
|
|
UNIXSocket.pair {|s1, s2|
|
|
|
|
assert(s1.close_on_exec?)
|
|
|
|
assert(s2.close_on_exec?)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-01-01 02:49:31 -05:00
|
|
|
def test_initialize
|
2009-01-05 08:40:42 -05:00
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
Socket.open(Socket::AF_UNIX, Socket::SOCK_STREAM, 0) {|s|
|
|
|
|
s.bind(Socket.pack_sockaddr_un("#{d}/s1"))
|
|
|
|
addr = s.getsockname
|
|
|
|
assert_nothing_raised { Socket.unpack_sockaddr_un(addr) }
|
|
|
|
assert_raise(ArgumentError) { Socket.unpack_sockaddr_in(addr) }
|
|
|
|
}
|
|
|
|
Socket.open("AF_UNIX", "SOCK_STREAM", 0) {|s|
|
|
|
|
s.bind(Socket.pack_sockaddr_un("#{d}/s2"))
|
|
|
|
addr = s.getsockname
|
|
|
|
assert_nothing_raised { Socket.unpack_sockaddr_un(addr) }
|
|
|
|
assert_raise(ArgumentError) { Socket.unpack_sockaddr_in(addr) }
|
|
|
|
}
|
2009-01-01 02:49:31 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-11 02:51:53 -05:00
|
|
|
def test_unix_server_socket
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
path = "#{d}/sock"
|
2009-02-11 02:54:52 -05:00
|
|
|
s0 = nil
|
2009-02-11 02:51:53 -05:00
|
|
|
Socket.unix_server_socket(path) {|s|
|
|
|
|
assert_equal(path, s.local_address.unix_path)
|
|
|
|
assert(File.socket?(path))
|
2009-02-11 02:54:52 -05:00
|
|
|
s0 = s
|
2009-02-11 02:51:53 -05:00
|
|
|
}
|
2009-02-11 02:54:52 -05:00
|
|
|
assert(s0.closed?)
|
2009-02-11 02:51:53 -05:00
|
|
|
assert_raise(Errno::ENOENT) { File.stat path }
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-08 09:23:16 -05:00
|
|
|
def test_getcred_ucred
|
|
|
|
return if /linux/ !~ RUBY_PLATFORM
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
sockpath = "#{d}/sock"
|
|
|
|
serv = Socket.unix_server_socket(sockpath)
|
|
|
|
c = Socket.unix(sockpath)
|
|
|
|
s, = serv.accept
|
|
|
|
cred = s.getsockopt(:SOCKET, :PEERCRED)
|
|
|
|
inspect = cred.inspect
|
|
|
|
assert_match(/ pid=#{$$} /, inspect)
|
2009-02-08 12:04:40 -05:00
|
|
|
assert_match(/ euid=#{Process.euid} /, inspect)
|
|
|
|
assert_match(/ egid=#{Process.egid} /, inspect)
|
2009-02-08 09:23:16 -05:00
|
|
|
assert_match(/ \(ucred\)/, inspect)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-08 10:37:55 -05:00
|
|
|
def test_getcred_xucred
|
2009-02-08 10:49:35 -05:00
|
|
|
return if /freebsd|darwin/ !~ RUBY_PLATFORM
|
2009-02-08 10:37:55 -05:00
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
sockpath = "#{d}/sock"
|
|
|
|
serv = Socket.unix_server_socket(sockpath)
|
|
|
|
c = Socket.unix(sockpath)
|
|
|
|
s, = serv.accept
|
|
|
|
cred = s.getsockopt(0, Socket::LOCAL_PEERCRED)
|
|
|
|
inspect = cred.inspect
|
2009-02-08 10:42:17 -05:00
|
|
|
assert_match(/ euid=#{Process.euid} /, inspect)
|
2009-02-08 10:37:55 -05:00
|
|
|
assert_match(/ \(xucred\)/, inspect)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-08 09:23:16 -05:00
|
|
|
def test_sendcred_ucred
|
2009-02-08 06:30:26 -05:00
|
|
|
return if /linux/ !~ RUBY_PLATFORM
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
sockpath = "#{d}/sock"
|
|
|
|
serv = Socket.unix_server_socket(sockpath)
|
|
|
|
c = Socket.unix(sockpath)
|
|
|
|
s, = serv.accept
|
|
|
|
s.setsockopt(:SOCKET, :PASSCRED, 1)
|
|
|
|
c.print "a"
|
|
|
|
msg, cliend_ai, rflags, cred = s.recvmsg
|
2009-02-08 07:53:55 -05:00
|
|
|
inspect = cred.inspect
|
2009-02-08 06:30:26 -05:00
|
|
|
assert_equal("a", msg)
|
2009-02-08 07:53:55 -05:00
|
|
|
assert_match(/ pid=#{$$} /, inspect)
|
|
|
|
assert_match(/ uid=#{Process.uid} /, inspect)
|
2009-02-08 09:23:16 -05:00
|
|
|
assert_match(/ gid=#{Process.gid} /, inspect)
|
2009-02-08 07:53:55 -05:00
|
|
|
assert_match(/ \(ucred\)/, inspect)
|
2009-02-08 06:36:18 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-08 09:23:16 -05:00
|
|
|
def test_sendcred_sockcred
|
2009-02-08 07:53:55 -05:00
|
|
|
return if /netbsd|freebsd/ !~ RUBY_PLATFORM
|
2009-02-08 06:36:18 -05:00
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
sockpath = "#{d}/sock"
|
|
|
|
serv = Socket.unix_server_socket(sockpath)
|
|
|
|
c = Socket.unix(sockpath)
|
|
|
|
s, = serv.accept
|
|
|
|
s.setsockopt(0, Socket::LOCAL_CREDS, 1)
|
|
|
|
c.print "a"
|
|
|
|
msg, cliend_ai, rflags, cred = s.recvmsg
|
|
|
|
assert_equal("a", msg)
|
2009-02-08 07:53:55 -05:00
|
|
|
inspect = cred.inspect
|
|
|
|
assert_match(/ uid=#{Process.uid} /, inspect)
|
|
|
|
assert_match(/ euid=#{Process.euid} /, inspect)
|
|
|
|
assert_match(/ gid=#{Process.gid} /, inspect)
|
|
|
|
assert_match(/ egid=#{Process.egid} /, inspect)
|
|
|
|
assert_match(/ \(sockcred\)/, inspect)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-08 09:23:16 -05:00
|
|
|
def test_sendcred_cmsgcred
|
2009-02-08 07:53:55 -05:00
|
|
|
return if /freebsd/ !~ RUBY_PLATFORM
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
sockpath = "#{d}/sock"
|
|
|
|
serv = Socket.unix_server_socket(sockpath)
|
|
|
|
c = Socket.unix(sockpath)
|
|
|
|
s, = serv.accept
|
2009-03-05 22:56:38 -05:00
|
|
|
c.sendmsg("a", 0, nil, [:SOCKET, Socket::SCM_CREDS, ""])
|
2009-02-08 07:53:55 -05:00
|
|
|
msg, cliend_ai, rflags, cred = s.recvmsg
|
|
|
|
assert_equal("a", msg)
|
|
|
|
inspect = cred.inspect
|
|
|
|
assert_match(/ pid=#{$$} /, inspect)
|
|
|
|
assert_match(/ uid=#{Process.uid} /, inspect)
|
|
|
|
assert_match(/ euid=#{Process.euid} /, inspect)
|
|
|
|
assert_match(/ gid=#{Process.gid} /, inspect)
|
|
|
|
assert_match(/ \(cmsgcred\)/, inspect)
|
2009-02-08 06:30:26 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2009-02-10 20:22:08 -05:00
|
|
|
def test_getpeereid
|
|
|
|
Dir.mktmpdir {|d|
|
|
|
|
path = "#{d}/sock"
|
|
|
|
serv = Socket.unix_server_socket(path)
|
|
|
|
c = Socket.unix(path)
|
|
|
|
s, = serv.accept
|
|
|
|
begin
|
|
|
|
assert_equal([Process.euid, Process.egid], c.getpeereid)
|
|
|
|
assert_equal([Process.euid, Process.egid], s.getpeereid)
|
|
|
|
rescue NotImplementedError
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-01-25 03:15:26 -05:00
|
|
|
def test_abstract_unix_server
|
|
|
|
return if /linux/ !~ RUBY_PLATFORM
|
|
|
|
name = "\0ruby-test_unix"
|
|
|
|
s0 = nil
|
|
|
|
UNIXServer.open(name) {|s|
|
|
|
|
assert_equal(name, s.local_address.unix_path)
|
|
|
|
s0 = s
|
|
|
|
UNIXSocket.open(name) {|c|
|
|
|
|
sock = s.accept
|
2013-01-30 23:59:31 -05:00
|
|
|
begin
|
|
|
|
assert_equal(name, c.remote_address.unix_path)
|
|
|
|
ensure
|
|
|
|
sock.close
|
|
|
|
end
|
2013-01-25 03:15:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(s0.closed?)
|
|
|
|
end
|
|
|
|
|
2013-01-30 01:30:12 -05:00
|
|
|
def test_abstract_unix_socket_econnrefused
|
|
|
|
return if /linux/ !~ RUBY_PLATFORM
|
|
|
|
name = "\0ruby-test_unix"
|
|
|
|
assert_raise(Errno::ECONNREFUSED) do
|
|
|
|
UNIXSocket.open(name) {}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-25 03:15:26 -05:00
|
|
|
def test_abstract_unix_server_socket
|
|
|
|
return if /linux/ !~ RUBY_PLATFORM
|
|
|
|
name = "\0ruby-test_unix"
|
|
|
|
s0 = nil
|
|
|
|
Socket.unix_server_socket(name) {|s|
|
|
|
|
assert_equal(name, s.local_address.unix_path)
|
|
|
|
s0 = s
|
|
|
|
Socket.unix(name) {|c|
|
|
|
|
sock, = s.accept
|
2013-01-30 23:59:31 -05:00
|
|
|
begin
|
|
|
|
assert_equal(name, c.remote_address.unix_path)
|
|
|
|
ensure
|
|
|
|
sock.close
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(s0.closed?)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_autobind
|
|
|
|
return if /linux/ !~ RUBY_PLATFORM
|
|
|
|
s0 = nil
|
|
|
|
Socket.unix_server_socket("") {|s|
|
|
|
|
name = s.local_address.unix_path
|
|
|
|
assert_match(/\A\0[0-9a-f]{5}\z/, name)
|
|
|
|
s0 = s
|
|
|
|
Socket.unix(name) {|c|
|
|
|
|
sock, = s.accept
|
|
|
|
begin
|
|
|
|
assert_equal(name, c.remote_address.unix_path)
|
|
|
|
ensure
|
|
|
|
sock.close
|
|
|
|
end
|
2013-01-25 03:15:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
assert(s0.closed?)
|
|
|
|
end
|
|
|
|
|
2006-07-23 10:03:28 -04:00
|
|
|
end if defined?(UNIXSocket) && /cygwin/ !~ RUBY_PLATFORM
|