diff options
author | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-11-03 11:15:15 +0000 |
---|---|---|
committer | akr <akr@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2011-11-03 11:15:15 +0000 |
commit | 88f1b8cf0c286d5a499d0832cde5ff6be4105d89 (patch) | |
tree | de55d3ce3930d527ccd672ed2cb58b92567dd3d0 /test/socket | |
parent | df6a0fe8bb0d41634689af8dcbee2c3e5eed4818 (diff) | |
download | ruby-88f1b8cf0c286d5a499d0832cde5ff6be4105d89.tar.gz |
add test for close-on-exec.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@33617 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'test/socket')
-rw-r--r-- | test/socket/test_socket.rb | 28 | ||||
-rw-r--r-- | test/socket/test_unix.rb | 23 |
2 files changed, 51 insertions, 0 deletions
diff --git a/test/socket/test_socket.rb b/test/socket/test_socket.rb index e2517671ff..6769576bab 100644 --- a/test/socket/test_socket.rb +++ b/test/socket/test_socket.rb @@ -1,6 +1,7 @@ begin require "socket" require "tmpdir" + require "fcntl" require "test/unit" rescue LoadError end @@ -15,6 +16,16 @@ class TestSocket < Test::Unit::TestCase end end + def test_socket_new_cloexec + return unless defined? Fcntl::FD_CLOEXEC + begin + s = Socket.new(:INET, :STREAM) + assert(s.close_on_exec?) + ensure + s.close + end + end + def test_unpack_sockaddr sockaddr_in = Socket.sockaddr_in(80, "") assert_raise(ArgumentError) { Socket.unpack_sockaddr_un(sockaddr_in) } @@ -103,6 +114,22 @@ class TestSocket < Test::Unit::TestCase } end + def test_tcp_cloexec + return unless defined? Fcntl::FD_CLOEXEC + TCPServer.open(0) {|serv| + addr = serv.connect_address + addr.connect {|s1| + s2 = serv.accept + begin + assert(s2.close_on_exec?) + ensure + s2.close + end + } + + } + end + def random_port # IANA suggests dynamic port for 49152 to 65535 # http://www.iana.org/assignments/port-numbers @@ -159,6 +186,7 @@ class TestSocket < Test::Unit::TestCase assert(s2raddr.to_sockaddr.empty? || s1laddr.to_sockaddr.empty? || s2raddr.unix_path == s1laddr.unix_path) + assert(s2.close_on_exec?) ensure s2.close end diff --git a/test/socket/test_unix.rb b/test/socket/test_unix.rb index b4c4592537..a9918b108b 100644 --- a/test/socket/test_unix.rb +++ b/test/socket/test_unix.rb @@ -22,6 +22,7 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase r2 = s2.recv_io assert_equal(r1.stat.ino, r2.stat.ino) assert_not_equal(r1.fileno, r2.fileno) + assert(r2.close_on_exec?) w.syswrite "a" assert_equal("a", r2.sysread(10)) ensure @@ -61,6 +62,7 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase 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])) + assert(recv_io_ary[i].close_on_exec?) } } } @@ -97,6 +99,7 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase 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])) + assert(recv_io_ary[i].close_on_exec?) } } } @@ -150,6 +153,7 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase r2 = s2.recv_io begin assert(File.identical?(r1, r2)) + assert(r2.close_on_exec?) ensure r2.close end @@ -230,6 +234,7 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase r2 = ios[0] begin assert(File.identical?(r1, r2)) + assert(r2.close_on_exec?) ensure r2.close end @@ -260,6 +265,16 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase } end + 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 + def test_noname_path s1, s2 = UNIXSocket.pair assert_equal("", s1.path) @@ -374,6 +389,14 @@ class TestSocket_UNIXSocket < Test::Unit::TestCase assert_kind_of(UNIXSocket, pair[1]) end + 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 + def test_initialize Dir.mktmpdir {|d| Socket.open(Socket::AF_UNIX, Socket::SOCK_STREAM, 0) {|s| |