diff options
-rw-r--r-- | test/ruby/test_io.rb | 25 | ||||
-rw-r--r-- | test/ruby/test_process.rb | 7 | ||||
-rw-r--r-- | test/socket/test_socket.rb | 28 | ||||
-rw-r--r-- | test/socket/test_unix.rb | 23 | ||||
-rw-r--r-- | test/test_pty.rb | 11 |
5 files changed, 94 insertions, 0 deletions
diff --git a/test/ruby/test_io.rb b/test/ruby/test_io.rb index c2eecb7397..e4af5e0e9f 100644 --- a/test/ruby/test_io.rb +++ b/test/ruby/test_io.rb @@ -2030,4 +2030,29 @@ End end assert_equal("[Feature #5029]\n[ruby-core:38070]\n", stderr) end + + def test_cloexec + return unless defined? Fcntl::FD_CLOEXEC + open(__FILE__) {|f| + assert(f.close_on_exec?) + g = f.dup + begin + assert(g.close_on_exec?) + f.reopen(g) + assert(f.close_on_exec?) + ensure + g.close + end + g = IO.new(f.fcntl(Fcntl::F_DUPFD)) + begin + assert(g.close_on_exec?) + ensure + g.close + end + } + IO.pipe {|r,w| + assert(r.close_on_exec?) + assert(w.close_on_exec?) + } + end end diff --git a/test/ruby/test_process.rb b/test/ruby/test_process.rb index 5c05a47af5..a731dee99d 100644 --- a/test/ruby/test_process.rb +++ b/test/ruby/test_process.rb @@ -1323,4 +1323,11 @@ class TestProcess < Test::Unit::TestCase end end end + + def test_popen_cloexec + return unless defined? Fcntl::FD_CLOEXEC + IO.popen([RUBY, "-e", ""]) {|io| + assert(io.close_on_exec?) + } + end end 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| diff --git a/test/test_pty.rb b/test/test_pty.rb index 025cafc8f8..80cc02c72e 100644 --- a/test/test_pty.rb +++ b/test/test_pty.rb @@ -195,5 +195,16 @@ class TestPTY < Test::Unit::TestCase assert_nil(st1) assert_equal(pid, st2.pid) end + + def test_cloexec + PTY.open {|m, s| + assert(m.close_on_exec?) + assert(s.close_on_exec?) + } + PTY.spawn(RUBY, '-e', '') {|r, w, pid| + assert(r.close_on_exec?) + assert(w.close_on_exec?) + } + end end if defined? PTY |