[前][次][番号順一覧][スレッド一覧]

ruby-changes:21568

From: akr <ko1@a...>
Date: Thu, 3 Nov 2011 20:15:22 +0900 (JST)
Subject: [ruby-changes:21568] akr:r33617 (trunk): add test for close-on-exec.

akr	2011-11-03 20:15:15 +0900 (Thu, 03 Nov 2011)

  New Revision: 33617

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=33617

  Log:
    add test for close-on-exec.

  Modified files:
    trunk/test/ruby/test_io.rb
    trunk/test/ruby/test_process.rb
    trunk/test/socket/test_socket.rb
    trunk/test/socket/test_unix.rb
    trunk/test/test_pty.rb

Index: test/ruby/test_io.rb
===================================================================
--- test/ruby/test_io.rb	(revision 33616)
+++ test/ruby/test_io.rb	(revision 33617)
@@ -2030,4 +2030,29 @@
     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
Index: test/ruby/test_process.rb
===================================================================
--- test/ruby/test_process.rb	(revision 33616)
+++ test/ruby/test_process.rb	(revision 33617)
@@ -1323,4 +1323,11 @@
       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
Index: test/socket/test_unix.rb
===================================================================
--- test/socket/test_unix.rb	(revision 33616)
+++ test/socket/test_unix.rb	(revision 33617)
@@ -22,6 +22,7 @@
       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 @@
         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 @@
         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 @@
         r2 = s2.recv_io
         begin
           assert(File.identical?(r1, r2))
+          assert(r2.close_on_exec?)
         ensure
           r2.close
         end
@@ -230,6 +234,7 @@
 	  r2 = ios[0]
 	  begin
 	    assert(File.identical?(r1, r2))
+            assert(r2.close_on_exec?)
 	  ensure
 	    r2.close
 	  end
@@ -260,6 +265,16 @@
     }
   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 @@
     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|
Index: test/socket/test_socket.rb
===================================================================
--- test/socket/test_socket.rb	(revision 33616)
+++ test/socket/test_socket.rb	(revision 33617)
@@ -1,6 +1,7 @@
 begin
   require "socket"
   require "tmpdir"
+  require "fcntl"
   require "test/unit"
 rescue LoadError
 end
@@ -15,6 +16,16 @@
     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 @@
     }
   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 @@
               assert(s2raddr.to_sockaddr.empty? ||
                      s1laddr.to_sockaddr.empty? ||
                      s2raddr.unix_path == s1laddr.unix_path)
+              assert(s2.close_on_exec?)
             ensure
               s2.close
             end
Index: test/test_pty.rb
===================================================================
--- test/test_pty.rb	(revision 33616)
+++ test/test_pty.rb	(revision 33617)
@@ -195,5 +195,16 @@
     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
 

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]